addr_range.test.cc (13465:dee578a46d87) addr_range.test.cc (14048:9fca1b5cea8b)
1/*
1/*
2 * Copyright (c) 2018 ARM Limited
2 * Copyright (c) 2018-2019 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated

--- 24 unchanged lines hidden (view full) ---

35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Nikos Nikoleris
38 */
39
40#include <gtest/gtest.h>
41
42#include "base/addr_range.hh"
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated

--- 24 unchanged lines hidden (view full) ---

35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Nikos Nikoleris
38 */
39
40#include <gtest/gtest.h>
41
42#include "base/addr_range.hh"
43#include "base/bitfield.hh"
43
44TEST(AddrRangeComp, AddrRangeIsSubset)
45{
46 AddrRange r, r1, r2;
47
48 // Test non-interleaved ranges
49 r1 = AddrRange(0x0, 0x7f);
50 r2 = AddrRange(0x80, 0xff);

--- 33 unchanged lines hidden (view full) ---

84 r = AddrRange(0x40, 0x4f);
85 EXPECT_FALSE(r.isSubset(r1));
86 EXPECT_TRUE(r.isSubset(r2));
87
88 r = AddrRange(0xbf, 0xc0);
89 EXPECT_FALSE(r.isSubset(r1));
90 EXPECT_FALSE(r.isSubset(r2));
91}
44
45TEST(AddrRangeComp, AddrRangeIsSubset)
46{
47 AddrRange r, r1, r2;
48
49 // Test non-interleaved ranges
50 r1 = AddrRange(0x0, 0x7f);
51 r2 = AddrRange(0x80, 0xff);

--- 33 unchanged lines hidden (view full) ---

85 r = AddrRange(0x40, 0x4f);
86 EXPECT_FALSE(r.isSubset(r1));
87 EXPECT_TRUE(r.isSubset(r2));
88
89 r = AddrRange(0xbf, 0xc0);
90 EXPECT_FALSE(r.isSubset(r1));
91 EXPECT_FALSE(r.isSubset(r2));
92}
93
94class AddrRangeBase : public testing::Test {
95 protected:
96
97 virtual int getIndex(Addr addr) = 0;
98
99 void testContains()
100 {
101 for (Addr addr = start; addr <= end; addr++) {
102 int i = getIndex(addr);
103 ASSERT_TRUE(range[i].contains(addr));
104 for (int j = 1; j < intlvSize; j++) {
105 ASSERT_FALSE(range[(i + j) % intlvSize].contains(addr));
106 }
107 }
108 }
109
110 void testGetOffset()
111 {
112 Addr offsets[intlvSize] = {0, 0, 0, 0};
113 for (Addr addr = start; addr <= end; addr++) {
114 int i = getIndex(addr);
115 Addr offset = range[i].getOffset(addr);
116 ASSERT_EQ(offsets[i], offset);
117 offsets[i]++;
118 }
119 for (Addr offset: offsets) {
120 ASSERT_EQ(offset, (end - start + 1) / intlvSize);
121 }
122 }
123
124 static const Addr end = 0x1ffff;
125 static const Addr start = 0x0;
126 static const int intlvSize = 4;
127
128 AddrRange range[intlvSize];
129};
130
131
132class AddrRangeCont : public AddrRangeBase {
133 protected:
134 void SetUp() override
135 {
136 std::vector<Addr> masks = {
137 1UL << xorBits0[0] | 1UL << xorBits0[1],
138 1UL << xorBits1[0] | 1UL << xorBits1[1]
139 };
140 for (auto i = 0; i < intlvSize; i++) {
141 range[i] = AddrRange(start, end, masks, i);
142 }
143 }
144
145 int getIndex(Addr addr) override
146 {
147 return bits(addr, xorBits1[1], xorBits0[1]) ^
148 bits(addr, xorBits1[0], xorBits0[0]);
149 }
150
151 const int xorBits0[2] = {8, 14};
152 const int xorBits1[2] = {9, 15};
153};
154
155TEST_F(AddrRangeCont, AddrRangeContains)
156{
157 testContains();
158}
159
160TEST_F(AddrRangeCont, AddrRangeGetOffset)
161{
162 testGetOffset();
163}
164
165
166class AddrRangeContLegacy : public AddrRangeCont {
167 protected:
168 void SetUp() override
169 {
170 // Test interleaved ranges with hashing
171 for (auto i = 0; i < intlvSize; i++) {
172 range[i] = AddrRange(start, end, xorBits1[0], xorBits1[1],
173 2, i);
174 }
175 }
176};
177
178TEST_F(AddrRangeContLegacy, AddrRangeContains)
179{
180 testContains();
181}
182
183TEST_F(AddrRangeContLegacy, AddrRangeGetOffset)
184{
185 testGetOffset();
186}
187
188
189class AddrRangeArb : public AddrRangeBase {
190 protected:
191 void SetUp() override
192 {
193 std::vector<Addr> masks = {
194 1UL << xorBits0[0] | 1UL << xorBits0[1],
195 1UL << xorBits1[0] | 1UL << xorBits1[1]
196 };
197 for (auto i = 0; i < intlvSize; i++) {
198 range[i] = AddrRange(start, end, masks, i);
199 }
200 }
201
202 int getIndex(Addr addr) override
203 {
204 return (bits(addr, xorBits0[0]) ^ bits(addr, xorBits0[1])) |
205 (bits(addr, xorBits1[0]) ^ bits(addr, xorBits1[1])) << 1;
206 }
207
208 const int xorBits0[2] = {11, 12};
209 const int xorBits1[2] = {8, 15};
210};
211
212TEST_F(AddrRangeArb, AddrRangeContains)
213{
214 testContains();
215}
216
217TEST_F(AddrRangeArb, AddrRangeGetOffset)
218{
219 testGetOffset();
220}