| 1 | /* |
| 2 | * Copyright (C) 2017 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. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include <wtf/ConcurrentPtrHashSet.h> |
| 28 | |
| 29 | namespace TestWebKitAPI { |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | // `commands` is a string that tells this thing what to do. It has to have an even number of characters. |
| 34 | // The character pairs are commands. Commands: |
| 35 | // |
| 36 | // +x Adds 'x' to the set (casts 'x' to a void*) and asserts that it's new. |
| 37 | // !x Adds 'x' to the set and asserts that it was there already. |
| 38 | // =x Asserts that 'x' is in the set |
| 39 | // -x Asserts that 'x' is not in the set |
| 40 | void doTest(const char* commands) |
| 41 | { |
| 42 | ConcurrentPtrHashSet set; |
| 43 | for (const char* command = commands; command[0] && command[1]; command += 2) { |
| 44 | void* ptr = bitwise_cast<void*>(static_cast<uintptr_t>(command[1])); |
| 45 | switch (command[0]) { |
| 46 | case '+': |
| 47 | EXPECT_TRUE(set.add(ptr)); |
| 48 | break; |
| 49 | case '!': |
| 50 | EXPECT_FALSE(set.add(ptr)); |
| 51 | break; |
| 52 | case '=': |
| 53 | EXPECT_TRUE(set.contains(ptr)); |
| 54 | break; |
| 55 | case '-': |
| 56 | EXPECT_FALSE(set.contains(ptr)); |
| 57 | break; |
| 58 | default: |
| 59 | RELEASE_ASSERT_NOT_REACHED(); |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | } // anonymous namespace |
| 66 | |
| 67 | TEST(WTF_ConcurrentPtrHashSet, Empty) |
| 68 | { |
| 69 | doTest("" ); |
| 70 | } |
| 71 | |
| 72 | TEST(WTF_ConcurrentPtrHashSet, AddOneElement) |
| 73 | { |
| 74 | doTest("+x=x-y-0" ); |
| 75 | } |
| 76 | |
| 77 | TEST(WTF_ConcurrentPtrHashSet, AddOneElementMultipleTimes) |
| 78 | { |
| 79 | doTest("+x!x!x!x!x!x!x!x!x!x!x=x-y-0" ); |
| 80 | } |
| 81 | |
| 82 | TEST(WTF_ConcurrentPtrHashSet, AddOneElementManyTimes) |
| 83 | { |
| 84 | doTest("+x!x!x!x!x!x!x!x!x!x!x=x-y-0!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!x!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