1 | /* |
2 | * Copyright (C) 2019 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 | |
28 | #include <wtf/Packed.h> |
29 | |
30 | namespace TestWebKitAPI { |
31 | |
32 | struct PackedPair { |
33 | PackedPtr<uint8_t> key { nullptr }; |
34 | PackedPtr<uint8_t> value { nullptr }; |
35 | }; |
36 | |
37 | TEST(WTF_Packed, StructSize) |
38 | { |
39 | EXPECT_EQ(alignof(PackedPair), 1U); |
40 | #if CPU(X86_64) |
41 | EXPECT_EQ(sizeof(PackedPair), 12U); |
42 | #endif |
43 | { |
44 | Packed<double> value; |
45 | value = 4.2; |
46 | EXPECT_EQ(value.get(), 4.2); |
47 | } |
48 | { |
49 | uint64_t originalValue = 0xff00ff00dd00dd00UL; |
50 | Packed<uint64_t> value; |
51 | value = originalValue; |
52 | EXPECT_EQ(value.get(), originalValue); |
53 | EXPECT_EQ(alignof(Packed<uint64_t>), 1U); |
54 | EXPECT_EQ(sizeof(Packed<uint64_t>), sizeof(uint64_t)); |
55 | } |
56 | } |
57 | |
58 | TEST(WTF_Packed, AssignAndGet) |
59 | { |
60 | { |
61 | PackedPtr<uint8_t> key { nullptr }; |
62 | static_assert(WTF_CPU_EFFECTIVE_ADDRESS_WIDTH != 64, "" ); |
63 | uint8_t* max = bitwise_cast<uint8_t*>(static_cast<uintptr_t>(((1ULL) << WTF_CPU_EFFECTIVE_ADDRESS_WIDTH) - 1)); |
64 | key = max; |
65 | EXPECT_EQ(key.get(), max); |
66 | } |
67 | } |
68 | |
69 | TEST(WTF_Packed, PackedAlignedPtr) |
70 | { |
71 | { |
72 | PackedAlignedPtr<uint8_t, 256> key { nullptr }; |
73 | EXPECT_LE(sizeof(key), 5U); |
74 | } |
75 | { |
76 | PackedAlignedPtr<uint8_t, 16> key { nullptr }; |
77 | #if OS(DARWIN) && CPU(ARM64) |
78 | EXPECT_EQ(sizeof(key), 4U); |
79 | #else |
80 | EXPECT_LE(sizeof(key), 6U); |
81 | #endif |
82 | } |
83 | } |
84 | |
85 | } // namespace TestWebKitAPI |
86 | |