tests/test_hash_key.cpp

Sun, 21 May 2023 16:22:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 May 2023 16:22:09 +0200
changeset 710
2dd409ed056f
parent 690
2c2304622981
permissions
-rw-r--r--

fix const-ness of non-mutating iterator creation for maps

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@663 30 #include "cx/string.h"
universe@596 31
universe@596 32 #include <gtest/gtest.h>
universe@596 33
universe@596 34 TEST(cx_hash_key, functions) {
universe@596 35 auto str = "my key";
universe@604 36 auto len = strlen(str);
universe@596 37
universe@596 38 auto str_key = cx_hash_key_str(str);
universe@596 39 auto bytes_key = cx_hash_key_bytes(
universe@596 40 reinterpret_cast<unsigned char const *>(str), len);
universe@603 41 auto obj_key = cx_hash_key(
universe@603 42 reinterpret_cast<void const *>(str), len);
universe@663 43 auto cxstr_key = cx_hash_key_cxstr(cx_str(str));
universe@596 44
universe@596 45 EXPECT_EQ(str_key.hash, bytes_key.hash);
universe@596 46 EXPECT_EQ(obj_key.hash, bytes_key.hash);
universe@663 47 EXPECT_EQ(cxstr_key.hash, bytes_key.hash);
universe@596 48 EXPECT_EQ(str_key.len, len);
universe@663 49 EXPECT_EQ(cxstr_key.len, len);
universe@596 50 EXPECT_EQ(bytes_key.len, len);
universe@596 51 EXPECT_EQ(bytes_key.len, len);
universe@690 52 EXPECT_EQ(str_key.data, str);
universe@596 53 }
universe@604 54
universe@604 55 TEST(cx_hash_key, empty_string) {
universe@604 56 auto str = "";
universe@604 57
universe@604 58 auto str_key = cx_hash_key_str(str);
universe@604 59 auto bytes_key = cx_hash_key_bytes(
universe@604 60 reinterpret_cast<unsigned char const *>(str), 0);
universe@604 61 auto obj_key = cx_hash_key(
universe@604 62 reinterpret_cast<void const *>(str), 0);
universe@604 63
universe@604 64 EXPECT_EQ(bytes_key.hash, 4152238450u);
universe@604 65 EXPECT_EQ(str_key.hash, 4152238450u);
universe@604 66 EXPECT_EQ(obj_key.hash, 4152238450u);
universe@604 67 EXPECT_EQ(str_key.len, 0);
universe@604 68 EXPECT_EQ(bytes_key.len, 0);
universe@604 69 EXPECT_EQ(bytes_key.len, 0);
universe@690 70 EXPECT_EQ(str_key.data, 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@690 84 EXPECT_EQ(str_key.data, nullptr);
universe@604 85 }

mercurial