NumericalDifferentiationTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace MathPHP\Tests\NumericalAnalysis\NumericalDifferentiation;
  3. use MathPHP\NumericalAnalysis\NumericalDifferentiation\NumericalDifferentiation;
  4. use MathPHP\Exception;
  5. class NumericalDifferentiationTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test getPoints data is not a callback nor set of arrays
  9. * @throws \Exception
  10. */
  11. public function testIncorrectInput()
  12. {
  13. // Given
  14. $x = 10;
  15. $incorrectFunction = $x ** 2 + 2 * $x + 1;
  16. // Then
  17. $this->expectException(Exception\BadDataException::class);
  18. // When
  19. NumericalDifferentiation::getPoints($incorrectFunction, [0,4,5]);
  20. }
  21. /**
  22. * @test validate an array doesn't have precisely two numbers (coordinates)
  23. * @throws \Exception
  24. */
  25. public function testNotCoordinatesException()
  26. {
  27. // Given
  28. $points = [[0,0], [1,2,3], [2,2]];
  29. $degree = 3;
  30. // Then
  31. $this->expectException(Exception\BadDataException::class);
  32. // When
  33. NumericalDifferentiation::validate($points, $degree);
  34. }
  35. /**
  36. * @test validate there are not enough arrays in the input
  37. * @throws \Exception
  38. */
  39. public function testNotEnoughArraysException()
  40. {
  41. // Given
  42. $points = [[0,0]];
  43. $degree = 3;
  44. // Then
  45. $this->expectException(Exception\BadDataException::class);
  46. // When
  47. NumericalDifferentiation::validate($points, $degree);
  48. }
  49. /**
  50. * @test validate two arrays share the same first number (x-component)
  51. * @throws \Exception
  52. */
  53. public function testNotAFunctionException()
  54. {
  55. // Given
  56. $points = [[0,0], [0,5], [1,1]];
  57. $degree = 3;
  58. // Then
  59. $this->expectException(Exception\BadDataException::class);
  60. // When
  61. NumericalDifferentiation::validate($points, $degree);
  62. }
  63. /**
  64. * @test isSpacingConstant when there is not constant spacing between points
  65. * @throws \Exception
  66. */
  67. public function testSpacingNonConstant()
  68. {
  69. // Given
  70. $sortedPoints = [[0,0], [3,3], [2,2]];
  71. // Then
  72. $this->expectException(Exception\BadDataException::class);
  73. // When
  74. NumericalDifferentiation::assertSpacingConstant($sortedPoints);
  75. }
  76. /**
  77. * @test isTargetInPoints target is not the x-component of one of the points
  78. * @throws \Exception
  79. */
  80. public function testTargetNotInPoints()
  81. {
  82. // Given
  83. $target = 1;
  84. $sortedPoints = [[0,0], [3,3], [2,2]];
  85. // Then
  86. $this->expectException(Exception\BadDataException::class);
  87. // When
  88. NumericalDifferentiation::assertTargetInPoints($target, $sortedPoints);
  89. }
  90. }