Mercurial > code
comparison 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 |
comparison
equal
deleted
inserted
replaced
333:412ca7a5e1ea | 334:0b576ee64d45 |
---|---|
1 /* | |
2 * main.cpp -- test the zip wrapper functions | |
3 * | |
4 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <gtest/gtest.h> | |
20 | |
21 #include <ZipArchive.h> | |
22 | |
23 using namespace source; | |
24 | |
25 /* -------------------------------------------------------- | |
26 * Sources | |
27 * -------------------------------------------------------- */ | |
28 | |
29 TEST(Source, file) | |
30 { | |
31 remove("output.zip"); | |
32 | |
33 try { | |
34 ZipArchive archive("output.zip", ZIP_CREATE); | |
35 | |
36 archive.add(File("Zip/data.txt"), "data.txt"); | |
37 } catch (const std::exception &ex) { | |
38 std::cerr << ex.what() << std::endl; | |
39 } | |
40 | |
41 try { | |
42 ZipArchive archive("output.zip"); | |
43 | |
44 auto stats = archive.stat("data.txt"); | |
45 auto file = archive.open("data.txt"); | |
46 auto content = file.read(stats.size); | |
47 | |
48 ASSERT_EQ("abcdef\n", content); | |
49 } catch (const std::exception &ex) { | |
50 std::cerr << ex.what() << std::endl; | |
51 } | |
52 } | |
53 | |
54 TEST(Source, buffer) | |
55 { | |
56 remove("output.zip"); | |
57 | |
58 try { | |
59 ZipArchive archive{"output.zip", ZIP_CREATE}; | |
60 | |
61 archive.add(Buffer{"abcdef"}, "data.txt"); | |
62 } catch (const std::exception &ex) { | |
63 std::cerr << ex.what() << std::endl; | |
64 } | |
65 | |
66 try { | |
67 ZipArchive archive{"output.zip"}; | |
68 | |
69 auto stats = archive.stat("data.txt"); | |
70 auto file = archive.open("data.txt"); | |
71 auto content = file.read(stats.size); | |
72 | |
73 ASSERT_EQ("abcdef", content); | |
74 } catch (const std::exception &ex) { | |
75 std::cerr << ex.what() << std::endl; | |
76 } | |
77 } | |
78 | |
79 /* -------------------------------------------------------- | |
80 * Write | |
81 * -------------------------------------------------------- */ | |
82 | |
83 TEST(Write, simple) | |
84 { | |
85 remove("output.zip"); | |
86 | |
87 // Open first and save some data | |
88 try { | |
89 ZipArchive archive("output.zip", ZIP_CREATE); | |
90 | |
91 archive.add(Buffer("hello world!"), "DATA"); | |
92 } catch (const std::exception &ex) { | |
93 std::cerr << "warning: " << ex.what() << std::endl; | |
94 } | |
95 | |
96 try { | |
97 ZipArchive archive("output.zip"); | |
98 | |
99 auto stats = archive.stat("DATA"); | |
100 auto file = archive.open("DATA"); | |
101 auto content = file.read(stats.size); | |
102 | |
103 ASSERT_EQ(static_cast<decltype(stats.size)>(12), stats.size); | |
104 ASSERT_EQ("hello world!", content); | |
105 } catch (const std::exception &ex) { | |
106 std::cerr << "warning: " << ex.what() << std::endl; | |
107 } | |
108 } | |
109 | |
110 /* -------------------------------------------------------- | |
111 * Reading | |
112 * -------------------------------------------------------- */ | |
113 | |
114 class ReadingTest : public testing::Test { | |
115 protected: | |
116 ZipArchive m_archive; | |
117 | |
118 public: | |
119 ReadingTest() | |
120 : m_archive("Zip/stats.zip") | |
121 { | |
122 } | |
123 }; | |
124 | |
125 TEST_F(ReadingTest, numEntries) | |
126 { | |
127 ASSERT_EQ(static_cast<ZipInt64>(4), m_archive.numEntries()); | |
128 } | |
129 | |
130 TEST_F(ReadingTest, stat) | |
131 { | |
132 try { | |
133 ZipStat stats = m_archive.stat("README"); | |
134 | |
135 ASSERT_EQ(static_cast<decltype(stats.size)>(15), stats.size); | |
136 ASSERT_STREQ("README", stats.name); | |
137 } catch (const std::exception &ex) { | |
138 std::cerr << ex.what() << std::endl; | |
139 } | |
140 } | |
141 | |
142 TEST_F(ReadingTest, read) | |
143 { | |
144 try { | |
145 auto file = m_archive.open("README"); | |
146 auto stats = m_archive.stat("README"); | |
147 auto text = file.read(stats.size); | |
148 | |
149 ASSERT_EQ("This is a test\n", text); | |
150 } catch (const std::exception &ex) { | |
151 std::cerr << "warning: " << ex.what() << std::endl; | |
152 } | |
153 } | |
154 | |
155 TEST_F(ReadingTest, increment) | |
156 { | |
157 { | |
158 ZipArchive::iterator it = m_archive.begin(); | |
159 | |
160 ASSERT_STREQ("README", (*it++).name); | |
161 } | |
162 | |
163 { | |
164 ZipArchive::iterator it = m_archive.begin(); | |
165 | |
166 ASSERT_STREQ("INSTALL", (*++it).name); | |
167 } | |
168 | |
169 { | |
170 ZipArchive::iterator it = m_archive.begin() + 1; | |
171 | |
172 ASSERT_STREQ("INSTALL", (*it).name); | |
173 } | |
174 } | |
175 | |
176 TEST_F(ReadingTest, decrement) | |
177 { | |
178 { | |
179 ZipArchive::iterator it = m_archive.begin() + 1; | |
180 | |
181 ASSERT_STREQ("INSTALL", (*it--).name); | |
182 } | |
183 | |
184 { | |
185 ZipArchive::iterator it = m_archive.begin() + 1; | |
186 | |
187 ASSERT_STREQ("README", (*--it).name); | |
188 } | |
189 | |
190 { | |
191 ZipArchive::iterator it = m_archive.end() - 1; | |
192 | |
193 ASSERT_STREQ("doc/REFMAN", (*it).name); | |
194 } | |
195 } | |
196 | |
197 TEST_F(ReadingTest, constIncrement) | |
198 { | |
199 { | |
200 ZipArchive::const_iterator it = m_archive.cbegin(); | |
201 | |
202 ASSERT_STREQ("README", (*it++).name); | |
203 } | |
204 | |
205 { | |
206 ZipArchive::const_iterator it = m_archive.cbegin(); | |
207 | |
208 ASSERT_STREQ("INSTALL", (*++it).name); | |
209 } | |
210 | |
211 { | |
212 ZipArchive::const_iterator it = m_archive.cbegin() + 1; | |
213 | |
214 ASSERT_STREQ("INSTALL", (*it).name); | |
215 } | |
216 } | |
217 | |
218 TEST_F(ReadingTest, constDecrement) | |
219 { | |
220 { | |
221 ZipArchive::const_iterator it = m_archive.cbegin() + 1; | |
222 | |
223 ASSERT_STREQ("INSTALL", (*it--).name); | |
224 } | |
225 | |
226 { | |
227 ZipArchive::const_iterator it = m_archive.cbegin() + 1; | |
228 | |
229 ASSERT_STREQ("README", (*--it).name); | |
230 } | |
231 | |
232 { | |
233 ZipArchive::const_iterator it = m_archive.cend() - 1; | |
234 | |
235 ASSERT_STREQ("doc/REFMAN", (*it).name); | |
236 } | |
237 } | |
238 | |
239 TEST_F(ReadingTest, access) | |
240 { | |
241 { | |
242 ZipArchive::iterator it = m_archive.begin(); | |
243 | |
244 ASSERT_STREQ("README", it->name); | |
245 ASSERT_STREQ("INSTALL", it[1].name); | |
246 } | |
247 | |
248 { | |
249 ZipArchive::const_iterator it = m_archive.cbegin(); | |
250 | |
251 ASSERT_STREQ("README", it->name); | |
252 ASSERT_STREQ("INSTALL", it[1].name); | |
253 } | |
254 } | |
255 | |
256 TEST_F(ReadingTest, loop) | |
257 { | |
258 std::vector<std::string> names{"README", "INSTALL", "doc/", "doc/REFMAN"}; | |
259 int i = 0; | |
260 | |
261 for (const ZipStat &s : m_archive) | |
262 ASSERT_STREQ(names[i++].c_str(), s.name); | |
263 } | |
264 | |
265 int main(int argc, char **argv) | |
266 { | |
267 testing::InitGoogleTest(&argc, argv); | |
268 | |
269 return RUN_ALL_TESTS(); | |
270 } |