From 4907f610d6116949111fb6ff81c1489ec68b9d43 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 4 Sep 2014 15:15:58 +0200 Subject: [PATCH] test/test_protocol: unit test for protocol/ArgParser.cxx --- .gitignore | 1 + Makefile.am | 11 ++++++++ test/test_protocol.cxx | 60 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 test/test_protocol.cxx diff --git a/.gitignore b/.gitignore index 9b052a29f..790eac57b 100644 --- a/.gitignore +++ b/.gitignore @@ -63,6 +63,7 @@ test/run_normalize test/tmp test/run_inotify test/test_queue_priority +test/test_protocol test/run_ntp_server test/run_resolver test/run_tcp_connect diff --git a/Makefile.am b/Makefile.am index 6a5e9fb27..147a15bf8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1064,6 +1064,7 @@ C_TESTS = \ test/test_mixramp \ test/test_icy_parser \ test/test_pcm \ + test/test_protocol \ test/test_queue_priority if ENABLE_ARCHIVE @@ -1538,6 +1539,16 @@ test_test_archive_LDADD = \ $(GLIB_LIBS) \ $(CPPUNIT_LIBS) +test_test_protocol_SOURCES = \ + src/protocol/ArgParser.cxx \ + test/test_protocol.cxx +test_test_protocol_CPPFLAGS = $(AM_CPPFLAGS) $(CPPUNIT_CFLAGS) -DCPPUNIT_HAVE_RTTI=0 +test_test_protocol_CXXFLAGS = $(AM_CXXFLAGS) -Wno-error=deprecated-declarations +test_test_protocol_LDADD = \ + libsystem.a \ + libutil.a \ + $(CPPUNIT_LIBS) + test_test_queue_priority_SOURCES = \ src/Queue.cxx \ test/test_queue_priority.cxx diff --git a/test/test_protocol.cxx b/test/test_protocol.cxx new file mode 100644 index 000000000..d7ea7cd87 --- /dev/null +++ b/test/test_protocol.cxx @@ -0,0 +1,60 @@ +#include "config.h" +#include "protocol/ArgParser.hxx" +#include "protocol/Result.hxx" +#include "Compiler.h" + +#include +#include +#include +#include + +static enum ack last_error = ack(-1); + +void +command_error(gcc_unused Client &client, enum ack error, + gcc_unused const char *fmt, ...) +{ + last_error = error; +} + +class ArgParserTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(ArgParserTest); + CPPUNIT_TEST(TestRange); + CPPUNIT_TEST_SUITE_END(); + +public: + void TestRange(); +}; + +void +ArgParserTest::TestRange() +{ + Client &client = *(Client *)nullptr; + unsigned a, b; + + CPPUNIT_ASSERT(check_range(client, &a, &b, "1")); + CPPUNIT_ASSERT_EQUAL(1u, a); + CPPUNIT_ASSERT_EQUAL(2u, b); + + CPPUNIT_ASSERT(check_range(client, &a, &b, "1:5")); + CPPUNIT_ASSERT_EQUAL(1u, a); + CPPUNIT_ASSERT_EQUAL(5u, b); + + CPPUNIT_ASSERT(check_range(client, &a, &b, "1:")); + CPPUNIT_ASSERT_EQUAL(1u, a); + CPPUNIT_ASSERT(b >= 999999u); + + CPPUNIT_ASSERT(!check_range(client, &a, &b, "-2")); + CPPUNIT_ASSERT_EQUAL(ACK_ERROR_ARG, last_error); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(ArgParserTest); + +int +main(gcc_unused int argc, gcc_unused char **argv) +{ + CppUnit::TextUi::TestRunner runner; + auto ®istry = CppUnit::TestFactoryRegistry::getRegistry(); + runner.addTest(registry.makeTest()); + return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE; +}