MultiTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. namespace MathPHP\Tests\Functions\Map;
  3. use MathPHP\Functions\Map\Multi;
  4. use MathPHP\Exception;
  5. class MultiTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test add two arrays
  9. * @dataProvider dataProviderForAddTwoArrays
  10. * @param array $xs
  11. * @param array $ys
  12. * @param array $expected
  13. * @throws \Exception
  14. */
  15. public function testAddTwoArrays(array $xs, array $ys, array $expected)
  16. {
  17. // When
  18. $sums = Multi::add($xs, $ys);
  19. // Then
  20. $this->assertEquals($expected, $sums);
  21. }
  22. public function dataProviderForAddTwoArrays(): array
  23. {
  24. return [
  25. [
  26. [1, 2, 3, 4],
  27. [2, 3, 4, 5],
  28. [3, 5, 7, 9],
  29. ],
  30. [
  31. [1, 2, 3, 4],
  32. [6, 6, 6, 6],
  33. [7, 8, 9, 10],
  34. ],
  35. ];
  36. }
  37. /**
  38. * @test add multiple arrays
  39. * @dataProvider dataProviderForAddMulti
  40. * @param array $expected
  41. * @param array ...$arrays
  42. * @throws \Exception
  43. */
  44. public function testAddMulti(array $expected, array ...$arrays)
  45. {
  46. // When
  47. $sums = Multi::add(...$arrays);
  48. // Then
  49. $this->assertEquals($expected, $sums);
  50. }
  51. public function dataProviderForAddMulti(): array
  52. {
  53. return [
  54. [
  55. [3, 5, 7, 9],
  56. [1, 2, 3, 4],
  57. [2, 3, 4, 5],
  58. ],
  59. [
  60. [7, 8, 9, 10],
  61. [1, 2, 3, 4],
  62. [6, 6, 6, 6],
  63. ],
  64. [
  65. [6, 7, 9, 10],
  66. [1, 2, 3, 4],
  67. [2, 2, 2, 2],
  68. [3, 3, 4, 4],
  69. ]
  70. ];
  71. }
  72. /**
  73. * @test subtract two arrays
  74. * @dataProvider dataProviderForSubtractTwoArrays
  75. * @param array $xs
  76. * @param array $ys
  77. * @param array $expected
  78. * @throws \Exception
  79. */
  80. public function testSubtractTwoArrays(array $xs, array $ys, array $expected)
  81. {
  82. // When
  83. $differences = Multi::subtract($xs, $ys);
  84. // Then
  85. $this->assertEquals($expected, $differences);
  86. }
  87. public function dataProviderForSubtractTwoArrays(): array
  88. {
  89. return [
  90. [
  91. [1, 2, 3, 4],
  92. [2, 3, 4, 5],
  93. [-1, -1, -1, -1],
  94. ],
  95. [
  96. [1, 2, 3, 4],
  97. [6, 6, 6, 6],
  98. [-5, -4, -3, -2],
  99. ],
  100. ];
  101. }
  102. /**
  103. * @test subtract multiple arrays
  104. * @dataProvider dataProviderForSubtractMulti
  105. * @param array $expected
  106. * @param array[] $arrays
  107. * @throws \Exception
  108. */
  109. public function testSubtractMulti(array $expected, array ...$arrays)
  110. {
  111. // When
  112. $differences = Multi::subtract(...$arrays);
  113. // Then
  114. $this->assertEquals($expected, $differences);
  115. }
  116. public function dataProviderForSubtractMulti(): array
  117. {
  118. return [
  119. [
  120. [-1, -1, -1, -1],
  121. [1, 2, 3, 4],
  122. [2, 3, 4, 5],
  123. ],
  124. [
  125. [-5, -4, -3, -2],
  126. [1, 2, 3, 4],
  127. [6, 6, 6, 6],
  128. ],
  129. [
  130. [3, 3, 4, 4],
  131. [6, 7, 9, 10],
  132. [1, 2, 3, 4],
  133. [2, 2, 2, 2],
  134. ]
  135. ];
  136. }
  137. /**
  138. * @test multiply two arrays
  139. * @dataProvider dataProviderForMultiplyTwoArrays
  140. */
  141. public function testMultiplyTwoArrays(array $xs, array $ys, array $expected)
  142. {
  143. // When
  144. $products = Multi::multiply($xs, $ys);
  145. // Then
  146. $this->assertEquals($expected, $products);
  147. }
  148. public function dataProviderForMultiplyTwoArrays(): array
  149. {
  150. return [
  151. [
  152. [1, 2, 3, 4],
  153. [2, 3, 4, 5],
  154. [2, 6, 12, 20],
  155. ],
  156. [
  157. [1, 2, 3, 4],
  158. [6, 6, 6, 6],
  159. [6, 12, 18, 24],
  160. ],
  161. ];
  162. }
  163. /**
  164. * @test multiply multiple arrays
  165. * @dataProvider dataProviderForMultiplyMulti
  166. */
  167. public function testMultiplyMulti(array $expected, array ...$arrays)
  168. {
  169. // When
  170. $products = Multi::multiply(...$arrays);
  171. // Then
  172. $this->assertEquals($expected, $products);
  173. }
  174. public function dataProviderForMultiplyMulti(): array
  175. {
  176. return [
  177. [
  178. [2, 6, 12, 20],
  179. [1, 2, 3, 4],
  180. [2, 3, 4, 5],
  181. ],
  182. [
  183. [6, 12, 18, 24],
  184. [1, 2, 3, 4],
  185. [6, 6, 6, 6],
  186. ],
  187. [
  188. [12, 28, 54, 80],
  189. [6, 7, 9, 10],
  190. [1, 2, 3, 4],
  191. [2, 2, 2, 2],
  192. ]
  193. ];
  194. }
  195. /**
  196. * @test divide two arrays
  197. * @dataProvider dataProviderForDivideTwoArrays
  198. * @param array $xs
  199. * @param array $ys
  200. * @param array $expected
  201. * @throws \Exception
  202. */
  203. public function testDivideTwoArrays(array $xs, array $ys, array $expected)
  204. {
  205. // When
  206. $quotients = Multi::divide($xs, $ys);
  207. // Then
  208. $this->assertEquals($expected, $quotients);
  209. }
  210. public function dataProviderForDivideTwoArrays(): array
  211. {
  212. return [
  213. [
  214. [5, 10, 15, 20],
  215. [5, 5, 5, 5],
  216. [1, 2, 3, 4],
  217. ],
  218. [
  219. [5, 10, 15, 20],
  220. [2.5, 20, 3, 4],
  221. [2, 0.5, 5, 5],
  222. ],
  223. ];
  224. }
  225. /**
  226. * @test divide multiple arrays
  227. * @dataProvider dataProviderForDivideMulti
  228. * @param array $expected
  229. * @param array[] $arrays
  230. * @throws \Exception
  231. */
  232. public function testDivideMulti(array $expected, array ...$arrays)
  233. {
  234. // When
  235. $quotients = Multi::divide(...$arrays);
  236. // Then
  237. $this->assertEquals($expected, $quotients);
  238. }
  239. public function dataProviderForDivideMulti(): array
  240. {
  241. return [
  242. [
  243. [1, 2, 3, 4],
  244. [5, 10, 15, 20],
  245. [5, 5, 5, 5],
  246. ],
  247. [
  248. [2, 0.5, 5, 5],
  249. [5, 10, 15, 20],
  250. [2.5, 20, 3, 4],
  251. ],
  252. [
  253. [5, 20, 1, 8],
  254. [100, 80, 25, 64],
  255. [10, 2, 5, 2],
  256. [2, 2, 5, 4],
  257. ]
  258. ];
  259. }
  260. /**
  261. * @test max two arrays
  262. * @dataProvider dataProviderForMaxTwoArrays
  263. * @param array $xs
  264. * @param array $ys
  265. * @param array $expected
  266. * @throws \Exception
  267. */
  268. public function testMaxTwoArrays(array $xs, array $ys, array $expected)
  269. {
  270. // When
  271. $maxes = Multi::max($xs, $ys);
  272. // Then
  273. $this->assertEquals($expected, $maxes);
  274. }
  275. public function dataProviderForMaxTwoArrays(): array
  276. {
  277. return [
  278. [
  279. [1, 5, 3, 6],
  280. [5, 5, 5, 5],
  281. [5, 5, 5, 6],
  282. ],
  283. [
  284. [5, 10, 15, 20],
  285. [2.5, 20, 3, 4],
  286. [5, 20, 15, 20],
  287. ],
  288. ];
  289. }
  290. /**
  291. * @test max multiple arrays
  292. * @dataProvider dataProviderForMaxMulti
  293. * @param array $expected
  294. * @param array[] $arrays
  295. * @throws \Exception
  296. */
  297. public function testMaxMulti(array $expected, array ...$arrays)
  298. {
  299. // When
  300. $maxes = Multi::max(...$arrays);
  301. // Then
  302. $this->assertEquals($expected, $maxes);
  303. }
  304. public function dataProviderForMaxMulti(): array
  305. {
  306. return [
  307. [
  308. [5, 10, 15, 20],
  309. [5, 10, 15, 20],
  310. [5, 5, 5, 5],
  311. ],
  312. [
  313. [5, 20, 15, 20],
  314. [5, 10, 15, 20],
  315. [2.5, 20, 3, 4],
  316. ],
  317. [
  318. [100, 80, 55, 664],
  319. [100, 80, 25, 64],
  320. [10, 2, 55, 2],
  321. [2, 2, 5, 664],
  322. ]
  323. ];
  324. }
  325. /**
  326. * @test min
  327. * @dataProvider dataProviderForMin
  328. * @param array $xs
  329. * @param array $ys
  330. * @param array $expected
  331. * @throws \Exception
  332. */
  333. public function testMin(array $xs, array $ys, array $expected)
  334. {
  335. // When
  336. $mins = Multi::min($xs, $ys);
  337. // Then
  338. $this->assertEquals($expected, $mins);
  339. }
  340. public function dataProviderForMin(): array
  341. {
  342. return [
  343. [
  344. [1, 5, 3, 6],
  345. [5, 5, 5, 5],
  346. [1, 5, 3, 5],
  347. ],
  348. [
  349. [5, 10, 15, 20],
  350. [2.5, 20, 3, 4],
  351. [2.5, 10, 3, 4],
  352. ],
  353. ];
  354. }
  355. /**
  356. * @test min multiple arrays
  357. * @dataProvider dataProviderForMinMulti
  358. * @param array $expected
  359. * @param array[] $arrays
  360. * @throws \Exception
  361. */
  362. public function testMinMulti(array $expected, array ...$arrays)
  363. {
  364. // When
  365. $mins = Multi::min(...$arrays);
  366. // Then
  367. $this->assertEquals($expected, $mins);
  368. }
  369. public function dataProviderForMinMulti(): array
  370. {
  371. return [
  372. [
  373. [5, 5, 5, 5],
  374. [5, 10, 15, 20],
  375. [5, 5, 5, 5],
  376. ],
  377. [
  378. [2.5, 10, 3, 4],
  379. [5, 10, 15, 20],
  380. [2.5, 20, 3, 4],
  381. ],
  382. [
  383. [2, 2, 5, 2],
  384. [100, 80, 25, 64],
  385. [10, 2, 55, 2],
  386. [2, 2, 5, 664],
  387. ]
  388. ];
  389. }
  390. /**
  391. * @test array lengths are not equal exception
  392. * @throws \Exception
  393. */
  394. public function testCheckArrayLengthsException()
  395. {
  396. // Given
  397. $xs = [1, 2, 3];
  398. $ys = [1, 2];
  399. // Then
  400. $this->expectException(Exception\BadDataException::class);
  401. // When
  402. Multi::add($xs, $ys);
  403. }
  404. /**
  405. * @test Only one array exception
  406. * @throws \Exception
  407. */
  408. public function testCheckArrayLengthsExceptionOnlyOneArray()
  409. {
  410. // Given
  411. $xs = [1, 2];
  412. // Then
  413. $this->expectException(Exception\BadDataException::class);
  414. // When
  415. Multi::add($xs);
  416. }
  417. }