Jelajahi Sumber

Fix wrong CRC16 calculated on integer value.

Closes #450.
Daniele Alessandri 7 tahun lalu
induk
melakukan
343afd312a
3 mengubah file dengan 20 tambahan dan 0 penghapusan
  1. 7 0
      CHANGELOG.md
  2. 2 0
      src/Cluster/Hash/CRC16.php
  3. 11 0
      tests/Predis/Cluster/Hash/CRC16Test.php

+ 7 - 0
CHANGELOG.md

@@ -1,3 +1,10 @@
+v1.1.2 (2017-xx-xx)
+================================================================================
+
+- __FIX__: pure CRC16 implementation failed to calculate the correct hash when
+  the input value passed to the `hash()` method is an integer (PR #450).
+
+
 v1.1.1 (2016-06-16)
 ================================================================================
 

+ 2 - 0
src/Cluster/Hash/CRC16.php

@@ -61,6 +61,8 @@ class CRC16 implements HashGeneratorInterface
         // CRC-CCITT-16 algorithm
         $crc = 0;
         $CCITT_16 = self::$CCITT_16;
+
+        $value = (string) $value;
         $strlen = strlen($value);
 
         for ($i = 0; $i < $strlen; ++$i) {

+ 11 - 0
tests/Predis/Cluster/Hash/CRC16Test.php

@@ -36,4 +36,15 @@ class CRC16Test extends PredisTestCase
         $this->assertSame(25343, $crc16->hash('key:008'));
         $this->assertSame(29406, $crc16->hash('key:009'));
     }
+
+    /**
+     * @group disconnected
+     */
+    public function testHashGenerationWithIntegerValues()
+    {
+        $crc16 = new CRC16();
+
+        $this->assertSame(13907, $crc16->hash(0));
+        $this->assertSame(55177, $crc16->hash(1234));
+    }
 }