From 3dbda2dda2268f5cb213b18d4e42a889d52c16ec Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Thu, 17 Oct 2013 00:19:22 +0200
Subject: [PATCH] test/test_archive: unit test for archive_lookup()

---
 Makefile.am           | 14 ++++++++
 test/test_archive.cxx | 76 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+)
 create mode 100644 test/test_archive.cxx

diff --git a/Makefile.am b/Makefile.am
index e37d49f15..569f66d18 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1033,6 +1033,10 @@ C_TESTS = \
 	test/test_pcm \
 	test/test_queue_priority
 
+if ENABLE_ARCHIVE
+C_TESTS += test/test_archive
+endif
+
 TESTS = $(C_TESTS)
 
 noinst_PROGRAMS = \
@@ -1435,6 +1439,16 @@ test_test_pcm_LDADD = \
 	$(CPPUNIT_LIBS) \
 	$(GLIB_LIBS)
 
+test_test_archive_SOURCES = \
+	src/Log.cxx \
+	test/test_archive.cxx
+test_test_archive_CPPFLAGS = $(AM_CPPFLAGS) $(CPPUNIT_CFLAGS) -DCPPUNIT_HAVE_RTTI=0
+test_test_archive_CXXFLAGS = $(AM_CXXFLAGS) -Wno-error=deprecated-declarations
+test_test_archive_LDADD = \
+	libarchive.a \
+	$(GLIB_LIBS) \
+	$(CPPUNIT_LIBS)
+
 test_test_queue_priority_SOURCES = \
 	src/Queue.cxx \
 	test/test_queue_priority.cxx
diff --git a/test/test_archive.cxx b/test/test_archive.cxx
new file mode 100644
index 000000000..2a2952885
--- /dev/null
+++ b/test/test_archive.cxx
@@ -0,0 +1,76 @@
+#include "config.h"
+#include "ArchiveLookup.hxx"
+#include "Compiler.h"
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <glib.h>
+
+#include <string.h>
+
+class ArchiveLookupTest : public CppUnit::TestFixture {
+	CPPUNIT_TEST_SUITE(ArchiveLookupTest);
+	CPPUNIT_TEST(TestArchiveLookup);
+	CPPUNIT_TEST_SUITE_END();
+
+public:
+	void TestArchiveLookup();
+};
+
+void
+ArchiveLookupTest::TestArchiveLookup()
+{
+	char *archive, *inpath, *suffix;
+
+	char *path = strdup("");
+	CPPUNIT_ASSERT_EQUAL(false,
+			     archive_lookup(path, &archive, &inpath, &suffix));
+	g_free(path);
+
+	path = strdup(".");
+	CPPUNIT_ASSERT_EQUAL(false,
+			     archive_lookup(path, &archive, &inpath, &suffix));
+	g_free(path);
+
+	path = strdup("config.h");
+	CPPUNIT_ASSERT_EQUAL(false,
+			     archive_lookup(path, &archive, &inpath, &suffix));
+	g_free(path);
+
+	path = strdup("src/foo/bar");
+	CPPUNIT_ASSERT_EQUAL(false,
+			     archive_lookup(path, &archive, &inpath, &suffix));
+	g_free(path);
+
+	path = strdup("Makefile/foo/bar");
+	CPPUNIT_ASSERT_EQUAL(true,
+			     archive_lookup(path, &archive, &inpath, &suffix));
+	CPPUNIT_ASSERT_EQUAL(path, archive);
+	CPPUNIT_ASSERT_EQUAL(0, strcmp(archive, "Makefile"));
+	CPPUNIT_ASSERT_EQUAL(0, strcmp(inpath, "foo/bar"));
+	CPPUNIT_ASSERT_EQUAL((char *)nullptr, suffix);
+	g_free(path);
+
+	path = strdup("config.h/foo/bar");
+	CPPUNIT_ASSERT_EQUAL(true,
+			     archive_lookup(path, &archive, &inpath, &suffix));
+	CPPUNIT_ASSERT_EQUAL(path, archive);
+	CPPUNIT_ASSERT_EQUAL(0, strcmp(archive, "config.h"));
+	CPPUNIT_ASSERT_EQUAL(0, strcmp(inpath, "foo/bar"));
+	CPPUNIT_ASSERT_EQUAL(0, strcmp(suffix, "h"));
+	g_free(path);
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(ArchiveLookupTest);
+
+int
+main(gcc_unused int argc, gcc_unused char **argv)
+{
+	CppUnit::TextUi::TestRunner runner;
+	auto &registry = CppUnit::TestFactoryRegistry::getRegistry();
+	runner.addTest(registry.makeTest());
+	return runner.run() ? EXIT_SUCCESS : EXIT_FAILURE;
+}