sum_tree_test.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // sum_tree_test.cpp
  28. //
  29. #include <nvbio/basic/sum_tree.h>
  30. #include <nvbio/basic/console.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <vector>
  34. namespace nvbio {
  35. int sum_tree_test()
  36. {
  37. printf("sum tree... started\n");
  38. // 128 leaves
  39. {
  40. const uint32 n_leaves = 128;
  41. std::vector<int32> vec( SumTree<uint32*>::node_count( n_leaves ) );
  42. SumTree<int32*> sum_tree( n_leaves, &vec[0] );
  43. // test 1
  44. for (uint32 i = 0; i < n_leaves; ++i)
  45. vec[i] = i;
  46. {
  47. sum_tree.setup();
  48. uint32 c0 = sample( sum_tree, 0.0f );
  49. uint32 c1 = sample( sum_tree, 0.5f );
  50. uint32 c2 = sample( sum_tree, 1.0f );
  51. if (c0 != 1 || c1 != 90 || c2 != 127)
  52. {
  53. log_error( stderr, "error in test(1):\n c(0.0) = %u (!= 1)\n c(0.5) = %u (!= 90) c(1.0) = %u (!= 127)\n", c0, c1, c2 );
  54. exit(1);
  55. }
  56. }
  57. // test 2
  58. for (uint32 i = 0; i < n_leaves; ++i)
  59. vec[i] = i < n_leaves/2 ? 0 : 1;
  60. {
  61. sum_tree.setup();
  62. uint32 c0 = sample( sum_tree, 0.0f );
  63. uint32 c1 = sample( sum_tree, 0.5f );
  64. uint32 c2 = sample( sum_tree, 1.0f );
  65. if (c0 != 64 || c1 != 96 || c2 != 127)
  66. {
  67. log_error( stderr, "error in test(2):\n c(0.0) = %u (!= 64)\n c(0.5) = %u (!= 90) c(1.0) = %u (!= 127)\n", c0, c1, c2 );
  68. return 1;
  69. }
  70. }
  71. // test 3
  72. for (uint32 i = 0; i < n_leaves; ++i)
  73. vec[i] = i < n_leaves/2 ? 1 : 0;
  74. {
  75. sum_tree.setup();
  76. uint32 c0 = sample( sum_tree, 0.0f );
  77. uint32 c1 = sample( sum_tree, 0.5f );
  78. uint32 c2 = sample( sum_tree, 1.0f );
  79. if (c0 != 0 || c1 != 32 || c2 != 63)
  80. {
  81. log_error( stderr, "error in test(3):\n c(0.0) = %u (!= 0)\n c(0.5) = %u (!= 32) c(1.0) = %u (!= 63)\n", c0, c1, c2 );
  82. exit(1);
  83. }
  84. }
  85. }
  86. // 80 leaves
  87. {
  88. const uint32 n_leaves = 80;
  89. std::vector<int32> vec( SumTree<uint32*>::node_count( n_leaves ) );
  90. SumTree<int32*> sum_tree( n_leaves, &vec[0] );
  91. if (sum_tree.padded_size() != 128)
  92. {
  93. log_error( stderr, "error: wrong padded size: %u != 128\n", sum_tree.padded_size() );
  94. exit(1);
  95. }
  96. // test 4
  97. for (uint32 i = 0; i < n_leaves; ++i)
  98. vec[i] = i < n_leaves/2 ? 0 : 1;
  99. {
  100. sum_tree.setup();
  101. uint32 c0 = sample( sum_tree, 0.0f );
  102. uint32 c1 = sample( sum_tree, 0.5f );
  103. uint32 c2 = sample( sum_tree, 1.0f );
  104. if (c0 != 40 || c1 != 60 || c2 != 79)
  105. {
  106. log_error( stderr, "error in test(3):\n c(0.0) = %u (!= 40)\n c(0.5) = %u (!= 60) c(1.0) = %u (!= 79)\n", c0, c1, c2 );
  107. exit(1);
  108. }
  109. }
  110. // test 5
  111. for (uint32 i = 0; i < n_leaves; ++i)
  112. vec[i] = i < n_leaves/2 ? 1 : 0;
  113. {
  114. sum_tree.setup();
  115. uint32 c0 = sample( sum_tree, 0.0f );
  116. uint32 c1 = sample( sum_tree, 0.5f );
  117. uint32 c2 = sample( sum_tree, 1.0f );
  118. if (c0 != 0 || c1 != 20 || c2 != 39)
  119. {
  120. log_error( stderr, "error in test(5):\n c(0.0) = %u (!= 0)\n c(0.5) = %u (!= 20) c(1.0) = %u (!= 39)\n", c0, c1, c2 );
  121. exit(1);
  122. }
  123. }
  124. // remove the last leaf
  125. sum_tree.add( 39, -1 );
  126. // test 6
  127. uint32 c = sample( sum_tree, 1.0f );
  128. if (c != 38)
  129. {
  130. log_error( stderr, "error in test(6):\n c(1.0) = %u (!= 38)\n", c );
  131. exit(1);
  132. }
  133. // remove one more leaf
  134. sum_tree.add( 38, -1 );
  135. c = sample( sum_tree, 1.0f );
  136. if (c != 37)
  137. {
  138. log_error( stderr, "error in test(7):\n c(1.0) = %u (!= 37)\n", c );
  139. exit(1);
  140. }
  141. // add it back
  142. sum_tree.add( 38, 1 );
  143. // and remove it using set
  144. sum_tree.set( 38, 0 );
  145. c = sample( sum_tree, 1.0f );
  146. if (c != 37)
  147. {
  148. log_error( stderr, "error in test(8):\n c(1.0) = %u (!= 37)\n", c );
  149. exit(1);
  150. }
  151. // remove the first ten leaves
  152. for (uint32 i = 0; i < 10; ++i)
  153. {
  154. sum_tree.set( i, 0 );
  155. c = sample( sum_tree, 0.0f );
  156. if (c != i+1)
  157. {
  158. log_error( stderr, "error in test(9):\n c(0.0) = %u (!= %u)\n", c, i );
  159. exit(1);
  160. }
  161. }
  162. }
  163. printf("sum tree... done\n");
  164. return 0;
  165. }
  166. }