diff C++/Tests/Zip/main.cpp @ 313:cd490a8ab82a

Zip: add iterators to ZipArchive Task: #331
author David Demelier <markand@malikania.fr>
date Wed, 25 Feb 2015 12:42:06 +0100
parents 2ff6559c5f1d
children
line wrap: on
line diff
--- a/C++/Tests/Zip/main.cpp	Fri Feb 13 18:02:07 2015 +0100
+++ b/C++/Tests/Zip/main.cpp	Wed Feb 25 12:42:06 2015 +0100
@@ -23,7 +23,7 @@
 using namespace source;
 
 /* --------------------------------------------------------
- * File source
+ * Sources
  * -------------------------------------------------------- */
 
 TEST(Source, file)
@@ -77,49 +77,10 @@
 }
 
 /* --------------------------------------------------------
- * Basic
+ * Write
  * -------------------------------------------------------- */
 
-TEST(Basic, numEntries)
-{
-	try {
-		ZipArchive archive{SOURCE "/data/stats.zip"};
-
-		ASSERT_EQ(static_cast<ZipInt64>(1), archive.numEntries());
-	} catch (const std::exception &ex) {
-		std::cerr << ex.what() << std::endl;
-	}
-}
-
-TEST(Basic, stat)
-{
-	try {
-		ZipArchive archive{SOURCE "/data/stats.zip"};
-		ZipStat stats = 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(Basic, read)
-{
-	try {
-		ZipArchive archive{SOURCE "/data/stats.zip"};
-
-		auto file = archive.open("README");
-		auto stats = 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(Basic, write)
+TEST(Write, simple)
 {
 	remove(BINARY "/output.zip");
 
@@ -146,6 +107,161 @@
 	}
 }
 
+/* --------------------------------------------------------
+ * Reading
+ * -------------------------------------------------------- */
+
+class ReadingTest : public testing::Test {
+protected:
+	ZipArchive m_archive;
+
+public:
+	ReadingTest()
+		: m_archive(SOURCE "/data/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);