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/CPP/blockstore.h" 10 #include "utils/UnitTest/Test.h" 11 12 struct Element { 13 int a; 14 long b; 15 unsigned c; 16 }; 17 18 class LlvmLibcBlockStoreTest : public __llvm_libc::testing::Test { 19 public: 20 template <size_t BLOCK_SIZE, size_t ELEMENT_COUNT, bool REVERSE> 21 void populate_and_iterate() { 22 __llvm_libc::cpp::BlockStore<Element, BLOCK_SIZE, REVERSE> block_store; 23 for (int i = 0; i < int(ELEMENT_COUNT); ++i) 24 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 __llvm_libc::cpp::BlockStore<Element, BLOCK_SIZE, REVERSE>::destroy( 42 &block_store); 43 } 44 }; 45 46 TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterate4) { 47 populate_and_iterate<4, 4, false>(); 48 } 49 50 TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterate8) { 51 populate_and_iterate<4, 8, false>(); 52 } 53 54 TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterate10) { 55 populate_and_iterate<4, 10, false>(); 56 } 57 58 TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterateReverse4) { 59 populate_and_iterate<4, 4, true>(); 60 } 61 62 TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterateReverse8) { 63 populate_and_iterate<4, 8, true>(); 64 } 65 66 TEST_F(LlvmLibcBlockStoreTest, PopulateAndIterateReverse10) { 67 populate_and_iterate<4, 10, true>(); 68 } 69