- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector-view-test.cpp
82 lines (64 loc) · 2.34 KB
/
vector-view-test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/// @file test/vector-view-test.cpp
/// @copyright 2022 Thomas E. Vaughan, all rights reserved.
/// @brief Tests for gsl::vector_view.
#include"gslcpp/vector-view.hpp"
#include<catch.hpp>
using gsl::v_iface;
using gsl::vector_view;
/// See that C-array matches gsl::v_iface with stride.
/// @tparam T Type of storage for v_iface<T>.
/// @param a C-array.
/// @param b Reference to instance of v_iface<T>.
/// @param s Stride.
template<typename T, size_t N, template<typename, size_t> classV>
voidcheck(doubleconst *a, v_iface<T, N, V> const &b, size_t s= 1) {
for(size_t j= 0, i= 0; j < b.size(); ++j) {
REQUIRE(a[i] == b[j]);
i+= s; // Explicitly step with stride through memory.
}
}
TEST_CASE("vector_view provides right view of array.", "[vector-view]") {
double a[]= {1.0, 1.0, 2.0, 3.0, 5.0, 8.0}; // Mutable, non-decayed C-array.
doubleconst *b= a; // Decayed, immutable C-array.
vector_view vv1(a);
auto mv= vv1.subvector(3, 0, 2); // Mutable view of a[].
REQUIRE(mv.size() == 3);
check(a, mv, 2);
// Immutable view, ultimately of a[].
vector_view iv(b, 4);
REQUIRE(iv.size() == 4);
check(a, iv);
// Modify mutable view.
mv[0]= -1.0;
mv[1]= -2.0;
mv[2]= -3.0;
// Verify that immutable view, ultimately of a[], shows appropriate change.
// - Immutable view does NOT guarantee immutability of what is viewed.
// - Rather, immutable view is just like const-pointer in C: What's pointed
// to might change, but const-pointer cannot be used to make change.
doubleconst c[]= {-1.0, 1.0, -2.0, 3.0, -3.0, 8.0};
check(c, iv);
}
TEST_CASE(
"vector_view with offset provides right view of array.",
"[vector-view]") {
double a[]= {1.0, 1.0, 2.0, 3.0, 5.0, 8.0}; // Mutable, non-decayed C-array.
doubleconst(&b)[6]= a; // Immutable, non-decayed C-array.
// Mutable view of a[], starting at Offset 1 and with Stride 2.
vector_view vv1(a);
auto mv= vv1.subvector(3, 1, 2);
REQUIRE(mv.size() == 3);
check(a + 1, mv, 2);
// Immutable view of a[], starting at Offset 0 and with Stride 1.
auto iv= vector_view(b);
REQUIRE(iv.size() == 6);
check(b, iv);
// Modify mutable view.
mv[0]= -1.0;
mv[1]= -2.0;
mv[2]= -3.0;
// Verify that immutable view, ultimately of a[], shows appropriate change.
doubleconst c[]= {1.0, -1.0, 2.0, -2.0, 5.0, -3.0};
check(c, iv);
}
// EOF