DecayTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Reputation;
  4. use App\Domain\Reputation\Decay;
  5. use App\Domain\Reputation\DecayFunction;
  6. use PHPUnit\Framework\TestCase;
  7. /**
  8. * Hand-computed reference values for both decay shapes. The exponential
  9. * cases hit half-life multiples so the answers are clean fractions.
  10. */
  11. final class DecayTest extends TestCase
  12. {
  13. public function testLinearAtZeroReturnsFullWeight(): void
  14. {
  15. self::assertSame(1.0, Decay::value(DecayFunction::Linear, 0.0, 30.0));
  16. }
  17. public function testLinearMidwayReturnsHalf(): void
  18. {
  19. self::assertEqualsWithDelta(0.5, Decay::value(DecayFunction::Linear, 15.0, 30.0), 1e-9);
  20. }
  21. public function testLinearAtOrPastDecayParamClampsToZero(): void
  22. {
  23. self::assertSame(0.0, Decay::value(DecayFunction::Linear, 30.0, 30.0));
  24. self::assertSame(0.0, Decay::value(DecayFunction::Linear, 100.0, 30.0));
  25. }
  26. public function testExponentialAtZeroReturnsFullWeight(): void
  27. {
  28. self::assertSame(1.0, Decay::value(DecayFunction::Exponential, 0.0, 14.0));
  29. }
  30. public function testExponentialAtOneHalfLifeReturnsHalf(): void
  31. {
  32. self::assertEqualsWithDelta(0.5, Decay::value(DecayFunction::Exponential, 14.0, 14.0), 1e-9);
  33. }
  34. public function testExponentialAtTwoHalfLivesReturnsQuarter(): void
  35. {
  36. self::assertEqualsWithDelta(0.25, Decay::value(DecayFunction::Exponential, 28.0, 14.0), 1e-9);
  37. }
  38. public function testNegativeAgeClampsToFullWeight(): void
  39. {
  40. // Future-dated reports shouldn't happen, but if they do they count
  41. // at full weight rather than blowing up.
  42. self::assertSame(1.0, Decay::value(DecayFunction::Linear, -5.0, 30.0));
  43. self::assertSame(1.0, Decay::value(DecayFunction::Exponential, -5.0, 14.0));
  44. }
  45. public function testZeroDecayParamReturnsZero(): void
  46. {
  47. self::assertSame(0.0, Decay::value(DecayFunction::Linear, 5.0, 0.0));
  48. self::assertSame(0.0, Decay::value(DecayFunction::Exponential, 5.0, 0.0));
  49. }
  50. }