Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a logarithm expectation #22

Merged
merged 7 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Loading