From 04f275fea9186a75836b589022a9fa410aea7b02 Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 14 Feb 2011 14:09:04 +0000 Subject: Added gcov (coverage measurement) in unittests. --- server/src/luaquerymapper.cc | 145 ++++++++++++++++--------------------------- server/src/widgetvalue.cc | 42 +++++++++++-- tools/Makefile.am.test | 6 ++ tools/test | 19 +++++- tools/test.h | 16 ++--- tools/testlist | 2 +- 6 files changed, 126 insertions(+), 104 deletions(-) diff --git a/server/src/luaquerymapper.cc b/server/src/luaquerymapper.cc index aefbabf..fcdce85 100644 --- a/server/src/luaquerymapper.cc +++ b/server/src/luaquerymapper.cc @@ -204,101 +204,66 @@ std::string LUAQueryMapper::automap(const std::string &name) #ifdef TEST_LUAQUERYMAPPER +//deps: exception.cc log.cc debug.cc +//cflags: -I.. ${LUA_CFLAGS} +//libs:${LUA_LIBS} +#include -int main() -{ - QueryResult res; +TEST_BEGIN; - time_t now = time(NULL); +QueryResult res; - res.groups["test"].timestamp = now; - res.groups["test"].source = "test app"; - res.groups["test"].values["somevalue"] = "hello world"; - res.groups["test"].values["pi"] = "3.1416"; - - printf("%s\n", loadresultstring(res).c_str()); - - LUAQueryMapper mapper; - mapper.addQueryResult(res); - - // Test simple value forwarding - std::string luamap = "return test.somevalue.value, test.somevalue.timestamp, test.somevalue.source"; - Value value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "hello world" || value.timestamp != now || value.source != "test app") - return 1; - - // Do some calculations - luamap = "return 2 * tonumber(test.pi.value), test.pi.timestamp, test.pi.source"; - value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "6.2832" || value.timestamp != now || value.source != "test app") - return 1; - - // Attempt to access nonexisting value (should throw an exception) - try { - luamap = "return test.somevalue2.value, test.somevalue2.timestamp, test.somevalue2.source"; - value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "hello world" || value.timestamp != now || value.source != "test app") - return 1; - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); - goto onandon; - } - return 1; - onandon: - - // Attempt to access nonexisting group (should throw an exception) - try { - luamap = "return test2.somevalue.value, test2.somevalue.timestamp, test2.somevalue.source"; - value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "hello world" || value.timestamp != now || value.source != "test app") - return 1; - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); - goto stillonandon; - } - return 1; - stillonandon: +time_t now = time(NULL); + +res.groups["test"].timestamp = now; +res.groups["test"].source = "test app"; +res.groups["test"].values["somevalue"] = "hello world"; +res.groups["test"].values["pi"] = "3.1416"; - // Switch order of return vars (should throw an exception) - try { - luamap = "return test.somevalue.source, test.somevalue.value, test.somevalue.timestamp"; - value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "hello world" || value.timestamp != now || value.source != "test app") - return 1; - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); - goto onandonagain; - } - return 1; - onandonagain: - - // Syntax error (should throw an exception) - try { - luamap = "this(is{] not() - a != legal lua program!]"; - value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "hello world" || value.timestamp != now || value.source != "test app") - return 1; - } catch(Exception &e) { - printf("ERROR: %s\n", e.what()); - goto stillonandonagain; - } - return 1; - stillonandonagain: +//printf("%s\n", loadresultstring(res).c_str()); - // And finally test if we haven't broken enything while being hostile to the lua engine... - luamap = "return test.somevalue.value, test.somevalue.timestamp, test.somevalue.source"; - value = mapper.map(luamap); - printf("%s =>\n %s, %lu, %s\n", luamap.c_str(), value.value.c_str(), value.timestamp, value.source.c_str()); - if(value.value != "hello world" || value.timestamp != now || value.source != "test app") - return 1; +LUAQueryMapper mapper; +mapper.addQueryResult(res); - return 0; -} +// Test simple value forwarding +std::string luamap = "return test.somevalue.value, test.somevalue.timestamp, test.somevalue.source"; +Value value = mapper.map(luamap); + +TEST_EQUAL_STR(value.value, "hello world", "Test value"); +TEST_EQUAL_INT(value.timestamp, now, "Test timestamp"); +TEST_EQUAL_STR(value.source, "test app", "Test source"); + +// Do some calculations +luamap = "return 2 * tonumber(test.pi.value), test.pi.timestamp, test.pi.source"; +value = mapper.map(luamap); + +TEST_EQUAL_STR(value.value, "6.2832", "Test value"); +TEST_EQUAL_INT(value.timestamp, now, "Test timestamp"); +TEST_EQUAL_STR(value.source, "test app", "Test source"); + +// Attempt to access nonexisting value (should throw an exception) +luamap = "return test.somevalue2.value, test.somevalue2.timestamp, test.somevalue2.source"; +TEST_EXCEPTION(mapper.map(luamap), Exception, "Throw exception"); + +// Attempt to access nonexisting group (should throw an exception) +luamap = "return test2.somevalue.value, test2.somevalue.timestamp, test2.somevalue.source"; +TEST_EXCEPTION(mapper.map(luamap), Exception, "Throw exception"); + +// Switch order of return vars (should throw an exception) +luamap = "return test.somevalue.source, test.somevalue.value, test.somevalue.timestamp"; +TEST_EXCEPTION(mapper.map(luamap), Exception, "Throw exception"); + +// Syntax error (should throw an exception) +luamap = "this(is{] not() - a != legal lua program!]"; +TEST_EXCEPTION(mapper.map(luamap), Exception, "Throw exception"); + +// And finally test if we haven't broken enything while being hostile to the lua engine... +luamap = "return test.somevalue.value, test.somevalue.timestamp, test.somevalue.source"; +TEST_NOEXCEPTION(mapper.map(luamap), "Throw no exception"); +TEST_EQUAL_STR(value.value, "6.2832", "Test value"); +TEST_EQUAL_INT(value.timestamp, now, "Test timestamp"); +TEST_EQUAL_STR(value.source, "test app", "Test source"); + +TEST_END; #endif/*TEST_LUAQUERYMAPPER*/ diff --git a/server/src/widgetvalue.cc b/server/src/widgetvalue.cc index 053eecc..3b68e8e 100644 --- a/server/src/widgetvalue.cc +++ b/server/src/widgetvalue.cc @@ -40,8 +40,10 @@ static bool getMapValue(Value &value, maps_t::iterator li = maps.begin(); while(li != maps.end()) { Map &_map = *li; - if(_map.attributes["name"] == map) { - luamap = _map.attributes["lua"]; + if(_map.attributes.find("name") != _map.attributes.end() && + _map.attributes["name"] == map) { + if(_map.attributes.find("lua") != _map.attributes.end()) + luamap = _map.attributes["lua"]; } li++; } @@ -145,8 +147,7 @@ bool getValue(Value &value, TEST_BEGIN; -pracro_debug_init(); -pracro_debug_parse("+all"); +debug_parse("+all"); time_t now = time(NULL); @@ -349,6 +350,39 @@ time_t now = time(NULL); TEST_EQUAL_STR(value.source, v.source, "Got the right source?"); } +{ + Conf::db_max_ttl = 1000; + Conf::pentominos_max_ttl = 500; + + Value value; + + attr_t attr; + attr["name"] = "foo"; + attr["value"] = "hello"; + attr["map"] = "bar"; + + maps_t maps; + Map m; + char tbuf[32]; sprintf(tbuf, "%ld", now - 1); + std::string val = "le valu"; + m.attributes["name"] = "bar"; + m.attributes["lua"] = "return '"+val+"', "+tbuf+", 'artefact'"; + maps.push_back(m); + LUAQueryMapper mapper; + + Values values; + Value v; + v.value = "world"; + v.source = "pracro"; + v.timestamp = now ; + values["foo"] = v; + + TEST_TRUE(getValue(value, attr, maps, mapper, values), "Got value?"); + TEST_EQUAL_STR(value.value, v.value, "Got the right value?"); + TEST_EQUAL_INT(value.timestamp, v.timestamp, "Got the right timestamp?"); + TEST_EQUAL_STR(value.source, v.source, "Got the right source?"); +} + TEST_END; #endif/*TEST_WIDGETVALUE*/ diff --git a/tools/Makefile.am.test b/tools/Makefile.am.test index 5c5e8c7..77ca17e 100644 --- a/tools/Makefile.am.test +++ b/tools/Makefile.am.test @@ -8,6 +8,12 @@ test: Makefile.am.test $(TESTFILES) test_clean: rm -f $(TESTFILES) $(TESTLOGS) +test_report: + lcov --directory . --capture --output-file app.info + genhtml -o lcov app.info + +test_all: test_clean test test_report + TESTLOGS = `for F in ${TESTFILES}; do echo $$F.log; done` CLEANFILES = $(TESTFILES) $(TESTLOGS) Makefile.am.test *~ diff --git a/tools/test b/tools/test index a52609d..1ebf87d 100755 --- a/tools/test +++ b/tools/test @@ -7,13 +7,30 @@ DEFINE=TEST_$UPPER SCRIPTDIR=`dirname $0` -COMPILE="g++ -DHAVE_CONFIG_H -I$SCRIPTDIR -g -Wall -Werror -D$DEFINE -o $OUTPUT $*" +INFILE=$1 +shift +OBJFILES="" +for f in $TEST_DEPS +do + of=`echo -n $f | cut -d'.' -f1`.o; + OBJFILES="$OBJFILES $of" +done + +COMMON_FLAGS="-DHAVE_CONFIG_H -I$SCRIPTDIR -g -D$DEFINE $TEST_LIBS $TEST_CFLAGS" +CLEAN="rm -f $OBJFILES" +PRECOMPILE="g++ -c $TEST_DEPS $COMMON_FLAGS" +COMPILE="g++ -fprofile-arcs -ftest-coverage -Wall -Werror $COMMON_FLAGS -o $OUTPUT $INFILE $OBJFILES" echo -e "\033[0;2mTesting $TEST:" echo Testing $TEST: > $OUTPUT.log echo -n "* Compiling $TEST test" echo Compiling $TEST test: > $OUTPUT.log + +echo ${CLEAN} >> $OUTPUT.log +${CLEAN} >> ${OUTPUT}.log 2>&1 +echo ${PRECOMPILE} >> $OUTPUT.log +${PRECOMPILE} >> ${OUTPUT}.log 2>&1 echo ${COMPILE} >> $OUTPUT.log if ${COMPILE} >> ${OUTPUT}.log 2>&1; then diff --git a/tools/test.h b/tools/test.h index f470162..4770a1e 100644 --- a/tools/test.h +++ b/tools/test.h @@ -109,11 +109,11 @@ #define TEST_EQUAL_STR(x, y, fmt...) { \ TEST_BASE(fmt); \ - std::string s1 = x; \ - std::string s2 = y; \ + std::string __s1 = x; \ + std::string __s2 = y; \ fprintf(stderr, "Comparing: \"%s\" == \"%s\"\n", \ - s1.c_str(), s2.c_str()); \ - if(s1 == s2) { \ + __s1.c_str(), __s2.c_str()); \ + if(__s1 == __s2) { \ TEST_OK(#x" and "#y" are equal."); \ } else { \ TEST_FAIL(#x" and "#y" are not equal."); \ @@ -122,11 +122,11 @@ #define TEST_NOTEQUAL_STR(x, y, fmt...) { \ TEST_BASE(fmt); \ - std::string s1 = x; \ - std::string s2 = y; \ + std::string __s1 = x; \ + std::string __s2 = y; \ fprintf(stderr, "Comparing: \"%s\" != \"%s\"\n", \ - s1.c_str(), s2.c_str()); \ - if(s1 != s2) { \ + __s1.c_str(), __s2.c_str()); \ + if(__s1 != __s2) { \ TEST_OK(#x" and "#y" not are equal."); \ } else { \ TEST_FAIL(#x" and "#y" are equal."); \ diff --git a/tools/testlist b/tools/testlist index c42824f..01a48e2 100755 --- a/tools/testlist +++ b/tools/testlist @@ -24,7 +24,7 @@ do CFLAGS=`cat $FILE | grep "cflags:" | cut -d':' -f2` TEST=test_$NAME echo "$TEST: $FILE $DEPS" - echo -e "\t@${SCRIPTDIR}/test $FILE $DEPS $CFLAGS $LIBS" + echo -e "\t@TEST_DEPS=\"$DEPS\" TEST_CFLAGS=\"$CFLAGS\" TEST_LIBS=\"$LIBS\" ${SCRIPTDIR}/test $FILE" echo "" done < tmp -- cgit v1.2.3