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//===----------------------------------------------------------------------===//
8
9#include "src/__support/blockstore.h"
10#include "test/UnitTest/Test.h"
11
12struct Element {
13 int a;
14 long b;
15 unsigned c;
16};
17
18class LlvmLibcBlockStoreTest : public LIBC_NAMESPACE::testing::Test {
19public:
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));
38 }
39 }
40 ASSERT_EQ(i, int(ELEMENT_COUNT));
41 LIBC_NAMESPACE::BlockStore<Element, BLOCK_SIZE, REVERSE>::destroy(
42 &block_store);
43 }
44
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);
53 }
54
55 template <bool REVERSE> void empty_test() {
56 using LIBC_NAMESPACE::BlockStore;
57 BlockStore<int, 2, REVERSE> block_store;
58
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));
64 }
65 block_store.destroy(&block_store);
66 }
67};
68
69TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterate4) {
70 populate_and_iterate<4, 4, false>();
71}
72
73TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterate8) {
74 populate_and_iterate<4, 8, false>();
75}
76
77TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterate10) {
78 populate_and_iterate<4, 10, false>();
79}
80
81TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterateReverse4) {
82 populate_and_iterate<4, 4, true>();
83}
84
85TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterateReverse8) {
86 populate_and_iterate<4, 8, true>();
87}
88
89TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterateReverse10) {
90 populate_and_iterate<4, 10, true>();
91}
92
93TEST_F(LlvmLibcBlockStoreTest, Back) { back_test<false>(); }
94
95// FIXME: Combing this test with the above test makes the AMDGPU backend
96// generate code which hangs. This should be fixed in the clang compiler.
97TEST_F(LlvmLibcBlockStoreTest, BackReverse) { back_test<true>(); }
98
99TEST_F(LlvmLibcBlockStoreTest, Empty) {
100 empty_test<false>();
101 empty_test<true>();
102}
103

source code of libc/test/src/__support/blockstore_test.cpp