1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #include "platform/assert.h" |
6 | |
7 | #include "vm/bitfield.h" |
8 | #include "vm/globals.h" |
9 | #include "vm/unit_test.h" |
10 | |
11 | namespace dart { |
12 | |
13 | VM_UNIT_TEST_CASE(BitFields) { |
14 | class TestBitFields : public BitField<uword, int32_t, 1, 8> {}; |
15 | EXPECT(TestBitFields::is_valid(16)); |
16 | EXPECT(!TestBitFields::is_valid(256)); |
17 | EXPECT_EQ(0x00ffU, TestBitFields::mask()); |
18 | EXPECT_EQ(0x001feU, TestBitFields::mask_in_place()); |
19 | EXPECT_EQ(1, TestBitFields::shift()); |
20 | EXPECT_EQ(8, TestBitFields::bitsize()); |
21 | EXPECT_EQ(32U, TestBitFields::encode(16)); |
22 | EXPECT_EQ(16, TestBitFields::decode(32)); |
23 | EXPECT_EQ(2U, TestBitFields::update(1, 16)); |
24 | } |
25 | |
26 | template <typename T> |
27 | static void TestSignExtendedBitField() { |
28 | class F1 : public BitField<T, intptr_t, 0, 8, /*sign_extend=*/true> {}; |
29 | class F2 |
30 | : public BitField<T, uintptr_t, F1::kNextBit, 8, /*sign_extend=*/false> { |
31 | }; |
32 | class F3 |
33 | : public BitField<T, intptr_t, F2::kNextBit, 8, /*sign_extend=*/true> {}; |
34 | class F4 |
35 | : public BitField<T, uintptr_t, F3::kNextBit, 8, /*sign_extend=*/false> { |
36 | }; |
37 | |
38 | const uint32_t value = |
39 | F1::encode(-1) | F2::encode(1) | F3::encode(-2) | F4::encode(2); |
40 | EXPECT_EQ(0x02fe01ffU, value); |
41 | EXPECT_EQ(-1, F1::decode(value)); |
42 | EXPECT_EQ(1U, F2::decode(value)); |
43 | EXPECT_EQ(-2, F3::decode(value)); |
44 | EXPECT_EQ(2U, F4::decode(value)); |
45 | } |
46 | |
47 | template <typename T> |
48 | static void TestNotSignExtendedBitField() { |
49 | class F1 : public BitField<T, intptr_t, 0, 8, /*sign_extend=*/false> {}; |
50 | class F2 |
51 | : public BitField<T, uintptr_t, F1::kNextBit, 8, /*sign_extend=*/false> { |
52 | }; |
53 | class F3 |
54 | : public BitField<T, intptr_t, F2::kNextBit, 8, /*sign_extend=*/false> {}; |
55 | class F4 |
56 | : public BitField<T, uintptr_t, F3::kNextBit, 8, /*sign_extend=*/false> { |
57 | }; |
58 | |
59 | const uint32_t value = |
60 | F1::encode(-1) | F2::encode(1) | F3::encode(-2) | F4::encode(2); |
61 | EXPECT_EQ(0x02fe01ffU, value); |
62 | EXPECT_EQ(3, F1::decode(value)); |
63 | EXPECT_EQ(1, F2::decode(value)); |
64 | EXPECT_EQ(2, F3::decode(value)); |
65 | EXPECT_EQ(2, F3::decode(value)); |
66 | } |
67 | |
68 | VM_UNIT_TEST_CASE(BitFields_SignedField) { |
69 | TestSignExtendedBitField<uint32_t>(); |
70 | TestSignExtendedBitField<int32_t>(); |
71 | } |
72 | |
73 | #if defined(DEBUG) |
74 | #define DEBUG_CRASH "Crash" |
75 | #else |
76 | #define DEBUG_CRASH "Pass" |
77 | #endif |
78 | |
79 | VM_UNIT_TEST_CASE_WITH_EXPECTATION(BitFields_Assert, DEBUG_CRASH) { |
80 | class F : public BitField<uint32_t, uint32_t, 0, 8, /*sign_extend=*/false> {}; |
81 | const uint32_t value = F::encode(value: kMaxUint32); |
82 | EXPECT_EQ(kMaxUint8, value); |
83 | } |
84 | |
85 | } // namespace dart |
86 | |