Path: blob/master/test/hotspot/gtest/classfile/test_AltHashing.cpp
41145 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22#include "precompiled.hpp"23#include "classfile/altHashing.hpp"24#include "utilities/debug.hpp"25#include "utilities/formatBuffer.hpp"26#include "unittest.hpp"2728class AltHashingTest : public ::testing::Test {2930public:3132static void testHalfsiphash_32_ByteArray() {33const int factor = 4;3435uint8_t vector[256];36uint8_t hashes[factor * 256];3738for (int i = 0; i < 256; i++) {39vector[i] = (uint8_t) i;40}4142// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}43for (int i = 0; i < 256; i++) {44uint32_t hash = AltHashing::halfsiphash_32(256 - i, vector, i);45hashes[i * factor] = (uint8_t) hash;46hashes[i * factor + 1] = (uint8_t)(hash >> 8);47hashes[i * factor + 2] = (uint8_t)(hash >> 16);48hashes[i * factor + 3] = (uint8_t)(hash >> 24);49}5051// hash to get const result.52uint32_t final_hash = AltHashing::halfsiphash_32(0, hashes, factor*256);5354// Value found using reference implementation for the hashes array.55//uint64_t k = 0; // seed56//uint32_t reference;57//halfsiphash((const uint8_t*)hashes, factor*256, (const uint8_t *)&k, (uint8_t*)&reference, 4);58//printf("0x%x", reference);5960static const uint32_t HALFSIPHASH_32_BYTE_CHECK_VALUE = 0xd2be7fd8;6162ASSERT_EQ(HALFSIPHASH_32_BYTE_CHECK_VALUE, final_hash) <<63err_msg(64"Calculated hash result not as expected. Expected " UINT32_FORMAT " got " UINT32_FORMAT,65HALFSIPHASH_32_BYTE_CHECK_VALUE,66final_hash);67}6869static void testHalfsiphash_32_CharArray() {70const int factor = 2;7172uint16_t vector[256];73uint16_t hashes[factor * 256];7475for (int i = 0; i < 256; i++) {76vector[i] = (uint16_t) i;77}7879// Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}80for (int i = 0; i < 256; i++) {81uint32_t hash = AltHashing::halfsiphash_32(256 - i, vector, i);82hashes[i * factor] = (uint16_t) hash;83hashes[i * factor + 1] = (uint16_t)(hash >> 16);84}8586// hash to get const result.87uint32_t final_hash = AltHashing::halfsiphash_32(0, hashes, factor*256);8889// Value found using reference implementation for the hashes array.90//uint64_t k = 0; // seed91//uint32_t reference;92//halfsiphash((const uint8_t*)hashes, 2*factor*256, (const uint8_t *)&k, (uint8_t*)&reference, 4);93//printf("0x%x", reference);9495static const uint32_t HALFSIPHASH_32_CHAR_CHECK_VALUE = 0x428bf8a5;9697ASSERT_EQ(HALFSIPHASH_32_CHAR_CHECK_VALUE, final_hash) <<98err_msg(99"Calculated hash result not as expected. Expected " UINT32_FORMAT " got " UINT32_FORMAT,100HALFSIPHASH_32_CHAR_CHECK_VALUE,101final_hash);102}103104// Test against sample hashes published with the reference implementation:105// https://github.com/veorq/SipHash106static void testHalfsiphash_64_FromReference() {107108const uint64_t seed = 0x0706050403020100;109const uint64_t results[16] = {1100xc83cb8b9591f8d21, 0xa12ee55b178ae7d5,1110x8c85e4bc20e8feed, 0x99c7f5ae9f1fc77b,1120xb5f37b5fd2aa3673, 0xdba7ee6f0a2bf51b,1130xf1a63fae45107470, 0xb516001efb5f922d,1140x6c6211d8469d7028, 0xdc7642ec407ad686,1150x4caec8671cc8385b, 0x5ab1dc27adf3301e,1160x3e3ea94bc0a8eaa9, 0xe150f598795a4402,1170x1d5ff142f992a4a1, 0x60e426bf902876d6118};119uint32_t vector[16];120121for (int i = 0; i < 16; i++)122vector[i] = 0x03020100 + i * 0x04040404;123124for (int i = 0; i < 16; i++) {125uint64_t hash = AltHashing::halfsiphash_64(seed, vector, i);126ASSERT_EQ(results[i], hash) <<127err_msg(128"Calculated hash result not as expected. Round %d: "129"Expected " UINT64_FORMAT_X " got " UINT64_FORMAT_X "\n",130i,131results[i],132hash);133}134}135};136137TEST_F(AltHashingTest, halfsiphash_test_ByteArray) {138AltHashingTest::testHalfsiphash_32_ByteArray();139}140TEST_F(AltHashingTest, halfsiphash_test_CharArray) {141AltHashingTest::testHalfsiphash_32_CharArray();142}143TEST_F(AltHashingTest, halfsiphash_test_FromReference) {144AltHashingTest::testHalfsiphash_64_FromReference();145}146147148