From 997a75ecfe0ea36962a03550e576132e327a5b16 Mon Sep 17 00:00:00 2001 From: skarg Date: Sat, 6 Mar 2010 00:22:26 +0000 Subject: [PATCH] Added unit test to timer module. --- bacnet-stack/ports/bdk-atxx4-mstp/timer.c | 105 ++++++++++++++++++++++ bacnet-stack/ports/bdk-atxx4-mstp/timer.h | 1 - bacnet-stack/test/timer.mak | 32 +++++++ 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 bacnet-stack/test/timer.mak diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/timer.c b/bacnet-stack/ports/bdk-atxx4-mstp/timer.c index c5d868fe..9d5bdf96 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/timer.c +++ b/bacnet-stack/ports/bdk-atxx4-mstp/timer.c @@ -260,3 +260,108 @@ void timer_interval_restart( t->start = timer_milliseconds(); } } + +#ifdef TEST +#include +#include + +#include "ctest.h" + +static uint32_t Milliseconds; + +uint32_t timer_milliseconds(void) +{ + return Milliseconds; +} + +uint32_t timer_milliseconds_set( + uint32_t value) +{ + uint32_t old_value = Milliseconds; + + Milliseconds = value; + + return old_value; +} + +void testElapsedTimer( + Test * pTest) +{ + struct etimer t; + uint32_t test_time = 0; + + timer_milliseconds_set(test_time); + timer_elapsed_start(&t); + ct_test(pTest, timer_elapsed_time(&t) == test_time); + test_time = 0xffff; + timer_milliseconds_set(test_time); + ct_test(pTest, timer_elapsed_time(&t) == test_time); + test_time = 0xffffffff; + timer_milliseconds_set(test_time); + ct_test(pTest, timer_elapsed_time(&t) == test_time); +} + +void testIntervalTimer( + Test * pTest) +{ + struct itimer t; + uint32_t interval = 0; + uint32_t test_time = 0; + + timer_milliseconds_set(test_time); + timer_interval_start(&t, interval); + test_time = 0xffff; + timer_milliseconds_set(test_time); + ct_test(pTest, timer_interval(&t) == interval); + ct_test(pTest, timer_interval_elapsed(&t) == test_time); + test_time = 0xffffffff; + timer_milliseconds_set(test_time); + ct_test(pTest, timer_interval(&t) == interval); + ct_test(pTest, timer_interval_elapsed(&t) == test_time); + test_time = 0; + timer_milliseconds_set(test_time); + interval = 0xffff; + timer_interval_start(&t, interval); + ct_test(pTest, timer_interval(&t) == interval); + interval = 0xffffffff; + timer_interval_start(&t, interval); + ct_test(pTest, timer_interval(&t) == interval); + + interval = 0; + timer_interval_start_seconds(&t, interval); + ct_test(pTest, timer_interval(&t) == interval); + interval = 60L; + timer_interval_start_seconds(&t, interval); + interval *= 1000L; + ct_test(pTest, timer_interval(&t) == interval); + +} + + +#ifdef TEST_TIMER +int main( + void) +{ + Test *pTest; + bool rc; + + pTest = ct_create("Timer", NULL); + + /* individual tests */ + rc = ct_addTestFunction(pTest, testElapsedTimer); + assert(rc); + rc = ct_addTestFunction(pTest, testIntervalTimer); + assert(rc); + + + ct_setStream(pTest, stdout); + ct_run(pTest); + (void) ct_report(pTest); + + ct_destroy(pTest); + + return 0; +} +#endif +#endif + diff --git a/bacnet-stack/ports/bdk-atxx4-mstp/timer.h b/bacnet-stack/ports/bdk-atxx4-mstp/timer.h index 0c6b2016..38f6eefe 100644 --- a/bacnet-stack/ports/bdk-atxx4-mstp/timer.h +++ b/bacnet-stack/ports/bdk-atxx4-mstp/timer.h @@ -26,7 +26,6 @@ #include #include -#include "hardware.h" /* Timer Module */ diff --git a/bacnet-stack/test/timer.mak b/bacnet-stack/test/timer.mak new file mode 100644 index 00000000..2019d6a8 --- /dev/null +++ b/bacnet-stack/test/timer.mak @@ -0,0 +1,32 @@ +#Makefile to build test case +CC = gcc +SRC_DIR = ../ports/bdk-atxx4-mstp +INCLUDES = -I../include -I${SRC_DIR} -I. +DEFINES = -DBIG_ENDIAN=0 -DTEST -DTEST_TIMER + +CFLAGS = -Wall $(INCLUDES) $(DEFINES) -g + +SRCS = $(SRC_DIR)/timer.c \ + ctest.c + +TARGET = timer + +all: ${TARGET} + +OBJS = ${SRCS:.c=.o} + +${TARGET}: ${OBJS} + ${CC} -o $@ ${OBJS} + +.c.o: + ${CC} -c ${CFLAGS} $*.c -o $@ + +depend: + rm -f .depend + ${CC} -MM ${CFLAGS} *.c >> .depend + +clean: + rm -rf core ${TARGET} $(OBJS) *.bak *.1 *.ini + +include: .depend +