SieveTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /*
  3. * By adding type hints and enabling strict type checking, code can become
  4. * easier to read, self-documenting and reduce the number of potential bugs.
  5. * By default, type declarations are non-strict, which means they will attempt
  6. * to change the original type to match the type specified by the
  7. * type-declaration.
  8. *
  9. * In other words, if you pass a string to a function requiring a float,
  10. * it will attempt to convert the string value to a float.
  11. *
  12. * To enable strict mode, a single declare directive must be placed at the top
  13. * of the file.
  14. * This means that the strictness of typing is configured on a per-file basis.
  15. * This directive not only affects the type declarations of parameters, but also
  16. * a function's return type.
  17. *
  18. * For more info review the Concept on strict type checking in the PHP track
  19. * <link>.
  20. *
  21. * To disable strict typing, comment out the directive below.
  22. */
  23. declare(strict_types=1);
  24. class SieveTest extends PHPUnit\Framework\TestCase
  25. {
  26. public static function setUpBeforeClass(): void
  27. {
  28. require_once 'Sieve.php';
  29. }
  30. public function testNoPrimesUnderTwo(): void
  31. {
  32. $this->assertEquals([], sieve(1));
  33. }
  34. public function testFindFirstPrime(): void
  35. {
  36. $this->assertEquals([2], sieve(2));
  37. }
  38. public function testFindPrimesUpTo10(): void
  39. {
  40. $this->assertEquals([2, 3, 5, 7], sieve(10));
  41. }
  42. public function testLimitIsPrime(): void
  43. {
  44. $this->assertEquals([2, 3, 5, 7, 11, 13], sieve(13));
  45. }
  46. public function testFindPrimesUpTo1000(): void
  47. {
  48. $this->assertEquals(
  49. [
  50. 2,
  51. 3,
  52. 5,
  53. 7,
  54. 11,
  55. 13,
  56. 17,
  57. 19,
  58. 23,
  59. 29,
  60. 31,
  61. 37,
  62. 41,
  63. 43,
  64. 47,
  65. 53,
  66. 59,
  67. 61,
  68. 67,
  69. 71,
  70. 73,
  71. 79,
  72. 83,
  73. 89,
  74. 97,
  75. 101,
  76. 103,
  77. 107,
  78. 109,
  79. 113,
  80. 127,
  81. 131,
  82. 137,
  83. 139,
  84. 149,
  85. 151,
  86. 157,
  87. 163,
  88. 167,
  89. 173,
  90. 179,
  91. 181,
  92. 191,
  93. 193,
  94. 197,
  95. 199,
  96. 211,
  97. 223,
  98. 227,
  99. 229,
  100. 233,
  101. 239,
  102. 241,
  103. 251,
  104. 257,
  105. 263,
  106. 269,
  107. 271,
  108. 277,
  109. 281,
  110. 283,
  111. 293,
  112. 307,
  113. 311,
  114. 313,
  115. 317,
  116. 331,
  117. 337,
  118. 347,
  119. 349,
  120. 353,
  121. 359,
  122. 367,
  123. 373,
  124. 379,
  125. 383,
  126. 389,
  127. 397,
  128. 401,
  129. 409,
  130. 419,
  131. 421,
  132. 431,
  133. 433,
  134. 439,
  135. 443,
  136. 449,
  137. 457,
  138. 461,
  139. 463,
  140. 467,
  141. 479,
  142. 487,
  143. 491,
  144. 499,
  145. 503,
  146. 509,
  147. 521,
  148. 523,
  149. 541,
  150. 547,
  151. 557,
  152. 563,
  153. 569,
  154. 571,
  155. 577,
  156. 587,
  157. 593,
  158. 599,
  159. 601,
  160. 607,
  161. 613,
  162. 617,
  163. 619,
  164. 631,
  165. 641,
  166. 643,
  167. 647,
  168. 653,
  169. 659,
  170. 661,
  171. 673,
  172. 677,
  173. 683,
  174. 691,
  175. 701,
  176. 709,
  177. 719,
  178. 727,
  179. 733,
  180. 739,
  181. 743,
  182. 751,
  183. 757,
  184. 761,
  185. 769,
  186. 773,
  187. 787,
  188. 797,
  189. 809,
  190. 811,
  191. 821,
  192. 823,
  193. 827,
  194. 829,
  195. 839,
  196. 853,
  197. 857,
  198. 859,
  199. 863,
  200. 877,
  201. 881,
  202. 883,
  203. 887,
  204. 907,
  205. 911,
  206. 919,
  207. 929,
  208. 937,
  209. 941,
  210. 947,
  211. 953,
  212. 967,
  213. 971,
  214. 977,
  215. 983,
  216. 991,
  217. 997,
  218. ],
  219. sieve(1000)
  220. );
  221. }
  222. }