pw_span: pw::span / std::span compatibility test
Simple test that std::span and pw::span are compatible.
Change-Id: I3f90dbc94e0fe1a008b6bcd9ff1dfa2b2ca14911
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/99542
Reviewed-by: Ted Pudlik <tpudlik@google.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
diff --git a/pw_span/BUILD.bazel b/pw_span/BUILD.bazel
index ca6e901..71b7875 100644
--- a/pw_span/BUILD.bazel
+++ b/pw_span/BUILD.bazel
@@ -74,3 +74,12 @@
"//pw_unit_test",
],
)
+
+pw_cc_test(
+ name = "compatibility_test",
+ srcs = ["compatibility_test.cc"],
+ deps = [
+ ":pigweed_span",
+ ":pw_span",
+ ],
+)
diff --git a/pw_span/BUILD.gn b/pw_span/BUILD.gn
index e73e05f..6878ebb 100644
--- a/pw_span/BUILD.gn
+++ b/pw_span/BUILD.gn
@@ -80,6 +80,7 @@
tests = [
":polyfill_test",
":pw_span_test",
+ ":compatibility_test",
]
}
@@ -103,6 +104,14 @@
]
}
+pw_test("compatibility_test") {
+ deps = [
+ ":polyfill",
+ ":pw_span",
+ ]
+ sources = [ "compatibility_test.cc" ]
+}
+
pw_doc_group("docs") {
sources = [ "docs.rst" ]
}
diff --git a/pw_span/compatibility_test.cc b/pw_span/compatibility_test.cc
new file mode 100644
index 0000000..3c410f4
--- /dev/null
+++ b/pw_span/compatibility_test.cc
@@ -0,0 +1,71 @@
+// Copyright 2022 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include <span>
+
+#include "gtest/gtest.h"
+#include "pw_span/span.h"
+
+namespace {
+
+constexpr int kCArray[5] = {0, 1, 2, 3, 4};
+
+void TakesPwSpan(pw::span<const int>) {}
+void TakesStdSpan(std::span<const int>) {}
+
+TEST(SpanCompatibility, CallFunction) {
+ TakesPwSpan(std::span<const int>(kCArray));
+ TakesStdSpan(pw::span<const int>(kCArray));
+}
+
+TEST(SpanCompatibility, StdToPwConversions) {
+ std::span<const int> std_span(kCArray);
+ pw::span<const int> pw_span(std_span);
+
+ EXPECT_EQ(std_span.data(), pw_span.data());
+ EXPECT_EQ(std_span.size(), pw_span.size());
+
+ pw_span = std_span;
+
+ EXPECT_EQ(std_span.data(), pw_span.data());
+ EXPECT_EQ(std_span.size(), pw_span.size());
+}
+
+TEST(SpanCompatibility, PwToStdConversions) {
+ pw::span<const int> pw_span(kCArray);
+ std::span<const int> std_span(pw_span);
+
+ EXPECT_EQ(std_span.data(), pw_span.data());
+ EXPECT_EQ(std_span.size(), pw_span.size());
+
+ std_span = pw_span;
+
+ EXPECT_EQ(std_span.data(), pw_span.data());
+ EXPECT_EQ(std_span.size(), pw_span.size());
+}
+
+TEST(SpanCompatibility, SameArray) {
+ pw::span<const int> pw_span(kCArray);
+ std::span<const int> std_span(kCArray);
+
+ EXPECT_EQ(std_span.data(), pw_span.data());
+ EXPECT_EQ(std_span.size(), pw_span.size());
+
+ EXPECT_EQ(std_span[0], 0);
+ EXPECT_EQ(pw_span[0], 0);
+ EXPECT_EQ(std_span[4], 4);
+ EXPECT_EQ(pw_span[4], 4);
+}
+
+} // namespace
diff --git a/pw_span/docs.rst b/pw_span/docs.rst
index 65a0466..c7ee5eb 100644
--- a/pw_span/docs.rst
+++ b/pw_span/docs.rst
@@ -8,6 +8,9 @@
``std::span`` is a non-owning view of an array of values. The intent is for
:cpp:class:`pw::span` is to match the C++20 standard as closely as possible.
+:cpp:class:`pw::span` and ``std::span`` are fully compatible and
+interchangeable. The classes implicitly convert to one another.
+
.. note::
``pw_span:polyfill`` provides ``<span>`` and a ``std::span`` class that is