1//===-- Unittests for FixedVector -----------------------------------------===//
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/fixedvector.h"
10#include "test/UnitTest/Test.h"
11
12TEST(LlvmLibcFixedVectorTest, PushAndPop) {
13 LIBC_NAMESPACE::FixedVector<int, 20> fixed_vector;
14 ASSERT_TRUE(fixed_vector.empty());
15 for (int i = 0; i < 20; i++)
16 ASSERT_TRUE(fixed_vector.push_back(i));
17 ASSERT_FALSE(fixed_vector.empty());
18 ASSERT_FALSE(fixed_vector.push_back(123));
19 for (int i = 20; i > 0; --i) {
20 ASSERT_EQ(fixed_vector.back(), i - 1);
21 ASSERT_TRUE(fixed_vector.pop_back());
22 }
23 ASSERT_FALSE(fixed_vector.pop_back());
24 ASSERT_TRUE(fixed_vector.empty());
25}
26
27TEST(LlvmLibcFixedVectorTest, Reset) {
28 LIBC_NAMESPACE::FixedVector<int, 20> fixed_vector;
29 ASSERT_TRUE(fixed_vector.empty());
30 for (int i = 0; i < 20; i++)
31 ASSERT_TRUE(fixed_vector.push_back(i));
32 ASSERT_FALSE(fixed_vector.empty());
33 fixed_vector.reset();
34 ASSERT_TRUE(fixed_vector.empty());
35}
36
37TEST(LlvmLibcFixedVectorTest, Destroy) {
38 LIBC_NAMESPACE::FixedVector<int, 20> fixed_vector;
39 ASSERT_TRUE(fixed_vector.empty());
40 for (int i = 0; i < 20; i++)
41 ASSERT_TRUE(fixed_vector.push_back(i));
42 ASSERT_FALSE(fixed_vector.empty());
43 LIBC_NAMESPACE::FixedVector<int, 20>::destroy(store: &fixed_vector);
44 ASSERT_TRUE(fixed_vector.empty());
45}
46
47TEST(LlvmLibcFixedVectorTest, Iteration) {
48 LIBC_NAMESPACE::FixedVector<int, 20> v;
49 for (int i = 0; i < 3; i++)
50 v.push_back(obj: i);
51 auto it = v.rbegin();
52 ASSERT_EQ(*it, 2);
53 ASSERT_EQ(*++it, 1);
54 ASSERT_EQ(*++it, 0);
55 // TODO: need an overload of Test::test for iterators?
56 // ASSERT_EQ(++it, v.rend());
57 // ASSERT_EQ(v.rbegin(), v.rbegin());
58 ASSERT_TRUE(++it == v.rend());
59 for (auto it = v.rbegin(), e = v.rend(); it != e; ++it)
60 ASSERT_GT(*it, -1);
61}
62

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