aboutsummaryrefslogtreecommitdiff
path: root/tests/span_tests.cpp
diff options
context:
space:
mode:
authorRian Quinn <rianquinn@gmail.com>2016-11-03 19:55:41 -0600
committerNeil MacIntosh <neilmac@microsoft.com>2016-11-03 18:55:41 -0700
commit2b51b8767a31edbcfb6ee5642552c45f65db1c27 (patch)
tree5e9a70dfb62e076a6326eed54c5d3911530aefd7 /tests/span_tests.cpp
parentd641796b21cef4730efa5bac268c28662fd67d80 (diff)
downloadMicrosoft-GSL-2b51b8767a31edbcfb6ee5642552c45f65db1c27.tar.gz
Add span construction from unique_ptr and shared_ptr
This patch adds support for std::unique_ptr and std::shared_ptr to the gsl::span class instead of having to manually grab the pointer via get(). For reference, this is part of the following issue: https://github.com/Microsoft/GSL/issues/402
Diffstat (limited to 'tests/span_tests.cpp')
-rw-r--r--tests/span_tests.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp
index 9ef2721..1bdc158 100644
--- a/tests/span_tests.cpp
+++ b/tests/span_tests.cpp
@@ -483,6 +483,72 @@ SUITE(span_tests)
#endif
}
+ TEST(from_unique_pointer_construction)
+ {
+ {
+ auto ptr = std::make_unique<int>(4);
+
+ {
+ span<int> s{ptr};
+ CHECK(s.length() == 1 && s.data() == ptr.get());
+ CHECK(s[0] == 4);
+ }
+ }
+
+ {
+ auto ptr = std::unique_ptr<int>{nullptr};
+
+ {
+ span<int> s{ptr};
+ CHECK(s.length() == 0 && s.data() == nullptr);
+ }
+ }
+
+ {
+ auto arr = std::make_unique<int[]>(4);
+
+ for (auto i = 0; i < 4; i++)
+ arr[i] = i + 1;
+
+ {
+ span<int> s{arr, 4};
+ CHECK(s.length() == 4 && s.data() == arr.get());
+ CHECK(s[0] == 1 && s[1] == 2);
+ }
+ }
+
+ {
+ auto ptr = std::unique_ptr<int[]>{nullptr};
+
+ {
+ span<int> s{ptr, 0};
+ CHECK(s.length() == 0 && s.data() == nullptr);
+ }
+ }
+ }
+
+ TEST(from_shared_pointer_construction)
+ {
+ {
+ auto ptr = std::make_shared<int>(4);
+
+ {
+ span<int> s{ptr};
+ CHECK(s.length() == 1 && s.data() == ptr.get());
+ CHECK(s[0] == 4);
+ }
+ }
+
+ {
+ auto ptr = std::shared_ptr<int>{nullptr};
+
+ {
+ span<int> s{ptr};
+ CHECK(s.length() == 0 && s.data() == nullptr);
+ }
+ }
+ }
+
TEST(from_container_constructor)
{
std::vector<int> v = {1, 2, 3};