sparsetable_unittest.cc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. // Copyright (c) 2005, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // ---
  30. //
  31. // Since sparsetable is templatized, it's important that we test every
  32. // function in every class in this file -- not just to see if it
  33. // works, but even if it compiles.
  34. #include <sparsehash/internal/sparseconfig.h>
  35. #include <config.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <sys/types.h> // for size_t
  39. #include <stdlib.h> // defines unlink() on some windows platforms(?)
  40. #ifdef HAVE_UNISTD_H
  41. # include <unistd.h>
  42. #endif // for unlink()
  43. #include <memory> // for allocator
  44. #include <string>
  45. #include <sparsehash/sparsetable>
  46. using std::string;
  47. using std::allocator;
  48. using GOOGLE_NAMESPACE::sparsetable;
  49. using GOOGLE_NAMESPACE::DEFAULT_SPARSEGROUP_SIZE;
  50. typedef u_int16_t uint16;
  51. string FLAGS_test_tmpdir = "/tmp/";
  52. // Many sparsetable operations return a size_t. Rather than have to
  53. // use PRIuS everywhere, we'll just cast to a "big enough" value.
  54. #define UL(x) ( static_cast<unsigned long>(x) )
  55. static char outbuf[10240]; // big enough for these tests
  56. static char* out = outbuf; // where to write next
  57. #define LEFT (outbuf + sizeof(outbuf) - out)
  58. #define TEST(cond) out += snprintf(out, LEFT, #cond "? %s\n", \
  59. (cond) ? "yes" : "no");
  60. inline string AsString(int n) {
  61. const int N = 64;
  62. char buf[N];
  63. snprintf(buf, N, "%d", n);
  64. return string(buf);
  65. }
  66. // Test sparsetable with a POD type, int.
  67. void TestInt() {
  68. out += snprintf(out, LEFT, "int test\n");
  69. sparsetable<int> x(7), y(70), z;
  70. x.set(4, 10);
  71. y.set(12, -12);
  72. y.set(47, -47);
  73. y.set(48, -48);
  74. y.set(49, -49);
  75. const sparsetable<int> constx(x);
  76. const sparsetable<int> consty(y);
  77. // ----------------------------------------------------------------------
  78. // Test the plain iterators
  79. for ( sparsetable<int>::iterator it = x.begin(); it != x.end(); ++it ) {
  80. out += snprintf(out, LEFT, "x[%lu]: %d\n", UL(it - x.begin()), int(*it));
  81. }
  82. for ( sparsetable<int>::const_iterator it = x.begin(); it != x.end(); ++it ) {
  83. out += snprintf(out, LEFT, "x[%lu]: %d\n", UL(it - x.begin()), *it);
  84. }
  85. for ( sparsetable<int>::reverse_iterator it = x.rbegin(); it != x.rend(); ++it ) {
  86. out += snprintf(out, LEFT, "x[%lu]: %d\n", UL(x.rend()-1 - it), int(*it));
  87. }
  88. for ( sparsetable<int>::const_reverse_iterator it = constx.rbegin(); it != constx.rend(); ++it ) {
  89. out += snprintf(out, LEFT, "x[%lu]: %d\n", UL(constx.rend()-1 - it), *it);
  90. }
  91. for ( sparsetable<int>::iterator it = z.begin(); it != z.end(); ++it ) {
  92. out += snprintf(out, LEFT, "z[%lu]: %d\n", UL(it - z.begin()), int(*it));
  93. }
  94. { // array version
  95. out += snprintf(out, LEFT, "x[3]: %d\n", int(x[3]));
  96. out += snprintf(out, LEFT, "x[4]: %d\n", int(x[4]));
  97. out += snprintf(out, LEFT, "x[5]: %d\n", int(x[5]));
  98. }
  99. {
  100. sparsetable<int>::iterator it; // non-const version
  101. out += snprintf(out, LEFT, "x[4]: %d\n", int(x.begin()[4]));
  102. it = x.begin() + 4; // should point to the non-zero value
  103. out += snprintf(out, LEFT, "x[4]: %d\n", int(*it));
  104. it--;
  105. --it;
  106. it += 5;
  107. it -= 2;
  108. it++;
  109. ++it;
  110. it = it - 3;
  111. it = 1 + it; // now at 5
  112. out += snprintf(out, LEFT, "x[3]: %d\n", int(it[-2]));
  113. out += snprintf(out, LEFT, "x[4]: %d\n", int(it[-1]));
  114. *it = 55;
  115. out += snprintf(out, LEFT, "x[5]: %d\n", int(it[0]));
  116. out += snprintf(out, LEFT, "x[5]: %d\n", int(*it));
  117. int *x6 = &(it[1]);
  118. *x6 = 66;
  119. out += snprintf(out, LEFT, "x[6]: %d\n", int(*(it + 1)));
  120. // Let's test comparitors as well
  121. TEST(it == it);
  122. TEST(!(it != it));
  123. TEST(!(it < it));
  124. TEST(!(it > it));
  125. TEST(it <= it);
  126. TEST(it >= it);
  127. sparsetable<int>::iterator it_minus_1 = it - 1;
  128. TEST(!(it == it_minus_1));
  129. TEST(it != it_minus_1);
  130. TEST(!(it < it_minus_1));
  131. TEST(it > it_minus_1);
  132. TEST(!(it <= it_minus_1));
  133. TEST(it >= it_minus_1);
  134. TEST(!(it_minus_1 == it));
  135. TEST(it_minus_1 != it);
  136. TEST(it_minus_1 < it);
  137. TEST(!(it_minus_1 > it));
  138. TEST(it_minus_1 <= it);
  139. TEST(!(it_minus_1 >= it));
  140. sparsetable<int>::iterator it_plus_1 = it + 1;
  141. TEST(!(it == it_plus_1));
  142. TEST(it != it_plus_1);
  143. TEST(it < it_plus_1);
  144. TEST(!(it > it_plus_1));
  145. TEST(it <= it_plus_1);
  146. TEST(!(it >= it_plus_1));
  147. TEST(!(it_plus_1 == it));
  148. TEST(it_plus_1 != it);
  149. TEST(!(it_plus_1 < it));
  150. TEST(it_plus_1 > it);
  151. TEST(!(it_plus_1 <= it));
  152. TEST(it_plus_1 >= it);
  153. }
  154. {
  155. sparsetable<int>::const_iterator it; // const version
  156. out += snprintf(out, LEFT, "x[4]: %d\n", int(x.begin()[4]));
  157. it = x.begin() + 4; // should point to the non-zero value
  158. out += snprintf(out, LEFT, "x[4]: %d\n", *it);
  159. it--;
  160. --it;
  161. it += 5;
  162. it -= 2;
  163. it++;
  164. ++it;
  165. it = it - 3;
  166. it = 1 + it; // now at 5
  167. out += snprintf(out, LEFT, "x[3]: %d\n", it[-2]);
  168. out += snprintf(out, LEFT, "x[4]: %d\n", it[-1]);
  169. out += snprintf(out, LEFT, "x[5]: %d\n", *it);
  170. out += snprintf(out, LEFT, "x[6]: %d\n", *(it + 1));
  171. // Let's test comparitors as well
  172. TEST(it == it);
  173. TEST(!(it != it));
  174. TEST(!(it < it));
  175. TEST(!(it > it));
  176. TEST(it <= it);
  177. TEST(it >= it);
  178. sparsetable<int>::const_iterator it_minus_1 = it - 1;
  179. TEST(!(it == it_minus_1));
  180. TEST(it != it_minus_1);
  181. TEST(!(it < it_minus_1));
  182. TEST(it > it_minus_1);
  183. TEST(!(it <= it_minus_1));
  184. TEST(it >= it_minus_1);
  185. TEST(!(it_minus_1 == it));
  186. TEST(it_minus_1 != it);
  187. TEST(it_minus_1 < it);
  188. TEST(!(it_minus_1 > it));
  189. TEST(it_minus_1 <= it);
  190. TEST(!(it_minus_1 >= it));
  191. sparsetable<int>::const_iterator it_plus_1 = it + 1;
  192. TEST(!(it == it_plus_1));
  193. TEST(it != it_plus_1);
  194. TEST(it < it_plus_1);
  195. TEST(!(it > it_plus_1));
  196. TEST(it <= it_plus_1);
  197. TEST(!(it >= it_plus_1));
  198. TEST(!(it_plus_1 == it));
  199. TEST(it_plus_1 != it);
  200. TEST(!(it_plus_1 < it));
  201. TEST(it_plus_1 > it);
  202. TEST(!(it_plus_1 <= it));
  203. TEST(it_plus_1 >= it);
  204. }
  205. TEST(x.begin() == x.begin() + 1 - 1);
  206. TEST(x.begin() < x.end());
  207. TEST(z.begin() < z.end());
  208. TEST(z.begin() <= z.end());
  209. TEST(z.begin() == z.end());
  210. // ----------------------------------------------------------------------
  211. // Test the non-empty iterators
  212. for ( sparsetable<int>::nonempty_iterator it = x.nonempty_begin(); it != x.nonempty_end(); ++it ) {
  213. out += snprintf(out, LEFT, "x[??]: %d\n", *it);
  214. }
  215. for ( sparsetable<int>::const_nonempty_iterator it = y.nonempty_begin(); it != y.nonempty_end(); ++it ) {
  216. out += snprintf(out, LEFT, "y[??]: %d\n", *it);
  217. }
  218. for ( sparsetable<int>::reverse_nonempty_iterator it = y.nonempty_rbegin(); it != y.nonempty_rend(); ++it ) {
  219. out += snprintf(out, LEFT, "y[??]: %d\n", *it);
  220. }
  221. for ( sparsetable<int>::const_reverse_nonempty_iterator it = consty.nonempty_rbegin(); it != consty.nonempty_rend(); ++it ) {
  222. out += snprintf(out, LEFT, "y[??]: %d\n", *it);
  223. }
  224. for ( sparsetable<int>::nonempty_iterator it = z.nonempty_begin(); it != z.nonempty_end(); ++it ) {
  225. out += snprintf(out, LEFT, "z[??]: %d\n", *it);
  226. }
  227. {
  228. sparsetable<int>::nonempty_iterator it; // non-const version
  229. out += snprintf(out, LEFT, "first non-empty y: %d\n", *y.nonempty_begin());
  230. out += snprintf(out, LEFT, "first non-empty x: %d\n", *x.nonempty_begin());
  231. it = x.nonempty_begin();
  232. ++it; // should be at end
  233. --it;
  234. out += snprintf(out, LEFT, "first non-empty x: %d\n", *it++);
  235. it--;
  236. out += snprintf(out, LEFT, "first non-empty x: %d\n", *it++);
  237. }
  238. {
  239. sparsetable<int>::const_nonempty_iterator it; // non-const version
  240. out += snprintf(out, LEFT, "first non-empty y: %d\n", *y.nonempty_begin());
  241. out += snprintf(out, LEFT, "first non-empty x: %d\n", *x.nonempty_begin());
  242. it = x.nonempty_begin();
  243. ++it; // should be at end
  244. --it;
  245. out += snprintf(out, LEFT, "first non-empty x: %d\n", *it++);
  246. it--;
  247. out += snprintf(out, LEFT, "first non-empty x: %d\n", *it++);
  248. }
  249. TEST(x.begin() == x.begin() + 1 - 1);
  250. TEST(z.begin() != z.end());
  251. // ----------------------------------------------------------------------
  252. // Test the non-empty iterators get_pos function
  253. sparsetable<unsigned int> gp(100);
  254. for (int i = 0; i < 100; i += 9) {
  255. gp.set(i,i);
  256. }
  257. for (sparsetable<unsigned int>::const_nonempty_iterator
  258. it = gp.nonempty_begin(); it != gp.nonempty_end(); ++it) {
  259. out += snprintf(out, LEFT,
  260. "get_pos() for const nonempty_iterator: %u == %lu\n",
  261. *it, UL(gp.get_pos(it)));
  262. }
  263. for (sparsetable<unsigned int>::nonempty_iterator
  264. it = gp.nonempty_begin(); it != gp.nonempty_end(); ++it) {
  265. out += snprintf(out, LEFT,
  266. "get_pos() for nonempty_iterator: %u == %lu\n",
  267. *it, UL(gp.get_pos(it)));
  268. }
  269. // ----------------------------------------------------------------------
  270. // Test sparsetable functions
  271. out += snprintf(out, LEFT, "x has %lu/%lu buckets, "
  272. "y %lu/%lu, z %lu/%lu\n",
  273. UL(x.num_nonempty()), UL(x.size()),
  274. UL(y.num_nonempty()), UL(y.size()),
  275. UL(z.num_nonempty()), UL(z.size()));
  276. y.resize(48); // should get rid of 48 and 49
  277. y.resize(70); // 48 and 49 should still be gone
  278. out += snprintf(out, LEFT, "y shrank and grew: it's now %lu/%lu\n",
  279. UL(y.num_nonempty()), UL(y.size()));
  280. out += snprintf(out, LEFT, "y[12] = %d, y.get(12) = %d\n", int(y[12]), y.get(12));
  281. y.erase(12);
  282. out += snprintf(out, LEFT, "y[12] cleared. y now %lu/%lu. "
  283. "y[12] = %d, y.get(12) = %d\n",
  284. UL(y.num_nonempty()), UL(y.size()), int(y[12]), y.get(12));
  285. swap(x, y);
  286. y.clear();
  287. TEST(y == z);
  288. y.resize(70);
  289. for ( int i = 10; i < 40; ++i )
  290. y[i] = -i;
  291. y.erase(y.begin() + 15, y.begin() + 30);
  292. y.erase(y.begin() + 34);
  293. y.erase(12);
  294. y.resize(38);
  295. y.resize(10000);
  296. y[9898] = -9898;
  297. for ( sparsetable<int>::const_iterator it = y.begin(); it != y.end(); ++it ) {
  298. if ( y.test(it) )
  299. out += snprintf(out, LEFT, "y[%lu] is set\n", UL(it - y.begin()));
  300. }
  301. out += snprintf(out, LEFT, "That's %lu set buckets\n", UL(y.num_nonempty()));
  302. out += snprintf(out, LEFT, "Starting from y[32]...\n");
  303. for ( sparsetable<int>::const_nonempty_iterator it = y.get_iter(32);
  304. it != y.nonempty_end(); ++it )
  305. out += snprintf(out, LEFT, "y[??] = %d\n", *it);
  306. out += snprintf(out, LEFT, "From y[32] down...\n");
  307. for ( sparsetable<int>::nonempty_iterator it = y.get_iter(32);
  308. it != y.nonempty_begin(); )
  309. out += snprintf(out, LEFT, "y[??] = %d\n", *--it);
  310. // ----------------------------------------------------------------------
  311. // Test I/O using deprecated read/write_metadata
  312. string filestr = FLAGS_test_tmpdir + "/.sparsetable.test";
  313. const char *file = filestr.c_str();
  314. FILE *fp = fopen(file, "wb");
  315. if ( fp == NULL ) {
  316. // maybe we can't write to /tmp/. Try the current directory
  317. file = ".sparsetable.test";
  318. fp = fopen(file, "wb");
  319. }
  320. if ( fp == NULL ) {
  321. out += snprintf(out, LEFT, "Can't open %s, skipping disk write...\n", file);
  322. } else {
  323. y.write_metadata(fp); // only write meta-information
  324. y.write_nopointer_data(fp);
  325. fclose(fp);
  326. }
  327. fp = fopen(file, "rb");
  328. if ( fp == NULL ) {
  329. out += snprintf(out, LEFT, "Can't open %s, skipping disk read...\n", file);
  330. } else {
  331. sparsetable<int> y2;
  332. y2.read_metadata(fp);
  333. y2.read_nopointer_data(fp);
  334. fclose(fp);
  335. for ( sparsetable<int>::const_iterator it = y2.begin(); it != y2.end(); ++it ) {
  336. if ( y2.test(it) )
  337. out += snprintf(out, LEFT, "y2[%lu] is %d\n", UL(it - y2.begin()), *it);
  338. }
  339. out += snprintf(out, LEFT, "That's %lu set buckets\n", UL(y2.num_nonempty()));
  340. }
  341. unlink(file);
  342. // ----------------------------------------------------------------------
  343. // Also test I/O using serialize()/unserialize()
  344. fp = fopen(file, "wb");
  345. if ( fp == NULL ) {
  346. out += snprintf(out, LEFT, "Can't open %s, skipping disk write...\n", file);
  347. } else {
  348. y.serialize(sparsetable<int>::NopointerSerializer(), fp);
  349. fclose(fp);
  350. }
  351. fp = fopen(file, "rb");
  352. if ( fp == NULL ) {
  353. out += snprintf(out, LEFT, "Can't open %s, skipping disk read...\n", file);
  354. } else {
  355. sparsetable<int> y2;
  356. y2.unserialize(sparsetable<int>::NopointerSerializer(), fp);
  357. fclose(fp);
  358. for ( sparsetable<int>::const_iterator it = y2.begin(); it != y2.end(); ++it ) {
  359. if ( y2.test(it) )
  360. out += snprintf(out, LEFT, "y2[%lu] is %d\n", UL(it - y2.begin()), *it);
  361. }
  362. out += snprintf(out, LEFT, "That's %lu set buckets\n", UL(y2.num_nonempty()));
  363. }
  364. unlink(file);
  365. }
  366. // Test sparsetable with a non-POD type, std::string
  367. void TestString() {
  368. out += snprintf(out, LEFT, "string test\n");
  369. sparsetable<string> x(7), y(70), z;
  370. x.set(4, "foo");
  371. y.set(12, "orange");
  372. y.set(47, "grape");
  373. y.set(48, "pear");
  374. y.set(49, "apple");
  375. // ----------------------------------------------------------------------
  376. // Test the plain iterators
  377. for ( sparsetable<string>::iterator it = x.begin(); it != x.end(); ++it ) {
  378. out += snprintf(out, LEFT, "x[%lu]: %s\n",
  379. UL(it - x.begin()), static_cast<string>(*it).c_str());
  380. }
  381. for ( sparsetable<string>::iterator it = z.begin(); it != z.end(); ++it ) {
  382. out += snprintf(out, LEFT, "z[%lu]: %s\n",
  383. UL(it - z.begin()), static_cast<string>(*it).c_str());
  384. }
  385. TEST(x.begin() == x.begin() + 1 - 1);
  386. TEST(x.begin() < x.end());
  387. TEST(z.begin() < z.end());
  388. TEST(z.begin() <= z.end());
  389. TEST(z.begin() == z.end());
  390. // ----------------------------------------------------------------------
  391. // Test the non-empty iterators
  392. for ( sparsetable<string>::nonempty_iterator it = x.nonempty_begin(); it != x.nonempty_end(); ++it ) {
  393. out += snprintf(out, LEFT, "x[??]: %s\n", it->c_str());
  394. }
  395. for ( sparsetable<string>::const_nonempty_iterator it = y.nonempty_begin(); it != y.nonempty_end(); ++it ) {
  396. out += snprintf(out, LEFT, "y[??]: %s\n", it->c_str());
  397. }
  398. for ( sparsetable<string>::nonempty_iterator it = z.nonempty_begin(); it != z.nonempty_end(); ++it ) {
  399. out += snprintf(out, LEFT, "z[??]: %s\n", it->c_str());
  400. }
  401. // ----------------------------------------------------------------------
  402. // Test sparsetable functions
  403. out += snprintf(out, LEFT, "x has %lu/%lu buckets, y %lu/%lu, z %lu/%lu\n",
  404. UL(x.num_nonempty()), UL(x.size()),
  405. UL(y.num_nonempty()), UL(y.size()),
  406. UL(z.num_nonempty()), UL(z.size()));
  407. y.resize(48); // should get rid of 48 and 49
  408. y.resize(70); // 48 and 49 should still be gone
  409. out += snprintf(out, LEFT, "y shrank and grew: it's now %lu/%lu\n",
  410. UL(y.num_nonempty()), UL(y.size()));
  411. out += snprintf(out, LEFT, "y[12] = %s, y.get(12) = %s\n",
  412. static_cast<string>(y[12]).c_str(), y.get(12).c_str());
  413. y.erase(12);
  414. out += snprintf(out, LEFT, "y[12] cleared. y now %lu/%lu. "
  415. "y[12] = %s, y.get(12) = %s\n",
  416. UL(y.num_nonempty()), UL(y.size()),
  417. static_cast<string>(y[12]).c_str(),
  418. static_cast<string>(y.get(12)).c_str());
  419. swap(x, y);
  420. y.clear();
  421. TEST(y == z);
  422. y.resize(70);
  423. for ( int i = 10; i < 40; ++i )
  424. y.set(i, AsString(-i));
  425. y.erase(y.begin() + 15, y.begin() + 30);
  426. y.erase(y.begin() + 34);
  427. y.erase(12);
  428. y.resize(38);
  429. y.resize(10000);
  430. y.set(9898, AsString(-9898));
  431. for ( sparsetable<string>::const_iterator it = y.begin(); it != y.end(); ++it ) {
  432. if ( y.test(it) )
  433. out += snprintf(out, LEFT, "y[%lu] is set\n", UL(it - y.begin()));
  434. }
  435. out += snprintf(out, LEFT, "That's %lu set buckets\n", UL(y.num_nonempty()));
  436. out += snprintf(out, LEFT, "Starting from y[32]...\n");
  437. for ( sparsetable<string>::const_nonempty_iterator it = y.get_iter(32);
  438. it != y.nonempty_end(); ++it )
  439. out += snprintf(out, LEFT, "y[??] = %s\n", it->c_str());
  440. out += snprintf(out, LEFT, "From y[32] down...\n");
  441. for ( sparsetable<string>::nonempty_iterator it = y.get_iter(32);
  442. it != y.nonempty_begin(); )
  443. out += snprintf(out, LEFT, "y[??] = %s\n", (*--it).c_str());
  444. }
  445. // An instrumented allocator that keeps track of all calls to
  446. // allocate/deallocate/construct/destroy. It stores the number of times
  447. // they were called and the values they were called with. Such information is
  448. // stored in the following global variables.
  449. static size_t sum_allocate_bytes;
  450. static size_t sum_deallocate_bytes;
  451. void ResetAllocatorCounters() {
  452. sum_allocate_bytes = 0;
  453. sum_deallocate_bytes = 0;
  454. }
  455. template <class T> class instrumented_allocator {
  456. public:
  457. typedef T value_type;
  458. typedef uint16 size_type;
  459. typedef ptrdiff_t difference_type;
  460. typedef T* pointer;
  461. typedef const T* const_pointer;
  462. typedef T& reference;
  463. typedef const T& const_reference;
  464. instrumented_allocator() {}
  465. instrumented_allocator(const instrumented_allocator&) {}
  466. ~instrumented_allocator() {}
  467. pointer address(reference r) const { return &r; }
  468. const_pointer address(const_reference r) const { return &r; }
  469. pointer allocate(size_type n, const_pointer = 0) {
  470. sum_allocate_bytes += n * sizeof(value_type);
  471. return static_cast<pointer>(malloc(n * sizeof(value_type)));
  472. }
  473. void deallocate(pointer p, size_type n) {
  474. sum_deallocate_bytes += n * sizeof(value_type);
  475. free(p);
  476. }
  477. size_type max_size() const {
  478. return static_cast<size_type>(-1) / sizeof(value_type);
  479. }
  480. void construct(pointer p, const value_type& val) {
  481. new(p) value_type(val);
  482. }
  483. void destroy(pointer p) {
  484. p->~value_type();
  485. }
  486. template <class U>
  487. explicit instrumented_allocator(const instrumented_allocator<U>&) {}
  488. template<class U>
  489. struct rebind {
  490. typedef instrumented_allocator<U> other;
  491. };
  492. private:
  493. void operator=(const instrumented_allocator&);
  494. };
  495. template<class T>
  496. inline bool operator==(const instrumented_allocator<T>&,
  497. const instrumented_allocator<T>&) {
  498. return true;
  499. }
  500. template<class T>
  501. inline bool operator!=(const instrumented_allocator<T>&,
  502. const instrumented_allocator<T>&) {
  503. return false;
  504. }
  505. // Test sparsetable with instrumented_allocator.
  506. void TestAllocator() {
  507. out += snprintf(out, LEFT, "allocator test\n");
  508. ResetAllocatorCounters();
  509. // POD (int32) with instrumented_allocator.
  510. typedef sparsetable<int, DEFAULT_SPARSEGROUP_SIZE,
  511. instrumented_allocator<int> > IntSparseTable;
  512. IntSparseTable* s1 = new IntSparseTable(10000);
  513. TEST(sum_allocate_bytes > 0);
  514. for (int i = 0; i < 10000; ++i) {
  515. s1->set(i, 0);
  516. }
  517. TEST(sum_allocate_bytes >= 10000 * sizeof(int));
  518. ResetAllocatorCounters();
  519. delete s1;
  520. TEST(sum_deallocate_bytes >= 10000 * sizeof(int));
  521. IntSparseTable* s2 = new IntSparseTable(1000);
  522. IntSparseTable* s3 = new IntSparseTable(1000);
  523. for (int i = 0; i < 1000; ++i) {
  524. s2->set(i, 0);
  525. s3->set(i, 0);
  526. }
  527. TEST(sum_allocate_bytes >= 2000 * sizeof(int));
  528. ResetAllocatorCounters();
  529. s3->clear();
  530. TEST(sum_deallocate_bytes >= 1000 * sizeof(int));
  531. ResetAllocatorCounters();
  532. s2->swap(*s3); // s2 is empty after the swap
  533. s2->clear();
  534. TEST(sum_deallocate_bytes < 1000 * sizeof(int));
  535. for (int i = 0; i < s3->size(); ++i) {
  536. s3->erase(i);
  537. }
  538. TEST(sum_deallocate_bytes >= 1000 * sizeof(int));
  539. delete s2;
  540. delete s3;
  541. // POD (int) with default allocator.
  542. sparsetable<int> x, y;
  543. for (int s = 1000; s <= 40000; s += 1000) {
  544. x.resize(s);
  545. for (int i = 0; i < s; ++i) {
  546. x.set(i, i + 1);
  547. }
  548. y = x;
  549. for (int i = 0; i < s; ++i) {
  550. y.erase(i);
  551. }
  552. y.swap(x);
  553. }
  554. TEST(x.num_nonempty() == 0);
  555. out += snprintf(out, LEFT, "y[0]: %d\n", int(y[0]));
  556. out += snprintf(out, LEFT, "y[39999]: %d\n", int(y[39999]));
  557. y.clear();
  558. // POD (int) with std allocator.
  559. sparsetable<int, DEFAULT_SPARSEGROUP_SIZE, allocator<int> > u, v;
  560. for (int s = 1000; s <= 40000; s += 1000) {
  561. u.resize(s);
  562. for (int i = 0; i < s; ++i) {
  563. u.set(i, i + 1);
  564. }
  565. v = u;
  566. for (int i = 0; i < s; ++i) {
  567. v.erase(i);
  568. }
  569. v.swap(u);
  570. }
  571. TEST(u.num_nonempty() == 0);
  572. out += snprintf(out, LEFT, "v[0]: %d\n", int(v[0]));
  573. out += snprintf(out, LEFT, "v[39999]: %d\n", int(v[39999]));
  574. v.clear();
  575. // Non-POD (string) with default allocator.
  576. sparsetable<string> a, b;
  577. for (int s = 1000; s <= 40000; s += 1000) {
  578. a.resize(s);
  579. for (int i = 0; i < s; ++i) {
  580. a.set(i, "aa");
  581. }
  582. b = a;
  583. for (int i = 0; i < s; ++i) {
  584. b.erase(i);
  585. }
  586. b.swap(a);
  587. }
  588. TEST(a.num_nonempty() == 0);
  589. out += snprintf(out, LEFT, "b[0]: %s\n", b.get(0).c_str());
  590. out += snprintf(out, LEFT, "b[39999]: %s\n", b.get(39999).c_str());
  591. b.clear();
  592. }
  593. // The expected output from all of the above: TestInt(), TestString() and
  594. // TestAllocator().
  595. static const char g_expected[] = (
  596. "int test\n"
  597. "x[0]: 0\n"
  598. "x[1]: 0\n"
  599. "x[2]: 0\n"
  600. "x[3]: 0\n"
  601. "x[4]: 10\n"
  602. "x[5]: 0\n"
  603. "x[6]: 0\n"
  604. "x[0]: 0\n"
  605. "x[1]: 0\n"
  606. "x[2]: 0\n"
  607. "x[3]: 0\n"
  608. "x[4]: 10\n"
  609. "x[5]: 0\n"
  610. "x[6]: 0\n"
  611. "x[6]: 0\n"
  612. "x[5]: 0\n"
  613. "x[4]: 10\n"
  614. "x[3]: 0\n"
  615. "x[2]: 0\n"
  616. "x[1]: 0\n"
  617. "x[0]: 0\n"
  618. "x[6]: 0\n"
  619. "x[5]: 0\n"
  620. "x[4]: 10\n"
  621. "x[3]: 0\n"
  622. "x[2]: 0\n"
  623. "x[1]: 0\n"
  624. "x[0]: 0\n"
  625. "x[3]: 0\n"
  626. "x[4]: 10\n"
  627. "x[5]: 0\n"
  628. "x[4]: 10\n"
  629. "x[4]: 10\n"
  630. "x[3]: 0\n"
  631. "x[4]: 10\n"
  632. "x[5]: 55\n"
  633. "x[5]: 55\n"
  634. "x[6]: 66\n"
  635. "it == it? yes\n"
  636. "!(it != it)? yes\n"
  637. "!(it < it)? yes\n"
  638. "!(it > it)? yes\n"
  639. "it <= it? yes\n"
  640. "it >= it? yes\n"
  641. "!(it == it_minus_1)? yes\n"
  642. "it != it_minus_1? yes\n"
  643. "!(it < it_minus_1)? yes\n"
  644. "it > it_minus_1? yes\n"
  645. "!(it <= it_minus_1)? yes\n"
  646. "it >= it_minus_1? yes\n"
  647. "!(it_minus_1 == it)? yes\n"
  648. "it_minus_1 != it? yes\n"
  649. "it_minus_1 < it? yes\n"
  650. "!(it_minus_1 > it)? yes\n"
  651. "it_minus_1 <= it? yes\n"
  652. "!(it_minus_1 >= it)? yes\n"
  653. "!(it == it_plus_1)? yes\n"
  654. "it != it_plus_1? yes\n"
  655. "it < it_plus_1? yes\n"
  656. "!(it > it_plus_1)? yes\n"
  657. "it <= it_plus_1? yes\n"
  658. "!(it >= it_plus_1)? yes\n"
  659. "!(it_plus_1 == it)? yes\n"
  660. "it_plus_1 != it? yes\n"
  661. "!(it_plus_1 < it)? yes\n"
  662. "it_plus_1 > it? yes\n"
  663. "!(it_plus_1 <= it)? yes\n"
  664. "it_plus_1 >= it? yes\n"
  665. "x[4]: 10\n"
  666. "x[4]: 10\n"
  667. "x[3]: 0\n"
  668. "x[4]: 10\n"
  669. "x[5]: 55\n"
  670. "x[6]: 66\n"
  671. "it == it? yes\n"
  672. "!(it != it)? yes\n"
  673. "!(it < it)? yes\n"
  674. "!(it > it)? yes\n"
  675. "it <= it? yes\n"
  676. "it >= it? yes\n"
  677. "!(it == it_minus_1)? yes\n"
  678. "it != it_minus_1? yes\n"
  679. "!(it < it_minus_1)? yes\n"
  680. "it > it_minus_1? yes\n"
  681. "!(it <= it_minus_1)? yes\n"
  682. "it >= it_minus_1? yes\n"
  683. "!(it_minus_1 == it)? yes\n"
  684. "it_minus_1 != it? yes\n"
  685. "it_minus_1 < it? yes\n"
  686. "!(it_minus_1 > it)? yes\n"
  687. "it_minus_1 <= it? yes\n"
  688. "!(it_minus_1 >= it)? yes\n"
  689. "!(it == it_plus_1)? yes\n"
  690. "it != it_plus_1? yes\n"
  691. "it < it_plus_1? yes\n"
  692. "!(it > it_plus_1)? yes\n"
  693. "it <= it_plus_1? yes\n"
  694. "!(it >= it_plus_1)? yes\n"
  695. "!(it_plus_1 == it)? yes\n"
  696. "it_plus_1 != it? yes\n"
  697. "!(it_plus_1 < it)? yes\n"
  698. "it_plus_1 > it? yes\n"
  699. "!(it_plus_1 <= it)? yes\n"
  700. "it_plus_1 >= it? yes\n"
  701. "x.begin() == x.begin() + 1 - 1? yes\n"
  702. "x.begin() < x.end()? yes\n"
  703. "z.begin() < z.end()? no\n"
  704. "z.begin() <= z.end()? yes\n"
  705. "z.begin() == z.end()? yes\n"
  706. "x[??]: 10\n"
  707. "x[??]: 55\n"
  708. "x[??]: 66\n"
  709. "y[??]: -12\n"
  710. "y[??]: -47\n"
  711. "y[??]: -48\n"
  712. "y[??]: -49\n"
  713. "y[??]: -49\n"
  714. "y[??]: -48\n"
  715. "y[??]: -47\n"
  716. "y[??]: -12\n"
  717. "y[??]: -49\n"
  718. "y[??]: -48\n"
  719. "y[??]: -47\n"
  720. "y[??]: -12\n"
  721. "first non-empty y: -12\n"
  722. "first non-empty x: 10\n"
  723. "first non-empty x: 10\n"
  724. "first non-empty x: 10\n"
  725. "first non-empty y: -12\n"
  726. "first non-empty x: 10\n"
  727. "first non-empty x: 10\n"
  728. "first non-empty x: 10\n"
  729. "x.begin() == x.begin() + 1 - 1? yes\n"
  730. "z.begin() != z.end()? no\n"
  731. "get_pos() for const nonempty_iterator: 0 == 0\n"
  732. "get_pos() for const nonempty_iterator: 9 == 9\n"
  733. "get_pos() for const nonempty_iterator: 18 == 18\n"
  734. "get_pos() for const nonempty_iterator: 27 == 27\n"
  735. "get_pos() for const nonempty_iterator: 36 == 36\n"
  736. "get_pos() for const nonempty_iterator: 45 == 45\n"
  737. "get_pos() for const nonempty_iterator: 54 == 54\n"
  738. "get_pos() for const nonempty_iterator: 63 == 63\n"
  739. "get_pos() for const nonempty_iterator: 72 == 72\n"
  740. "get_pos() for const nonempty_iterator: 81 == 81\n"
  741. "get_pos() for const nonempty_iterator: 90 == 90\n"
  742. "get_pos() for const nonempty_iterator: 99 == 99\n"
  743. "get_pos() for nonempty_iterator: 0 == 0\n"
  744. "get_pos() for nonempty_iterator: 9 == 9\n"
  745. "get_pos() for nonempty_iterator: 18 == 18\n"
  746. "get_pos() for nonempty_iterator: 27 == 27\n"
  747. "get_pos() for nonempty_iterator: 36 == 36\n"
  748. "get_pos() for nonempty_iterator: 45 == 45\n"
  749. "get_pos() for nonempty_iterator: 54 == 54\n"
  750. "get_pos() for nonempty_iterator: 63 == 63\n"
  751. "get_pos() for nonempty_iterator: 72 == 72\n"
  752. "get_pos() for nonempty_iterator: 81 == 81\n"
  753. "get_pos() for nonempty_iterator: 90 == 90\n"
  754. "get_pos() for nonempty_iterator: 99 == 99\n"
  755. "x has 3/7 buckets, y 4/70, z 0/0\n"
  756. "y shrank and grew: it's now 2/70\n"
  757. "y[12] = -12, y.get(12) = -12\n"
  758. "y[12] cleared. y now 1/70. y[12] = 0, y.get(12) = 0\n"
  759. "y == z? no\n"
  760. "y[10] is set\n"
  761. "y[11] is set\n"
  762. "y[13] is set\n"
  763. "y[14] is set\n"
  764. "y[30] is set\n"
  765. "y[31] is set\n"
  766. "y[32] is set\n"
  767. "y[33] is set\n"
  768. "y[35] is set\n"
  769. "y[36] is set\n"
  770. "y[37] is set\n"
  771. "y[9898] is set\n"
  772. "That's 12 set buckets\n"
  773. "Starting from y[32]...\n"
  774. "y[??] = -32\n"
  775. "y[??] = -33\n"
  776. "y[??] = -35\n"
  777. "y[??] = -36\n"
  778. "y[??] = -37\n"
  779. "y[??] = -9898\n"
  780. "From y[32] down...\n"
  781. "y[??] = -31\n"
  782. "y[??] = -30\n"
  783. "y[??] = -14\n"
  784. "y[??] = -13\n"
  785. "y[??] = -11\n"
  786. "y[??] = -10\n"
  787. "y2[10] is -10\n"
  788. "y2[11] is -11\n"
  789. "y2[13] is -13\n"
  790. "y2[14] is -14\n"
  791. "y2[30] is -30\n"
  792. "y2[31] is -31\n"
  793. "y2[32] is -32\n"
  794. "y2[33] is -33\n"
  795. "y2[35] is -35\n"
  796. "y2[36] is -36\n"
  797. "y2[37] is -37\n"
  798. "y2[9898] is -9898\n"
  799. "That's 12 set buckets\n"
  800. "y2[10] is -10\n"
  801. "y2[11] is -11\n"
  802. "y2[13] is -13\n"
  803. "y2[14] is -14\n"
  804. "y2[30] is -30\n"
  805. "y2[31] is -31\n"
  806. "y2[32] is -32\n"
  807. "y2[33] is -33\n"
  808. "y2[35] is -35\n"
  809. "y2[36] is -36\n"
  810. "y2[37] is -37\n"
  811. "y2[9898] is -9898\n"
  812. "That's 12 set buckets\n"
  813. "string test\n"
  814. "x[0]: \n"
  815. "x[1]: \n"
  816. "x[2]: \n"
  817. "x[3]: \n"
  818. "x[4]: foo\n"
  819. "x[5]: \n"
  820. "x[6]: \n"
  821. "x.begin() == x.begin() + 1 - 1? yes\n"
  822. "x.begin() < x.end()? yes\n"
  823. "z.begin() < z.end()? no\n"
  824. "z.begin() <= z.end()? yes\n"
  825. "z.begin() == z.end()? yes\n"
  826. "x[??]: foo\n"
  827. "y[??]: orange\n"
  828. "y[??]: grape\n"
  829. "y[??]: pear\n"
  830. "y[??]: apple\n"
  831. "x has 1/7 buckets, y 4/70, z 0/0\n"
  832. "y shrank and grew: it's now 2/70\n"
  833. "y[12] = orange, y.get(12) = orange\n"
  834. "y[12] cleared. y now 1/70. y[12] = , y.get(12) = \n"
  835. "y == z? no\n"
  836. "y[10] is set\n"
  837. "y[11] is set\n"
  838. "y[13] is set\n"
  839. "y[14] is set\n"
  840. "y[30] is set\n"
  841. "y[31] is set\n"
  842. "y[32] is set\n"
  843. "y[33] is set\n"
  844. "y[35] is set\n"
  845. "y[36] is set\n"
  846. "y[37] is set\n"
  847. "y[9898] is set\n"
  848. "That's 12 set buckets\n"
  849. "Starting from y[32]...\n"
  850. "y[??] = -32\n"
  851. "y[??] = -33\n"
  852. "y[??] = -35\n"
  853. "y[??] = -36\n"
  854. "y[??] = -37\n"
  855. "y[??] = -9898\n"
  856. "From y[32] down...\n"
  857. "y[??] = -31\n"
  858. "y[??] = -30\n"
  859. "y[??] = -14\n"
  860. "y[??] = -13\n"
  861. "y[??] = -11\n"
  862. "y[??] = -10\n"
  863. "allocator test\n"
  864. "sum_allocate_bytes > 0? yes\n"
  865. "sum_allocate_bytes >= 10000 * sizeof(int)? yes\n"
  866. "sum_deallocate_bytes >= 10000 * sizeof(int)? yes\n"
  867. "sum_allocate_bytes >= 2000 * sizeof(int)? yes\n"
  868. "sum_deallocate_bytes >= 1000 * sizeof(int)? yes\n"
  869. "sum_deallocate_bytes < 1000 * sizeof(int)? yes\n"
  870. "sum_deallocate_bytes >= 1000 * sizeof(int)? yes\n"
  871. "x.num_nonempty() == 0? yes\n"
  872. "y[0]: 1\n"
  873. "y[39999]: 40000\n"
  874. "u.num_nonempty() == 0? yes\n"
  875. "v[0]: 1\n"
  876. "v[39999]: 40000\n"
  877. "a.num_nonempty() == 0? yes\n"
  878. "b[0]: aa\n"
  879. "b[39999]: aa\n"
  880. );
  881. // defined at bottom of file for ease of maintainence
  882. int main(int argc, char **argv) { // though we ignore the args
  883. (void)argc;
  884. (void)argv;
  885. TestInt();
  886. TestString();
  887. TestAllocator();
  888. // Finally, check to see if our output (in out) is what it's supposed to be.
  889. const size_t r = sizeof(g_expected) - 1;
  890. if ( r != static_cast<size_t>(out - outbuf) || // output not the same size
  891. memcmp(outbuf, g_expected, r) ) { // or bytes differed
  892. fprintf(stderr, "TESTS FAILED\n\nEXPECTED:\n\n%s\n\nACTUAL:\n\n%s\n\n",
  893. g_expected, outbuf);
  894. return 1;
  895. } else {
  896. printf("PASS.\n");
  897. return 0;
  898. }
  899. }