|
| 1 | +#include "pch.h" |
| 2 | +#include "catch.hpp" |
| 3 | +#include <array> |
| 4 | + |
| 5 | +using namespace winrt; |
| 6 | +using namespace Windows::Foundation; |
| 7 | +using namespace Windows::Storage::Streams; |
| 8 | +using namespace Windows::Data::Json; |
| 9 | + |
| 10 | +// |
| 11 | +// This is a helper to create a data reader for use in testing arrays. |
| 12 | +// |
| 13 | +static IAsyncOperation<IDataReader> CreateDataReader(std::initializer_list<byte> values) |
| 14 | +{ |
| 15 | + InMemoryRandomAccessStream stream; |
| 16 | + DataWriter writer(stream); |
| 17 | + writer.WriteByte(1); |
| 18 | + writer.WriteByte(2); |
| 19 | + writer.WriteByte(3); |
| 20 | + co_await writer.StoreAsync(); |
| 21 | + |
| 22 | + stream.Seek(0); |
| 23 | + DataReader reader(stream); |
| 24 | + co_await reader.LoadAsync(3); |
| 25 | + co_return reader; |
| 26 | +} |
| 27 | + |
| 28 | +// |
| 29 | +// This test illustrates an array_view<T> (non-const) bound to a std::span<T> on a std::array |
| 30 | +// |
| 31 | +TEST_CASE("array,DataReader,std::span") |
| 32 | +{ |
| 33 | + auto reader = CreateDataReader({ 1, 2, 3 }).get(); |
| 34 | + |
| 35 | + std::array<byte, 3> a{}; |
| 36 | + std::span<byte> sp(a); |
| 37 | + reader.ReadBytes(sp); // FillArray pattern |
| 38 | + |
| 39 | + REQUIRE(a.size() == 3); |
| 40 | + REQUIRE(a[0] == 1); |
| 41 | + REQUIRE(a[1] == 2); |
| 42 | + REQUIRE(a[2] == 3); |
| 43 | +} |
| 44 | + |
| 45 | +// |
| 46 | +// This test illustrates passing a std::array to a method that takes array_view<T> |
| 47 | +// |
| 48 | +TEST_CASE("array,DataReader,std::span,direct") |
| 49 | +{ |
| 50 | + auto reader = CreateDataReader({ 1, 2, 3 }).get(); |
| 51 | + |
| 52 | + std::array<byte, 3> a{}; |
| 53 | + reader.ReadBytes(a); // FillArray pattern |
| 54 | + |
| 55 | + REQUIRE(a.size() == 3); |
| 56 | + REQUIRE(a[0] == 1); |
| 57 | + REQUIRE(a[1] == 2); |
| 58 | + REQUIRE(a[2] == 3); |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +TEST_CASE("array_view,span") |
| 63 | +{ |
| 64 | + { |
| 65 | + int v[] = { 1, 2, 3 }; |
| 66 | + std::span<int> s(v); |
| 67 | + array_view<int> a = s; |
| 68 | + REQUIRE(a.data() == v); |
| 69 | + REQUIRE(a.size() == 3); |
| 70 | + } |
| 71 | + |
| 72 | + { |
| 73 | + int v[] = { 1, 2, 3 }; |
| 74 | + std::span<int const> s(v); |
| 75 | + array_view<int const> a = s; |
| 76 | + REQUIRE(a.data() == v); |
| 77 | + REQUIRE(a.size() == 3); |
| 78 | + } |
| 79 | + |
| 80 | + { |
| 81 | + int const v[] = { 1, 2, 3 }; |
| 82 | + std::span<int const> s(v); |
| 83 | + array_view<int const> a = s; |
| 84 | + REQUIRE(a.data() == v); |
| 85 | + REQUIRE(a.size() == 3); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// |
| 90 | +// Tests com_array support for span construction. |
| 91 | +// |
| 92 | +TEST_CASE("com_array,span") |
| 93 | +{ |
| 94 | + { |
| 95 | + int v[] = { 1, 2, 3 }; |
| 96 | + std::span<int> s(v); |
| 97 | + com_array<int> a(s); |
| 98 | + REQUIRE(a.size() == 3); |
| 99 | + REQUIRE(a[0] == 1); |
| 100 | + REQUIRE(a[1] == 2); |
| 101 | + REQUIRE(a[2] == 3); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// |
| 106 | +// Tests array_view support for conversion to span |
| 107 | +// |
| 108 | +TEST_CASE("array_view,span,as") |
| 109 | +{ |
| 110 | + { |
| 111 | + int v[] = { 1, 2, 3 }; |
| 112 | + array_view<int> a = v; |
| 113 | + std::span<int> s(a); |
| 114 | + REQUIRE(s.data() == v); |
| 115 | + REQUIRE(s.size() == 3); |
| 116 | + } |
| 117 | + |
| 118 | + { |
| 119 | + int v[] = { 1, 2, 3 }; |
| 120 | + array_view<int const> a = v; |
| 121 | + std::span<int const> s(a); |
| 122 | + REQUIRE(s.data() == v); |
| 123 | + REQUIRE(s.size() == 3); |
| 124 | + } |
| 125 | + |
| 126 | + { |
| 127 | + int const v[] = { 1, 2, 3 }; |
| 128 | + array_view<int const> a = v; |
| 129 | + std::span<int const> s(a); |
| 130 | + REQUIRE(s.data() == v); |
| 131 | + REQUIRE(s.size() == 3); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// |
| 136 | +// Tests com_array support for conversion to span |
| 137 | +// |
| 138 | +TEST_CASE("com_array,span,as") |
| 139 | +{ |
| 140 | + { |
| 141 | + int v[] = { 1, 2, 3 }; |
| 142 | + com_array<int> a(v); |
| 143 | + std::span<int> s(a); |
| 144 | + REQUIRE(s.size() == 3); |
| 145 | + REQUIRE(s[0] == 1); |
| 146 | + REQUIRE(s[1] == 2); |
| 147 | + REQUIRE(s[2] == 3); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// Verify that class template argument deduction works for array_view. |
| 152 | +TEST_CASE("array_view,span,ctad") |
| 153 | +{ |
| 154 | +#define REQUIRE_DEDUCED_AS(T, ...) \ |
| 155 | + static_assert(std::is_same_v<array_view<T>, decltype(array_view(__VA_ARGS__))>) |
| 156 | + |
| 157 | + uint8_t a[] = {1, 2, 3}; |
| 158 | + std::span<uint8_t, 3> sp{ a }; |
| 159 | + |
| 160 | + REQUIRE_DEDUCED_AS(uint8_t, sp); |
| 161 | + |
| 162 | + std::span<uint8_t const, 3> csp{ a }; |
| 163 | + REQUIRE_DEDUCED_AS(uint8_t const, csp); |
| 164 | + |
| 165 | + std::span<uint8_t, 3> const cs{ a }; |
| 166 | + REQUIRE_DEDUCED_AS(uint8_t const, cs); |
| 167 | + |
| 168 | +#undef REQUIRE_DEDUCED_AS |
| 169 | +} |
| 170 | + |
| 171 | +// Verify that class template argument deduction works for com_array. |
| 172 | +TEST_CASE("com_array,span,ctad") |
| 173 | +{ |
| 174 | +#define REQUIRE_DEDUCED_AS(T, ...) \ |
| 175 | + static_assert(std::is_same_v<com_array<T>, decltype(com_array(__VA_ARGS__))>) |
| 176 | + |
| 177 | + uint8_t a[] = { 1, 2, 3 }; |
| 178 | + |
| 179 | + std::span<uint8_t, 3> sp{ a }; |
| 180 | + REQUIRE_DEDUCED_AS(uint8_t, sp); |
| 181 | + |
| 182 | + std::span<uint8_t const, 3> csp{ a }; |
| 183 | + REQUIRE_DEDUCED_AS(uint8_t, csp); |
| 184 | + |
| 185 | + std::span<uint8_t, 3> const cs{ a }; |
| 186 | + REQUIRE_DEDUCED_AS(uint8_t, cs); |
| 187 | + |
| 188 | +#undef REQUIRE_DEDUCED_AS |
| 189 | +} |
0 commit comments