Skip to content

Commit 20c1f5d

Browse files
authored
Merge pull request #20 from Katalam/sum-of
Add sum of expectation
2 parents 8e2e9dc + 49ddec5 commit 20c1f5d

File tree

4 files changed

+124
-4
lines changed

4 files changed

+124
-4
lines changed

Diff for: README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PEST PLUGIN MATH
22

3-
This plugin afford math related expectations.
3+
This plugin affords math related expectations.
44

55

66
[![Tests](https://github.com/faissaloux/pest-plugin-math/actions/workflows/tests.yml/badge.svg)](https://github.com/faissaloux/pest-plugin-math/actions/workflows/tests.yml) ![Codecov](https://img.shields.io/codecov/c/github/faissaloux/pest-plugin-math) ![Packagist Version](https://img.shields.io/packagist/v/faissaloux/pest-plugin-math) ![Packagist License](https://img.shields.io/packagist/l/faissaloux/pest-plugin-math)
@@ -88,3 +88,16 @@ Base default is euler's number.
8888
expect(0.69897000433602)->toBeLogarithmOf(number: 5, base: 10);
8989
expect(1)->not->toBeLogarithmOf(number: 1);
9090
```
91+
92+
#### `toBeSumOf()`
93+
```php
94+
expect(6)->toBeSumOf([1, 2, 3]);
95+
expect(4)->not->toBeSumOf([2, 3]);
96+
```
97+
98+
#### `toBeSummationOf()`
99+
$$\sum\limits_n^k x * 2$$
100+
```php
101+
expect(2)->toBeSummationOf(fn (int $x) => $x * 2, from: 0, to: 1);
102+
expect(3)->not->toBeSummationOf(fn (int $x) => $x * 2, from: 0, to: 1);
103+
```

Diff for: src/Expectation.php

+36-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function toBePrime(): PestExpectation
6565
}
6666

6767
for ($i = 2; $i < $this->value; $i++) {
68-
if ($this->value % $i == 0) {
68+
if ($this->value % $i === 0) {
6969
return expect(true)->toBeFalse();
7070
}
7171
}
@@ -112,8 +112,8 @@ public function toBeSqrtOf(int|float $number, ?int $precision = null): PestExpec
112112
*/
113113
public function toBeFactorialOf(int $number): PestExpectation
114114
{
115-
expect($this->value)->tobeInt();
116-
expect($number >= 0)->toBeTrue();
115+
expect($this->value)->tobeInt()
116+
->and($number >= 0)->toBeTrue();
117117

118118
if ($number === 0 || $number === 1) {
119119
return expect($this->value === 1)->toBeTrue();
@@ -138,6 +138,23 @@ public function toBeLogarithmOf(float $number, float $base = M_E): PestExpectati
138138
->and((string) $this->value === (string) log($number, $base))->toBeTrue("$this->value doesn't equal log($number, $base)");
139139
}
140140

141+
/**
142+
* @param array<int|float> $numbers
143+
* @return PestExpectation<TValue>
144+
*/
145+
public function toBeSumOf(array $numbers): PestExpectation
146+
{
147+
$sum = 0;
148+
149+
foreach ($numbers as $number) {
150+
expect($number)->toBeNumeric();
151+
152+
$sum += $number;
153+
}
154+
155+
return expect($this->value === $sum)->toBeTrue("$this->value doesn't equal $sum");
156+
}
157+
141158
/**
142159
* @return PestExpectation<TValue>
143160
*/
@@ -163,4 +180,20 @@ public function toBeMinOf(array $stack): PestExpectation
163180
{
164181
return expect($this->value === min($stack))->toBeTrue();
165182
}
183+
184+
/**
185+
* @return PestExpectation<TValue>
186+
*/
187+
public function toBeSummationOf(callable $step, int $from, int $to): PestExpectation
188+
{
189+
$sum = 0;
190+
191+
foreach (range($from, $to) as $i) {
192+
$stepSum = $step($i);
193+
expect($stepSum)->toBeNumeric();
194+
$sum += $stepSum;
195+
}
196+
197+
return expect($this->value === $sum)->toBeTrue("$this->value doesn't equal $sum");
198+
}
166199
}

Diff for: tests/toBeSumOf.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use PHPUnit\Framework\ExpectationFailedException;
4+
5+
it('passes', function (int $sum, array $numbers): void {
6+
expect($sum)->toBeSumOf($numbers);
7+
})->with([
8+
[0, []],
9+
[0, [-1, 0, 1]],
10+
[10, [3, '3', 4]],
11+
[20, [4, 4, 4, 4, 4]],
12+
[-20, [-4, -30, 14]],
13+
[30, [5, 5, 5, 5, 5, 5]],
14+
[40, [6, 6, 6, 6, 6, 6, 4]],
15+
[50, [7, 7, 7, 7, 7, 7, 7, 1]],
16+
[72, [8, 8, 8, 8, 8, 8, 8, 8, 8]],
17+
[88, [9, 9, 9, 9, 9, 9, 9, 9, 9, 7]],
18+
[110, [10, 10, 10, 10, 10, 10, '10', 10, 10, 10, 10]],
19+
]);
20+
21+
it('passes not', function (array $numbers): void {
22+
expect(1)->not->toBeSumOf($numbers);
23+
})->with([
24+
[[2, 3]],
25+
[[1, 2, 3]],
26+
[[1, 2, 3, 4]],
27+
[[1, 2, 3, 4, 5]],
28+
]);
29+
30+
it('fails if not number on the list', function (): void {
31+
expect(0)->toBeSumOf([2, 1, 3, 'not number', 78, 99]);
32+
})->throws(ExpectationFailedException::class);
33+
34+
test('failures', function (): void {
35+
expect(1)->toBeSumOf([1, 2]);
36+
})->throws(ExpectationFailedException::class);
37+
38+
test('failures not', function (): void {
39+
expect(2)->not->toBeSumOf([2]);
40+
})->throws(ExpectationFailedException::class);

Diff for: tests/toBeSummationOf.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use PHPUnit\Framework\ExpectationFailedException;
4+
5+
it('passes', function (int $sum, int $from, int $to, callable $step): void {
6+
expect($sum)->toBeSummationOf($step, $from, $to);
7+
})->with([
8+
[-2, -1, 0, fn (int $i): int => $i * 2],
9+
[0, 0, 0, fn (int $i): int => $i * 2],
10+
[110, 0, 10, fn (int $i): int => $i * 2],
11+
[110, 10, 0, fn (int $i): int => $i * 2],
12+
[418, 2, 20, function (int $i): int {
13+
return $i * 2;
14+
}],
15+
]);
16+
17+
it('passes not', function (callable $step, int $from, int $to): void {
18+
expect(1)->not->toBeSummationOf($step, $from, $to);
19+
})->with([
20+
[fn (int $i): int => $i * 2, 0, 10],
21+
[fn (int $i): int => $i * 2, 2, 20],
22+
]);
23+
24+
it('fails if step does not return a number', function (): void {
25+
expect(0)->toBeSummationOf(fn (): string => 'not number', 0, 0);
26+
})->throws(ExpectationFailedException::class);
27+
28+
test('failures', function (): void {
29+
expect(1)->toBeSummationOf(fn (int $i): int => $i * 2, 0, 1);
30+
})->throws(ExpectationFailedException::class);
31+
32+
test('failures not', function (): void {
33+
expect(2)->not->toBeSummationOf(fn (int $i): int => $i * 2, 0, 1);
34+
})->throws(ExpectationFailedException::class);

0 commit comments

Comments
 (0)