forked from f4pga/prjxray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_ops_test.cc
76 lines (63 loc) · 2.31 KB
/
bit_ops_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Copyright (C) 2017-2020 The Project X-Ray Authors.
*
* Use of this source code is governed by a ISC-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/ISC
*
* SPDX-License-Identifier: ISC
*/
#include <prjxray/bit_ops.h>
#include <gtest/gtest.h>
TEST(BitMaskTest, Bit0) {
uint32_t expected = prjxray::bit_mask<uint32_t>(0);
EXPECT_EQ(static_cast<uint32_t>(0x1), expected);
}
TEST(BitMaskTest, Bit3) {
uint32_t expected = prjxray::bit_mask<uint32_t>(3);
EXPECT_EQ(static_cast<uint32_t>(0x8), expected);
}
TEST(BitMaskRange, SingleBit) {
uint32_t expected = prjxray::bit_mask_range<uint32_t>(23, 23);
EXPECT_EQ(static_cast<uint32_t>(0x800000), expected);
}
TEST(BitMaskRange, DownToZero) {
uint32_t expected = prjxray::bit_mask_range<uint32_t>(7, 0);
EXPECT_EQ(static_cast<uint32_t>(0xFF), expected);
}
TEST(BitMaskRange, MiddleBits) {
uint32_t expected = prjxray::bit_mask_range<uint32_t>(18, 8);
EXPECT_EQ(static_cast<uint32_t>(0x7FF00), expected);
}
TEST(BitFieldGetTest, OneSelectedBit) {
uint32_t expected = prjxray::bit_field_get(0xFFFFFFFF, 23, 23);
EXPECT_EQ(static_cast<uint32_t>(1), expected);
}
TEST(BitFieldGetTest, SelectDownToZero) {
uint32_t expected = prjxray::bit_field_get(0xFFCCBBAA, 7, 0);
EXPECT_EQ(static_cast<uint32_t>(0xAA), expected);
}
TEST(BitFieldGetTest, SelectMidway) {
uint32_t expected = prjxray::bit_field_get(0xFFCCBBAA, 18, 8);
EXPECT_EQ(static_cast<uint32_t>(0x4BB), expected);
}
TEST(BitFieldSetTest, WriteOneBit) {
uint32_t actual = prjxray::bit_field_set(
static_cast<uint32_t>(0x0), 23, 23, static_cast<uint32_t>(0x1));
EXPECT_EQ(actual, static_cast<uint32_t>(0x800000));
}
TEST(BitFieldSetTest, WriteOneBitWithOutOfRangeValue) {
uint32_t actual = prjxray::bit_field_set(
static_cast<uint32_t>(0x0), 23, 23, static_cast<uint32_t>(0x3));
EXPECT_EQ(actual, static_cast<uint32_t>(0x800000));
}
TEST(BitFieldSetTest, WriteMultipleBits) {
uint32_t actual = prjxray::bit_field_set(
static_cast<uint32_t>(0x0), 18, 8, static_cast<uint32_t>(0x123));
EXPECT_EQ(actual, static_cast<uint32_t>(0x12300));
}
TEST(BitFieldSetTest, WriteMultipleBitsWithOutOfRangeValue) {
uint32_t actual = prjxray::bit_field_set(
static_cast<uint32_t>(0x0), 18, 8, static_cast<uint32_t>(0x1234));
EXPECT_EQ(actual, static_cast<uint32_t>(0x23400));
}