1//===-- Unittests for span ------------------------------------------------===//
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 "src/__support/CPP/span.h"
11#include "test/UnitTest/Test.h"
12
13using LIBC_NAMESPACE::cpp::array;
14using LIBC_NAMESPACE::cpp::span;
15
16TEST(LlvmLibcSpanTest, InitializeEmpty) {
17 span<int> s;
18 ASSERT_EQ(s.size(), size_t(0));
19 ASSERT_TRUE(s.empty());
20 ASSERT_TRUE(s.data() == nullptr);
21}
22
23TEST(LlvmLibcSpanTest, InitializeSingleton) {
24 int a = 42;
25 span<int> s(&a, 1);
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);
31}
32
33TEST(LlvmLibcSpanTest, InitializeCArray) {
34 int a[] = {1, 2, 3};
35 span<int> s(a);
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);
41 ASSERT_EQ(s[0], 1);
42 ASSERT_EQ(s[1], 2);
43 ASSERT_EQ(s[2], 3);
44}
45
46TEST(LlvmLibcSpanTest, InitializeArray) {
47 array<int, 3> a = {1, 2, 3};
48 span<int> s(a);
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);
54 ASSERT_EQ(s[0], 1);
55 ASSERT_EQ(s[1], 2);
56 ASSERT_EQ(s[2], 3);
57}
58
59TEST(LlvmLibcSpanTest, InitializeViewFormMutableSingleton) {
60 int a = 42;
61 span<const int> s(&a, 1);
62 ASSERT_EQ(s.size(), size_t(1));
63 ASSERT_TRUE(s.data() == &a);
64}
65
66TEST(LlvmLibcSpanTest, InitializeViewFormMutableCArray) {
67 int a[] = {1, 2, 3};
68 span<const int> s(a);
69 ASSERT_EQ(s.size(), size_t(3));
70 ASSERT_EQ(s[0], 1);
71 ASSERT_EQ(s[1], 2);
72 ASSERT_EQ(s[2], 3);
73}
74
75TEST(LlvmLibcSpanTest, InitializeViewFormMutableArray) {
76 array<int, 3> a = {1, 2, 3};
77 span<const int> s(a);
78 ASSERT_EQ(s.size(), size_t(3));
79 ASSERT_EQ(s[0], 1);
80 ASSERT_EQ(s[1], 2);
81 ASSERT_EQ(s[2], 3);
82}
83
84TEST(LlvmLibcSpanTest, InitializeFromMutable) {
85 span<int> s;
86 span<const int> view(s);
87 (void)view;
88}
89
90TEST(LlvmLibcSpanTest, Assign) {
91 span<int> s;
92 span<int> other;
93 other = s;
94}
95
96TEST(LlvmLibcSpanTest, AssignFromMutable) {
97 span<int> s;
98 span<const int> view;
99 view = s;
100}
101
102TEST(LlvmLibcSpanTest, Modify) {
103 int a[] = {1, 2, 3};
104 span<int> s(a);
105 for (int &value : s)
106 ++value;
107 ASSERT_EQ(s.size(), size_t(3));
108 ASSERT_EQ(s[0], 2);
109 ASSERT_EQ(s[1], 3);
110 ASSERT_EQ(s[2], 4);
111}
112
113TEST(LlvmLibcSpanTest, SubSpan) {
114 int a[] = {1, 2, 3};
115 span<const int> s(a);
116 { // same span
117 const auto _ = s.subspan(offset: 0);
118 ASSERT_EQ(_.size(), size_t(3));
119 ASSERT_EQ(_[0], 1);
120 ASSERT_EQ(_[1], 2);
121 ASSERT_EQ(_[2], 3);
122 }
123 { // last element
124 const auto _ = s.subspan(offset: 2);
125 ASSERT_EQ(_.size(), size_t(1));
126 ASSERT_EQ(_[0], 3);
127 }
128 { // no element
129 const auto _ = s.subspan(offset: 3);
130 ASSERT_EQ(_.size(), size_t(0));
131 }
132 { // first element
133 const auto _ = s.subspan(offset: 0, count: 1);
134 ASSERT_EQ(_.size(), size_t(1));
135 ASSERT_EQ(_[0], 1);
136 }
137}
138
139TEST(LlvmLibcSpanTest, FirstAndLastSubSpan) {
140 int a[] = {1, 2, 3};
141 span<const int> s(a);
142
143 const auto first = s.first(count: 2);
144 ASSERT_EQ(first.size(), size_t(2));
145 ASSERT_EQ(first[0], 1);
146 ASSERT_EQ(first[1], 2);
147
148 const auto last = s.last(count: 2);
149 ASSERT_EQ(last.size(), size_t(2));
150 ASSERT_EQ(last[0], 2);
151 ASSERT_EQ(last[1], 3);
152}
153

source code of libc/test/src/__support/CPP/span_test.cpp