FractionTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. <?php
  2. /*
  3. * This file is part of the Phospr Fraction package.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace Phospr\Tests;
  9. use Phospr\Fraction;
  10. /**
  11. * FractionTest
  12. *
  13. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  14. * @since 0.1.0
  15. */
  16. class FractionTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * Big Even Fractions provider
  20. *
  21. * @return array
  22. */
  23. public static function bigFractionsProvider()
  24. {
  25. return array(
  26. array(PHP_INT_MAX * 2, PHP_INT_MAX * 4, '1/2'),
  27. array(PHP_INT_MAX, PHP_INT_MAX * 8, '1/8'),
  28. array(-PHP_INT_MAX * 4, PHP_INT_MAX, '-4'),
  29. array(-PHP_INT_MAX * 6, PHP_INT_MAX * 12, '-1/2'),
  30. array(PHP_INT_MAX * 1, PHP_INT_MAX * 3, '1/3'),
  31. array(PHP_INT_MAX * 2, PHP_INT_MAX * 9, '2/9'),
  32. array(-PHP_INT_MAX * 5, PHP_INT_MAX * 8, '-5/8'),
  33. array(-PHP_INT_MAX * 7, PHP_INT_MAX * 3, '-7/3'),
  34. );
  35. }
  36. /**
  37. * Test Big Fractions.
  38. *
  39. * @dataProvider bigFractionsProvider
  40. */
  41. public function testBigFractions(
  42. $numerator,
  43. $denominator,
  44. $expectedFraction
  45. ) {
  46. $fraction = new Fraction($numerator, $denominator);
  47. $expected = Fraction::fromString($expectedFraction);
  48. $this->assertLessThan(0.000000000001, abs($fraction->subtract($expected)->toFloat()));
  49. }
  50. /**
  51. * Test half
  52. *
  53. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  54. * @since 0.1.0
  55. */
  56. public function testHalf()
  57. {
  58. $half = new Fraction(1, 2);
  59. $this->assertEquals('1/2', (string) $half);
  60. $this->assertSame(1, $half->getNumerator());
  61. $this->assertSame(2, $half->getDenominator());
  62. }
  63. /**
  64. * Test __toString()
  65. *
  66. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  67. * @since 0.1.0
  68. *
  69. * @dataProvider toStringProvider
  70. */
  71. public function testToString(
  72. $numerator,
  73. $denominator,
  74. $string
  75. ) {
  76. if (null === $denominator) {
  77. $fraction = new Fraction($numerator);
  78. } else {
  79. $fraction = new Fraction($numerator, $denominator);
  80. }
  81. $this->assertSame($string, (string) $fraction);
  82. }
  83. /**
  84. * Test multiplication
  85. *
  86. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  87. * @since 0.1.0
  88. *
  89. * @dataProvider multiplicationProvider
  90. */
  91. public function testMultiply(
  92. $numerator1,
  93. $denominator1,
  94. $numerator2,
  95. $denominator2,
  96. $string
  97. ) {
  98. if (null === $denominator1) {
  99. $fraction1 = new Fraction($numerator1);
  100. } else {
  101. $fraction1 = new Fraction($numerator1, $denominator1);
  102. }
  103. if (null === $denominator2) {
  104. $fraction2 = new Fraction($numerator2);
  105. } else {
  106. $fraction2 = new Fraction($numerator2, $denominator2);
  107. }
  108. $this->assertSame($string, (string) $fraction1->multiply($fraction2));
  109. }
  110. /**
  111. * Test division
  112. *
  113. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  114. * @since 0.1.0
  115. *
  116. * @dataProvider divisionProvider
  117. */
  118. public function testDivision(
  119. $numerator1,
  120. $denominator1,
  121. $numerator2,
  122. $denominator2,
  123. $string
  124. ) {
  125. if (null === $denominator1) {
  126. $fraction1 = new Fraction($numerator1);
  127. } else {
  128. $fraction1 = new Fraction($numerator1, $denominator1);
  129. }
  130. if (null === $denominator2) {
  131. $fraction2 = new Fraction($numerator2);
  132. } else {
  133. $fraction2 = new Fraction($numerator2, $denominator2);
  134. }
  135. $this->assertSame($string, (string) $fraction1->divide($fraction2));
  136. }
  137. /**
  138. * Test addition
  139. *
  140. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  141. * @since 0.1.0
  142. *
  143. * @dataProvider additionProvider
  144. */
  145. public function testAddition(
  146. $numerator1,
  147. $denominator1,
  148. $numerator2,
  149. $denominator2,
  150. $string
  151. ) {
  152. if (null === $denominator1) {
  153. $fraction1 = new Fraction($numerator1);
  154. } else {
  155. $fraction1 = new Fraction($numerator1, $denominator1);
  156. }
  157. if (null === $denominator2) {
  158. $fraction2 = new Fraction($numerator2);
  159. } else {
  160. $fraction2 = new Fraction($numerator2, $denominator2);
  161. }
  162. $this->assertSame($string, (string) $fraction1->add($fraction2));
  163. }
  164. /**
  165. * Test subtraction
  166. *
  167. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  168. * @since 0.1.0
  169. *
  170. * @dataProvider subtractionProvider
  171. */
  172. public function testSubtraction(
  173. $numerator1,
  174. $denominator1,
  175. $numerator2,
  176. $denominator2,
  177. $string
  178. ) {
  179. if (null === $denominator1) {
  180. $fraction1 = new Fraction($numerator1);
  181. } else {
  182. $fraction1 = new Fraction($numerator1, $denominator1);
  183. }
  184. if (null === $denominator2) {
  185. $fraction2 = new Fraction($numerator2);
  186. } else {
  187. $fraction2 = new Fraction($numerator2, $denominator2);
  188. }
  189. $this->assertSame($string, (string) $fraction1->subtract($fraction2));
  190. }
  191. /**
  192. * Test isInteger()
  193. *
  194. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  195. * @since 0.1.0
  196. *
  197. * @dataProvider isIntegerProvider
  198. */
  199. public function testIsInteger(
  200. $numerator,
  201. $denominator,
  202. $result
  203. ) {
  204. if (null === $denominator) {
  205. $fraction = new Fraction($numerator);
  206. } else {
  207. $fraction = new Fraction($numerator, $denominator);
  208. }
  209. $this->assertSame($result, $fraction->isInteger());
  210. }
  211. /**
  212. * Test fromFloat()
  213. *
  214. * @author Christopher Tatro <c.m.tatro@gmail.com>
  215. * @since 0.2.0
  216. *
  217. * @dataProvider fromFloatProvider
  218. */
  219. public function testFromFloat($float, $numerator, $denominator)
  220. {
  221. $fraction = Fraction::fromFloat($float);
  222. $this->assertSame($numerator, $fraction->getNumerator());
  223. $this->assertSame($denominator, $fraction->getDenominator());
  224. }
  225. /**
  226. * Test fromFloat() exceptions
  227. *
  228. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  229. * @since 0.5.0
  230. *
  231. * @dataProvider fromFloatExceptionProvider
  232. * @expectedException InvalidArgumentException
  233. */
  234. public function testFromFloatException($float)
  235. {
  236. $fraction = Fraction::fromFloat($float);
  237. }
  238. /**
  239. * Test toFloat()
  240. *
  241. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  242. * @since 0.1.0
  243. *
  244. * @dataProvider toFloatProvider
  245. */
  246. public function testToFloat(
  247. $numerator,
  248. $denominator,
  249. $result
  250. ) {
  251. if (null === $denominator) {
  252. $fraction = new Fraction($numerator);
  253. } else {
  254. $fraction = new Fraction($numerator, $denominator);
  255. }
  256. $this->assertSame($result, $fraction->toFloat());
  257. }
  258. /**
  259. * Test bad denominator
  260. *
  261. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  262. * @since 0.1.0
  263. *
  264. * @expectedException Phospr\Exception\Fraction\InvalidDenominatorException
  265. * @dataProvider badIntegerProvider
  266. */
  267. public function testBadDenominator($denominator)
  268. {
  269. $fraction = new Fraction(1, $denominator);
  270. }
  271. /**
  272. * Test negative denominator
  273. *
  274. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  275. * @since 0.1.0
  276. *
  277. * @expectedException Phospr\Exception\Fraction\InvalidDenominatorException
  278. */
  279. public function testNegativeDenominator()
  280. {
  281. $fraction = new Fraction(1, -1);
  282. }
  283. /**
  284. * Test bad numerator
  285. *
  286. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  287. * @since 0.1.0
  288. *
  289. * @expectedException Phospr\Exception\Fraction\InvalidNumeratorException
  290. * @dataProvider badIntegerProvider
  291. */
  292. public function testBadNumerator($numerator)
  293. {
  294. $fraction = new Fraction($numerator, 1);
  295. }
  296. /**
  297. * Test fromString
  298. *
  299. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  300. * @since 0.4.0
  301. *
  302. * @dataProvider fromStringProvider
  303. */
  304. public function testFromString($fromString, $toString)
  305. {
  306. $this->assertSame(
  307. $toString,
  308. (string) Fraction::fromString($fromString)
  309. );
  310. }
  311. /**
  312. * Test fromString exception
  313. *
  314. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  315. * @since 0.4.0
  316. *
  317. * @expectedException \InvalidArgumentException
  318. * @dataProvider fromStringExceptionProvider
  319. */
  320. public function testFromStringException($string)
  321. {
  322. Fraction::fromString($string);
  323. }
  324. /**
  325. * Test isSameValueAs
  326. *
  327. * @author Christopher Tatro <c.m.tatro@gmail.com>
  328. * @since 1.1.0
  329. *
  330. * @dataProvider isSameValueAsProvider
  331. */
  332. public function testIsSameValueAs(
  333. $numerator1,
  334. $denominator1,
  335. $numerator2,
  336. $denominator2,
  337. $result
  338. ) {
  339. $fraction = new Fraction($numerator1, $denominator1);
  340. $this->assertSame($result, $fraction->isSameValueAs(new Fraction($numerator2, $denominator2)));
  341. }
  342. /**
  343. * __toString provider
  344. *
  345. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  346. * @since 0.1.0
  347. *
  348. * @return array
  349. */
  350. public static function toStringProvider()
  351. {
  352. return array(
  353. array(0, 2, '0'),
  354. array(1, 2, '1/2'),
  355. array(5, 2, '2 1/2'),
  356. array(15, 3, '5'),
  357. array(13, 3, '4 1/3'),
  358. array(1, null, '1'),
  359. array(8, null, '8'),
  360. array(8, 1, '8'),
  361. array(-8, 1, '-8'),
  362. array(-13, 3, '-4 1/3'),
  363. array(1, 4, '1/4'),
  364. array(7, 7, '1'),
  365. array(7, 21, '1/3'),
  366. );
  367. }
  368. /**
  369. * Bad integer provider
  370. *
  371. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  372. * @since 0.1.0
  373. *
  374. * @return array
  375. */
  376. public static function badIntegerProvider()
  377. {
  378. return array(
  379. array(null),
  380. array(0.4),
  381. array(.5),
  382. array(''),
  383. array(' '),
  384. array(1.0),
  385. array('5'),
  386. array('5i'),
  387. array('hello'),
  388. );
  389. }
  390. /**
  391. * Multiplication provider
  392. *
  393. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  394. * @since 0.1.0
  395. *
  396. * @return array
  397. */
  398. public static function multiplicationProvider()
  399. {
  400. return array(
  401. array(1, 1, 1, 1, '1'),
  402. array(-1, 1, -1, 1, '1'),
  403. array(-1, 1, 1, 1, '-1'),
  404. array(1, 2, 1, 2, '1/4'),
  405. array(-1, 2, 1, 2, '-1/4'),
  406. array(2, 1, 1, 2, '1'),
  407. array(-2, 1, 1, 2, '-1'),
  408. array(4, 8, 3, 12, '1/8'),
  409. );
  410. }
  411. /**
  412. * Division provider
  413. *
  414. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  415. * @since 0.1.0
  416. *
  417. * @return array
  418. */
  419. public static function divisionProvider()
  420. {
  421. return array(
  422. array(1, 1, 1, 1, '1'),
  423. array(5, null, 2, null, '2 1/2'),
  424. array(6, 13, 2, 7, '1 8/13'),
  425. array(-1, 2, -1, 2, '1'),
  426. array(-1, 2, 1, 2, '-1'),
  427. );
  428. }
  429. /**
  430. * Addition provider
  431. *
  432. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  433. * @since 0.1.0
  434. *
  435. * @return array
  436. */
  437. public static function additionProvider()
  438. {
  439. return array(
  440. array(1, 1, 1, 1, '2'),
  441. array(1, 2, 1, 2, '1'),
  442. array(1, 2, 1, null, '1 1/2'),
  443. array(-1, 2, 1, null, '1/2'),
  444. array(2, 7, 3, 11, '43/77'),
  445. );
  446. }
  447. /**
  448. * Subtraction provider
  449. *
  450. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  451. * @since 0.1.0
  452. *
  453. * @return array
  454. */
  455. public static function subtractionProvider()
  456. {
  457. return array(
  458. array(1, 1, 1, 1, '0'),
  459. array(2, 3, 1, 2, '1/6'),
  460. array(2, 7, 3, 11, '1/77'),
  461. array(2, 7, 8, 11, '-34/77'),
  462. array(6, null, 4, 6, '5 1/3'),
  463. );
  464. }
  465. /**
  466. * Is integer provider
  467. *
  468. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  469. * @since 0.1.0
  470. *
  471. * @return array
  472. */
  473. public static function isIntegerProvider()
  474. {
  475. return array(
  476. array(1, 1, true),
  477. array(0, 1, true),
  478. array(4, 1, true),
  479. array(14, 14, true),
  480. array(14, 7, true),
  481. array(-14, 7, true),
  482. );
  483. }
  484. /**
  485. * From float provider
  486. *
  487. * @author Christopher Tatro <ctatro@janeiredale.com>
  488. * @since 0.2.0
  489. *
  490. * @return array
  491. */
  492. public static function fromFloatProvider()
  493. {
  494. return [
  495. [12345.1234, 61725617, 5000],
  496. [9999.9999, 99999999, 10000],
  497. [0.000001, 1, 1000000],
  498. [0.0215011, 215011, 10000000],
  499. [0.0000025000, 1, 400000],
  500. [0.00001, 1, 100000],
  501. [1.25, 5, 4],
  502. [1.3245, 2649, 2000],
  503. [0.23, 23, 100],
  504. [1, 1, 1],
  505. [5.5000, 11, 2],
  506. [6.375, 51, 8],
  507. [235.63247, 23563247, 100000],
  508. // Test some negatives
  509. [-0.0215011, -215011, 10000000],
  510. [-0.0000025000, -1, 400000],
  511. [-0.00001, -1, 100000],
  512. [-1.25, -5, 4],
  513. [-1.3245, -2649, 2000],
  514. // Test some strings
  515. ['1', 1, 1],
  516. ['5', 5, 1],
  517. ['5.5000', 11, 2],
  518. ['6.375', 51, 8],
  519. ['-6.375', -51, 8],
  520. ['-1.25', -5, 4],
  521. ['-0.00001', -1, 100000],
  522. ];
  523. }
  524. /**
  525. * fromFloat exception provider
  526. *
  527. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  528. * @since 0.5.0
  529. *
  530. * @return array
  531. */
  532. public static function fromFloatExceptionProvider()
  533. {
  534. return [
  535. ['foo'],
  536. ['1.2.3'],
  537. ['1T'],
  538. ['- 5'],
  539. ['1/2'],
  540. ['seven'],
  541. ['r009'],
  542. ];
  543. }
  544. /**
  545. * To float provider
  546. *
  547. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  548. * @since 0.1.0
  549. *
  550. * @return array
  551. */
  552. public static function toFloatProvider()
  553. {
  554. return array(
  555. array(1, 1, 1),
  556. array(1, 4, 0.25),
  557. array(1, 8, 0.125),
  558. );
  559. }
  560. /**
  561. * fromString provider
  562. *
  563. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  564. * @since 0.4.0
  565. *
  566. * @return array
  567. */
  568. public static function fromStringProvider()
  569. {
  570. return [
  571. ['1/3', '1/3'],
  572. ['-1/3', '-1/3'],
  573. [' 1/3 ', '1/3'],
  574. ['1/3 ', '1/3'],
  575. [' 1/3', '1/3'],
  576. ['1/20', '1/20'],
  577. ['-1/20', '-1/20'],
  578. ['40', '40'],
  579. ['-40', '-40'],
  580. ['3 4/5', '3 4/5'],
  581. # ['-3 4/5', '-3 4/5'],
  582. # ['20 34/67', '20 34/67'],
  583. # ['-20 34/67', '-20 34/67'],
  584. ['40/20', '2'],
  585. ['-40/20', '-2'],
  586. ['40/2', '20'],
  587. ['-40/2', '-20'],
  588. ];
  589. }
  590. /**
  591. * fromString exception provider
  592. *
  593. * @author Tom Haskins-Vaughan <tom@tomhv.uk>
  594. * @since 0.4.0
  595. *
  596. * @return array
  597. */
  598. public static function fromStringExceptionProvider()
  599. {
  600. return [
  601. ['tom'],
  602. ['1 4/3 6'],
  603. ['1/4 3/6'],
  604. ['1 /3'],
  605. ['-1/-3'],
  606. ['1/'],
  607. ['/1'],
  608. ['10 4'],
  609. ];
  610. }
  611. /**
  612. * isSameValueAsProvider
  613. *
  614. * @author Christopher Tatro <c.m.tatro@gmail.com>
  615. * @since 1.1.0
  616. *
  617. * @return array
  618. */
  619. public static function isSameValueAsProvider()
  620. {
  621. return [
  622. [1, 2, 1, 2, true],
  623. [1, 3, 2, 6, true],
  624. [2, 2, 3, 3, true],
  625. [4, 2, 8, 4, true],
  626. [1, 1, 1, 2, false],
  627. [3, 2, 4, 2, false],
  628. [1, 4, 1, 2, false],
  629. [1650, 2, 825, 1, true],
  630. [-1, 3, -2, 6, true],
  631. [-6550, 50, -131, 1, true],
  632. [-4, 2, -4, 2, true],
  633. [4, 2, -4, 2, false],
  634. [-4, 2, 4, 2, false],
  635. [4, 2, 4, 2, true],
  636. [2605020, 159780620, 130251, 7989031, true],
  637. [-2605020, 159780620, -130251, 7989031, true],
  638. ];
  639. }
  640. }