From e12bd00b474fd2e748067eee2e3289a060097b43 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 22 Jun 2015 21:29:29 +0200 Subject: [PATCH] test/TestFs: new unit test for libfs.a --- Makefile.am | 9 ++++ test/TestFs.cxx | 107 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 test/TestFs.cxx diff --git a/Makefile.am b/Makefile.am index 349fb4c37..b230e2b66 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1514,6 +1514,7 @@ C_TESTS = \ test/test_pcm \ test/test_protocol \ test/test_queue_priority \ + test/TestFs \ test/TestIcu if ENABLE_CURL @@ -2164,6 +2165,14 @@ test_test_queue_priority_LDADD = \ libutil.a \ $(CPPUNIT_LIBS) +test_TestFs_SOURCES = \ + test/TestFs.cxx +test_TestFs_CPPFLAGS = $(AM_CPPFLAGS) $(CPPUNIT_CFLAGS) -DCPPUNIT_HAVE_RTTI=0 +test_TestFs_CXXFLAGS = $(AM_CXXFLAGS) -Wno-error=deprecated-declarations +test_TestFs_LDADD = \ + $(FS_LIBS) \ + $(CPPUNIT_LIBS) + test_TestIcu_SOURCES = \ test/TestIcu.cxx test_TestIcu_CPPFLAGS = $(AM_CPPFLAGS) $(CPPUNIT_CFLAGS) -DCPPUNIT_HAVE_RTTI=0 diff --git a/test/TestFs.cxx b/test/TestFs.cxx new file mode 100644 index 000000000..aa840f848 --- /dev/null +++ b/test/TestFs.cxx @@ -0,0 +1,107 @@ +/* + * Unit tests for src/fs/ + */ + +#include "config.h" +#include "fs/Glob.hxx" +#include "util/Error.hxx" + +#include +#include +#include +#include + +#include +#include + +#ifdef HAVE_CLASS_GLOB + +class TestGlob : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(TestGlob); + CPPUNIT_TEST(Basic); + CPPUNIT_TEST(Asterisk); + CPPUNIT_TEST(QuestionMark); + CPPUNIT_TEST(Wildcard); + CPPUNIT_TEST(PrefixWildcard); + CPPUNIT_TEST(SuffixWildcard); + CPPUNIT_TEST_SUITE_END(); + +public: + void Basic() { + const Glob glob("foo"); + CPPUNIT_ASSERT(glob.Check("foo")); + CPPUNIT_ASSERT(!glob.Check("fooo")); + CPPUNIT_ASSERT(!glob.Check("_foo")); + CPPUNIT_ASSERT(!glob.Check("a/foo")); + CPPUNIT_ASSERT(!glob.Check("")); + CPPUNIT_ASSERT(!glob.Check("*")); + } + + void Asterisk() { + const Glob glob("*"); + CPPUNIT_ASSERT(glob.Check("foo")); + CPPUNIT_ASSERT(glob.Check("bar")); + CPPUNIT_ASSERT(glob.Check("*")); + CPPUNIT_ASSERT(glob.Check("?")); + } + + void QuestionMark() { + const Glob glob("foo?bar"); + CPPUNIT_ASSERT(glob.Check("foo_bar")); + CPPUNIT_ASSERT(glob.Check("foo?bar")); + CPPUNIT_ASSERT(glob.Check("foo bar")); + CPPUNIT_ASSERT(!glob.Check("foobar")); + CPPUNIT_ASSERT(!glob.Check("foo__bar")); + } + + void Wildcard() { + const Glob glob("foo*bar"); + CPPUNIT_ASSERT(glob.Check("foo_bar")); + CPPUNIT_ASSERT(glob.Check("foo?bar")); + CPPUNIT_ASSERT(glob.Check("foo bar")); + CPPUNIT_ASSERT(glob.Check("foobar")); + CPPUNIT_ASSERT(glob.Check("foo__bar")); + CPPUNIT_ASSERT(!glob.Check("_foobar")); + CPPUNIT_ASSERT(!glob.Check("foobar_")); + } + + void PrefixWildcard() { + const Glob glob("*bar"); + CPPUNIT_ASSERT(glob.Check("foo_bar")); + CPPUNIT_ASSERT(glob.Check("foo?bar")); + CPPUNIT_ASSERT(glob.Check("foo bar")); + CPPUNIT_ASSERT(glob.Check("foobar")); + CPPUNIT_ASSERT(glob.Check("foo__bar")); + CPPUNIT_ASSERT(glob.Check("_foobar")); + CPPUNIT_ASSERT(glob.Check("bar")); + CPPUNIT_ASSERT(!glob.Check("foobar_")); + } + + void SuffixWildcard() { + const Glob glob("foo*"); + CPPUNIT_ASSERT(glob.Check("foo_bar")); + CPPUNIT_ASSERT(glob.Check("foo?bar")); + CPPUNIT_ASSERT(glob.Check("foo bar")); + CPPUNIT_ASSERT(glob.Check("foobar")); + CPPUNIT_ASSERT(glob.Check("foo__bar")); + CPPUNIT_ASSERT(glob.Check("foobar_")); + CPPUNIT_ASSERT(glob.Check("foo")); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(TestGlob); + +#endif + +int +main(gcc_unused int argc, gcc_unused char **argv) +{ +#ifdef HAVE_CLASS_GLOB + CppUnit::TextUi::TestRunner runner; + auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); + runner.addTest(registry.makeTest()); + return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; +#else + return EXIT_SUCCESS; +#endif +}