BuiltinTest.php
3 years ago
CRC32Test.php
3 years ago
DataIterator.php
3 years ago
GoogleTest.php
3 years ago
CRC32Test.php
159 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright 2019 Google LLC |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | include 'vendor/autoload.php'; |
| 19 | |
| 20 | use PHPUnit\Framework\TestCase; |
| 21 | use Google\CRC32\CRC32; |
| 22 | |
| 23 | require 'DataIterator.php'; |
| 24 | |
| 25 | final class CRC32Test extends TestCase |
| 26 | { |
| 27 | public function crcs() |
| 28 | { |
| 29 | return new CRCIterator(); |
| 30 | } |
| 31 | |
| 32 | public function data() |
| 33 | { |
| 34 | return new DataIterator(); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @dataProvider data |
| 39 | */ |
| 40 | public function testHash($crc_class, $poly, $input, $expected) |
| 41 | { |
| 42 | $crc = new $crc_class($poly); |
| 43 | $crc->update($input); |
| 44 | |
| 45 | $this->assertEquals( |
| 46 | $expected, |
| 47 | $crc->hash(false), |
| 48 | 'hash(' . $input . ')' |
| 49 | ); |
| 50 | |
| 51 | $this->assertEquals( |
| 52 | $expected, |
| 53 | $crc->hash(false), |
| 54 | 'hash(' . $input . ') again' |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @dataProvider data |
| 60 | */ |
| 61 | public function testHashRaw($crc_class, $poly, $input, $expected) |
| 62 | { |
| 63 | $crc = new $crc_class($poly); |
| 64 | $crc->update($input); |
| 65 | |
| 66 | $this->assertEquals( |
| 67 | hex2bin($expected), |
| 68 | $crc->hash(true), |
| 69 | 'hash(' . $input . ', true)' |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Extended hashing. Read increasingly sized chunks of input. |
| 75 | * @dataProvider data |
| 76 | */ |
| 77 | public function testExtendedHash($crc_class, $poly, $input, $expected) |
| 78 | { |
| 79 | $crc = new $crc_class($poly); |
| 80 | |
| 81 | $start = 0; |
| 82 | $len = 1; |
| 83 | while ($start < strlen($input)) { |
| 84 | $chunk = substr($input, $start, $len); |
| 85 | $crc->update($chunk); |
| 86 | |
| 87 | $start += $len; |
| 88 | $len *= 2; |
| 89 | } |
| 90 | |
| 91 | $this->assertEquals( |
| 92 | $expected, |
| 93 | $crc->hash(), |
| 94 | 'hash(' . $input . ')' |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @dataProvider crcs |
| 100 | */ |
| 101 | public function testReset($crc_class, $poly) |
| 102 | { |
| 103 | $data = array( |
| 104 | 'abc' => array( |
| 105 | CRC32::IEEE => '352441c2', |
| 106 | CRC32::CASTAGNOLI => '364b3fb7'), |
| 107 | 'abcdef' => array( |
| 108 | CRC32::IEEE => '4b8e39ef', |
| 109 | CRC32::CASTAGNOLI => '53bceff1'), |
| 110 | ); |
| 111 | |
| 112 | $crc = new $crc_class($poly); |
| 113 | $this->assertEquals('00000000', $crc->hash()); |
| 114 | |
| 115 | $crc->update('abc'); |
| 116 | $this->assertEquals($data['abc'][$poly], $crc->hash()); |
| 117 | |
| 118 | $crc->reset(); |
| 119 | $this->assertEquals('00000000', $crc->hash()); |
| 120 | |
| 121 | $crc->update('abcdef'); |
| 122 | $this->assertEquals($data['abcdef'][$poly], $crc->hash()); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @dataProvider crcs |
| 127 | */ |
| 128 | public function testClone($crc_class, $poly) |
| 129 | { |
| 130 | $data = array( |
| 131 | 'abc' => array( |
| 132 | CRC32::IEEE => '352441c2', |
| 133 | CRC32::CASTAGNOLI => '364b3fb7'), |
| 134 | 'abcdefgh' => array( |
| 135 | CRC32::IEEE => 'aeef2a50', |
| 136 | CRC32::CASTAGNOLI => '0a9421b7'), |
| 137 | ); |
| 138 | |
| 139 | $a = new $crc_class($poly); |
| 140 | $a->update('abc'); |
| 141 | |
| 142 | $b = clone $a; |
| 143 | $b->update('defgh'); |
| 144 | |
| 145 | // $b should be updated, but $a should stay the same. |
| 146 | $this->assertEquals( |
| 147 | $data['abc'][$poly], |
| 148 | $a->hash(), |
| 149 | '$a->update("abc")' |
| 150 | ); |
| 151 | |
| 152 | $this->assertEquals( |
| 153 | $data['abcdefgh'][$poly], |
| 154 | $b->hash(), |
| 155 | 'clone($a)->update("abcdefgh")' |
| 156 | ); |
| 157 | } |
| 158 | } |
| 159 |