packedstream_test.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * nvbio
  3. * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the NVIDIA CORPORATION nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // packedstream_test.cpp
  28. //
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <algorithm>
  33. #include <nvbio/basic/types.h>
  34. #include <nvbio/basic/cached_iterator.h>
  35. #include <nvbio/basic/packedstream.h>
  36. #include <sais.h>
  37. using namespace nvbio;
  38. static const uint32 LEN = 100;
  39. template <typename CompStream>
  40. bool check_stream(const uint8* uncomp_stream, const CompStream& comp_stream)
  41. {
  42. for (uint32 i = 0; i < LEN; ++i)
  43. {
  44. if (uncomp_stream[i] != comp_stream[i])
  45. {
  46. fprintf(stderr, " error at %u : found %u, expected %u\n", i, uint32( comp_stream[i] ), uint32( uncomp_stream[i] ));
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. template <typename CompStream>
  53. bool check_forward_stream(const uint8* uncomp_stream, CompStream comp_stream)
  54. {
  55. for (uint32 i = 0; i < LEN; ++i)
  56. {
  57. if (uncomp_stream[i] != *comp_stream)
  58. {
  59. fprintf(stderr, " forward iterator error at %u : found %u, expected %u\n", i, uint32( *comp_stream ), uint32( uncomp_stream[i] ));
  60. return false;
  61. }
  62. ++comp_stream;
  63. }
  64. return true;
  65. }
  66. int packedstream_test()
  67. {
  68. {
  69. uint32 base_stream[LEN] = { 0u };
  70. uint8 uncomp_stream[2*LEN] = { 0u };
  71. typedef PackedStream<uint32*,uint8,2,true> Stream;
  72. Stream stream( base_stream );
  73. fprintf(stderr, "2-bit stream test... started\n");
  74. if (check_stream( uncomp_stream, stream ) == false)
  75. exit(1);
  76. stream.set( 0, uint8(3) );
  77. uncomp_stream[0] = uint8(3);
  78. if (check_stream( uncomp_stream, stream ) == false)
  79. exit(1);
  80. stream.set( 2, uint8(2) );
  81. uncomp_stream[2] = uint8(2);
  82. if (check_stream( uncomp_stream, stream ) == false)
  83. exit(1);
  84. stream.set( 16, uint8(3) );
  85. uncomp_stream[16] = uint8(3);
  86. if (check_stream( uncomp_stream, stream ) == false)
  87. exit(1);
  88. stream.set( 17, uint8(1) );
  89. uncomp_stream[17] = uint8(1);
  90. if (check_stream( uncomp_stream, stream ) == false)
  91. exit(1);
  92. // randomized test
  93. for (uint32 i = 0; i < 1000; ++i)
  94. {
  95. const uint32 j = rand() % LEN;
  96. const uint32 s = rand() % 4;
  97. stream[j] = s;
  98. uncomp_stream[j] = s;
  99. if (check_stream( uncomp_stream, stream ) == false)
  100. exit(1);
  101. }
  102. // randomized test
  103. Stream::iterator it = stream.begin();
  104. for (uint32 i = 0; i < 1000; ++i)
  105. {
  106. const uint32 j = rand() % LEN;
  107. const uint32 s = rand() % 4;
  108. it[j] = s;
  109. uncomp_stream[j] = s;
  110. if (check_stream( uncomp_stream, stream ) == false)
  111. exit(1);
  112. }
  113. std::swap( *uncomp_stream, *(uncomp_stream+1) );
  114. std::swap( *stream.begin(), *(stream.begin()+1) );
  115. if (check_stream( uncomp_stream, stream ) == false)
  116. exit(1);
  117. std::sort( uncomp_stream, uncomp_stream + LEN );
  118. std::sort( stream.begin(), stream.begin() + LEN );
  119. if (check_stream( uncomp_stream, stream ) == false)
  120. exit(1);
  121. typedef PackedStream<const_cached_iterator<uint32*>,uint8,2,true> CachedPackedStream;
  122. const_cached_iterator<uint32*> cached_base_stream( base_stream );
  123. CachedPackedStream cached_stream( cached_base_stream );
  124. for (uint32 i = 0; i < LEN; ++i)
  125. {
  126. if (uncomp_stream[i] != cached_stream[i])
  127. {
  128. fprintf(stderr, " error at %u : found %u, expected %u\n", i, uint32( cached_stream[i] ), uint32( uncomp_stream[i] ));
  129. return false;
  130. }
  131. }
  132. // test the SAIS module
  133. stream[0] = 1; // B
  134. stream[1] = 0; // A
  135. stream[2] = 2; // N
  136. stream[3] = 0; // A
  137. stream[4] = 2; // N
  138. stream[5] = 0; // A
  139. int32 SA[6];
  140. saisxx( stream.begin(), SA, 6, 4 );
  141. if (SA[0] != 5 ||
  142. SA[1] != 3 ||
  143. SA[2] != 1 ||
  144. SA[3] != 0 ||
  145. SA[4] != 4 ||
  146. SA[5] != 2)
  147. {
  148. fprintf(stderr, " wrong suffix tree for \"BANANA\"\n");
  149. for (uint32 i = 0; i < 6; ++i)
  150. fprintf(stderr, "%u : %u\n", i, SA[i]);
  151. exit(1);
  152. }
  153. uint32 base_bwt_stream[LEN] = { 0u };
  154. Stream bwt_stream( base_bwt_stream );
  155. saisxx_bwt( stream.begin(), bwt_stream.begin(), SA, 6, 4 );
  156. char bwt[7];
  157. for (uint32 i = 0; i < 6; ++i)
  158. {
  159. const uint8 c = bwt_stream[i];
  160. bwt[i] =
  161. c == 0 ? 'A' :
  162. c == 1 ? 'B' :
  163. c == 2 ? 'N' :
  164. 'T';
  165. }
  166. bwt[6] = '\0';
  167. if (strcmp( bwt, "ANNBAA" ) != 0)
  168. {
  169. fprintf(stderr, " wrong bwt: expected \"ANNBAA\", got \"%s\"\n", bwt);
  170. exit(1);
  171. }
  172. fprintf(stderr, "2-bit stream test... done\n");
  173. }
  174. {
  175. uint32 base_stream[LEN] = { 0u };
  176. uint8 uncomp_stream[4*LEN] = { 0u };
  177. fprintf(stderr, "4-bit uint32-stream test... started\n");
  178. PackedStream<uint32*,uint8,4,true> stream( base_stream );
  179. if (check_stream( uncomp_stream, stream ) == false)
  180. exit(1);
  181. stream.set( 9, uint8(5) );
  182. uncomp_stream[9] = uint8(5);
  183. if (check_stream( uncomp_stream, stream ) == false)
  184. exit(1);
  185. stream.set( 10, uint8(15) );
  186. uncomp_stream[10] = uint8(15);
  187. if (check_stream( uncomp_stream, stream ) == false)
  188. exit(1);
  189. // randomized test
  190. for (uint32 i = 0; i < 1000; ++i)
  191. {
  192. const uint32 j = rand() % LEN;
  193. const uint32 s = rand() % 16;
  194. stream[j] = s;
  195. uncomp_stream[j] = s;
  196. if (check_stream( uncomp_stream, stream ) == false)
  197. exit(1);
  198. }
  199. std::sort( uncomp_stream, uncomp_stream + LEN );
  200. std::sort( stream.begin(), stream.begin() + LEN );
  201. if (check_stream( uncomp_stream, stream ) == false)
  202. exit(1);
  203. ForwardPackedStream<const uint32*,uint8,4,true> forward_stream( stream );
  204. if (check_forward_stream( uncomp_stream, forward_stream ) == false)
  205. exit(1);
  206. const_cached_iterator<const uint32*> cached_base_stream( base_stream );
  207. PackedStream<const_cached_iterator<const uint32*>,uint8,4,true> cached_stream( cached_base_stream );
  208. if (check_stream( uncomp_stream, cached_stream ) == false)
  209. exit(1);
  210. fprintf(stderr, "4-bit uint32-stream test... done\n");
  211. }
  212. {
  213. uint4 base_stream[LEN] = { { 0u } };
  214. uint8 uncomp_stream[4*LEN] = { 0u };
  215. fprintf(stderr, "4-bit uint4-stream test... started\n");
  216. PackedStream<uint4*,uint8,4,true> stream( base_stream );
  217. if (check_stream( uncomp_stream, stream ) == false)
  218. exit(1);
  219. stream.set( 9, uint8(5) );
  220. uncomp_stream[9] = uint8(5);
  221. if (check_stream( uncomp_stream, stream ) == false)
  222. exit(1);
  223. stream.set( 10, uint8(15) );
  224. uncomp_stream[10] = uint8(15);
  225. if (check_stream( uncomp_stream, stream ) == false)
  226. exit(1);
  227. // randomized test
  228. for (uint32 i = 0; i < 1000; ++i)
  229. {
  230. const uint32 j = rand() % LEN;
  231. const uint32 s = rand() % 16;
  232. stream[j] = s;
  233. uncomp_stream[j] = s;
  234. if (check_stream( uncomp_stream, stream ) == false)
  235. exit(1);
  236. }
  237. std::sort( uncomp_stream, uncomp_stream + LEN );
  238. std::sort( stream.begin(), stream.begin() + LEN );
  239. if (check_stream( uncomp_stream, stream ) == false)
  240. exit(1);
  241. ForwardPackedStream<const uint4*,uint8,4,true> forward_stream( stream );
  242. if (check_forward_stream( uncomp_stream, forward_stream ) == false)
  243. exit(1);
  244. const_cached_iterator<const uint4*> cached_base_stream( base_stream );
  245. PackedStream<const_cached_iterator<const uint4*>,uint8,4,true> cached_stream( cached_base_stream );
  246. if (check_stream( uncomp_stream, cached_stream ) == false)
  247. exit(1);
  248. fprintf(stderr, "4-bit uint4-stream test... done\n");
  249. }
  250. {
  251. uint32 base_stream[LEN] = { 0u };
  252. uint8 uncomp_stream[3*LEN] = { 0u };
  253. fprintf(stderr, "3-bit uint32-stream test... started\n");
  254. PackedStream<uint32*,uint8,3,true> stream( base_stream );
  255. if (check_stream( uncomp_stream, stream ) == false)
  256. exit(1);
  257. stream.set( 9, uint8(5) );
  258. uncomp_stream[9] = uint8(5);
  259. if (check_stream( uncomp_stream, stream ) == false)
  260. exit(1);
  261. stream.set( 10, uint8(7) );
  262. uncomp_stream[10] = uint8(7);
  263. if (check_stream( uncomp_stream, stream ) == false)
  264. exit(1);
  265. // randomized test
  266. for (uint32 i = 0; i < 1000; ++i)
  267. {
  268. const uint32 j = rand() % LEN;
  269. const uint32 s = rand() % 8;
  270. stream[j] = s;
  271. uncomp_stream[j] = s;
  272. if (check_stream( uncomp_stream, stream ) == false)
  273. exit(1);
  274. }
  275. std::sort( uncomp_stream, uncomp_stream + LEN );
  276. std::sort( stream.begin(), stream.begin() + LEN );
  277. if (check_stream( uncomp_stream, stream ) == false)
  278. exit(1);
  279. fprintf(stderr, "3-bit uint32-stream test... done\n");
  280. }
  281. {
  282. uint8 base_stream[LEN] = { 0u };
  283. uint8 uncomp_stream[2*LEN] = { 0u };
  284. fprintf(stderr, "2-bit byte-stream test... started\n");
  285. PackedStream<uint8*,uint8,2,true> stream( base_stream );
  286. if (check_stream( uncomp_stream, stream ) == false)
  287. exit(1);
  288. stream.set( 9, uint8(3) );
  289. uncomp_stream[9] = uint8(3);
  290. if (check_stream( uncomp_stream, stream ) == false)
  291. exit(1);
  292. stream.set( 10, uint8(2) );
  293. uncomp_stream[10] = uint8(2);
  294. if (check_stream( uncomp_stream, stream ) == false)
  295. exit(1);
  296. // randomized test
  297. for (uint32 i = 0; i < 1000; ++i)
  298. {
  299. const uint32 j = rand() % LEN;
  300. const uint32 s = rand() % 4;
  301. stream[j] = s;
  302. uncomp_stream[j] = s;
  303. if (check_stream( uncomp_stream, stream ) == false)
  304. exit(1);
  305. }
  306. std::sort( uncomp_stream, uncomp_stream + LEN );
  307. std::sort( stream.begin(), stream.begin() + LEN );
  308. if (check_stream( uncomp_stream, stream ) == false)
  309. exit(1);
  310. ForwardPackedStream<const uint8*,uint8,2,true> forward_stream( stream );
  311. if (check_forward_stream( uncomp_stream, forward_stream ) == false)
  312. exit(1);
  313. fprintf(stderr, "2-bit byte-stream test... done\n");
  314. }
  315. {
  316. uint4 base_stream[LEN] = { { 0u } };
  317. uint8 uncomp_stream[4*LEN] = { 0u };
  318. fprintf(stderr, "2-bit uint4-stream test... started\n");
  319. PackedStream<uint4*,uint8,2,true> stream( base_stream );
  320. if (check_stream( uncomp_stream, stream ) == false)
  321. exit(1);
  322. stream.set( 9, uint8(3) );
  323. uncomp_stream[9] = uint8(3);
  324. if (check_stream( uncomp_stream, stream ) == false)
  325. exit(1);
  326. stream.set( 10, uint8(2) );
  327. uncomp_stream[10] = uint8(2);
  328. if (check_stream( uncomp_stream, stream ) == false)
  329. exit(1);
  330. // randomized test
  331. for (uint32 i = 0; i < 1000; ++i)
  332. {
  333. const uint32 j = rand() % LEN;
  334. const uint32 s = rand() % 4;
  335. stream[j] = s;
  336. uncomp_stream[j] = s;
  337. if (check_stream( uncomp_stream, stream ) == false)
  338. exit(1);
  339. }
  340. std::sort( uncomp_stream, uncomp_stream + LEN );
  341. std::sort( stream.begin(), stream.begin() + LEN );
  342. if (check_stream( uncomp_stream, stream ) == false)
  343. exit(1);
  344. fprintf(stderr, "2-bit uint4-stream test... done\n");
  345. }
  346. return 0;
  347. }