-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBenchmarkTest.php
More file actions
306 lines (271 loc) · 9.33 KB
/
BenchmarkTest.php
File metadata and controls
306 lines (271 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php declare(strict_types=1);
namespace Jspeedz\PhpConsistentHashing\Tests;
use Jspeedz\PhpConsistentHashing\Benchmark;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
#[CoversClass(Benchmark::class)]
class BenchmarkTest extends TestCase {
#[Test]
public function getAvailableHashCallbacks(): void {
$benchmark = new Benchmark();
$results = $benchmark->getAvailableHashCallbacks();
$this->assertNotEmpty($results);
foreach($results as $result) {
// Ignore this one, as there is no guarantee that the element is actually callable.
// @phpstan-ignore method.alreadyNarrowedType
$this->assertIsCallable($result);
}
$results = $benchmark->getAvailableHashCallbacks([
'md5',
]);
$this->assertCount(1, $results);
foreach($results as $result) {
// Ignore this one, as there is no guarantee that the element is actually callable.
// @phpstan-ignore method.alreadyNarrowedType
$this->assertIsCallable($result);
$this->assertSame(3895525021, $result('someValue'));
}
}
#[Test]
public function getCombinations(): void {
$benchmark = new Benchmark();
// Test case 1: Normal case
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$length = 2;
$expected = [
['a' => 1, 'b' => 2],
['a' => 1, 'c' => 3],
['b' => 2, 'c' => 3],
];
$this->assertEquals($expected, $benchmark->getCombinations($array, $length));
// Test case 2: Length greater than array count
$array = ['a' => 1, 'b' => 2];
$length = 3;
$expected = [];
$this->assertEquals($expected, $benchmark->getCombinations($array, $length));
// Test case 3: Length equal to array count
$array = ['a' => 1, 'b' => 2];
$length = 2;
$expected = [
['a' => 1, 'b' => 2],
];
$this->assertEquals($expected, $benchmark->getCombinations($array, $length));
// Test case 4: Length of 1
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$length = 1;
$expected = [
['a' => 1],
['b' => 2],
['c' => 3],
];
$this->assertEquals($expected, $benchmark->getCombinations($array, $length));
// Test case 5: Empty array
$array = [];
$length = 1;
$expected = [];
$this->assertEquals($expected, $benchmark->getCombinations($array, $length));
}
#[Test]
public function sortByTwoColumns(): void {
$benchmark = new Benchmark();
// Test case 1: Normal case with ascending sort
$array = [
['name' => 'John', 'age' => 25],
['name' => 'Jane', 'age' => 22],
['name' => 'John', 'age' => 20],
];
$expected = [
['name' => 'Jane', 'age' => 22],
['name' => 'John', 'age' => 20],
['name' => 'John', 'age' => 25],
];
$benchmark->sortByTwoColumns($array, 'name', 'age');
$this->assertEquals($expected, $array);
// Test case 2: Descending sort on primary column
$array = [
['name' => 'John', 'age' => 25],
['name' => 'Jane', 'age' => 22],
['name' => 'John', 'age' => 20],
];
$expected = [
['name' => 'John', 'age' => 20],
['name' => 'John', 'age' => 25],
['name' => 'Jane', 'age' => 22],
];
$benchmark->sortByTwoColumns($array, 'name', 'age', SORT_DESC);
$this->assertEquals($expected, $array);
// Test case 3: Descending sort on both columns
$array = [
['name' => 'John', 'age' => 25],
['name' => 'Jane', 'age' => 22],
['name' => 'John', 'age' => 20],
];
$expected = [
['name' => 'John', 'age' => 25],
['name' => 'John', 'age' => 20],
['name' => 'Jane', 'age' => 22],
];
$benchmark->sortByTwoColumns($array, 'name', 'age', SORT_DESC, SORT_DESC);
$this->assertEquals($expected, $array);
// Test case 4: Empty array
$array = [];
$expected = [];
$benchmark->sortByTwoColumns($array, 'name', 'age');
$this->assertEquals($expected, $array);
// Test case 5: Array with one element
$array = [['name' => 'John', 'age' => 25]];
$expected = [['name' => 'John', 'age' => 25]];
$benchmark->sortByTwoColumns($array, 'name', 'age');
$this->assertEquals($expected, $array);
}
#[Test]
public function formatNumber(): void {
$class = new ReflectionClass(Benchmark::class);
$method = $class->getMethod('formatNumber');
$method->setAccessible(true);
$benchmark = new Benchmark();
// Test case 1: Integer number
$number = 123;
$expected = ' 123.00';
$this->assertEquals(
$expected,
$method->invokeArgs(
$benchmark,
[
$number,
],
),
);
// Test case 2: Float number with two decimal places
$number = 123.45;
$expected = ' 123.45';
$this->assertEquals(
$expected,
$method->invokeArgs(
$benchmark,
[
$number,
],
),
);
// Test case 3: Float number with more than two decimal places
$number = 123.456;
$expected = ' 123.46';
$this->assertEquals(
$expected,
$method->invokeArgs(
$benchmark,
[
$number,
],
),
);
// Test case 4: Float number with less than two decimal places
$number = 123.4;
$expected = ' 123.40';
$this->assertEquals(
$expected,
$method->invokeArgs(
$benchmark,
[
$number,
],
),
);
// Test case 5: Negative number
$number = -123.4;
$expected = '-123.40';
$this->assertEquals(
$expected,
$method->invokeArgs(
$benchmark,
[
$number,
],
),
);
// Test case 6: Large number
$number = 1234567.89;
$expected = '1234567.89';
$this->assertEquals(
$expected,
$method->invokeArgs(
$benchmark,
[
$number,
],
),
);
// Test case 7: Number less than 7 characters
$number = 1.2;
$expected = ' 1.20';
$this->assertEquals(
$expected,
$method->invokeArgs(
$benchmark,
[
$number,
],
),
);
}
#[Test]
public function printResults(): void {
$mock = $this->getMockBuilder(Benchmark::class)
->onlyMethods(['formatNumber'])
->getMock();
// Mock the formatNumber method to return a fixed value for simplicity
$mock->method('formatNumber')
->willReturnCallback(
function(int|float $number): string {
return str_pad(number_format(round($number, 2), 2, '.', ''), 7, ' ', STR_PAD_LEFT);
},
);
// Test case 1: Normal case
$results = [
[
'name' => 'Test1',
'distribution' => 'Dist1',
'totalTime' => 123.456,
'avgTimePerHash' => 12.3456,
'avgDeviationFromPerfectPercentage' => 0.1234,
],
[
'name' => 'Test2',
'distribution' => 'Dist2',
'totalTime' => 789.012,
'avgTimePerHash' => 78.9012,
'avgDeviationFromPerfectPercentage' => 0.5678,
],
];
$expected = 'Results:' . PHP_EOL
.
'[Test1] -- [Dist1]totalTime: 123.46ms -- avgTimePerHash: 12.35ms -- avgDeviationFromPerfectPercentage: 0.12% -- ' .
PHP_EOL
.
'[Test2] -- [Dist2]totalTime: 789.01ms -- avgTimePerHash: 78.9ms -- avgDeviationFromPerfectPercentage: 0.57% -- ' .
PHP_EOL;
$this->assertEquals($expected, $mock->printResults($results));
// Test case 2: Empty results
$results = [];
$expected = 'Results:' . PHP_EOL;
$this->assertEquals($expected, $mock->printResults($results));
// Test case 3: Different lengths of names and distributions
$results = [
[
'name' => 'T',
'distribution' => 'D',
'totalTime' => 1.1,
'avgTimePerHash' => 0.1,
'avgDeviationFromPerfectPercentage' => 0.01,
],
];
$expected = 'Results:' . PHP_EOL
.
'[T ] -- [D ]totalTime: 1.1ms -- avgTimePerHash: 0.1ms -- avgDeviationFromPerfectPercentage: 0.01% -- ' .
PHP_EOL;
$this->assertEquals($expected, $mock->printResults($results));
}
}