tests/test_hash_key.cpp

Tue, 07 Feb 2023 21:55:37 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 07 Feb 2023 21:55:37 +0100
changeset 653
e081643aae2a
parent 604
test/test_hash_key.cpp@056e5f592d84
child 663
d50b5dc1e058
permissions
-rw-r--r--

rename test directory to avoid name clash with Makefile target

universe@596 1 /*
universe@596 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@596 3 *
universe@596 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@596 5 *
universe@596 6 * Redistribution and use in source and binary forms, with or without
universe@596 7 * modification, are permitted provided that the following conditions are met:
universe@596 8 *
universe@596 9 * 1. Redistributions of source code must retain the above copyright
universe@596 10 * notice, this list of conditions and the following disclaimer.
universe@596 11 *
universe@596 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@596 13 * notice, this list of conditions and the following disclaimer in the
universe@596 14 * documentation and/or other materials provided with the distribution.
universe@596 15 *
universe@596 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@596 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@596 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@596 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@596 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@596 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@596 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@596 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@596 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@596 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@596 26 * POSSIBILITY OF SUCH DAMAGE.
universe@596 27 */
universe@596 28
universe@596 29 #include "cx/hash_key.h"
universe@596 30
universe@596 31 #include <gtest/gtest.h>
universe@596 32
universe@596 33 TEST(cx_hash_key, functions) {
universe@596 34 auto str = "my key";
universe@604 35 auto len = strlen(str);
universe@596 36
universe@596 37 auto str_key = cx_hash_key_str(str);
universe@596 38 auto bytes_key = cx_hash_key_bytes(
universe@596 39 reinterpret_cast<unsigned char const *>(str), len);
universe@603 40 auto obj_key = cx_hash_key(
universe@603 41 reinterpret_cast<void const *>(str), len);
universe@596 42
universe@596 43 EXPECT_EQ(str_key.hash, bytes_key.hash);
universe@596 44 EXPECT_EQ(obj_key.hash, bytes_key.hash);
universe@596 45 EXPECT_EQ(str_key.len, len);
universe@596 46 EXPECT_EQ(bytes_key.len, len);
universe@596 47 EXPECT_EQ(bytes_key.len, len);
universe@596 48 EXPECT_EQ(str_key.data.cstr, str);
universe@596 49 EXPECT_EQ(bytes_key.data.cbytes, reinterpret_cast<unsigned char const *>(str));
universe@604 50 EXPECT_EQ(bytes_key.data.cobj, reinterpret_cast<void const *>(str));
universe@596 51 }
universe@604 52
universe@604 53 TEST(cx_hash_key, empty_string) {
universe@604 54 auto str = "";
universe@604 55
universe@604 56 auto str_key = cx_hash_key_str(str);
universe@604 57 auto bytes_key = cx_hash_key_bytes(
universe@604 58 reinterpret_cast<unsigned char const *>(str), 0);
universe@604 59 auto obj_key = cx_hash_key(
universe@604 60 reinterpret_cast<void const *>(str), 0);
universe@604 61
universe@604 62 EXPECT_EQ(bytes_key.hash, 4152238450u);
universe@604 63 EXPECT_EQ(str_key.hash, 4152238450u);
universe@604 64 EXPECT_EQ(obj_key.hash, 4152238450u);
universe@604 65 EXPECT_EQ(str_key.len, 0);
universe@604 66 EXPECT_EQ(bytes_key.len, 0);
universe@604 67 EXPECT_EQ(bytes_key.len, 0);
universe@604 68 EXPECT_EQ(str_key.data.cstr, str);
universe@604 69 EXPECT_EQ(bytes_key.data.cbytes, reinterpret_cast<unsigned char const *>(str));
universe@604 70 EXPECT_EQ(bytes_key.data.cobj, reinterpret_cast<void const *>(str));
universe@604 71 }
universe@604 72
universe@604 73 TEST(cx_hash_key, null_ptr) {
universe@604 74 auto str_key = cx_hash_key_str(nullptr);
universe@604 75 auto bytes_key = cx_hash_key_bytes(nullptr, 0);
universe@604 76 auto obj_key = cx_hash_key(nullptr, 0);
universe@604 77
universe@604 78 EXPECT_EQ(bytes_key.hash, 1574210520u);
universe@604 79 EXPECT_EQ(str_key.hash, 1574210520u);
universe@604 80 EXPECT_EQ(obj_key.hash, 1574210520u);
universe@604 81 EXPECT_EQ(str_key.len, 0);
universe@604 82 EXPECT_EQ(bytes_key.len, 0);
universe@604 83 EXPECT_EQ(bytes_key.len, 0);
universe@604 84 EXPECT_EQ(str_key.data.cstr, nullptr);
universe@604 85 EXPECT_EQ(bytes_key.data.cbytes, nullptr);
universe@604 86 EXPECT_EQ(bytes_key.data.cobj, nullptr);
universe@604 87 }

mercurial