Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / __support / CPP / span_test.cpp
blob6da06c31e7adfd1d6a81ce3dcdf8cdc3ddd05b33
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 //===----------------------------------------------------------------------===//
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) {
17 span<int> s;
18 ASSERT_EQ(s.size(), size_t(0));
19 ASSERT_TRUE(s.empty());
20 ASSERT_TRUE(s.data() == nullptr);
23 TEST(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);
33 TEST(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);
46 TEST(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);
59 TEST(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);
66 TEST(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);
75 TEST(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);
84 TEST(LlvmLibcSpanTest, InitializeFromMutable) {
85 span<int> s;
86 span<const int> view(s);
87 (void)view;
90 TEST(LlvmLibcSpanTest, Assign) {
91 span<int> s;
92 span<int> other;
93 other = s;
96 TEST(LlvmLibcSpanTest, AssignFromMutable) {
97 span<int> s;
98 span<const int> view;
99 view = s;
102 TEST(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);
113 TEST(LlvmLibcSpanTest, SubSpan) {
114 int a[] = {1, 2, 3};
115 span<const int> s(a);
116 { // same span
117 const auto _ = s.subspan(0);
118 ASSERT_EQ(_.size(), size_t(3));
119 ASSERT_EQ(_[0], 1);
120 ASSERT_EQ(_[1], 2);
121 ASSERT_EQ(_[2], 3);
123 { // last element
124 const auto _ = s.subspan(2);
125 ASSERT_EQ(_.size(), size_t(1));
126 ASSERT_EQ(_[0], 3);
128 { // no element
129 const auto _ = s.subspan(3);
130 ASSERT_EQ(_.size(), size_t(0));
132 { // first element
133 const auto _ = s.subspan(0, 1);
134 ASSERT_EQ(_.size(), size_t(1));
135 ASSERT_EQ(_[0], 1);
139 TEST(LlvmLibcSpanTest, FirstAndLastSubSpan) {
140 int a[] = {1, 2, 3};
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);