[OptTable] Fix typo VALUE => VALUES (NFCI) (#121523)
[llvm-project.git] / libc / test / src / __support / blockstore_test.cpp
blobde7bd72b96c80b381cf9c60592e89eacf215312d
1 //===-- Unittests for BlockStore ------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/blockstore.h"
10 #include "test/UnitTest/Test.h"
12 struct Element {
13 int a;
14 long b;
15 unsigned c;
18 class LlvmLibcBlockStoreTest : public LIBC_NAMESPACE::testing::Test {
19 public:
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();
26 int i = 0;
27 for (auto iter = block_store.begin(); iter != end; ++iter, ++i) {
28 Element &e = *iter;
29 if (REVERSE) {
30 int j = ELEMENT_COUNT - 1 - i;
31 ASSERT_EQ(e.a, j);
32 ASSERT_EQ(e.b, long(j * 2));
33 ASSERT_EQ(e.c, unsigned(j * 3));
34 } else {
35 ASSERT_EQ(e.a, i);
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(
42 &block_store);
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;
71 int i;
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) {
89 if (!REVERSE) {
90 ASSERT_EQ(*iter, i + 2);
91 } else {
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) {
105 if (!REVERSE) {
106 ASSERT_EQ(*iter, i + 2);
107 } else {
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) {
124 if (!REVERSE) {
125 ASSERT_EQ(*iter, FORWARD_RESULTS[i]);
126 } else {
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();
137 if (!REVERSE) {
138 ASSERT_EQ(*iter, 2);
139 ASSERT_EQ(*(iter + 1), 5);
140 } else {
141 ASSERT_EQ(*iter, 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();
149 if (!REVERSE) {
150 ASSERT_EQ(*iter, 5);
151 } else {
152 ASSERT_EQ(*iter, 2);
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) {
187 back_test<false>();
188 back_test<true>();
191 TEST_F(LlvmLibcBlockStoreTest, Empty) {
192 empty_test<false>();
193 empty_test<true>();
196 TEST_F(LlvmLibcBlockStoreTest, Erase) {
197 erase_test<false>();
198 erase_test<true>();