| 1 | //===-- Unittests for Array -----------------------------------------------===// |
| 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/array.h" |
| 10 | #include "test/UnitTest/Test.h" |
| 11 | |
| 12 | using LIBC_NAMESPACE::cpp::array; |
| 13 | |
| 14 | TEST(LlvmLibcArrayTest, Basic) { |
| 15 | array<int, 3> a = {0, 1, 2}; |
| 16 | |
| 17 | ASSERT_EQ(a.data(), &a.front()); |
| 18 | ASSERT_EQ(a.front(), 0); |
| 19 | ASSERT_EQ(a.back(), 2); |
| 20 | ASSERT_EQ(a.size(), size_t{3}); |
| 21 | ASSERT_EQ(a[1], 1); |
| 22 | ASSERT_FALSE(a.empty()); |
| 23 | ASSERT_NE(a.begin(), a.end()); |
| 24 | ASSERT_EQ(*a.begin(), a.front()); |
| 25 | |
| 26 | auto it = a.rbegin(); |
| 27 | ASSERT_EQ(*it, 2); |
| 28 | ASSERT_EQ(*(++it), 1); |
| 29 | ASSERT_EQ(*(++it), 0); |
| 30 | |
| 31 | for (int &x : a) |
| 32 | ASSERT_GE(x, 0); |
| 33 | } |
| 34 | |
| 35 | // Test const_iterator and const variant methods. |
| 36 | TEST(LlvmLibcArrayTest, Const) { |
| 37 | const array<int, 3> z = {3, 4, 5}; |
| 38 | |
| 39 | ASSERT_EQ(3, z.front()); |
| 40 | ASSERT_EQ(4, z[1]); |
| 41 | ASSERT_EQ(5, z.back()); |
| 42 | ASSERT_EQ(3, *z.data()); |
| 43 | |
| 44 | // begin, cbegin, end, cend |
| 45 | array<int, 3>::const_iterator it2 = z.begin(); |
| 46 | ASSERT_EQ(*it2, z.front()); |
| 47 | it2 = z.cbegin(); |
| 48 | ASSERT_EQ(*it2, z.front()); |
| 49 | it2 = z.end(); |
| 50 | ASSERT_NE(it2, z.begin()); |
| 51 | it2 = z.cend(); |
| 52 | ASSERT_NE(it2, z.begin()); |
| 53 | |
| 54 | // rbegin, crbegin, rend, crend |
| 55 | array<int, 3>::const_reverse_iterator it = z.rbegin(); |
| 56 | ASSERT_EQ(*it, z.back()); |
| 57 | it = z.crbegin(); |
| 58 | ASSERT_EQ(*it, z.back()); |
| 59 | it = z.rend(); |
| 60 | ASSERT_EQ(*--it, z.front()); |
| 61 | it = z.crend(); |
| 62 | ASSERT_EQ(*--it, z.front()); |
| 63 | |
| 64 | for (const int &x : z) |
| 65 | ASSERT_GE(x, 0); |
| 66 | } |
| 67 | |