SingleTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. namespace MathPHP\Tests\Functions\Map;
  3. use MathPHP\Functions\Map\Single;
  4. use MathPHP\Exception;
  5. class SingleTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test square
  9. * @dataProvider dataProviderForSquare
  10. * @param array $xs
  11. * @param array $expected
  12. */
  13. public function testSquare(array $xs, array $expected)
  14. {
  15. // When
  16. $squares = Single::square($xs);
  17. // Then
  18. $this->assertEquals($expected, $squares);
  19. }
  20. public function dataProviderForSquare(): array
  21. {
  22. return [
  23. [
  24. [1, 2, 3, 4],
  25. [1, 4, 9, 16],
  26. ],
  27. [
  28. [7, 8, 9, 10],
  29. [49, 64, 81, 100],
  30. ],
  31. ];
  32. }
  33. /**
  34. * @test cube
  35. * @dataProvider dataProviderForCube
  36. * @param array $xs
  37. * @param array $expected
  38. */
  39. public function testCube(array $xs, array $expected)
  40. {
  41. // When
  42. $cubes = Single::cube($xs);
  43. // Then
  44. $this->assertEquals($expected, $cubes);
  45. }
  46. public function dataProviderForCube(): array
  47. {
  48. return [
  49. [
  50. [1, 2, 3, 4],
  51. [1, 8, 27, 64],
  52. ],
  53. [
  54. [7, 8, 9, 10],
  55. [343, 512, 729, 1000],
  56. ],
  57. ];
  58. }
  59. /**
  60. * @test pow
  61. * @dataProvider dataProviderForPow
  62. * @param array $xs
  63. * @param int $n
  64. * @param array $expected
  65. */
  66. public function testPow(array $xs, int $n, array $expected)
  67. {
  68. // When
  69. $pows = Single::pow($xs, $n);
  70. // Then
  71. $this->assertEquals($expected, $pows);
  72. }
  73. public function dataProviderForPow(): array
  74. {
  75. return [
  76. [
  77. [1, 2, 3, 4], 5,
  78. [1, 32, 243, 1024],
  79. ],
  80. [
  81. [7, 8, 9, 10], 4,
  82. [2401, 4096, 6561, 10000],
  83. ],
  84. ];
  85. }
  86. /**
  87. * @test equals
  88. * @dataProvider dataProviderForSqrt
  89. * @param array $xs
  90. * @param array $expected
  91. */
  92. public function testSqrt(array $xs, array $expected)
  93. {
  94. // When
  95. $sqrts = Single::sqrt($xs);
  96. // Then
  97. $this->assertEquals($expected, $sqrts);
  98. }
  99. public function dataProviderForSqrt(): array
  100. {
  101. return [
  102. [
  103. [4, 9, 16, 25],
  104. [2, 3, 4, 5],
  105. ],
  106. [
  107. [64, 81, 100, 144],
  108. [8, 9, 10, 12],
  109. ],
  110. ];
  111. }
  112. /**
  113. * @test equals
  114. * @dataProvider dataProviderForAbs
  115. * @param array $xs
  116. * @param array $expected
  117. */
  118. public function testAbs(array $xs, array $expected)
  119. {
  120. // When
  121. $abs = Single::abs($xs);
  122. // Then
  123. $this->assertEquals($expected, $abs);
  124. }
  125. public function dataProviderForAbs(): array
  126. {
  127. return [
  128. [
  129. [1, 2, 3, 4],
  130. [1, 2, 3, 4],
  131. ],
  132. [
  133. [1, -2, 3, -4],
  134. [1, 2, 3, 4],
  135. ],
  136. [
  137. [-1, -2, -3, -4],
  138. [1, 2, 3, 4],
  139. ],
  140. ];
  141. }
  142. /**
  143. * @test add
  144. * @dataProvider dataProviderForAdd
  145. * @param array $xs
  146. * @param mixed $k
  147. * @param array $expected
  148. */
  149. public function testAdd(array $xs, $k, array $expected)
  150. {
  151. // When
  152. $sums = Single::add($xs, $k);
  153. // Then
  154. $this->assertEquals($expected, $sums);
  155. }
  156. public function dataProviderForAdd(): array
  157. {
  158. return [
  159. [ [1, 2, 3, 4, 5], 4, [5, 6, 7, 8, 9] ],
  160. [ [5, 7, 23, 5, 2], 9.1, [14.1, 16.1, 32.1, 14.1, 11.1] ],
  161. ];
  162. }
  163. /**
  164. * @test subtract
  165. * @dataProvider dataProviderForSubtract
  166. * @param array $xs
  167. * @param int $k
  168. * @param array $expected
  169. */
  170. public function testSubtract(array $xs, int $k, array $expected)
  171. {
  172. // When
  173. $differences = Single::subtract($xs, $k);
  174. // Then
  175. $this->assertEquals($expected, $differences);
  176. }
  177. public function dataProviderForSubtract(): array
  178. {
  179. return [
  180. [ [1, 2, 3, 4, 5], 1, [0, 1, 2, 3, 4] ],
  181. [ [5, 7, 23, 5, 2], 3, [2, 4, 20, 2, -1] ],
  182. ];
  183. }
  184. /**
  185. * @test multiply
  186. * @dataProvider dataProviderForMultiply
  187. * @param array $xs
  188. * @param int $k
  189. * @param array $expected
  190. */
  191. public function testMultiply(array $xs, int $k, array $expected)
  192. {
  193. // When
  194. $products = Single::multiply($xs, $k);
  195. // Then
  196. $this->assertEquals($expected, $products);
  197. }
  198. public function dataProviderForMultiply(): array
  199. {
  200. return [
  201. [ [1, 2, 3, 4, 5], 4, [4, 8, 12, 16, 20] ],
  202. [ [5, 7, 23, 5, 2], 3, [15, 21, 69, 15, 6] ],
  203. ];
  204. }
  205. /**
  206. * @test multiply
  207. * @dataProvider dataProviderForDivide
  208. * @param array $xs
  209. * @param int $k
  210. * @param array $expected
  211. */
  212. public function testDivide(array $xs, int $k, array $expected)
  213. {
  214. // When
  215. $quotients = Single::divide($xs, $k);
  216. // Then
  217. $this->assertEquals($expected, $quotients);
  218. }
  219. public function dataProviderForDivide(): array
  220. {
  221. return [
  222. [ [1, 2, 3, 4, 5], 2, [0.5, 1, 1.5, 2, 2.5] ],
  223. [ [5, 10, 15, 20, 25], 5, [1, 2, 3, 4, 5] ],
  224. ];
  225. }
  226. /**
  227. * @test max
  228. * @dataProvider dataProviderForMax
  229. * @param array $xs
  230. * @param mixed $value
  231. * @param array $expected
  232. */
  233. public function testMax(array $xs, $value, array $expected)
  234. {
  235. // When
  236. $maxes = Single::max($xs, $value);
  237. // Then
  238. $this->assertEquals($expected, $maxes);
  239. }
  240. public function dataProviderForMax(): array
  241. {
  242. return [
  243. [[1, 2, 3, 4, 5], 0, [1, 2, 3, 4, 5]],
  244. [[1, 2, 3, 4, 5], 1, [1, 2, 3, 4, 5]],
  245. [[1, 2, 3, 4, 5], 3, [3, 3, 3, 4, 5]],
  246. [[1, 2, 3, 4, 5], 6, [6, 6, 6, 6, 6]],
  247. [[1, 2, 3, 4, 5], 9, [9, 9, 9, 9, 9]],
  248. [[1, 2, 3, 4, 5], 3.4, [3.4, 3.4, 3.4, 4, 5]],
  249. [[1, 2, 3, 4, 5], 6.7, [6.7, 6.7, 6.7, 6.7, 6.7]],
  250. ];
  251. }
  252. /**
  253. * @test min
  254. * @dataProvider dataProviderForMin
  255. * @param array $xs
  256. * @param mixed $value
  257. * @param array $expected
  258. */
  259. public function testMin(array $xs, $value, array $expected)
  260. {
  261. // When
  262. $mins = Single::min($xs, $value);
  263. // Then
  264. $this->assertEquals($expected, $mins);
  265. }
  266. public function dataProviderForMin(): array
  267. {
  268. return [
  269. [[1, 2, 3, 4, 5], 0, [0, 0, 0, 0, 0]],
  270. [[1, 2, 3, 4, 5], 1, [1, 1, 1, 1, 1]],
  271. [[1, 2, 3, 4, 5], 3, [1, 2, 3, 3, 3]],
  272. [[1, 2, 3, 4, 5], 6, [1, 2, 3, 4, 5]],
  273. [[1, 2, 3, 4, 5], 9, [1, 2, 3, 4, 5]],
  274. [[1, 2, 3, 4, 5], 3.4, [1, 2, 3, 3.4, 3.4]],
  275. [[1, 2, 3, 4, 5], 6.7, [1, 2, 3, 4, 5]],
  276. ];
  277. }
  278. /**
  279. * @test reciprocal
  280. * @dataProvider dataProviderForReciprocal
  281. * @param array $xs
  282. * @param array $expectedReciprocals
  283. * @throws \Exception
  284. */
  285. public function testReciprocal(array $xs, array $expectedReciprocals)
  286. {
  287. // When
  288. $reciprocals = Single::reciprocal($xs);
  289. // Then
  290. $this->assertEquals($expectedReciprocals, $reciprocals);
  291. }
  292. /**
  293. * @return array
  294. */
  295. public function dataProviderForReciprocal(): array
  296. {
  297. return [
  298. [
  299. [1, 2, 3, 4],
  300. [1 / 1, 1 / 2, 1 / 3, 1 / 4],
  301. ],
  302. [
  303. [7, 8, 9, 10],
  304. [1 / 7, 1 / 8, 1 / 9, 1 / 10],
  305. ],
  306. [
  307. [-2, -1, 1.1, 2.5, 6.73],
  308. [-1 / 2, -1 / 1, 1 / 1.1, 1 / 2.5, 1 / 6.73],
  309. ]
  310. ];
  311. }
  312. /**
  313. * @test reciprocal when there are zeros
  314. * @throws Exception\BadDataException
  315. */
  316. public function testReciprocalWithZeros()
  317. {
  318. // Given
  319. $xs = [1, 2, 0, 3, 0];
  320. // Then
  321. $this->expectException(Exception\BadDataException::class);
  322. // When
  323. Single::reciprocal($xs);
  324. }
  325. }