Sun, 09 Apr 2023 19:03:58 +0200
refactoring of collections to make use of destructors in map implementations
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@596 | 52 | EXPECT_EQ(str_key.data.cstr, str); |
universe@596 | 53 | EXPECT_EQ(bytes_key.data.cbytes, reinterpret_cast<unsigned char const *>(str)); |
universe@604 | 54 | EXPECT_EQ(bytes_key.data.cobj, reinterpret_cast<void const *>(str)); |
universe@663 | 55 | EXPECT_EQ(cxstr_key.data.cobj, reinterpret_cast<void const *>(str)); |
universe@596 | 56 | } |
universe@604 | 57 | |
universe@604 | 58 | TEST(cx_hash_key, empty_string) { |
universe@604 | 59 | auto str = ""; |
universe@604 | 60 | |
universe@604 | 61 | auto str_key = cx_hash_key_str(str); |
universe@604 | 62 | auto bytes_key = cx_hash_key_bytes( |
universe@604 | 63 | reinterpret_cast<unsigned char const *>(str), 0); |
universe@604 | 64 | auto obj_key = cx_hash_key( |
universe@604 | 65 | reinterpret_cast<void const *>(str), 0); |
universe@604 | 66 | |
universe@604 | 67 | EXPECT_EQ(bytes_key.hash, 4152238450u); |
universe@604 | 68 | EXPECT_EQ(str_key.hash, 4152238450u); |
universe@604 | 69 | EXPECT_EQ(obj_key.hash, 4152238450u); |
universe@604 | 70 | EXPECT_EQ(str_key.len, 0); |
universe@604 | 71 | EXPECT_EQ(bytes_key.len, 0); |
universe@604 | 72 | EXPECT_EQ(bytes_key.len, 0); |
universe@604 | 73 | EXPECT_EQ(str_key.data.cstr, str); |
universe@604 | 74 | EXPECT_EQ(bytes_key.data.cbytes, reinterpret_cast<unsigned char const *>(str)); |
universe@604 | 75 | EXPECT_EQ(bytes_key.data.cobj, reinterpret_cast<void const *>(str)); |
universe@604 | 76 | } |
universe@604 | 77 | |
universe@604 | 78 | TEST(cx_hash_key, null_ptr) { |
universe@604 | 79 | auto str_key = cx_hash_key_str(nullptr); |
universe@604 | 80 | auto bytes_key = cx_hash_key_bytes(nullptr, 0); |
universe@604 | 81 | auto obj_key = cx_hash_key(nullptr, 0); |
universe@604 | 82 | |
universe@604 | 83 | EXPECT_EQ(bytes_key.hash, 1574210520u); |
universe@604 | 84 | EXPECT_EQ(str_key.hash, 1574210520u); |
universe@604 | 85 | EXPECT_EQ(obj_key.hash, 1574210520u); |
universe@604 | 86 | EXPECT_EQ(str_key.len, 0); |
universe@604 | 87 | EXPECT_EQ(bytes_key.len, 0); |
universe@604 | 88 | EXPECT_EQ(bytes_key.len, 0); |
universe@604 | 89 | EXPECT_EQ(str_key.data.cstr, nullptr); |
universe@604 | 90 | EXPECT_EQ(bytes_key.data.cbytes, nullptr); |
universe@604 | 91 | EXPECT_EQ(bytes_key.data.cobj, nullptr); |
universe@604 | 92 | } |