BuiltinTest.php
3 years ago
CRC32Test.php
3 years ago
DataIterator.php
3 years ago
GoogleTest.php
3 years ago
DataIterator.php
188 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 | use PHPUnit\Framework\TestCase; |
| 18 | |
| 19 | use Google\CRC32\CRC32; |
| 20 | |
| 21 | class CRCIterator implements Iterator |
| 22 | { |
| 23 | protected $crcs = [ |
| 24 | 'Google\CRC32\PHP', |
| 25 | 'Google\CRC32\PHPSlicedBy4', |
| 26 | 'Google\CRC32\Builtin', |
| 27 | 'Google\CRC32\Google', |
| 28 | ]; |
| 29 | |
| 30 | protected $algos = [ |
| 31 | CRC32::IEEE, |
| 32 | CRC32::CASTAGNOLI |
| 33 | ]; |
| 34 | |
| 35 | protected $count = 0; |
| 36 | |
| 37 | public function rewind() |
| 38 | { |
| 39 | reset($this->crcs); |
| 40 | reset($this->algos); |
| 41 | } |
| 42 | |
| 43 | public function valid() |
| 44 | { |
| 45 | return current($this->crcs) !== false && |
| 46 | current($this->algos) !== false; |
| 47 | } |
| 48 | |
| 49 | public function key() |
| 50 | { |
| 51 | return $this->count; |
| 52 | } |
| 53 | |
| 54 | public function current() |
| 55 | { |
| 56 | return [current($this->crcs), current($this->algos)]; |
| 57 | } |
| 58 | |
| 59 | public function next() |
| 60 | { |
| 61 | $this->count++; |
| 62 | if (next($this->algos) === false) { |
| 63 | reset($this->algos); |
| 64 | next($this->crcs); |
| 65 | } |
| 66 | |
| 67 | // Skip unsupported polynomials |
| 68 | if ($this->valid()) { |
| 69 | $crc_class = current($this->crcs); |
| 70 | $poly = current($this->algos); |
| 71 | if (!$crc_class::supports($poly)) { |
| 72 | $this->next(); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | class DataIterator implements Iterator |
| 79 | { |
| 80 | protected $crcs = [ |
| 81 | 'Google\CRC32\PHP', |
| 82 | 'Google\CRC32\PHPSlicedBy4', |
| 83 | 'Google\CRC32\Builtin', |
| 84 | 'Google\CRC32\Google', |
| 85 | ]; |
| 86 | |
| 87 | protected $algos = [ |
| 88 | CRC32::IEEE, |
| 89 | CRC32::CASTAGNOLI |
| 90 | ]; |
| 91 | |
| 92 | /** |
| 93 | * Various test data, taken from: |
| 94 | * * https://github.com/php/php-src/blob/master/ext/hash/tests/crc32.phpt |
| 95 | * * https://golang.org/src/hash/crc32/crc32_test.go |
| 96 | * * https://tools.ietf.org/html/rfc3720#appendix-B.4 |
| 97 | * |
| 98 | * @var array Hashes for CRC32::IEEE and CRC32::CASTAGNOLI |
| 99 | */ |
| 100 | protected $data = [ |
| 101 | '' => array('00000000', '00000000'), |
| 102 | 'a' => array('e8b7be43', 'c1d04330'), |
| 103 | 'ab' => array('9e83486d', 'e2a22936'), |
| 104 | 'abc' => array('352441c2', '364b3fb7'), |
| 105 | 'abcd' => array('ed82cd11', '92c80a31'), |
| 106 | 'abcde' => array('8587d865', 'c450d697'), |
| 107 | 'abcdef' => array('4b8e39ef', '53bceff1'), |
| 108 | 'abcdefg' => array('312a6aa6', 'e627f441'), |
| 109 | 'abcdefgh' => array('aeef2a50', '0a9421b7'), |
| 110 | 'abcdefghi' => array('8da988af', '2ddc99fc'), |
| 111 | 'abcdefghij' => array('3981703a', 'e6599437'), |
| 112 | 'abcdefghijklmnopqrstuvwxyz' => array('4c2750bd', '9ee6ef25'), |
| 113 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' => array('1fc2e6d2', 'a245d57d'), |
| 114 | '12345678901234567890123456789012345678901234567890123456789012345678901234567890' => array('7ca94a72', '477a6781'), |
| 115 | 'message digest' => array('20159d7f', '02bd79d0'), |
| 116 | "I can't remember anything" => array('69147a4e', '5e405e93'), |
| 117 | "I can't remember anythingCan’t tell if this is true or dream" => array('3ee63999', '516ad412'), |
| 118 | 'Discard medicine more than two years old.' => array('6b9cdfe7', 'b2cc01fe'), |
| 119 | 'He who has a shady past knows that nice guys finish last.' => array('c90ef73f', '0e28207f'), |
| 120 | "I wouldn't marry him with a ten foot pole." => array('b902341f', 'be93f964'), |
| 121 | "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave" => array('042080e8', '9e3be0c3'), |
| 122 | "The days of the digital watch are numbered. -Tom Stoppard" => array('154c6d11', 'f505ef04'), |
| 123 | "Nepal premier won't resign." => array('4c418325', '85d3dc82'), |
| 124 | "For every action there is an equal and opposite government program." => array('33955150', 'c5142380'), |
| 125 | "His money is twice tainted: 'taint yours and 'taint mine." => array('26216a4b', '75eb77dd'), |
| 126 | "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977" => array('1abbe45e', '91ebe9f7'), |
| 127 | "It's a tiny change to the code and not completely disgusting. - Bob Manchek" => array('c89a94f7', 'f0b1168e'), |
| 128 | "size: a.out: bad magic" => array('ab3abe14', '572b74e2'), |
| 129 | "The major problem is with sendmail. -Mark Horton" => array('bab102b6', '8a58a6d5'), |
| 130 | "Give me a rock, paper and scissors and I will move the world. CCFestoon" => array('999149d7', '9c426c50'), |
| 131 | "If the enemy is within range, then so are you." => array('6d52a33c', '735400a4'), |
| 132 | "It's well we cannot hear the screams/That we create in others' dreams." => array('90631e8d', 'bec49c95'), |
| 133 | "You remind me of a TV show, but that's all right: I watch it anyway." => array('78309130', 'a95a2079'), |
| 134 | "C is as portable as Stonehedge!!" => array('7d0a377f', 'de2e65c5'), |
| 135 | "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley" => array('8c79fd79', '297a88ed'), |
| 136 | "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule" => array('a20b7167', '66ed1d8b'), |
| 137 | "How can you write a big system without C++? -Paul Glick" => array('8e0bb443', 'dcded527'), |
| 138 | "\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#\$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff" => array('29058c73', '9c44184b') |
| 139 | ]; |
| 140 | |
| 141 | protected $count = 0; |
| 142 | |
| 143 | public function rewind() |
| 144 | { |
| 145 | reset($this->crcs); |
| 146 | reset($this->algos); |
| 147 | reset($this->data); |
| 148 | } |
| 149 | |
| 150 | public function valid() |
| 151 | { |
| 152 | return current($this->crcs) !== false && |
| 153 | current($this->algos) !== false && |
| 154 | current($this->data) !== false; |
| 155 | } |
| 156 | |
| 157 | public function key() |
| 158 | { |
| 159 | return $this->count; |
| 160 | } |
| 161 | |
| 162 | public function current() |
| 163 | { |
| 164 | return [current($this->crcs), current($this->algos), key($this->data), current($this->data)[key($this->algos)]]; |
| 165 | } |
| 166 | |
| 167 | public function next() |
| 168 | { |
| 169 | $this->count++; |
| 170 | if (next($this->data) === false) { |
| 171 | reset($this->data); |
| 172 | if (next($this->algos) === false) { |
| 173 | reset($this->algos); |
| 174 | next($this->crcs); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Skip unsupported polynomials |
| 179 | if ($this->valid()) { |
| 180 | $crc_class = current($this->crcs); |
| 181 | $poly = current($this->algos); |
| 182 | if (!$crc_class::supports($poly)) { |
| 183 | $this->next(); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 |