1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_polyfill/standard.h"
16
17 #if PW_CXX_STANDARD_IS_SUPPORTED(20)
18
19 #include <span>
20
21 #include "pw_span/span.h"
22 #include "pw_unit_test/framework.h"
23
24 namespace {
25
26 constexpr int kCArray[5] = {0, 1, 2, 3, 4};
27
TakesPwSpan(pw::span<const int>)28 void TakesPwSpan(pw::span<const int>) {}
TakesStdSpan(std::span<const int>)29 void TakesStdSpan(std::span<const int>) {}
30
TEST(SpanCompatibility,CallFunction)31 TEST(SpanCompatibility, CallFunction) {
32 TakesPwSpan(std::span<const int>(kCArray));
33 TakesStdSpan(pw::span<const int>(kCArray));
34 }
35
TEST(SpanCompatibility,StdToPwConversions)36 TEST(SpanCompatibility, StdToPwConversions) {
37 std::span<const int> std_span(kCArray);
38 pw::span<const int> pw_span(std_span);
39
40 EXPECT_EQ(std_span.data(), pw_span.data());
41 EXPECT_EQ(std_span.size(), pw_span.size());
42
43 pw_span = std_span;
44
45 EXPECT_EQ(std_span.data(), pw_span.data());
46 EXPECT_EQ(std_span.size(), pw_span.size());
47 }
48
TEST(SpanCompatibility,PwToStdConversions)49 TEST(SpanCompatibility, PwToStdConversions) {
50 pw::span<const int> pw_span(kCArray);
51 std::span<const int> std_span(pw_span);
52
53 EXPECT_EQ(std_span.data(), pw_span.data());
54 EXPECT_EQ(std_span.size(), pw_span.size());
55
56 std_span = pw_span;
57
58 EXPECT_EQ(std_span.data(), pw_span.data());
59 EXPECT_EQ(std_span.size(), pw_span.size());
60 }
61
TEST(SpanCompatibility,SameArray)62 TEST(SpanCompatibility, SameArray) {
63 pw::span<const int> pw_span(kCArray);
64 std::span<const int> std_span(kCArray);
65
66 EXPECT_EQ(std_span.data(), pw_span.data());
67 EXPECT_EQ(std_span.size(), pw_span.size());
68
69 EXPECT_EQ(std_span[0], 0);
70 EXPECT_EQ(pw_span[0], 0);
71 EXPECT_EQ(std_span[4], 4);
72 EXPECT_EQ(pw_span[4], 4);
73 }
74
75 } // namespace
76
77 #endif // PW_CXX_STANDARD_IS_SUPPORTED(20)
78