Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
Add tests for bits1to14.h
Browse files Browse the repository at this point in the history
Issue #3
  • Loading branch information
Rene Ladan committed Dec 24, 2017
1 parent e47957b commit 717290e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
5 changes: 3 additions & 2 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test: $(exebin)
./testcentury
./test_bits1to14

JSON_L?=`pkg-config --libs json-c`
PREFIX?=.
ETCDIR?=etc/dcf77pi
CFLAGS+=-Wall -D_POSIX_C_SOURCE=200809L -DETCDIR=\"$(PREFIX)/$(ETCDIR)\" \
Expand All @@ -22,8 +23,8 @@ testcentury: testcentury.o ../calendar.o
$(CC) -o $@ testcentury.o ../calendar.o
test_bits1to14.o: ../bits1to14.h
$(CC) -fpic $(CFLAGS) -I.. -c test_bits1to14.c -o $@
test_bits1to14: test_bits1to14.o ../bits1to14.o
$(CC) -o $@ test_bits1to14.o ../bits1to14.o
test_bits1to14: test_bits1to14.o ../bits1to14.o ../input.o
$(CC) -o $@ test_bits1to14.o ../bits1to14.o ../input.o -lm $(JSON_L)

clean:
rm -f $(objbin) $(exebin)
56 changes: 56 additions & 0 deletions tests/test_bits1to14.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,68 @@
// SPDX-License-Identifier: BSD-2-Clause

#include "bits1to14.h"
#include "input.h"

#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>

int
main(int argc, char *argv[])
{
unsigned in_buf[TPBUFLEN];
const unsigned *rb_ptr;
enum eTP tp_res[4] = { eTP_weather, eTP_unknown, eTP_unknown,
eTP_alarm };
struct GB_result gbr;

srand(1); /* INSECURE random function, but C99-compliant */
for (int ofs = 0; ofs < 3; ofs++) {
for (unsigned type = 0; type < 4; type++) {
/* fill the buffer */
for (int i = 0; i < TPBUFLEN + 2; i++) {
/*
* Calculate absolute position in buffer when
* jumping in half-way a message.
*/
int k = i - 14 * ofs;
if (k < 0)
k += TPBUFLEN + 2;
if (k == 0) {
gbr.bitval = ((type & 1) == 1 ? ebv_1 :
ebv_0);
} else if (k == 7) {
gbr.bitval = ((type & 2) == 2 ? ebv_1 :
ebv_0);
} else {
int j = (k < 7) ? k - 1 : k - 2;
in_buf[j] = rand() % 2;
gbr.bitval = (in_buf[j] == 0 ? ebv_0 :
ebv_1);
}
fill_thirdparty_buffer(k / 14, k % 14 + 1, gbr);
}

/* test retrieving the buffer */
rb_ptr = get_thirdparty_buffer();
for (unsigned i = 0; i < TPBUFLEN; i++) {
if (in_buf[i] != rb_ptr[i]) {
printf("%s: ofs %i type %u bit %u:"
" %u should be %u\n", argv[0], ofs,
type, i, rb_ptr[i], in_buf[i]);
return EX_SOFTWARE;
}
}

/* test retrieving the type */
enum eTP tp_type;
tp_type = get_thirdparty_type();
if (tp_type != tp_res[type]) {
printf("%s: ofs %i: type %i should be %i\n",
argv[0], ofs, tp_type, tp_res[type]);
return EX_SOFTWARE;
}
}
}
return EX_OK;
}

0 comments on commit 717290e

Please sign in to comment.