TestGenerator.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2023, Martin Janiczek <martin@janiczek.cz>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StdLibExtras.h>
  7. #include <LibTest/Randomized/Generator.h>
  8. #include <LibTest/TestCase.h>
  9. #include <math.h>
  10. using namespace Test::Randomized;
  11. RANDOMIZED_TEST_CASE(number_u64_max_bounds)
  12. {
  13. GEN(n, Gen::number_u64(10));
  14. EXPECT(n <= 10);
  15. }
  16. RANDOMIZED_TEST_CASE(number_u64_min_max_bounds)
  17. {
  18. GEN(n, Gen::number_u64(3, 6));
  19. EXPECT(n >= 3 && n <= 6);
  20. }
  21. RANDOMIZED_TEST_CASE(assume)
  22. {
  23. GEN(n, Gen::number_u64(10));
  24. ASSUME(n % 2 == 0); // This will try to generate until it finds an even number
  25. EXPECT(n % 2 == 0); // This will then succeed
  26. // It will give up if the value doesn't pass the ASSUME(...) predicate 15 times in a row.
  27. }
  28. // TODO find a way to test that a test "number_u64(3) can't reach 0" fails
  29. // TODO find a way to test that a test "number_u64(3) can't reach 3" fails
  30. // TODO find a way to test that a test "number_u64(3,6) can't reach 3" fails
  31. // TODO find a way to test that a test "number_u64(3,6) can't reach 6" fails
  32. // TODO find a way to test that a test "number_u64(10) can reach n>10" fails
  33. RANDOMIZED_TEST_CASE(map_like)
  34. {
  35. GEN(n1, Gen::number_u64(10));
  36. GEN(n2, n1 * 2);
  37. EXPECT(n2 % 2 == 0);
  38. }
  39. RANDOMIZED_TEST_CASE(bind_like)
  40. {
  41. GEN(n1, Gen::number_u64(1, 9));
  42. GEN(n2, Gen::number_u64(n1 * 10, n1 * 100));
  43. EXPECT(n2 >= 10 && n2 <= 900);
  44. }
  45. // An example of an user-defined generator (for the test bind_vector_suboptimal).
  46. //
  47. // For why this is a suboptimal way to generate collections, see the comment in
  48. // Shrink::shrink_delete().
  49. //
  50. // TL;DR: this makes the length non-local to the items we're trying to delete
  51. // (except the first item).
  52. //
  53. // There's a better way: flip a (biased) coin to decide whether to generate
  54. // a next item. That makes each item much better shrinkable, since its
  55. // contribution to the sequence length (a boolean 0 or 1) is right next to its
  56. // own data.
  57. //
  58. // Because it's a pretty natural way to do this, we take special care in the
  59. // internal shrinker to work well on this style too.
  60. template<typename FN>
  61. Vector<InvokeResult<FN>> vector_suboptimal(FN item_gen)
  62. {
  63. u32 length = Gen::number_u64(5);
  64. Vector<InvokeResult<FN>> acc;
  65. for (u32 i = 0; i < length; ++i) {
  66. acc.append(item_gen());
  67. }
  68. return acc;
  69. }
  70. RANDOMIZED_TEST_CASE(bind_vector_suboptimal)
  71. {
  72. u32 max_item = 5;
  73. GEN(vec, vector_suboptimal([&]() { return Gen::number_u64(max_item); }));
  74. u32 sum = 0;
  75. for (u32 n : vec) {
  76. sum += n;
  77. }
  78. EXPECT(sum <= vec.size() * max_item);
  79. }
  80. RANDOMIZED_TEST_CASE(vector)
  81. {
  82. u32 max_item = 5;
  83. GEN(vec, Gen::vector([&]() { return Gen::number_u64(max_item); }));
  84. EXPECT(vec.size() <= 32);
  85. }
  86. RANDOMIZED_TEST_CASE(vector_length)
  87. {
  88. u32 max_item = 5;
  89. GEN(vec, Gen::vector(3, [&]() { return Gen::number_u64(max_item); }));
  90. EXPECT(vec.size() == 3);
  91. }
  92. RANDOMIZED_TEST_CASE(vector_min_max)
  93. {
  94. u32 max_item = 5;
  95. GEN(vec, Gen::vector(1, 4, [&]() { return Gen::number_u64(max_item); }));
  96. EXPECT(vec.size() >= 1 && vec.size() <= 4);
  97. }
  98. RANDOMIZED_TEST_CASE(weighted_boolean_below0)
  99. {
  100. GEN(b, Gen::weighted_boolean(-0.5));
  101. EXPECT(b == false);
  102. }
  103. RANDOMIZED_TEST_CASE(weighted_boolean_0)
  104. {
  105. GEN(b, Gen::weighted_boolean(0));
  106. EXPECT(b == false);
  107. }
  108. RANDOMIZED_TEST_CASE(weighted_boolean_1)
  109. {
  110. GEN(b, Gen::weighted_boolean(1));
  111. EXPECT(b == true);
  112. }
  113. RANDOMIZED_TEST_CASE(weighted_boolean_above1)
  114. {
  115. GEN(b, Gen::weighted_boolean(1.5));
  116. EXPECT(b == true);
  117. }
  118. RANDOMIZED_TEST_CASE(weighted_boolean_fair_true)
  119. {
  120. GEN(b, Gen::weighted_boolean(0.5));
  121. ASSUME(b == true);
  122. EXPECT(b == true);
  123. }
  124. RANDOMIZED_TEST_CASE(weighted_boolean_fair_false)
  125. {
  126. GEN(b, Gen::weighted_boolean(0.5));
  127. ASSUME(b == false);
  128. EXPECT(b == false);
  129. }
  130. RANDOMIZED_TEST_CASE(boolean_true)
  131. {
  132. GEN(b, Gen::boolean());
  133. ASSUME(b == true);
  134. EXPECT(b == true);
  135. }
  136. RANDOMIZED_TEST_CASE(boolean_false)
  137. {
  138. GEN(b, Gen::boolean());
  139. ASSUME(b == false);
  140. EXPECT(b == false);
  141. }
  142. RANDOMIZED_TEST_CASE(one_of_int)
  143. {
  144. GEN(x, Gen::one_of(1, 2));
  145. EXPECT(x == 1 || x == 2);
  146. }
  147. RANDOMIZED_TEST_CASE(frequency_int)
  148. {
  149. GEN(x, Gen::frequency(Gen::Choice { 5, 'x' }, Gen::Choice { 1, 'o' }));
  150. ASSUME(x == 'x');
  151. EXPECT(x == 'x');
  152. }
  153. RANDOMIZED_TEST_CASE(percentage)
  154. {
  155. GEN(x, Gen::percentage());
  156. EXPECT(x >= 0 && x <= 1);
  157. }
  158. RANDOMIZED_TEST_CASE(number_f64_max_bounds)
  159. {
  160. GEN(x, Gen::number_f64(10));
  161. EXPECT(x <= 10);
  162. }
  163. RANDOMIZED_TEST_CASE(number_f64_min_max_bounds)
  164. {
  165. GEN(x, Gen::number_f64(-10, 10));
  166. EXPECT(x >= -10 && x <= 10);
  167. }
  168. RANDOMIZED_TEST_CASE(number_f64_never_nan)
  169. {
  170. GEN(x, Gen::number_f64());
  171. EXPECT(!isnan(x));
  172. }
  173. RANDOMIZED_TEST_CASE(number_f64_never_infinite)
  174. {
  175. GEN(x, Gen::number_f64());
  176. EXPECT(!isinf(x));
  177. }
  178. RANDOMIZED_TEST_CASE(number_u32_max_bounds)
  179. {
  180. GEN(n, Gen::number_u32(10));
  181. EXPECT(n <= 10);
  182. }
  183. RANDOMIZED_TEST_CASE(number_u32_min_max_bounds)
  184. {
  185. GEN(n, Gen::number_u32(3, 6));
  186. EXPECT(n >= 3 && n <= 6);
  187. }