1 //===-- Unittests for span ------------------------------------------------===//
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/CPP/span.h"
11 #include "test/UnitTest/Test.h"
13 using LIBC_NAMESPACE::cpp::array
;
14 using LIBC_NAMESPACE::cpp::span
;
16 TEST(LlvmLibcSpanTest
, InitializeEmpty
) {
18 ASSERT_EQ(s
.size(), size_t(0));
19 ASSERT_TRUE(s
.empty());
20 ASSERT_TRUE(s
.data() == nullptr);
23 TEST(LlvmLibcSpanTest
, InitializeSingleton
) {
26 ASSERT_EQ(s
.size(), size_t(1));
27 ASSERT_FALSE(s
.empty());
28 ASSERT_TRUE(s
.data() == &a
);
29 ASSERT_EQ(s
.front(), 42);
30 ASSERT_EQ(s
.back(), 42);
33 TEST(LlvmLibcSpanTest
, InitializeCArray
) {
36 ASSERT_EQ(s
.size(), size_t(3));
37 ASSERT_FALSE(s
.empty());
38 ASSERT_TRUE(s
.data() == &a
[0]);
39 ASSERT_EQ(s
.front(), 1);
40 ASSERT_EQ(s
.back(), 3);
46 TEST(LlvmLibcSpanTest
, InitializeArray
) {
47 array
<int, 3> a
= {1, 2, 3};
49 ASSERT_EQ(s
.size(), size_t(3));
50 ASSERT_FALSE(s
.empty());
51 ASSERT_TRUE(s
.data() == &a
[0]);
52 ASSERT_EQ(s
.front(), 1);
53 ASSERT_EQ(s
.back(), 3);
59 TEST(LlvmLibcSpanTest
, InitializeViewFormMutableSingleton
) {
61 span
<const int> s(&a
, 1);
62 ASSERT_EQ(s
.size(), size_t(1));
63 ASSERT_TRUE(s
.data() == &a
);
66 TEST(LlvmLibcSpanTest
, InitializeViewFormMutableCArray
) {
69 ASSERT_EQ(s
.size(), size_t(3));
75 TEST(LlvmLibcSpanTest
, InitializeViewFormMutableArray
) {
76 array
<int, 3> a
= {1, 2, 3};
78 ASSERT_EQ(s
.size(), size_t(3));
84 TEST(LlvmLibcSpanTest
, InitializeFromMutable
) {
86 span
<const int> view(s
);
90 TEST(LlvmLibcSpanTest
, Assign
) {
96 TEST(LlvmLibcSpanTest
, AssignFromMutable
) {
102 TEST(LlvmLibcSpanTest
, Modify
) {
107 ASSERT_EQ(s
.size(), size_t(3));
113 TEST(LlvmLibcSpanTest
, SubSpan
) {
115 span
<const int> s(a
);
117 const auto _
= s
.subspan(0);
118 ASSERT_EQ(_
.size(), size_t(3));
124 const auto _
= s
.subspan(2);
125 ASSERT_EQ(_
.size(), size_t(1));
129 const auto _
= s
.subspan(3);
130 ASSERT_EQ(_
.size(), size_t(0));
133 const auto _
= s
.subspan(0, 1);
134 ASSERT_EQ(_
.size(), size_t(1));
139 TEST(LlvmLibcSpanTest
, FirstAndLastSubSpan
) {
141 span
<const int> s(a
);
143 const auto first
= s
.first(2);
144 ASSERT_EQ(first
.size(), size_t(2));
145 ASSERT_EQ(first
[0], 1);
146 ASSERT_EQ(first
[1], 2);
148 const auto last
= s
.last(2);
149 ASSERT_EQ(last
.size(), size_t(2));
150 ASSERT_EQ(last
[0], 2);
151 ASSERT_EQ(last
[1], 3);