1 //===-- Unittests for BlockStore ------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/blockstore.h"
10 #include "test/UnitTest/Test.h"
18 class LlvmLibcBlockStoreTest
: public LIBC_NAMESPACE::testing::Test
{
20 template <size_t BLOCK_SIZE
, size_t ELEMENT_COUNT
, bool REVERSE
>
21 void populate_and_iterate() {
22 LIBC_NAMESPACE::BlockStore
<Element
, BLOCK_SIZE
, REVERSE
> block_store
;
23 for (int i
= 0; i
< int(ELEMENT_COUNT
); ++i
)
24 ASSERT_TRUE(block_store
.push_back({i
, 2 * i
, 3 * unsigned(i
)}));
25 auto end
= block_store
.end();
27 for (auto iter
= block_store
.begin(); iter
!= end
; ++iter
, ++i
) {
30 int j
= ELEMENT_COUNT
- 1 - i
;
32 ASSERT_EQ(e
.b
, long(j
* 2));
33 ASSERT_EQ(e
.c
, unsigned(j
* 3));
36 ASSERT_EQ(e
.b
, long(i
* 2));
37 ASSERT_EQ(e
.c
, unsigned(i
* 3));
40 ASSERT_EQ(i
, int(ELEMENT_COUNT
));
41 LIBC_NAMESPACE::BlockStore
<Element
, BLOCK_SIZE
, REVERSE
>::destroy(
45 template <bool REVERSE
> void back_test() {
46 using LIBC_NAMESPACE::BlockStore
;
47 BlockStore
<int, 4, REVERSE
> block_store
;
48 for (int i
= 0; i
< 20; i
++)
49 ASSERT_TRUE(block_store
.push_back(i
));
50 for (int i
= 19; i
>= 0; i
--, block_store
.pop_back())
51 ASSERT_EQ(block_store
.back(), i
);
52 block_store
.destroy(&block_store
);
55 template <bool REVERSE
> void empty_test() {
56 using LIBC_NAMESPACE::BlockStore
;
57 BlockStore
<int, 2, REVERSE
> block_store
;
59 ASSERT_TRUE(block_store
.empty());
60 ASSERT_TRUE(block_store
.push_back(1));
61 for (int i
= 0; i
< 10; i
++) {
62 ASSERT_FALSE(block_store
.empty());
63 ASSERT_TRUE(block_store
.push_back(1));
65 block_store
.destroy(&block_store
);
68 template <bool REVERSE
> void erase_test() {
69 using LIBC_NAMESPACE::BlockStore
;
70 BlockStore
<int, 2, REVERSE
> block_store
;
73 constexpr int ARR_SIZE
= 6;
75 ASSERT_TRUE(block_store
.empty());
76 for (int i
= 0; i
< ARR_SIZE
; i
++) {
77 ASSERT_TRUE(block_store
.push_back(i
+ 1));
80 // block_store state should be {1,2,3,4,5,6}
82 block_store
.erase(block_store
.begin());
84 // FORWARD: block_store state should be {2,3,4,5,6}
85 // REVERSE: block_store state should be {1,2,3,4,5}
87 auto iter
= block_store
.begin();
88 for (i
= 0; iter
!= block_store
.end(); ++i
, ++iter
) {
90 ASSERT_EQ(*iter
, i
+ 2);
92 ASSERT_EQ(*iter
, (ARR_SIZE
- 1) - i
);
96 // Assert that there were the correct number of elements
97 ASSERT_EQ(i
, ARR_SIZE
- 1);
99 block_store
.erase(block_store
.end());
101 // BOTH: block_store state should be {2,3,4,5}
103 iter
= block_store
.begin();
104 for (i
= 0; iter
!= block_store
.end(); ++i
, ++iter
) {
106 ASSERT_EQ(*iter
, i
+ 2);
108 ASSERT_EQ(*iter
, (ARR_SIZE
- 1) - i
);
112 ASSERT_EQ(i
, ARR_SIZE
- 2);
114 block_store
.erase(block_store
.begin() + 1);
116 // FORWARD: block_store state should be {2,4,5}
117 // REVERSE: block_store state should be {2,3,5}
119 const int FORWARD_RESULTS
[] = {2, 4, 5};
120 const int REVERSE_RESULTS
[] = {2, 3, 5};
122 iter
= block_store
.begin();
123 for (i
= 0; iter
!= block_store
.end(); ++i
, ++iter
) {
125 ASSERT_EQ(*iter
, FORWARD_RESULTS
[i
]);
127 ASSERT_EQ(*iter
, REVERSE_RESULTS
[ARR_SIZE
- 4 - i
]); // reversed
131 ASSERT_EQ(i
, ARR_SIZE
- 3);
133 block_store
.erase(block_store
.begin() + 1);
134 // BOTH: block_store state should be {2,5}
136 iter
= block_store
.begin();
139 ASSERT_EQ(*(iter
+ 1), 5);
142 ASSERT_EQ(*(iter
+ 1), 2);
145 block_store
.erase(block_store
.begin());
146 // FORWARD: block_store state should be {5}
147 // REVERSE: block_store state should be {2}
148 iter
= block_store
.begin();
155 block_store
.erase(block_store
.begin());
156 // BOTH: block_store state should be {}
158 block_store
.destroy(&block_store
);
162 TEST_F(LlvmLibcBlockStoreTest
, PopulateAndIterate4
) {
163 populate_and_iterate
<4, 4, false>();
166 TEST_F(LlvmLibcBlockStoreTest
, PopulateAndIterate8
) {
167 populate_and_iterate
<4, 8, false>();
170 TEST_F(LlvmLibcBlockStoreTest
, PopulateAndIterate10
) {
171 populate_and_iterate
<4, 10, false>();
174 TEST_F(LlvmLibcBlockStoreTest
, PopulateAndIterateReverse4
) {
175 populate_and_iterate
<4, 4, true>();
178 TEST_F(LlvmLibcBlockStoreTest
, PopulateAndIterateReverse8
) {
179 populate_and_iterate
<4, 8, true>();
182 TEST_F(LlvmLibcBlockStoreTest
, PopulateAndIterateReverse10
) {
183 populate_and_iterate
<4, 10, true>();
186 TEST_F(LlvmLibcBlockStoreTest
, Back
) {
191 TEST_F(LlvmLibcBlockStoreTest
, Empty
) {
196 TEST_F(LlvmLibcBlockStoreTest
, Erase
) {