universe@596: /* universe@596: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@596: * universe@596: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@596: * universe@596: * Redistribution and use in source and binary forms, with or without universe@596: * modification, are permitted provided that the following conditions are met: universe@596: * universe@596: * 1. Redistributions of source code must retain the above copyright universe@596: * notice, this list of conditions and the following disclaimer. universe@596: * universe@596: * 2. Redistributions in binary form must reproduce the above copyright universe@596: * notice, this list of conditions and the following disclaimer in the universe@596: * documentation and/or other materials provided with the distribution. universe@596: * universe@596: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@596: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@596: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@596: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@596: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@596: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@596: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@596: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@596: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@596: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@596: * POSSIBILITY OF SUCH DAMAGE. universe@596: */ universe@596: universe@596: #include "cx/hash_key.h" universe@663: #include "cx/string.h" universe@596: universe@596: #include universe@596: universe@596: TEST(cx_hash_key, functions) { universe@596: auto str = "my key"; universe@604: auto len = strlen(str); universe@596: universe@596: auto str_key = cx_hash_key_str(str); universe@596: auto bytes_key = cx_hash_key_bytes( universe@596: reinterpret_cast(str), len); universe@603: auto obj_key = cx_hash_key( universe@603: reinterpret_cast(str), len); universe@663: auto cxstr_key = cx_hash_key_cxstr(cx_str(str)); universe@596: universe@596: EXPECT_EQ(str_key.hash, bytes_key.hash); universe@596: EXPECT_EQ(obj_key.hash, bytes_key.hash); universe@663: EXPECT_EQ(cxstr_key.hash, bytes_key.hash); universe@596: EXPECT_EQ(str_key.len, len); universe@663: EXPECT_EQ(cxstr_key.len, len); universe@596: EXPECT_EQ(bytes_key.len, len); universe@596: EXPECT_EQ(bytes_key.len, len); universe@690: EXPECT_EQ(str_key.data, str); universe@596: } universe@604: universe@604: TEST(cx_hash_key, empty_string) { universe@604: auto str = ""; universe@604: universe@604: auto str_key = cx_hash_key_str(str); universe@604: auto bytes_key = cx_hash_key_bytes( universe@604: reinterpret_cast(str), 0); universe@604: auto obj_key = cx_hash_key( universe@604: reinterpret_cast(str), 0); universe@604: universe@604: EXPECT_EQ(bytes_key.hash, 4152238450u); universe@604: EXPECT_EQ(str_key.hash, 4152238450u); universe@604: EXPECT_EQ(obj_key.hash, 4152238450u); universe@604: EXPECT_EQ(str_key.len, 0); universe@604: EXPECT_EQ(bytes_key.len, 0); universe@604: EXPECT_EQ(bytes_key.len, 0); universe@690: EXPECT_EQ(str_key.data, str); universe@604: } universe@604: universe@604: TEST(cx_hash_key, null_ptr) { universe@604: auto str_key = cx_hash_key_str(nullptr); universe@604: auto bytes_key = cx_hash_key_bytes(nullptr, 0); universe@604: auto obj_key = cx_hash_key(nullptr, 0); universe@604: universe@604: EXPECT_EQ(bytes_key.hash, 1574210520u); universe@604: EXPECT_EQ(str_key.hash, 1574210520u); universe@604: EXPECT_EQ(obj_key.hash, 1574210520u); universe@604: EXPECT_EQ(str_key.len, 0); universe@604: EXPECT_EQ(bytes_key.len, 0); universe@604: EXPECT_EQ(bytes_key.len, 0); universe@690: EXPECT_EQ(str_key.data, nullptr); universe@604: }