Passed
Push — main ( e49f5b...ddf3a0 )
by Eric
04:22
created

testMurmur3HashSpaceLookupsAreValidTargets()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Esi\ConsistentHash.
7
 *
8
 * (c) Eric Sizemore <[email protected]>
9
 * (c) Paul Annesley <[email protected]>
10
 *
11
 * This source file is subject to the MIT license. For the full copyright and
12
 * license information, please view the LICENSE file that was distributed with
13
 * this source code.
14
 */
15
16
namespace Esi\ConsistentHash\Tests\Hasher;
17
18
use Esi\ConsistentHash\ConsistentHash;
19
use Esi\ConsistentHash\Hasher\Crc32Hasher;
20
use Esi\ConsistentHash\Hasher\Md5Hasher;
21
use Esi\ConsistentHash\Hasher\Murmur3Hasher;
22
use Esi\ConsistentHash\Hasher\Xxh32Hasher;
23
use PHPUnit\Framework\Attributes\CoversClass;
24
use PHPUnit\Framework\Attributes\UsesClass;
25
use PHPUnit\Framework\TestCase;
26
27
/**
28
 * @internal
29
 */
30
#[CoversClass(Crc32Hasher::class)]
31
#[CoversClass(Md5Hasher::class)]
32
#[CoversClass(Murmur3Hasher::class)]
33
#[CoversClass(Xxh32Hasher::class)]
34
#[UsesClass(ConsistentHash::class)]
35
class HasherTest extends TestCase
36
{
37
    public function testCrc32Hash(): void
38
    {
39
        $hasher  = new Crc32Hasher();
40
        $result1 = $hasher->hash('test');
41
        $result2 = $hasher->hash('test');
42
        $result3 = $hasher->hash('different');
43
44
        self::assertEquals($result1, $result2);
45
        self::assertNotEquals($result1, $result3); // fragile but worthwhile
46
    }
47
48
    public function testCrc32HashSpaceLookupsAreValidTargets(): void
49
    {
50
        $targets = [];
51
        foreach (range(1, 10) as $i) {
52
            $targets[] = \sprintf('target%s', $i);
53
        }
54
55
        $hashSpace = new ConsistentHash(new Crc32Hasher());
56
        $hashSpace->addTargets($targets);
57
58
        foreach (range(1, 10) as $i) {
59
            self::assertTrue(
60
                \in_array($hashSpace->lookup(\sprintf('r%s', $i)), $targets, true),
61
                'target must be in list of targets',
62
            );
63
        }
64
    }
65
66
    public function testMd5Hash(): void
67
    {
68
        $hasher  = new Md5Hasher();
69
        $result1 = $hasher->hash('test');
70
        $result2 = $hasher->hash('test');
71
        $result3 = $hasher->hash('different');
72
73
        self::assertEquals($result1, $result2);
74
        self::assertNotEquals($result1, $result3); // fragile but worthwhile
75
    }
76
77
    public function testMd5HashSpaceLookupsAreValidTargets(): void
78
    {
79
        $targets = [];
80
        foreach (range(1, 10) as $i) {
81
            $targets[] = \sprintf('target%s', $i);
82
        }
83
84
        $hashSpace = new ConsistentHash(new Md5Hasher());
85
        $hashSpace->addTargets($targets);
86
87
        foreach (range(1, 10) as $i) {
88
            self::assertTrue(
89
                \in_array($hashSpace->lookup(\sprintf('r%s', $i)), $targets, true),
90
                'target must be in list of targets',
91
            );
92
        }
93
    }
94
95
    public function testMurmur3Hash(): void
96
    {
97
        $hasher  = new Murmur3Hasher();
98
        $result1 = $hasher->hash('test');
99
        $result2 = $hasher->hash('test');
100
        $result3 = $hasher->hash('different');
101
102
        self::assertEquals($result1, $result2);
103
        self::assertNotEquals($result1, $result3); // fragile but worthwhile
104
    }
105
106
    public function testMurmur3HashSpaceLookupsAreValidTargets(): void
107
    {
108
        $targets = [];
109
        foreach (range(1, 10) as $i) {
110
            $targets[] = \sprintf('target%s', $i);
111
        }
112
113
        $hashSpace = new ConsistentHash(new Murmur3Hasher());
114
        $hashSpace->addTargets($targets);
115
116
        foreach (range(1, 10) as $i) {
117
            self::assertTrue(
118
                \in_array($hashSpace->lookup(\sprintf('r%s', $i)), $targets, true),
119
                'target must be in list of targets',
120
            );
121
        }
122
    }
123
124
    public function testXxh32Hash(): void
125
    {
126
        $hasher  = new Xxh32Hasher();
127
        $result1 = $hasher->hash('test');
128
        $result2 = $hasher->hash('test');
129
        $result3 = $hasher->hash('different');
130
131
        self::assertEquals($result1, $result2);
132
        self::assertNotEquals($result1, $result3); // fragile but worthwhile
133
    }
134
135
    public function testXxh32HashSpaceLookupsAreValidTargets(): void
136
    {
137
        $targets = [];
138
        foreach (range(1, 10) as $i) {
139
            $targets[] = \sprintf('target%s', $i);
140
        }
141
142
        $hashSpace = new ConsistentHash(new Xxh32Hasher());
143
        $hashSpace->addTargets($targets);
144
145
        foreach (range(1, 10) as $i) {
146
            self::assertTrue(
147
                \in_array($hashSpace->lookup(\sprintf('r%s', $i)), $targets, true),
148
                'target must be in list of targets',
149
            );
150
        }
151
    }
152
}
153