|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Apple Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * 1. Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * |
| 13 | + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +#include "config.h" |
| 27 | +#include <wtf/LoggingHashMap.h> |
| 28 | + |
| 29 | +namespace TestWebKitAPI { |
| 30 | + |
| 31 | +namespace { |
| 32 | + |
| 33 | +constexpr char intMapTypeArguments[] = "int, int"; |
| 34 | +using IntLoggingHashMap = LoggingHashMap<intMapTypeArguments, int, int>; |
| 35 | + |
| 36 | +} |
| 37 | + |
| 38 | +TEST(WTF_LoggingHashMap, SetForwardsToUnderlyingMap) |
| 39 | +{ |
| 40 | + IntLoggingHashMap map; |
| 41 | + int key = 1; |
| 42 | + map.set(key, 10); |
| 43 | + map.set(2, 20); |
| 44 | + EXPECT_EQ(2u, map.size()); |
| 45 | + |
| 46 | + auto result = map.set(key, 11); |
| 47 | + EXPECT_FALSE(result.isNewEntry); |
| 48 | + EXPECT_EQ(2u, map.size()); |
| 49 | + |
| 50 | + unsigned sum = 0; |
| 51 | + for (auto& entry : map) |
| 52 | + sum += entry.value; |
| 53 | + EXPECT_EQ(31u, sum); |
| 54 | +} |
| 55 | + |
| 56 | +TEST(WTF_LoggingHashMap, AddForwardsToUnderlyingMap) |
| 57 | +{ |
| 58 | + IntLoggingHashMap map; |
| 59 | + int key = 1; |
| 60 | + EXPECT_TRUE(map.add(key, 10).isNewEntry); |
| 61 | + EXPECT_TRUE(map.add(2, 20).isNewEntry); |
| 62 | + EXPECT_FALSE(map.add(key, 99).isNewEntry); |
| 63 | + EXPECT_EQ(2u, map.size()); |
| 64 | + |
| 65 | + unsigned sum = 0; |
| 66 | + for (auto& entry : map) |
| 67 | + sum += entry.value; |
| 68 | + EXPECT_EQ(30u, sum); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(WTF_LoggingHashMap, FastAddForwardsToUnderlyingMap) |
| 72 | +{ |
| 73 | + IntLoggingHashMap map; |
| 74 | + int key = 1; |
| 75 | + EXPECT_TRUE(map.fastAdd(key, 10).isNewEntry); |
| 76 | + EXPECT_TRUE(map.fastAdd(2, 20).isNewEntry); |
| 77 | + EXPECT_FALSE(map.fastAdd(key, 99).isNewEntry); |
| 78 | + EXPECT_EQ(2u, map.size()); |
| 79 | +} |
| 80 | + |
| 81 | +TEST(WTF_LoggingHashMap, AssignmentReturnsThis) |
| 82 | +{ |
| 83 | + IntLoggingHashMap source; |
| 84 | + source.add(1, 10); |
| 85 | + |
| 86 | + IntLoggingHashMap copy; |
| 87 | + IntLoggingHashMap& copyResult = (copy = source); |
| 88 | + EXPECT_EQ(©, ©Result); |
| 89 | + EXPECT_EQ(1u, copy.size()); |
| 90 | + |
| 91 | + IntLoggingHashMap moved; |
| 92 | + IntLoggingHashMap& moveResult = (moved = WTF::move(copy)); |
| 93 | + EXPECT_EQ(&moved, &moveResult); |
| 94 | + EXPECT_EQ(1u, moved.size()); |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace TestWebKitAPI |
0 commit comments