@Test public void randomizedStressTestBytes() { int size = 65536; Random rand = new Random(); // A set used to track collision rate. Set<Integer> hashcodes = new HashSet<>(); for (int i = 0; i < size; i++) { int byteArrSize = rand.nextInt(100) * 8; byte[] bytes = new byte[byteArrSize]; rand.nextBytes(bytes); Assert.assertEquals( HiveHasher.hashUnsafeBytes(bytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize), HiveHasher.hashUnsafeBytes(bytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize)); hashcodes.add(HiveHasher.hashUnsafeBytes( bytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize)); } // A very loose bound. Assert.assertTrue(hashcodes.size() > size * 0.95); }
@Test public void randomizedStressTestBytes() { int size = 65536; Random rand = new Random(); // A set used to track collision rate. Set<Integer> hashcodes = new HashSet<>(); for (int i = 0; i < size; i++) { int byteArrSize = rand.nextInt(100) * 8; byte[] bytes = new byte[byteArrSize]; rand.nextBytes(bytes); Assert.assertEquals( HiveHasher.hashUnsafeBytes(bytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize), HiveHasher.hashUnsafeBytes(bytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize)); hashcodes.add(HiveHasher.hashUnsafeBytes( bytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize)); } // A very loose bound. Assert.assertTrue(hashcodes.size() > size * 0.95); }
@Test public void randomizedStressTestBytes() { int size = 65536; Random rand = new Random(); // A set used to track collision rate. Set<Integer> hashcodes = new HashSet<>(); for (int i = 0; i < size; i++) { int byteArrSize = rand.nextInt(100) * 8; byte[] bytes = new byte[byteArrSize]; rand.nextBytes(bytes); Assert.assertEquals( HiveHasher.hashUnsafeBytes(bytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize), HiveHasher.hashUnsafeBytes(bytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize)); hashcodes.add(HiveHasher.hashUnsafeBytes( bytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize)); } // A very loose bound. Assert.assertTrue(hashcodes.size() > size * 0.95); }
@Test public void randomizedStressTestPaddedStrings() { int size = 64000; // A set used to track collision rate. Set<Integer> hashcodes = new HashSet<>(); for (int i = 0; i < size; i++) { int byteArrSize = 8; byte[] strBytes = String.valueOf(i).getBytes(StandardCharsets.UTF_8); byte[] paddedBytes = new byte[byteArrSize]; System.arraycopy(strBytes, 0, paddedBytes, 0, strBytes.length); Assert.assertEquals( HiveHasher.hashUnsafeBytes(paddedBytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize), HiveHasher.hashUnsafeBytes(paddedBytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize)); hashcodes.add(HiveHasher.hashUnsafeBytes( paddedBytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize)); } // A very loose bound. Assert.assertTrue(hashcodes.size() > size * 0.95); } }
@Test public void randomizedStressTestPaddedStrings() { int size = 64000; // A set used to track collision rate. Set<Integer> hashcodes = new HashSet<>(); for (int i = 0; i < size; i++) { int byteArrSize = 8; byte[] strBytes = String.valueOf(i).getBytes(StandardCharsets.UTF_8); byte[] paddedBytes = new byte[byteArrSize]; System.arraycopy(strBytes, 0, paddedBytes, 0, strBytes.length); Assert.assertEquals( HiveHasher.hashUnsafeBytes(paddedBytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize), HiveHasher.hashUnsafeBytes(paddedBytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize)); hashcodes.add(HiveHasher.hashUnsafeBytes( paddedBytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize)); } // A very loose bound. Assert.assertTrue(hashcodes.size() > size * 0.95); } }
@Test public void randomizedStressTestPaddedStrings() { int size = 64000; // A set used to track collision rate. Set<Integer> hashcodes = new HashSet<>(); for (int i = 0; i < size; i++) { int byteArrSize = 8; byte[] strBytes = String.valueOf(i).getBytes(StandardCharsets.UTF_8); byte[] paddedBytes = new byte[byteArrSize]; System.arraycopy(strBytes, 0, paddedBytes, 0, strBytes.length); Assert.assertEquals( HiveHasher.hashUnsafeBytes(paddedBytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize), HiveHasher.hashUnsafeBytes(paddedBytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize)); hashcodes.add(HiveHasher.hashUnsafeBytes( paddedBytes, Platform.BYTE_ARRAY_OFFSET, byteArrSize)); } // A very loose bound. Assert.assertTrue(hashcodes.size() > size * 0.95); } }
@Test public void testKnownStringAndIntInputs() { int[] inputs = {84, 19, 8}; int[] expected = {-823832826, -823835053, 111972242}; for (int i = 0; i < inputs.length; i++) { UTF8String s = UTF8String.fromString("val_" + inputs[i]); int hash = HiveHasher.hashUnsafeBytes(s.getBaseObject(), s.getBaseOffset(), s.numBytes()); Assert.assertEquals(expected[i], ((31 * inputs[i]) + hash)); } }
@Test public void testKnownStringAndIntInputs() { int[] inputs = {84, 19, 8}; int[] expected = {-823832826, -823835053, 111972242}; for (int i = 0; i < inputs.length; i++) { UTF8String s = UTF8String.fromString("val_" + inputs[i]); int hash = HiveHasher.hashUnsafeBytes(s.getBaseObject(), s.getBaseOffset(), s.numBytes()); Assert.assertEquals(expected[i], ((31 * inputs[i]) + hash)); } }
@Test public void testKnownStringAndIntInputs() { int[] inputs = {84, 19, 8}; int[] expected = {-823832826, -823835053, 111972242}; for (int i = 0; i < inputs.length; i++) { UTF8String s = UTF8String.fromString("val_" + inputs[i]); int hash = HiveHasher.hashUnsafeBytes(s.getBaseObject(), s.getBaseOffset(), s.numBytes()); Assert.assertEquals(expected[i], ((31 * inputs[i]) + hash)); } }