1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4 //
5 // This code is licensed under the MIT License (MIT).
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13 // THE SOFTWARE.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #include <catch/catch.hpp>
18
19 #include <gsl/multi_span>
20
21 #include <vector>
22
23 using namespace std;
24 using namespace gsl;
25
26 namespace
27 {
use(std::ptrdiff_t &)28 void use(std::ptrdiff_t&) {}
29 }
30
31 TEST_CASE("basic_bounds")
32 {
33 for (auto point : static_bounds<dynamic_range, 3, 4>{2}) {
34 for (decltype(point)::size_type j = 0;
35 j < static_cast<decltype(point)::size_type>(decltype(point)::rank); j++)
36 {
37 use(j);
38 use(point[static_cast<std::size_t>(j)]);
39 }
40 }
41 }
42
43 TEST_CASE("bounds_basic")
44 {
45 static_bounds<3, 4, 5> b;
46 const auto a = b.slice();
47 (void) a;
48 static_bounds<4, dynamic_range, 2> x{4};
49 x.slice().slice();
50 }
51
52 TEST_CASE("arrayview_iterator")
53 {
54 static_bounds<4, dynamic_range, 2> bounds{3};
55
56 const auto itr = bounds.begin();
57 (void) itr;
58 #ifdef CONFIRM_COMPILATION_ERRORS
59 multi_span<int, 4, dynamic_range, 2> av(nullptr, bounds);
60
61 auto itr2 = av.cbegin();
62
63 for (auto& v : av) {
64 v = 4;
65 }
66 fill(av.begin(), av.end(), 0);
67 #endif
68 }
69
70 TEST_CASE("bounds_convertible")
71 {
72 static_bounds<7, 4, 2> b1;
73 static_bounds<7, dynamic_range, 2> b2 = b1;
74 (void) b2;
75 #ifdef CONFIRM_COMPILATION_ERRORS
76 static_bounds<7, dynamic_range, 1> b4 = b2;
77 #endif
78
79 static_bounds<dynamic_range, dynamic_range, dynamic_range> b3 = b1;
80 static_bounds<7, 4, 2> b4 = b3;
81 (void) b4;
82
83 static_bounds<dynamic_range> b11;
84
85 static_bounds<dynamic_range> b5;
86 static_bounds<34> b6;
87
88 b5 = static_bounds<20>();
89 CHECK_THROWS_AS(b6 = b5, fail_fast);
90 b5 = static_bounds<34>();
91 b6 = b5;
92
93 CHECK(b5 == b6);
94 CHECK(b5.size() == b6.size());
95 }
96