BasicTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace MathPHP\Tests\Sequence;
  3. use MathPHP\Sequence\Basic;
  4. use MathPHP\Exception;
  5. class BasicTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test arithmeticProgression produces the expected sequence
  9. * @dataProvider dataProviderForArithmeticProgression
  10. * @param int $n
  11. * @param int $d
  12. * @param int $a₁
  13. * @param array $expected
  14. */
  15. public function testArithmeticProgression(int $n, int $d, int $a₁, array $expected)
  16. {
  17. // When
  18. $progression = Basic::arithmeticProgression($n, $d, $a₁);
  19. // Then
  20. $this->assertEquals($expected, $progression);
  21. }
  22. public function dataProviderForArithmeticProgression(): array
  23. {
  24. return [
  25. [-1, 2, 1, []],
  26. [0, 2, 1, []],
  27. [1, 2, 1, [1 => 1]],
  28. [10, 2, 0, [1 => 0, 2, 4, 6, 8, 10, 12, 14, 16, 18]],
  29. [10, 2, 1, [1 => 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]],
  30. [10, 3, 0, [1 => 0, 3, 6, 9, 12, 15, 18, 21, 24, 27]],
  31. [10, 3, 1, [1 => 1, 4, 7, 10, 13, 16, 19, 22, 25, 28]],
  32. ];
  33. }
  34. /**
  35. * @test geometricProgression produces the expected sequence
  36. * @dataProvider dataProviderForGeometricProgression
  37. * @param int $n
  38. * @param int $a
  39. * @param float $r
  40. * @param array $expected
  41. * @throws \Exception
  42. */
  43. public function testGeometricProgression(int $n, int $a, float $r, array $expected)
  44. {
  45. // When
  46. $progression = Basic::geometricProgression($n, $a, $r);
  47. // Then
  48. $this->assertEquals($expected, $progression);
  49. }
  50. public function dataProviderForGeometricProgression(): array
  51. {
  52. return [
  53. [-1, 2, 2, []],
  54. [0, 2, 2, []],
  55. [4, 2, 3, [2, 6, 18, 54]],
  56. [6, 1, -3, [1, -3, 9, -27, 81, -243]],
  57. [9, 1, 2, [1, 2, 4, 8, 16, 32, 64, 128, 256]],
  58. [6, 10, 3, [10, 30, 90, 270, 810, 2430]],
  59. [5, 4, 0.5, [4, 2, 1, 0.5, 0.25]],
  60. ];
  61. }
  62. /**
  63. * @test geometricProgression throws a BadParameterException when R is zero
  64. */
  65. public function testGeometricProgressionExceptionRIsZero()
  66. {
  67. // Given
  68. $r = 0;
  69. // Then
  70. $this->expectException(Exception\BadParameterException::class);
  71. // When
  72. Basic::geometricProgression(10, 2, $r);
  73. }
  74. /**
  75. * @test squareNumber produces the expected sequence
  76. * @dataProvider dataProviderForSquareNumber
  77. * @param int $n
  78. * @param array $expected
  79. */
  80. public function testSquareNumber(int $n, array $expected)
  81. {
  82. // When
  83. $squares = Basic::squareNumber($n);
  84. // Then
  85. $this->assertEquals($expected, $squares);
  86. }
  87. public function dataProviderForSquareNumber(): array
  88. {
  89. return [
  90. [-1, []],
  91. [0, []],
  92. [1, [0]],
  93. [2, [0, 1]],
  94. [3, [0, 1, 4]],
  95. [20, [0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361]],
  96. ];
  97. }
  98. /**
  99. * @test cubicNumber produces the expected sequence
  100. * @dataProvider dataProviderForCubicNumber
  101. * @param int $n
  102. * @param array $expected
  103. */
  104. public function testCubicNumber(int $n, array $expected)
  105. {
  106. // When
  107. $cubes = Basic::cubicNumber($n);
  108. // Then
  109. $this->assertEquals($expected, $cubes);
  110. }
  111. public function dataProviderForCubicNumber(): array
  112. {
  113. return [
  114. [-1, []],
  115. [0, []],
  116. [1, [0]],
  117. [2, [0, 1]],
  118. [3, [0, 1, 8]],
  119. [20, [0,1,8,27,64,125,216,343,512,729,1000,1331,1728,2197,2744,3375,4096,4913,5832,6859]],
  120. ];
  121. }
  122. /**
  123. * @test powersOfTwo produces the expected sequence
  124. * @dataProvider dataProviderForPowersOfTwo
  125. * @param int $n
  126. * @param array $expected
  127. */
  128. public function testPowersOfTwo(int $n, array $expected)
  129. {
  130. // When
  131. $powers = Basic::powersOfTwo($n);
  132. // Then
  133. $this->assertEquals($expected, $powers);
  134. }
  135. public function dataProviderForPowersOfTwo(): array
  136. {
  137. return [
  138. [-1, []],
  139. [0, []],
  140. [1, [1]],
  141. [2, [1, 2]],
  142. [3, [1, 2, 4]],
  143. [20, [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288]],
  144. ];
  145. }
  146. /**
  147. * @test powersOfTen produces the expected sequence
  148. * @dataProvider dataProviderForPowersOfTen
  149. * @param int $n
  150. * @param array $expected
  151. */
  152. public function testPowersOfTen(int $n, array $expected)
  153. {
  154. // When
  155. $powers = Basic::powersOfTen($n);
  156. // Then
  157. $this->assertEquals($expected, $powers);
  158. }
  159. public function dataProviderForPowersOfTen(): array
  160. {
  161. return [
  162. [-1, []],
  163. [0, []],
  164. [1, [1]],
  165. [2, [1, 10]],
  166. [3, [1, 10, 100]],
  167. [10, [1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000]],
  168. ];
  169. }
  170. /**
  171. * @test factorial produces the expected sequence
  172. * @dataProvider dataProviderForFactorial
  173. * @param int $n
  174. * @param array $expected
  175. */
  176. public function testFactorial(int $n, array $expected)
  177. {
  178. // When
  179. $powers = Basic::factorial($n);
  180. // Then
  181. $this->assertEquals($expected, $powers);
  182. }
  183. public function dataProviderForFactorial(): array
  184. {
  185. return [
  186. [-1, []],
  187. [0, []],
  188. [1, [1]],
  189. [2, [1, 1]],
  190. [3, [1, 1, 2]],
  191. [10, [1,1,2,6,24,120,720,5040,40320,362880]],
  192. ];
  193. }
  194. /**
  195. * @test digitSum produces the expected sequence
  196. * @dataProvider dataProviderForDigitSum
  197. * @param int $n
  198. * @param array $expected
  199. */
  200. public function testDigitSum(int $n, array $expected)
  201. {
  202. // When
  203. $digitSums = Basic::digitSum($n);
  204. // Then
  205. $this->assertEquals($expected, $digitSums);
  206. }
  207. public function dataProviderForDigitSum(): array
  208. {
  209. return [
  210. [0, []],
  211. [1, [0]],
  212. [2, [0, 1]],
  213. [3, [0, 1, 2]],
  214. [4, [0, 1, 2, 3]],
  215. [5, [0, 1, 2, 3, 4]],
  216. [6, [0, 1, 2, 3, 4, 5]],
  217. [7, [0, 1, 2, 3, 4, 5, 6]],
  218. [8, [0, 1, 2, 3, 4, 5, 6, 7]],
  219. [9, [0, 1, 2, 3, 4, 5, 6, 7, 8]],
  220. [10, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]],
  221. [11, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1]],
  222. [12, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2]],
  223. [88, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 8, 9, 10, 11, 12, 13, 14, 15]],
  224. ];
  225. }
  226. /**
  227. * @test digitalRoot produces the expected sequence
  228. * @dataProvider dataProviderForDigitalRoot
  229. * @param int $n
  230. * @param array $expected
  231. */
  232. public function testDigitalRoot(int $n, array $expected)
  233. {
  234. // When
  235. $digitRoots = Basic::digitalRoot($n);
  236. // Then
  237. $this->assertEquals($expected, $digitRoots);
  238. }
  239. public function dataProviderForDigitalRoot(): array
  240. {
  241. return [
  242. [0, []],
  243. [1, [0]],
  244. [2, [0, 1]],
  245. [3, [0, 1, 2]],
  246. [4, [0, 1, 2, 3]],
  247. [5, [0, 1, 2, 3, 4]],
  248. [6, [0, 1, 2, 3, 4, 5]],
  249. [7, [0, 1, 2, 3, 4, 5, 6]],
  250. [8, [0, 1, 2, 3, 4, 5, 6, 7]],
  251. [9, [0, 1, 2, 3, 4, 5, 6, 7, 8]],
  252. [10, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]],
  253. [11, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1]],
  254. [12, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2]],
  255. [105, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5]],
  256. ];
  257. }
  258. }