Skip to content

Commit

Permalink
Merge pull request #22 from Katalam/log
Browse files Browse the repository at this point in the history
add a logarithm expectation
  • Loading branch information
faissaloux authored Aug 1, 2024
2 parents f0bb570 + 1a226cb commit dda5016
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,12 @@ $$\mid -3 \mid$$
expect(3)->toBeAbsoluteOf(-3);
expect(-3)->not->toBeAbsoluteOf(-3);
```

#### `toBeLogarithmOf()`
$$\log_{base}(number)$$
<br>
Base default is euler's number.
```php
expect(0.69897000433602)->toBeLogarithmOf(number: 5, base: 10);
expect(1)->not->toBeLogarithmOf(number: 1);
```
10 changes: 10 additions & 0 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ public function toBeFactorialOf(int $number): PestExpectation
return expect($this->value === $factorial)->toBeTrue();
}

/**
* @return PestExpectation<TValue>
*/
public function toBeLogarithmOf(float $number, float $base = M_E): PestExpectation
{
return expect($base > 0)->toBeTrue('The base must be greater than 0')
->and($number > 0)->toBeTrue('The number must be greater than 0')
->and((string) $this->value === (string) log($number, $base))->toBeTrue("$this->value doesn't equal log($number, $base)");
}

/**
* @return PestExpectation<TValue>
*/
Expand Down
31 changes: 31 additions & 0 deletions tests/toBeLogarithmOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;

it('passes', function (int|float $value, float $number, float $base): void {
expect($value)->toBeLogarithmOf($number, $base);
})->with([
[1.6094379124341, 5, M_E],
[2.302585092994, 10, M_E],
[-3.5849625007212, 12, .5],
]);

it('passes not', function (float $number, float $base): void {
expect(1)->not->toBeLogarithmOf($number, $base);
})->with([
[5, M_E],
[10, M_E],
[12, .5],
]);

test('failures', function (float $number, float $base): void {
expect(1)->toBeLogarithmOf($number, $base);
})->with([
[1, M_E],
[-1, M_E],
[1, -M_E],
])->throws(ExpectationFailedException::class);

test('failures not', function (): void {
expect(0)->not->toBeLogarithmOf(1);
})->throws(ExpectationFailedException::class);

0 comments on commit dda5016

Please sign in to comment.