From c7b77ae329e61480079e98fbcfd35d90ff5dcb0e Mon Sep 17 00:00:00 2001 From: Davi de Castro Reis Date: Sat, 2 Jun 2012 21:47:18 -0300 Subject: [PATCH] Improved test support. --- cxxmph/Makefile.am | 20 ++++++++++++++------ cxxmph/map_tester.h | 20 ++------------------ cxxmph/map_tester_test.cc | 31 ++++++++----------------------- cxxmph/mph_map_test.cc | 17 +++++++++++++++-- cxxmph/test.cc | 16 ++++++++++++++++ cxxmph/test.h | 31 +++++++++++++++++++++++++++++++ cxxmph/test_test.cc | 4 ++++ 7 files changed, 90 insertions(+), 49 deletions(-) create mode 100644 cxxmph/test.cc create mode 100644 cxxmph/test.h create mode 100644 cxxmph/test_test.cc diff --git a/cxxmph/Makefile.am b/cxxmph/Makefile.am index 5db78af..08cdc44 100644 --- a/cxxmph/Makefile.am +++ b/cxxmph/Makefile.am @@ -1,14 +1,22 @@ TESTS = $(check_PROGRAMS) -check_PROGRAMS = seeded_hash_test mph_bits_test hollow_iterator_test mph_map_test mph_index_test trigraph_test map_tester_test string_util_test +check_PROGRAMS = test_test seeded_hash_test mph_bits_test hollow_iterator_test mph_map_test mph_index_test trigraph_test map_tester_test string_util_test noinst_PROGRAMS = bm_index bm_map bin_PROGRAMS = cxxmph -lib_LTLIBRARIES = libcxxmph.la -libcxxmph_la_SOURCES = MurmurHash3.h MurmurHash3.cpp trigragh.h trigraph.cc mph_bits.h mph_bits.cc mph_index.h mph_index.cc seeded_hash.h stringpiece.h benchmark.h benchmark.cc string_util.cc -libcxxmph_la_LDFLAGS = -version-info 0:0:0 + cxxmph_includedir = $(includedir)/cxxmph/ cxxmph_include_HEADERS = mph_map.h mph_index.h MurmurHash3.h trigraph.h seeded_hash.h stringpiece.h hollow_iterator.h -mph_map_test_LDADD = libcxxmph.la +noinst_LTLIBRARIES = libcxxmph_test.la +lib_LTLIBRARIES = libcxxmph.la +libcxxmph_la_SOURCES = MurmurHash3.h MurmurHash3.cpp trigragh.h trigraph.cc mph_bits.h mph_bits.cc mph_index.h mph_index.cc seeded_hash.h stringpiece.h benchmark.h benchmark.cc string_util.cc +libcxxmph_la_LDFLAGS = -version-info 0:0:0 +libcxxmph_test_la_SOURCES = test.h test.cc +libcxxmph_test_la_LIBADD = libcxxmph.la + +test_test_SOURCES = test_test.cc +test_test_LDADD = libcxxmph_test.la $(CHECK_LIBS) + +mph_map_test_LDADD = libcxxmph_test.la $(CHECK_LIBS) mph_map_test_SOURCES = mph_map_test.cc mph_index_test_LDADD = libcxxmph.la @@ -38,5 +46,5 @@ string_util_test_SOURCES = string_util_test.cc string_util_test_LDADD = libcxxmph.la map_tester_test_SOURCES = map_tester.cc map_tester_test.cc -map_tester_test_LDADD = $(CHECK_LIBS) +map_tester_test_LDADD = libcxxmph.la libcxxmph_test.la $(CHECK_LIBS) diff --git a/cxxmph/map_tester.h b/cxxmph/map_tester.h index 811860c..ea2b358 100644 --- a/cxxmph/map_tester.h +++ b/cxxmph/map_tester.h @@ -14,21 +14,7 @@ namespace cxxmph { using namespace std; template class map_type> -class MapTester { - public: - MapTester() {} - ~MapTester() {} - - bool Run(string* errors) const { - string e; - if (!small_insert()) e += "small insert failed\n"; - if (!large_insert()) e += "large insert failed\n"; - if (!small_search()) e += "small search failed\n"; - if (!default_search()) e += "default search failed\n"; - if (!large_search()) e += "large search failed\n"; - if (errors) *errors = e; - return !e.empty(); - } +struct MapTester { static bool small_insert() { map_type m; // Start counting from 1 to not touch default constructed value bugs @@ -68,9 +54,7 @@ class MapTester { int nkeys = 10 * 1000; vector keys; for (int i = 0; i < nkeys; ++i) { - char buf[128]; - cxxmph::format("%v", i); - keys.push_back(buf); + keys.push_back(cxxmph::format("%v", i)); } map_type m; for (int i = 0; i < nkeys; ++i) m.insert(make_pair(keys[i], i)); diff --git a/cxxmph/map_tester_test.cc b/cxxmph/map_tester_test.cc index 655418c..d83697d 100644 --- a/cxxmph/map_tester_test.cc +++ b/cxxmph/map_tester_test.cc @@ -1,28 +1,13 @@ #include "map_tester.h" -#include +#include "test.h" using namespace cxxmph; -START_TEST(search) { - MapTester tester; +typedef MapTester Tester; -} END_TEST - -Suite *MapTesterSuite() { - Suite *s = suite_create ("MapTester"); - /* Core test case */ - TCase *tc_core = tcase_create ("Core"); - tcase_add_test (tc_core, search); - suite_add_tcase (s, tc_core); - return s; -} - -int main (void) { - int number_failed; - Suite *s = MapTesterSuite(); - SRunner *sr = srunner_create (s); - srunner_run_all (sr, CK_NORMAL); - number_failed = srunner_ntests_failed (sr); - srunner_free (sr); - return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; -} +CXXMPH_CXX_TEST_CASE(small_insert, Tester::small_insert); +CXXMPH_CXX_TEST_CASE(large_insert, Tester::large_insert); +CXXMPH_CXX_TEST_CASE(small_search, Tester::small_search); +CXXMPH_CXX_TEST_CASE(default_search, Tester::default_search); +CXXMPH_CXX_TEST_CASE(large_search, Tester::large_search); +CXXMPH_CXX_TEST_CASE(string_search, Tester::string_search); diff --git a/cxxmph/mph_map_test.cc b/cxxmph/mph_map_test.cc index dd8eb5a..3310ad4 100644 --- a/cxxmph/mph_map_test.cc +++ b/cxxmph/mph_map_test.cc @@ -4,7 +4,21 @@ #include #include "mph_map.h" +#include "map_tester.h" +#include "test.h" +using namespace cxxmph; + +typedef MapTester Tester; + +CXXMPH_CXX_TEST_CASE(small_insert, Tester::small_insert); +CXXMPH_CXX_TEST_CASE(large_insert, Tester::large_insert); +CXXMPH_CXX_TEST_CASE(small_search, Tester::small_search); +CXXMPH_CXX_TEST_CASE(default_search, Tester::default_search); +CXXMPH_CXX_TEST_CASE(large_search, Tester::large_search); +CXXMPH_CXX_TEST_CASE(string_search, Tester::string_search); + +/* using std::make_pair; using std::string; using cxxmph::mph_map; @@ -27,7 +41,6 @@ int main(int argc, char** argv) { exit(-1); } } - /* mph_map h; h.insert(std::make_pair("-1",-1)); mph_map::const_iterator it; @@ -57,5 +70,5 @@ int main(int argc, char** argv) { if (key < num_valid && it->second != key) exit(-1); } } - */ } +*/ diff --git a/cxxmph/test.cc b/cxxmph/test.cc new file mode 100644 index 0000000..366e39b --- /dev/null +++ b/cxxmph/test.cc @@ -0,0 +1,16 @@ +#include // For EXIT_SUCCESS, EXIT_FAILURE + +#include "test.h" + +Suite* global_suite = suite_create("cxxmph_test_suite"); +TCase* global_tc_core = tcase_create("Core"); + +int main (void) { + suite_add_tcase(global_suite, global_tc_core); + int number_failed; + SRunner *sr = srunner_create (global_suite); + srunner_run_all (sr, CK_NORMAL); + number_failed = srunner_ntests_failed (sr); + srunner_free (sr); + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/cxxmph/test.h b/cxxmph/test.h new file mode 100644 index 0000000..3054b6f --- /dev/null +++ b/cxxmph/test.h @@ -0,0 +1,31 @@ +#ifndef __CXXMPH_TEST_H__ +#define __CXXMPH_TEST_H__ + +// Thin wrapper on top of check.h to get rid of boilerplate in tests. Assumes a +// single test suite and test case per file, with each fixture represented by a +// parameter-less boolean function. +// +// The check.h header macro-clashes with c++ libraries so this file needs to be +// included last. + +#include + +extern Suite* global_suite; +extern TCase* global_tc_core; + +// Creates a new test case calling boolean_function. Name must be a valid, +// unique c identifier when prefixed with tc_. +#define CXXMPH_CXX_TEST_CASE(name, boolean_function) \ + START_TEST(tc_ ## name) \ + { fail_unless(boolean_function()); } END_TEST \ + static TestCase global_cxxmph_tc_ ## name(tc_ ## name); + +#define CXXMPH_TEST_CASE(name) CXXMPH_CXX_TEST_CASE(name, name) + +struct TestCase { + TestCase(void (*f)(int)) { + tcase_add_test(global_tc_core, f); + } +}; + +#endif // __CXXMPH_TEST_H__ diff --git a/cxxmph/test_test.cc b/cxxmph/test_test.cc new file mode 100644 index 0000000..a9116a3 --- /dev/null +++ b/cxxmph/test_test.cc @@ -0,0 +1,4 @@ +#include "test.h" + +bool tautology() { return true; } +CXXMPH_TEST_CASE(tautology)