1 //===-- Unittests for FixedVector -----------------------------------------===//
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/CPP/array.h"
10 #include "src/__support/fixedvector.h"
11 #include "test/UnitTest/Test.h"
13 TEST(LlvmLibcFixedVectorTest
, PushAndPop
) {
14 LIBC_NAMESPACE::FixedVector
<int, 20> fixed_vector
;
15 ASSERT_TRUE(fixed_vector
.empty());
16 for (int i
= 0; i
< 20; i
++)
17 ASSERT_TRUE(fixed_vector
.push_back(i
));
18 ASSERT_FALSE(fixed_vector
.empty());
19 ASSERT_FALSE(fixed_vector
.push_back(123));
20 for (int i
= 20; i
> 0; --i
) {
21 ASSERT_EQ(fixed_vector
.back(), i
- 1);
22 ASSERT_TRUE(fixed_vector
.pop_back());
24 ASSERT_FALSE(fixed_vector
.pop_back());
25 ASSERT_TRUE(fixed_vector
.empty());
28 TEST(LlvmLibcFixedVectorTest
, Reset
) {
29 LIBC_NAMESPACE::FixedVector
<int, 20> fixed_vector
;
30 ASSERT_TRUE(fixed_vector
.empty());
31 for (int i
= 0; i
< 20; i
++)
32 ASSERT_TRUE(fixed_vector
.push_back(i
));
33 ASSERT_FALSE(fixed_vector
.empty());
35 ASSERT_TRUE(fixed_vector
.empty());
38 TEST(LlvmLibcFixedVectorTest
, Destroy
) {
39 LIBC_NAMESPACE::FixedVector
<int, 20> fixed_vector
;
40 ASSERT_TRUE(fixed_vector
.empty());
41 for (int i
= 0; i
< 20; i
++)
42 ASSERT_TRUE(fixed_vector
.push_back(i
));
43 ASSERT_FALSE(fixed_vector
.empty());
44 LIBC_NAMESPACE::FixedVector
<int, 20>::destroy(&fixed_vector
);
45 ASSERT_TRUE(fixed_vector
.empty());
48 TEST(LlvmLibcFixedVectorTest
, Iteration
) {
49 LIBC_NAMESPACE::FixedVector
<int, 20> v
;
50 for (int i
= 0; i
< 3; i
++)
56 // TODO: need an overload of Test::test for iterators?
57 // ASSERT_EQ(++it, v.rend());
58 // ASSERT_EQ(v.rbegin(), v.rbegin());
59 ASSERT_TRUE(++it
== v
.rend());
60 for (auto it
= v
.rbegin(), e
= v
.rend(); it
!= e
; ++it
)
63 auto forward_it
= v
.begin();
64 ASSERT_EQ(*forward_it
, 0);
65 ASSERT_EQ(*++forward_it
, 1);
66 ASSERT_EQ(*++forward_it
, 2);
67 ASSERT_TRUE(++forward_it
== v
.end());
68 for (auto it
= v
.begin(), e
= v
.end(); it
!= e
; ++it
)
74 TEST(LlvmLibcFixedVectorTest
, ConstructionFromIterators
) {
75 LIBC_NAMESPACE::cpp::array
<int, 4> arr
{1, 2, 3, 4};
76 LIBC_NAMESPACE::FixedVector
<int, 5> vec(arr
.begin(), arr
.end());
77 ASSERT_EQ(vec
.size(), arr
.size());
78 for (size_t i
= 0; i
< arr
.size(); ++i
)
79 ASSERT_EQ(vec
[i
], arr
[i
]);
82 TEST(LlvmLibcFixedVectorTest
, ConstructionFromCountAndValue
) {
83 constexpr int kVal
= 10;
84 LIBC_NAMESPACE::FixedVector
<int, 5> vec(4, kVal
);
85 ASSERT_EQ(vec
.size(), size_t(4));
86 for (size_t i
= 0; i
< vec
.size(); ++i
)
87 ASSERT_EQ(vec
[i
], kVal
);
90 TEST(LlvmLibcFixedVectorTest
, ForwardIteration
) {
91 LIBC_NAMESPACE::cpp::array
<int, 4> arr
{1, 2, 3, 4};
92 LIBC_NAMESPACE::FixedVector
<int, 5> vec(arr
.begin(), arr
.end());
93 ASSERT_EQ(vec
.size(), arr
.size());
94 for (auto it
= vec
.begin(); it
!= vec
.end(); ++it
) {
95 auto idx
= it
- vec
.begin();
96 ASSERT_EQ(*it
, arr
[idx
]);
100 TEST(LlvmLibcFixedVectorTest
, ConstForwardIteration
) {
101 const LIBC_NAMESPACE::cpp::array
<int, 4> arr
{1, 2, 3, 4};
102 const LIBC_NAMESPACE::FixedVector
<int, 5> vec(arr
.begin(), arr
.end());
103 ASSERT_EQ(vec
.size(), arr
.size());
104 for (auto it
= vec
.begin(); it
!= vec
.end(); ++it
) {
105 auto idx
= it
- vec
.begin();
106 ASSERT_EQ(*it
, arr
[idx
]);