2 * Copyright (c) 2019 The Regents of the University of California
3 * Copyright (c) 2018-2019 ARM Limited
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
17 * met: redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer;
19 * redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution;
22 * neither the name of the copyright holders nor the names of its
23 * contributors may be used to endorse or promote products derived from
24 * this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Authors: Nikos Nikoleris
42 #include <gtest/gtest.h>
46 #include "base/addr_range.hh"
47 #include "base/bitfield.hh"
49 TEST(AddrRangeTest
, ValidRange
)
52 EXPECT_FALSE(r
.valid());
56 * This following tests check the behavior of AddrRange when initialized with
57 * a start and end address. The expected behavior is that the first address
58 * within the range will be the start address, and the last address in the
59 * range will be the (end - 1) address.
61 TEST(AddrRangeTest
, EmptyRange
)
63 AddrRange
r(0x0, 0x0);
66 * Empty ranges are valid.
68 EXPECT_TRUE(r
.valid());
69 EXPECT_EQ(0x0, r
.start());
70 EXPECT_EQ(0x0, r
.end());
71 EXPECT_EQ(0, r
.size());
74 * With no masks, granularity equals the size of the range.
76 EXPECT_EQ(0, r
.granularity());
79 * With no masks, "interleaved()" returns false.
81 EXPECT_FALSE(r
.interleaved());
84 * With no masks, "stripes()" returns ULL(1).
86 EXPECT_EQ(ULL(1), r
.stripes());
87 EXPECT_EQ("[0:0]", r
.to_string());
90 TEST(AddrRangeTest
, RangeSizeOfOne
)
92 AddrRange
r(0x0, 0x1);
93 EXPECT_TRUE(r
.valid());
94 EXPECT_EQ(0x0, r
.start());
95 EXPECT_EQ(0x1, r
.end());
96 EXPECT_EQ(1, r
.size());
97 EXPECT_EQ(1, r
.granularity());
98 EXPECT_FALSE(r
.interleaved());
99 EXPECT_EQ(ULL(1), r
.stripes());
100 EXPECT_EQ("[0:0x1]", r
.to_string());
103 TEST(AddrRangeTest
, Range16Bit
)
105 AddrRange
r(0xF000, 0xFFFF);
106 EXPECT_TRUE(r
.valid());
107 EXPECT_EQ(0xF000, r
.start());
108 EXPECT_EQ(0xFFFF, r
.end());
109 EXPECT_EQ(0x0FFF, r
.size());
110 EXPECT_EQ(0x0FFF, r
.granularity());
111 EXPECT_FALSE(r
.interleaved());
112 EXPECT_EQ(ULL(1), r
.stripes());
113 EXPECT_EQ("[0xf000:0xffff]", r
.to_string());
116 TEST(AddrRangeTest
, InvalidRange
)
118 AddrRange
r(0x1, 0x0);
119 EXPECT_FALSE(r
.valid());
122 TEST(AddrRangeTest
, LessThan
)
125 * The less-than override is a bit unintuitive and does not have a
126 * corresponding greater than. It compares the AddrRange.start() values.
127 * If they are equal, the "intlvMatch" values are compared. This is
128 * zero when AddRange is initialized with a just a start and end address.
130 AddrRange
r1(0xF000, 0xFFFF);
131 AddrRange
r2(0xF001, 0xFFFF);
132 AddrRange
r3(0xF000, 0xFFFF);
134 EXPECT_TRUE(r1
< r2
);
135 EXPECT_FALSE(r2
< r1
);
136 EXPECT_FALSE(r1
< r3
);
137 EXPECT_FALSE(r3
< r1
);
140 TEST(AddrRangeTest
, EqualToNotEqualTo
)
142 AddrRange
r1(0x1234, 0x5678);
143 AddrRange
r2(0x1234, 0x5678);
144 AddrRange
r3(0x1234, 0x5679);
146 EXPECT_TRUE(r1
== r2
);
147 EXPECT_FALSE(r1
== r3
);
148 EXPECT_FALSE(r1
!= r2
);
149 EXPECT_TRUE(r1
!= r3
);
151 EXPECT_TRUE(r2
== r1
);
152 EXPECT_FALSE(r3
== r1
);
153 EXPECT_FALSE(r2
!= r1
);
154 EXPECT_TRUE(r3
!= r1
);
157 TEST(AddrRangeTest
, MergesWith
)
160 * AddrRange.mergesWith will return true if the start, end, and masks
163 AddrRange
r1(0x10, 0x1F);
164 AddrRange
r2(0x10, 0x1F);
166 EXPECT_TRUE(r1
.mergesWith(r2
));
167 EXPECT_TRUE(r2
.mergesWith(r1
));
170 TEST(AddrRangeTest
, DoesNotMergeWith
)
172 AddrRange
r1(0x10, 0x1E);
173 AddrRange
r2(0x10, 0x1F);
175 EXPECT_FALSE(r1
.mergesWith(r2
));
176 EXPECT_FALSE(r2
.mergesWith(r1
));
179 TEST(AddrRangeTest
, IntersectsCompleteOverlap
)
181 AddrRange
r1(0x21, 0x30);
182 AddrRange
r2(0x21, 0x30);
184 EXPECT_TRUE(r1
.intersects(r2
));
185 EXPECT_TRUE(r2
.intersects(r1
));
188 TEST(AddrRangeTest
, IntersectsAddressWithin
)
190 AddrRange
r1(0x0, 0xF);
191 AddrRange
r2(0x1, 0xE);
193 EXPECT_TRUE(r1
.intersects(r2
));
194 EXPECT_TRUE(r2
.intersects(r1
));
197 TEST(AddrRangeTest
, IntersectsPartialOverlap
)
199 AddrRange
r1(0x0F0, 0x0FF);
200 AddrRange
r2(0x0F5, 0xF00);
202 EXPECT_TRUE(r1
.intersects(r2
));
203 EXPECT_TRUE(r2
.intersects(r1
));
206 TEST(AddrRangeTest
, IntersectsNoOverlap
)
208 AddrRange
r1(0x00, 0x10);
209 AddrRange
r2(0x11, 0xFF);
211 EXPECT_FALSE(r1
.intersects(r2
));
212 EXPECT_FALSE(r2
.intersects(r1
));
215 TEST(AddrRangeTest
, IntersectsFirstLastAddressOverlap
)
217 AddrRange
r1(0x0, 0xF);
218 AddrRange
r2(0xF, 0xF0);
221 * The "end address" is not in the range. Therefore, if
222 * r1.end() == r2.start(), the ranges do not intersect.
224 EXPECT_FALSE(r1
.intersects(r2
));
225 EXPECT_FALSE(r2
.intersects(r1
));
228 TEST(AddrRangeTest
, isSubsetCompleteOverlap
)
230 AddrRange
r1(0x10, 0x20);
231 AddrRange
r2(0x10, 0x20);
233 EXPECT_TRUE(r1
.isSubset(r2
));
234 EXPECT_TRUE(r2
.isSubset(r1
));
237 TEST(AddrRangeTest
, isSubsetNoOverlap
)
239 AddrRange
r1(0x10, 0x20);
240 AddrRange
r2(0x20, 0x22);
242 EXPECT_FALSE(r1
.isSubset(r2
));
243 EXPECT_FALSE(r2
.isSubset(r1
));
246 TEST(AddrRangeTest
, isSubsetTrueSubset
)
248 AddrRange
r1(0x10, 0x20);
249 AddrRange
r2(0x15, 0x17);
251 EXPECT_TRUE(r2
.isSubset(r1
));
252 EXPECT_FALSE(r1
.isSubset(r2
));
255 TEST(AddrRangeTest
, isSubsetPartialSubset
)
257 AddrRange
r1(0x20, 0x30);
258 AddrRange
r2(0x26, 0xF0);
260 EXPECT_FALSE(r1
.isSubset(r2
));
261 EXPECT_FALSE(r2
.isSubset(r1
));
264 TEST(AddrRangeTest
, Contains
)
266 AddrRange
r(0xF0, 0xF5);
268 EXPECT_FALSE(r
.contains(0xEF));
269 EXPECT_TRUE(r
.contains(0xF0));
270 EXPECT_TRUE(r
.contains(0xF1));
271 EXPECT_TRUE(r
.contains(0xF2));
272 EXPECT_TRUE(r
.contains(0xF3));
273 EXPECT_TRUE(r
.contains(0xF4));
274 EXPECT_FALSE(r
.contains(0xF5));
275 EXPECT_FALSE(r
.contains(0xF6));
278 TEST(AddrRangeTest
, ContainsInAnEmptyRange
)
280 AddrRange
r(0x1, 0x1);
282 EXPECT_FALSE(r
.contains(0x1));
285 TEST(AddrRangeTest
, RemoveIntlvBits
)
287 AddrRange
r(0x01, 0x10);
290 * When there are no masks, AddrRange.removeIntlBits just returns the
294 a
= r
.removeIntlvBits(a
);
298 TEST(AddrRangeTest
, addIntlvBits
)
300 AddrRange
r(0x01, 0x10);
303 * As with AddrRange.removeIntlBits, when there are no masks,
304 * AddrRange.addIntlvBits just returns the address parameter.
307 a
= r
.addIntlvBits(a
);
311 TEST(AddrRangeTest
, OffsetInRange
)
313 AddrRange
r(0x01, 0xF0);
314 EXPECT_EQ(0x04, r
.getOffset(0x5));
317 TEST(AddrRangeTest
, OffsetOutOfRangeAfter
)
320 * If the address is less than the range, MaxAddr is returned.
322 AddrRange
r(0x01, 0xF0);
323 EXPECT_EQ(MaxAddr
, r
.getOffset(0xF0));
326 TEST(AddrRangeTest
, OffsetOutOfRangeBefore
)
328 AddrRange
r(0x05, 0xF0);
329 EXPECT_EQ(MaxAddr
, r
.getOffset(0x04));
333 * The following tests check the behavior of AddrRange when initialized with
334 * a start and end address, as well as masks to distinguish interleaving bits.
336 TEST(AddrRangeTest
, LsbInterleavingMask
)
340 std::vector
<Addr
> masks
;
342 * The address is in range if the LSB is set, i.e. is the value is odd.
345 uint8_t intlv_match
= 1;
347 AddrRange
r(start
, end
, masks
, intlv_match
);
348 EXPECT_TRUE(r
.valid());
349 EXPECT_EQ(start
, r
.start());
350 EXPECT_EQ(end
, r
.end());
352 * With interleaving, it's assumed the size is equal to
353 * start - end >> [number of masks].
355 EXPECT_EQ(0x7F, r
.size());
357 * The Granularity, the size of regions created by the interleaving bits,
358 * which, in this case, is one.
360 EXPECT_EQ(1, r
.granularity());
361 EXPECT_TRUE(r
.interleaved());
362 EXPECT_EQ(ULL(2), r
.stripes());
363 EXPECT_EQ("[0:0xff] a[0]^\b=1", r
.to_string());
366 TEST(AddrRangeTest
, TwoInterleavingMasks
)
370 std::vector
<Addr
> masks
;
372 * There are two marks, the two LSBs.
375 masks
.push_back((1 << 1));
376 uint8_t intlv_match
= (1 << 1) | 1;
378 AddrRange
r(start
, end
, masks
, intlv_match
);
379 EXPECT_TRUE(r
.valid());
380 EXPECT_EQ(start
, r
.start());
381 EXPECT_EQ(end
, r
.end());
383 EXPECT_EQ(0x3FFF, r
.size());
384 EXPECT_TRUE(r
.interleaved());
385 EXPECT_EQ(ULL(4), r
.stripes());
386 EXPECT_EQ("[0:0xffff] a[0]^\b=1 a[1]^\b=1", r
.to_string());
389 TEST(AddrRangeTest
, ComplexInterleavingMasks
)
393 std::vector
<Addr
> masks
;
394 masks
.push_back((1 << 1) | 1);
395 masks
.push_back((ULL(1) << 63) | (ULL(1) << 62));
396 uint8_t intlv_match
= 0;
398 AddrRange
r(start
, end
, masks
, intlv_match
);
399 EXPECT_TRUE(r
.valid());
400 EXPECT_EQ(start
, r
.start());
401 EXPECT_EQ(end
, r
.end());
403 EXPECT_EQ(0x3FFF, r
.size());
404 EXPECT_TRUE(r
.interleaved());
405 EXPECT_EQ(ULL(4), r
.stripes());
406 EXPECT_EQ("[0:0xffff] a[0]^a[1]^\b=0 a[62]^a[63]^\b=0", r
.to_string());
409 TEST(AddrRangeTest
, InterleavingAddressesMergesWith
)
411 Addr start1
= 0x0000;
413 std::vector
<Addr
> masks
;
414 masks
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
415 masks
.push_back((1 << 2));
416 uint8_t intlv_match1
= 0;
417 AddrRange
r1(start1
, end1
, masks
, intlv_match1
);
419 Addr start2
= 0x0000;
421 uint8_t intlv_match2
= 1; // intlv_match may differ.
422 AddrRange
r2(start2
, end2
, masks
, intlv_match2
);
424 EXPECT_TRUE(r1
.mergesWith(r2
));
425 EXPECT_TRUE(r2
.mergesWith(r1
));
428 TEST(AddrRangeTest
, InterleavingAddressesDoNotMergeWith
)
430 Addr start1
= 0x0000;
432 std::vector
<Addr
> masks1
;
433 masks1
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
434 masks1
.push_back((1 << 2));
435 uint8_t intlv_match1
= 0;
436 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
438 Addr start2
= 0x0000;
440 std::vector
<Addr
> masks2
;
441 masks2
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
442 masks2
.push_back((1 << 3)); // Different mask here.
443 uint8_t intlv_match2
= 1; // intlv_match may differ.
444 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
446 EXPECT_FALSE(r1
.mergesWith(r2
));
447 EXPECT_FALSE(r2
.mergesWith(r1
));
450 TEST(AddrRangeTest
, InterleavingAddressesDoNotIntersect
)
453 * Range 1: all the odd addresses between 0x0000 and 0xFFFF.
455 Addr start1
= 0x0000;
457 std::vector
<Addr
> masks1
;
459 uint8_t intlv_match1
= 1;
460 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
463 * Range 2: all the even addresses between 0x0000 and 0xFFFF. These
464 * addresses should thereby not intersect.
466 Addr start2
= 0x0000;
468 std::vector
<Addr
> masks2
;
470 uint8_t intv_match2
= 0;
471 AddrRange
r2(start2
, end2
, masks2
, intv_match2
);
473 EXPECT_FALSE(r1
.intersects(r2
));
474 EXPECT_FALSE(r2
.intersects(r1
));
477 TEST(AddrRangeTest
, InterleavingAddressesIntersectsViaMerging
)
479 Addr start1
= 0x0000;
481 std::vector
<Addr
> masks1
;
482 masks1
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
483 masks1
.push_back((1 << 2));
484 uint8_t intlv_match1
= 0;
485 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
487 Addr start2
= 0x0000;
489 std::vector
<Addr
> masks2
;
490 masks2
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
491 masks2
.push_back((1 << 2));
492 uint8_t intlv_match2
= 0;
493 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
495 EXPECT_TRUE(r1
.intersects(r2
));
496 EXPECT_TRUE(r2
.intersects(r1
));
499 TEST(AddrRangeTest
, InterleavingAddressesDoesNotIntersectViaMerging
)
501 Addr start1
= 0x0000;
503 std::vector
<Addr
> masks1
;
504 masks1
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
505 masks1
.push_back((1 << 2));
506 uint8_t intlv_match1
= 0;
507 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
509 Addr start2
= 0x0000;
511 std::vector
<Addr
> masks2
;
512 masks2
.push_back((1 << 29) | (1 << 20) | (1 << 10) | 1);
513 masks2
.push_back((1 << 2));
515 * These addresses can merge, but their intlv_match values differ. They
516 * therefore do not intersect.
518 uint8_t intlv_match2
= 1;
519 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
521 EXPECT_FALSE(r1
.intersects(r2
));
522 EXPECT_FALSE(r2
.intersects(r1
));
526 * The following tests were created to test more complex cases where
527 * interleaving addresses may intersect. However, the "intersects" function
528 * does not cover all cases (a "Cannot test intersection..." exception will
529 * be thrown outside of very simple checks to see if an intersection occurs).
530 * The tests below accurately test whether two ranges intersect but, for now,
531 * code has yet to be implemented to utilize these tests. They are therefore
532 * disabled, but may be enabled at a later date if/when the "intersects"
533 * function is enhanced.
535 TEST(AddrRangeTest
, DISABLED_InterleavingAddressesIntersect
)
538 * Range 1: all the odd addresses between 0x0000 and 0xFFFF.
540 Addr start1
= 0x0000;
542 std::vector
<Addr
> masks1
;
544 uint8_t intlv_match1
= 0;
545 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
548 * Range 2: all the addresses divisible by 4 between 0x0000 and
549 * 0xFFFF. These addresses should thereby intersect.
551 Addr start2
= 0x0000;
553 std::vector
<Addr
> masks2
;
554 masks2
.push_back(1 << 2);
555 uint8_t intlv_match2
= 1;
556 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
558 EXPECT_TRUE(r1
.intersects(r2
));
559 EXPECT_TRUE(r2
.intersects(r1
));
562 TEST(AddrRangeTest
, DISABLED_InterleavingAddressesIntersectsOnOneByteAddress
)
565 * Range: all the odd addresses between 0x0000 and 0xFFFF.
569 std::vector
<Addr
> masks
;
571 uint8_t intlv_match
= 1;
572 AddrRange
r1(start
, end
, masks
, intlv_match
);
574 AddrRange
r2(0x0000, 0x0001);
576 EXPECT_FALSE(r1
.intersects(r2
));
577 EXPECT_FALSE(r2
.intersects(r1
));
581 DISABLED_InterleavingAddressesDoesNotIntersectOnOneByteAddress
)
584 * Range: all the odd addresses between 0x0000 and 0xFFFF.
588 std::vector
<Addr
> masks
;
590 uint8_t intlv_match
= 1;
591 AddrRange
r1(start
, end
, masks
, intlv_match
);
593 AddrRange
r2(0x0001, 0x0002);
595 EXPECT_TRUE(r1
.intersects(r2
));
596 EXPECT_TRUE(r2
.intersects(r1
));
601 * The following three tests were created to test the addr_range.isSubset
602 * function for Interleaving address ranges. However, for now, this
603 * functionality has not been implemented. These tests are therefore disabled.
605 TEST(AddrRangeTest
, DISABLED_InterleavingAddressIsSubset
)
607 // Range 1: all the even addresses between 0x0000 and 0xFFFF.
608 Addr start1
= 0x0000;
610 std::vector
<Addr
> masks1
;
612 uint8_t intlv_match1
= 0;
613 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
615 // Range 2: all the even addresses between 0xF000 and 0x0FFF, this is
616 // a subset of Range 1.
617 Addr start2
= 0xF000;
619 std::vector
<Addr
> masks2
;
621 uint8_t intlv_match2
= 0;
622 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
624 EXPECT_TRUE(r1
.isSubset(r2
));
625 EXPECT_TRUE(r2
.isSubset(r1
));
628 TEST(AddrRangeTest
, DISABLED_InterleavingAddressIsNotSubset
)
630 //Range 1: all the even addresses between 0x0000 and 0xFFFF.
631 Addr start1
= 0x0000;
633 std::vector
<Addr
> masks1
;
635 uint8_t intlv_match1
= 0;
636 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
639 // Range 2: all the odd addresses between 0xF000 and 0x0FFF, this is
640 //a subset of Range 1.
641 Addr start2
= 0xF000;
643 std::vector
<Addr
> masks2
;
645 uint8_t intlv_match2
= 1;
646 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
648 EXPECT_FALSE(r1
.isSubset(r2
));
649 EXPECT_FALSE(r2
.isSubset(r1
));
652 TEST(AddrRangeTest
, DISABLED_InterleavingAddressContains
)
655 * Range: all the address between 0x0 and 0xFF which have both the 1st
656 * and 5th bits 1, or both are 0
660 std::vector
<Addr
> masks
;
661 masks
.push_back((1 << 4) | 1);
662 uint8_t intlv_match
= 0;
663 AddrRange
r(start
, end
, masks
, intlv_match
);
665 for (Addr addr
= start
; addr
< end
; addr
++) {
666 if (((addr
& 1) && ((1 << 4) & addr
)) || // addr[0] && addr[4]
667 (!(addr
& 1) && !((1 << 4) & addr
))) { //!addr[0] && !addr[4]
668 EXPECT_TRUE(r
.contains(addr
));
670 EXPECT_FALSE(r
.contains(addr
));
675 TEST(AddrRangeTest
, InterleavingAddressAddRemoveInterlvBits
)
677 Addr start
= 0x00000;
679 std::vector
<Addr
> masks
;
681 uint8_t intlv_match
= 1;
682 AddrRange
r(start
, end
, masks
, intlv_match
);
685 Addr output
= r
.removeIntlvBits(input
);
688 * The removeIntlvBits function removes the LSB from each mask from the
689 * input address. For example, two masks:
692 * with an input address of:
695 * we would remove bit at position 0, and at position 2, resulting in:
698 * In this test there is is one mask, with a LSB at position 0.
699 * Therefore, removing the interleaving bits is equivilant to bitshifting
700 * the input to the right.
702 EXPECT_EQ(input
>> 1, output
);
705 * The addIntlvBits function will re-insert bits at the removed locations
707 EXPECT_EQ(input
, r
.addIntlvBits(output
));
710 TEST(AddrRangeTest
, InterleavingAddressAddRemoveInterlvBitsTwoMasks
)
712 Addr start
= 0x00000;
714 std::vector
<Addr
> masks
;
715 masks
.push_back((1 << 3) | (1 << 2) | (1 << 1) | 1);
716 masks
.push_back((1 << 11) | (1 << 10) | (1 << 9) | (1 << 8));
717 uint8_t intlv_match
= 1;
718 AddrRange
r(start
, end
, masks
, intlv_match
);
720 Addr input
= (1 << 9) | (1 << 8) | 1;
722 * (1 << 8) and 1 are interleaving bits to be removed.
724 Addr output
= r
.removeIntlvBits(input
);
727 * The bit, formally at position 9, is now at 7.
729 EXPECT_EQ((1 << 7), output
);
732 * Re-adding the interleaving.
734 EXPECT_EQ(input
, r
.addIntlvBits(output
));
737 TEST(AddrRangeTest
, AddRemoveInterleavBitsAcrossRange
)
740 * This purpose of this test is to ensure that removing then adding
741 * interleaving bits has no net effect.
743 * addr_range.addIntlvBits(add_range.removeIntlvBits(an_address)) should
744 * always return an_address.
746 Addr start
= 0x00000;
748 std::vector
<Addr
> masks
;
749 masks
.push_back(1 << 2);
750 masks
.push_back(1 << 3);
751 masks
.push_back(1 << 16);
752 masks
.push_back(1 << 30);
753 uint8_t intlv_match
= 0xF;
754 AddrRange
r(start
, end
, masks
, intlv_match
);
756 for (Addr i
= 0; i
< 0xFFF; i
++) {
757 Addr removedBits
= r
.removeIntlvBits(i
);
759 * As intlv_match = 0xF, all the interleaved bits should be set.
761 EXPECT_EQ(i
| (1 << 2) | (1 << 3) | (1 << 16) | (1 << 30),
762 r
.addIntlvBits(removedBits
));
766 TEST(AddrRangeTest
, InterleavingAddressesGetOffset
)
770 std::vector
<Addr
> masks
;
771 masks
.push_back((1 << 4) | (1 << 2));
772 uint8_t intlv_match
= 0;
773 AddrRange
r(start
, end
, masks
, intlv_match
);
775 Addr value
= ((1 << 10) | (1 << 9) | (1 << 8) | (1 << 2) | (1 << 1) | 1);
776 Addr value_interleaving_bits_removed
=
777 ((1 << 9) | (1 << 8) | (1 << 7) | (1 << 1) | 1);
779 Addr expected_output
= value_interleaving_bits_removed
- start
;
781 EXPECT_EQ(expected_output
, r
.getOffset(value
));
784 TEST(AddrRangeTest
, InterleavingLessThanStartEquals
)
786 Addr start1
= 0x0000FFFF;
787 Addr end1
= 0xFFFF0000;
788 std::vector
<Addr
> masks1
;
789 masks1
.push_back((1 << 4) | (1 << 2));
790 uint8_t intlv_match1
= 0;
791 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
793 Addr start2
= 0x0000FFFF;
794 Addr end2
= 0x000F0000;
795 std::vector
<Addr
> masks2
;
796 masks2
.push_back((1 << 4) | (1 << 2));
797 masks2
.push_back((1 << 10));
798 uint8_t intlv_match2
= 2;
799 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
802 * When The start addresses are equal, the intlv_match values are
805 EXPECT_TRUE(r1
< r2
);
806 EXPECT_FALSE(r2
< r1
);
809 TEST(AddrRangeTest
, InterleavingLessThanStartNotEquals
)
811 Addr start1
= 0x0000FFFF;
812 Addr end1
= 0xFFFF0000;
813 std::vector
<Addr
> masks1
;
814 masks1
.push_back((1 << 4) | (1 << 2));
815 uint8_t intlv_match1
= 0;
816 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
818 Addr start2
= 0x0000FFFE;
819 Addr end2
= 0x000F0000;
820 std::vector
<Addr
> masks2
;
821 masks2
.push_back((1 << 4) | (1 << 2));
822 masks2
.push_back((1 << 10));
823 uint8_t intlv_match2
= 2;
824 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
826 EXPECT_TRUE(r2
< r1
);
827 EXPECT_FALSE(r1
< r2
);
830 TEST(AddrRangeTest
, InterleavingEqualTo
)
832 Addr start1
= 0x0000FFFF;
833 Addr end1
= 0xFFFF0000;
834 std::vector
<Addr
> masks1
;
835 masks1
.push_back((1 << 4) | (1 << 2));
836 uint8_t intlv_match1
= 0;
837 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
839 Addr start2
= 0x0000FFFF;
840 Addr end2
= 0xFFFF0000;
841 std::vector
<Addr
> masks2
;
842 masks2
.push_back((1 << 4) | (1 << 2));
843 uint8_t intlv_match2
= 0;
844 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
846 EXPECT_TRUE(r1
== r2
);
849 TEST(AddrRangeTest
, InterleavingNotEqualTo
)
851 Addr start1
= 0x0000FFFF;
852 Addr end1
= 0xFFFF0000;
853 std::vector
<Addr
> masks1
;
854 masks1
.push_back((1 << 4) | (1 << 2));
855 uint8_t intlv_match1
= 0;
856 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
858 Addr start2
= 0x0000FFFF;
859 Addr end2
= 0xFFFF0000;
860 std::vector
<Addr
> masks2
;
861 masks2
.push_back((1 << 4) | (1 << 2));
862 masks2
.push_back((1 << 10));
863 uint8_t intlv_match2
= 2;
864 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
867 * These ranges are not equal due to having different masks.
869 EXPECT_FALSE(r1
== r2
);
873 * The AddrRange(std::vector<AddrRange>) constructor "merges" the interleaving
874 * address ranges. It should be noted that this constructor simply checks that
875 * these interleaving addresses can be merged then creates a new address from
876 * the start and end addresses of the first address range in the vector.
878 TEST(AddrRangeTest
, MergingInterleavingAddressRanges
)
880 Addr start1
= 0x0000;
882 std::vector
<Addr
> masks1
;
883 masks1
.push_back((1 << 4) | (1 << 2));
884 uint8_t intlv_match1
= 0;
885 AddrRange
r1(start1
, end1
, masks1
, intlv_match1
);
887 Addr start2
= 0x0000;
889 std::vector
<Addr
> masks2
;
890 masks2
.push_back((1 << 4) | (1 << 2));
891 uint8_t intlv_match2
= 1;
892 AddrRange
r2(start2
, end2
, masks2
, intlv_match2
);
894 std::vector
<AddrRange
> to_merge
;
895 to_merge
.push_back(r1
);
896 to_merge
.push_back(r2
);
898 AddrRange
output(to_merge
);
900 EXPECT_EQ(0x0000, output
.start());
901 EXPECT_EQ(0xFFFF, output
.end());
902 EXPECT_FALSE(output
.interleaved());
905 TEST(AddrRangeTest
, MergingInterleavingAddressRangesOneRange
)
908 * In the case where there is just one range in the vector, the merged
909 * address range is equal to that range.
913 std::vector
<Addr
> masks
;
914 masks
.push_back((1 << 4) | (1 << 2));
915 uint8_t intlv_match
= 0;
916 AddrRange
r(start
, end
, masks
, intlv_match
);
918 std::vector
<AddrRange
> to_merge
;
919 to_merge
.push_back(r
);
921 AddrRange
output(to_merge
);
923 EXPECT_EQ(r
, output
);
927 * The following tests verify the soundness of the "legacy constructor",
928 * AddrRange(Addr, Addr, uint8_t, uint8_t, uint8_t, uint8_t).
930 * The address is assumed to contain two ranges; the interleaving bits, and
931 * the xor bits. The first two arguments of this constructor specify the
932 * start and end addresses. The third argument specifies the MSB of the
933 * interleaving bits. The fourth argument specifies the MSB of the xor bits.
934 * The firth argument specifies the size (in bits) of the xor and interleaving
935 * bits. These cannot overlap. The sixth argument specifies the value the
936 * XORing of the xor and interleaving bits should equal to be considered in
939 * This constructor does a lot of complex translation to migrate this
940 * constructor to the masks/intlv_match format.
942 TEST(AddrRangeTest
, LegacyConstructorNoInterleaving
)
945 * This constructor should create a range with no interleaving.
947 AddrRange
range(0x0000, 0xFFFF, 0, 0, 0 ,0);
948 AddrRange
expected(0x0000, 0xFFFF);
950 EXPECT_EQ(expected
, range
);
953 TEST(AddrRangeTest
, LegacyConstructorOneBitMask
)
956 * In this test, the LSB of the address determines whether an address is
957 * in range. If even, it's in range, if not, it's out of range. the XOR
958 * bit range is not used.
960 AddrRange
range(0x00000000, 0xFFFFFFFF, 0, 0, 1, 0);
962 std::vector
<Addr
> masks
;
964 AddrRange
expected(0x00000000, 0xFFFFFFFF, masks
, 0);
966 EXPECT_TRUE(expected
== range
);
969 TEST(AddrRangeTest
, LegacyConstructorTwoBitMask
)
972 * In this test, the two LSBs of the address determines whether an address
973 * is in range. If the two are set, the address is in range. The XOR bit
976 AddrRange
range(0x00000000, 0xFFFFFFFF, 1, 0, 2, 3);
978 std::vector
<Addr
> masks
;
980 masks
.push_back((1 << 1));
981 AddrRange
expected(0x00000000, 0xFFFFFFFF, masks
, 3);
983 EXPECT_TRUE(expected
== range
);
986 TEST(AddrRangeTest
, LegacyConstructorTwoBitMaskWithXOR
)
989 * In this test, the two LSBs of the address determine wether an address
990 * is in range. They are XORed to the 10th and 11th bits in the address.
991 * If XORed value is equal to 3, then the address is in range.
994 AddrRange
range(0x00000000, 0xFFFFFFFF, 1, 11, 2, 3);
997 * The easiest way to ensure this range is correct is to iterate throguh
998 * the address range and ensure the correct set of addresses are contained
1001 * We start with the xor_mask: a mask to select the 10th and 11th bits.
1003 Addr xor_mask
= (1 << 11) | (1 << 10);
1004 for (Addr i
= 0; i
< 0x0000FFFF; i
++) {
1006 Addr xor_value
= (xor_mask
& i
) >> 10;
1007 /* If the XOR of xor_bits and the intlv bits (the 0th and 1st bits) is
1008 * equal to intlv_match (3, i.e., the 0th and 1st bit is set),then the
1009 * address is within range.
1011 if (((xor_value
^ i
) & 3) == 3) {
1012 EXPECT_TRUE(range
.contains(i
));
1014 EXPECT_FALSE(range
.contains(i
));
1020 * addr_range.hh contains some convenience constructors. The following tests
1021 * verify they construct AddrRange correctly.
1023 TEST(AddrRangeTest
, RangeExConstruction
)
1025 AddrRange r
= RangeEx(0x6, 0xE);
1026 EXPECT_EQ(0x6, r
.start());
1027 EXPECT_EQ(0xE, r
.end());
1030 TEST(AddrRangeTest
, RangeInConstruction
)
1032 AddrRange r
= RangeIn(0x6, 0xE);
1033 EXPECT_EQ(0x6, r
.start());
1034 EXPECT_EQ(0xF, r
.end());
1037 TEST(AddrRangeTest
, RangeSizeConstruction
){
1038 AddrRange r
= RangeSize(0x5, 5);
1039 EXPECT_EQ(0x5, r
.start());
1040 EXPECT_EQ(0xA, r
.end());