Skip to content

Commit dda5016

Browse files
authored
Merge pull request #22 from Katalam/log
add a logarithm expectation
2 parents f0bb570 + 1a226cb commit dda5016

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Diff for: README.md

+9
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,12 @@ $$\mid -3 \mid$$
7979
expect(3)->toBeAbsoluteOf(-3);
8080
expect(-3)->not->toBeAbsoluteOf(-3);
8181
```
82+
83+
#### `toBeLogarithmOf()`
84+
$$\log_{base}(number)$$
85+
<br>
86+
Base default is euler's number.
87+
```php
88+
expect(0.69897000433602)->toBeLogarithmOf(number: 5, base: 10);
89+
expect(1)->not->toBeLogarithmOf(number: 1);
90+
```

Diff for: src/Expectation.php

+10
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ public function toBeFactorialOf(int $number): PestExpectation
128128
return expect($this->value === $factorial)->toBeTrue();
129129
}
130130

131+
/**
132+
* @return PestExpectation<TValue>
133+
*/
134+
public function toBeLogarithmOf(float $number, float $base = M_E): PestExpectation
135+
{
136+
return expect($base > 0)->toBeTrue('The base must be greater than 0')
137+
->and($number > 0)->toBeTrue('The number must be greater than 0')
138+
->and((string) $this->value === (string) log($number, $base))->toBeTrue("$this->value doesn't equal log($number, $base)");
139+
}
140+
131141
/**
132142
* @return PestExpectation<TValue>
133143
*/

Diff for: tests/toBeLogarithmOf.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use PHPUnit\Framework\ExpectationFailedException;
4+
5+
it('passes', function (int|float $value, float $number, float $base): void {
6+
expect($value)->toBeLogarithmOf($number, $base);
7+
})->with([
8+
[1.6094379124341, 5, M_E],
9+
[2.302585092994, 10, M_E],
10+
[-3.5849625007212, 12, .5],
11+
]);
12+
13+
it('passes not', function (float $number, float $base): void {
14+
expect(1)->not->toBeLogarithmOf($number, $base);
15+
})->with([
16+
[5, M_E],
17+
[10, M_E],
18+
[12, .5],
19+
]);
20+
21+
test('failures', function (float $number, float $base): void {
22+
expect(1)->toBeLogarithmOf($number, $base);
23+
})->with([
24+
[1, M_E],
25+
[-1, M_E],
26+
[1, -M_E],
27+
])->throws(ExpectationFailedException::class);
28+
29+
test('failures not', function (): void {
30+
expect(0)->not->toBeLogarithmOf(1);
31+
})->throws(ExpectationFailedException::class);

0 commit comments

Comments
 (0)