NonIntegerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace MathPHP\Tests\Sequence;
  3. use MathPHP\Exception;
  4. use MathPHP\Sequence\NonInteger;
  5. class NonIntegerTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test HarmonicNumber produces the expected sequence
  9. * @dataProvider dataProviderForHarmonicNumbers
  10. * @param int $n
  11. * @param array $expectedSequence
  12. */
  13. public function testHarmonicNumbers(int $n, array $expectedSequence)
  14. {
  15. // When
  16. $harmonicSequence = NonInteger::harmonic($n);
  17. // Then
  18. $this->assertEqualsWithDelta($expectedSequence, $harmonicSequence, 0.00001);
  19. }
  20. /**
  21. * @return array
  22. */
  23. public function dataProviderForHarmonicNumbers(): array
  24. {
  25. return [
  26. [-1, []],
  27. [0, []],
  28. [1, [1 => 1]],
  29. [10, [1 => 1, 3 / 2, 11 / 6, 25 / 12, 137 / 60, 49 / 20, 363 / 140, 761 / 280, 7129 / 2520, 7381 / 2520]],
  30. ];
  31. }
  32. /**
  33. * @test generalizedHarmonic produces the expected sequence
  34. * @dataProvider dataProviderForGeneralizedHarmonicNumbers
  35. * @param int $n
  36. * @param float $p
  37. * @param array $expectedSequence
  38. */
  39. public function testGeneralizedHarmonicNumbers(int $n, float $p, array $expectedSequence)
  40. {
  41. // When
  42. $harmonicSequence = NonInteger::generalizedHarmonic($n, $p);
  43. // Then
  44. $this->assertEqualsWithDelta($expectedSequence, $harmonicSequence, 0.00001);
  45. }
  46. /**
  47. * @return array
  48. */
  49. public function dataProviderForGeneralizedHarmonicNumbers(): array
  50. {
  51. return [
  52. [-1, 2, []],
  53. [0, 2, []],
  54. [1, 2, [1 => 1]],
  55. [4, 2, [1 => 1, 5 / 4, 49 / 36, 205 / 144]],
  56. [3, 1.01, [1 => 1, 1.4965462477, 1.8262375824425]],
  57. ];
  58. }
  59. /**
  60. * @test HyperharmonicNumber produces the expected sequence
  61. * @dataProvider dataProviderForHyperharmonicNumbers
  62. * @param int $n
  63. * @param int $r
  64. * @param array $expectedSequence
  65. */
  66. public function testHyperharmonicNumbers(int $n, int $r, array $expectedSequence)
  67. {
  68. // When
  69. $hyperharmonicSequence = NonInteger::hyperharmonic($n, $r);
  70. // Then
  71. $this->assertEqualsWithDelta($expectedSequence, $hyperharmonicSequence, .0001);
  72. }
  73. /**
  74. * @return array
  75. */
  76. public function dataProviderForHyperharmonicNumbers(): array
  77. {
  78. return [
  79. [-1, 5, []],
  80. [0, 2, []],
  81. [10, 0, [1 => 1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10]],
  82. [10, 1, [1 => 1, 3/2, 11/6, 25/12, 137/60, 49/20, 363/140, 761/280, 7129/2520, 7381/2520]],
  83. [10, 2, [1 => 1, 5/2, 26/6, 77/12, 87/10, 223/20, 481/35, 4609/280, 4861/252, 55991/2520]],
  84. ];
  85. }
  86. /**
  87. * @test hyperharmonic throws a OutOfBoundsException when r is less than zero
  88. */
  89. public function testHyperharmonicSeriesException()
  90. {
  91. // Given
  92. $r = -1;
  93. // Then
  94. $this->expectException(Exception\OutOfBoundsException::class);
  95. // When
  96. NonInteger::hyperharmonic(10, $r);
  97. }
  98. /**
  99. * @test hyperharmonic arithmetic operations exceed the bounds of the max integer precision
  100. * and produce a controlled MathException rather than a TypeError.
  101. */
  102. public function testHyperharmonicSequenceTypeError()
  103. {
  104. // Given
  105. $n = 10000;
  106. $r = 10000;
  107. // Then
  108. $this->expectException(Exception\OutOfBoundsException::class);
  109. // When
  110. NonInteger::hyperharmonic($n, $r);
  111. }
  112. }