diff C++/tests/Zip/main.cpp @ 334:0b576ee64d45

* Create brand new hierarchy * Rename DynLib to Dynlib * Remove some warnings
author David Demelier <markand@malikania.fr>
date Sun, 08 Mar 2015 14:26:33 +0100
parents C++/Tests/Zip/main.cpp@cd490a8ab82a
children c293dbe181c0
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/tests/Zip/main.cpp	Sun Mar 08 14:26:33 2015 +0100
@@ -0,0 +1,270 @@
+/*
+ * main.cpp -- test the zip wrapper functions
+ *
+ * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <gtest/gtest.h>
+
+#include <ZipArchive.h>
+
+using namespace source;
+
+/* --------------------------------------------------------
+ * Sources
+ * -------------------------------------------------------- */
+
+TEST(Source, file)
+{
+	remove("output.zip");
+
+	try {
+		ZipArchive archive("output.zip", ZIP_CREATE);
+
+		archive.add(File("Zip/data.txt"), "data.txt");
+	} catch (const std::exception &ex) {
+		std::cerr << ex.what() << std::endl;
+	}
+
+	try {
+		ZipArchive archive("output.zip");
+
+		auto stats = archive.stat("data.txt");
+		auto file = archive.open("data.txt");
+		auto content = file.read(stats.size);
+
+		ASSERT_EQ("abcdef\n", content);
+	} catch (const std::exception &ex) {
+		std::cerr << ex.what() << std::endl;
+	}
+}
+
+TEST(Source, buffer)
+{
+	remove("output.zip");
+
+	try {
+		ZipArchive archive{"output.zip", ZIP_CREATE};
+
+		archive.add(Buffer{"abcdef"}, "data.txt");
+	} catch (const std::exception &ex) {
+		std::cerr << ex.what() << std::endl;
+	}
+
+	try {
+		ZipArchive archive{"output.zip"};
+
+		auto stats = archive.stat("data.txt");
+		auto file = archive.open("data.txt");
+		auto content = file.read(stats.size);
+
+		ASSERT_EQ("abcdef", content);
+	} catch (const std::exception &ex) {
+		std::cerr << ex.what() << std::endl;
+	}
+}
+
+/* --------------------------------------------------------
+ * Write
+ * -------------------------------------------------------- */
+
+TEST(Write, simple)
+{
+	remove("output.zip");
+
+	// Open first and save some data
+	try {
+		ZipArchive archive("output.zip", ZIP_CREATE);
+
+		archive.add(Buffer("hello world!"), "DATA");
+	} catch (const std::exception &ex) {
+		std::cerr << "warning: " << ex.what() << std::endl;
+	}
+
+	try {
+		ZipArchive archive("output.zip");
+
+		auto stats = archive.stat("DATA");
+		auto file = archive.open("DATA");
+		auto content = file.read(stats.size);
+
+		ASSERT_EQ(static_cast<decltype(stats.size)>(12), stats.size);
+		ASSERT_EQ("hello world!", content);
+	} catch (const std::exception &ex) {
+		std::cerr << "warning: " << ex.what() << std::endl;
+	}
+}
+
+/* --------------------------------------------------------
+ * Reading
+ * -------------------------------------------------------- */
+
+class ReadingTest : public testing::Test {
+protected:
+	ZipArchive m_archive;
+
+public:
+	ReadingTest()
+		: m_archive("Zip/stats.zip")
+	{
+	}
+};
+
+TEST_F(ReadingTest, numEntries)
+{
+	ASSERT_EQ(static_cast<ZipInt64>(4), m_archive.numEntries());
+}
+
+TEST_F(ReadingTest, stat)
+{
+	try {
+		ZipStat stats = m_archive.stat("README");
+
+		ASSERT_EQ(static_cast<decltype(stats.size)>(15), stats.size);
+		ASSERT_STREQ("README", stats.name);
+	} catch (const std::exception &ex) {
+		std::cerr << ex.what() << std::endl;
+	}
+}
+
+TEST_F(ReadingTest, read)
+{
+	try {
+		auto file = m_archive.open("README");
+		auto stats = m_archive.stat("README");
+		auto text = file.read(stats.size);
+
+		ASSERT_EQ("This is a test\n", text);
+	} catch (const std::exception &ex) {
+		std::cerr << "warning: " << ex.what() << std::endl;
+	}
+}
+
+TEST_F(ReadingTest, increment)
+{
+	{
+		ZipArchive::iterator it = m_archive.begin();
+
+		ASSERT_STREQ("README", (*it++).name);
+	}
+
+	{
+		ZipArchive::iterator it = m_archive.begin();
+
+		ASSERT_STREQ("INSTALL", (*++it).name);
+	}
+
+	{
+		ZipArchive::iterator it = m_archive.begin() + 1;
+
+		ASSERT_STREQ("INSTALL", (*it).name);
+	}
+}
+
+TEST_F(ReadingTest, decrement)
+{
+	{
+		ZipArchive::iterator it = m_archive.begin() + 1;
+
+		ASSERT_STREQ("INSTALL", (*it--).name);
+	}
+
+	{
+		ZipArchive::iterator it = m_archive.begin() + 1;
+
+		ASSERT_STREQ("README", (*--it).name);
+	}
+
+	{
+		ZipArchive::iterator it = m_archive.end() - 1;
+
+		ASSERT_STREQ("doc/REFMAN", (*it).name);
+	}
+}
+
+TEST_F(ReadingTest, constIncrement)
+{
+	{
+		ZipArchive::const_iterator it = m_archive.cbegin();
+
+		ASSERT_STREQ("README", (*it++).name);
+	}
+
+	{
+		ZipArchive::const_iterator it = m_archive.cbegin();
+
+		ASSERT_STREQ("INSTALL", (*++it).name);
+	}
+
+	{
+		ZipArchive::const_iterator it = m_archive.cbegin() + 1;
+
+		ASSERT_STREQ("INSTALL", (*it).name);
+	}
+}
+
+TEST_F(ReadingTest, constDecrement)
+{
+	{
+		ZipArchive::const_iterator it = m_archive.cbegin() + 1;
+
+		ASSERT_STREQ("INSTALL", (*it--).name);
+	}
+
+	{
+		ZipArchive::const_iterator it = m_archive.cbegin() + 1;
+
+		ASSERT_STREQ("README", (*--it).name);
+	}
+
+	{
+		ZipArchive::const_iterator it = m_archive.cend() - 1;
+
+		ASSERT_STREQ("doc/REFMAN", (*it).name);
+	}
+}
+
+TEST_F(ReadingTest, access)
+{
+	{
+		ZipArchive::iterator it = m_archive.begin();
+
+		ASSERT_STREQ("README", it->name);
+		ASSERT_STREQ("INSTALL", it[1].name);
+	}
+
+	{
+		ZipArchive::const_iterator it = m_archive.cbegin();
+
+		ASSERT_STREQ("README", it->name);
+		ASSERT_STREQ("INSTALL", it[1].name);
+	}
+}
+
+TEST_F(ReadingTest, loop)
+{
+	std::vector<std::string> names{"README", "INSTALL", "doc/", "doc/REFMAN"};
+	int i = 0;
+
+	for (const ZipStat &s : m_archive)
+		ASSERT_STREQ(names[i++].c_str(), s.name);
+}
+
+int main(int argc, char **argv)
+{
+	testing::InitGoogleTest(&argc, argv);
+
+	return RUN_ALL_TESTS();
+}