ValidationTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace MathPHP\Tests\NumericalAnalysis\RootFinding;
  3. use MathPHP\Exception;
  4. use MathPHP\NumericalAnalysis\RootFinding\Validation;
  5. class ValidationTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test Tolerance is not negative
  9. * @dataProvider dataProviderForPositiveTolerance
  10. * @param float $tolerance
  11. * @throws \Exception
  12. */
  13. public function testToleranceNotNegative(float $tolerance)
  14. {
  15. // Given
  16. Validation::tolerance($tolerance);
  17. // Then no exception is thrown
  18. $this->assertTrue(true);
  19. }
  20. /**
  21. * @return array
  22. */
  23. public function dataProviderForPositiveTolerance(): array
  24. {
  25. return [
  26. [0],
  27. [0.0000001],
  28. [0.1],
  29. [0.5],
  30. [1],
  31. [3837],
  32. ];
  33. }
  34. /**
  35. * @test Tolerance is negative
  36. * @throws \Exception
  37. */
  38. public function testToleranceNegative()
  39. {
  40. // Given
  41. $tolerance = -0.1;
  42. // Then
  43. $this->expectException(Exception\OutOfBoundsException::class);
  44. // When
  45. Validation::tolerance($tolerance);
  46. }
  47. /**
  48. * @test Interval has different points
  49. * @dataProvider dataProviderForValidInterval
  50. * @param float $a
  51. * @param float$b
  52. * @throws Exception\BadDataException
  53. */
  54. public function testIntervalNotTheSame(float $a, float $b)
  55. {
  56. // When
  57. Validation::interval($a, $b);
  58. // Then no exception is thrown
  59. $this->assertTrue(true);
  60. }
  61. /**
  62. * @return array
  63. */
  64. public function dataProviderForValidInterval(): array
  65. {
  66. return [
  67. [1, 2],
  68. [-2, -1],
  69. [-2, 1],
  70. [0, 1],
  71. [-1, 0],
  72. [1.2, 1.3],
  73. [-1.3, -1.2],
  74. ];
  75. }
  76. /**
  77. * @test Interval start and end points are the same
  78. * @throws \Exception
  79. */
  80. public function testIntervalSamePoints()
  81. {
  82. // Given
  83. $a = 3.5;
  84. $b = 3.5;
  85. // Then
  86. $this->expectException(Exception\BadDataException::class);
  87. // When
  88. Validation::interval($a, $b);
  89. }
  90. }