-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestArray.cpp
More file actions
206 lines (181 loc) · 5.09 KB
/
testArray.cpp
File metadata and controls
206 lines (181 loc) · 5.09 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <memory>
#include <cassert>
#include "array.h"
#include "std_oversights.h"
#include "unittest.h"
using std::cout;
using std::endl;
using std::shared_ptr;
using std::make_shared;
using std::is_const;
using std::remove_reference;
using std::remove_pointer;
using cppToD::Array;
using cppToD::L$;
using mex::empty;
int main() {
unittest::runUnitTests();
return 0;
}
//TODO: Test void array - consider adding conversions
MEX_UNIT_TEST
//Basics
auto arr = Array<int>{1, 2, 3, 4, 5};
assert(arr.size() == 5);
assert(!empty(arr));
assert(arr[0] == 1);
assert(arr[4] == 5);
arr[0] = -1;
assert(arr[0] == -1);
assert(*arr.data() == -1);
*arr.data() = -5;
assert(*arr.data() == -5);
MEX_END_UNIT_TEST
MEX_UNIT_TEST
//Destructor check
auto refCounted = make_shared<int>(5);
{
auto arr = Array<shared_ptr<int>>{ refCounted, refCounted };
}
assert(refCounted.unique());
MEX_END_UNIT_TEST
MEX_UNIT_TEST
//Copy operations have reference semantics
auto arr = Array<int>{1, 2, 3, 4, 5};
auto alias = arr;
alias[0] = -1;
assert(arr[0] == -1);
MEX_END_UNIT_TEST
MEX_UNIT_TEST
//Swap
auto arr = Array<int>{1, 2, 3, 4, 5};
auto empty = Array<int>{};
arr.swap(empty);
assert(empty.size() == 5);
assert(empty[0] == 1);
MEX_END_UNIT_TEST
MEX_UNIT_TEST
//Const correctness
//Note: Would not compile with vector b.c. of internal copying!
auto arr = Array<const int>{1, 2, 3, 4, 5};
assert(arr[0] == 1);
assert(*arr.data() == 1);
using AccessorType = typename remove_reference<decltype(arr[0])>::type;
static_assert(is_const<AccessorType>::value, "Can't change elements");
using PtrType = typename remove_pointer<decltype(arr.data())>::type;
static_assert(is_const<AccessorType>::value, "Can't change elements");
MEX_END_UNIT_TEST
MEX_UNIT_TEST
//Dup has copy semantics
auto arr = Array<int>{1, 2, 3, 4, 5};
auto copy = arr.dup();
copy[0] = -1;
assert(arr[0] == 1);
assert(copy[0] == -1);
assert(copy[4] == 5);
MEX_END_UNIT_TEST
MEX_UNIT_TEST
//Const->non const (dup) and non-const->const (idup)
auto arr = Array<int>{1, 2, 3, 4, 5};
auto icopy = arr.idup();
using IAccessorType = typename remove_reference<decltype(icopy[0])>::type;
static_assert(is_const<IAccessorType>::value, "Can't change elements");
auto mutCopy = icopy.dup();
using MutAccessorType = typename remove_reference<decltype(mutCopy[0])>::type;
static_assert(!is_const<MutAccessorType>::value, "Can change elements");
mutCopy[0] = 75;
MEX_END_UNIT_TEST
MEX_UNIT_TEST
//Slicing
Array<int> uninit;
{
auto arr = Array<int>{1, 2, 3, 4, 5};
arr.sliceEq();
assert(arr.size() == 5);
uninit = arr.slice(1, L${});
}
assert(uninit.size() == 4);
assert(uninit[0] == 2);
uninit = uninit.slice(1, 3);
assert(uninit.size() == 2);
assert(uninit[0] == 3);
assert(uninit[1] == 4);
uninit = uninit.slice();
assert(uninit.size() == 2);
assert(uninit[0] == 3);
assert(uninit[1] == 4);
uninit.sliceEq(1, L${});
assert(uninit.size() == 1);
assert(uninit[0] == 4);
uninit.sliceEq(0, 0);
assert(uninit.size() == 0);
MEX_END_UNIT_TEST
MEX_UNIT_TEST
//Range interface - walk
auto arr = Array<int>{1, 2, 3};
assert(!arr.empty());
assert(arr.front() == 1);
assert(arr.back() == 3);
arr.popFront();
assert(!arr.empty());
assert(arr.front() == 2);
assert(arr.back() == 3);
arr.popFront();
assert(!arr.empty());
assert(arr.front() == 3);
assert(arr.back() == 3);
arr.popFront();
assert(arr.empty());
MEX_END_UNIT_TEST
MEX_UNIT_TEST
//Range interface - reverse walk
auto arr = Array<int>{1, 2, 3};
assert(!arr.empty());
assert(arr.front() == 1);
assert(arr.back() == 3);
arr.popBack();
assert(!arr.empty());
assert(arr.front() == 1);
assert(arr.back() == 2);
arr.popBack();
assert(!arr.empty());
assert(arr.front() == 1);
assert(arr.back() == 1);
arr.popBack();
assert(arr.empty());
MEX_END_UNIT_TEST
MEX_UNIT_TEST
//Concatenation
auto arr = Array<int>{1, 2, 3};
auto with4 = arr.concat(4);
assert(with4.size() == 4);
assert(with4[3] == 4);
auto arrayWiseConcat = arr.concat(with4);
assert(arrayWiseConcat.size() == 7);
assert(arrayWiseConcat[0] == 1);
assert(arrayWiseConcat[1] == 2);
assert(arrayWiseConcat[2] == 3);
assert(arrayWiseConcat[3] == 1);
assert(arrayWiseConcat[4] == 2);
assert(arrayWiseConcat[5] == 3);
assert(arrayWiseConcat[6] == 4);
MEX_END_UNIT_TEST
/*
* Proof of concept:
* 1) There is no GC:
* * a) Arbitrary growth will be disallowed. You may not grow an existing one of these.
* * b) You can slice/shrink as desired.
* 2) Supported operations:
* * a) construction via initializer list, iterators, (fill with value?)
* * b) subscripting
* * c) slicing - both accessing and setting, including a full-range slice
* * d) copy constructor/assignment is actually an aliasing operation, rebinding the internal pointers
* * e) explicit dup/idup functions
* * f) get non-owning raw pointer/array out
* * g) length + an overload on a special "instance.length" stand-in type
* h) equality
* * j) concatenation of arrays/elements
* * k) Range interface
*
*/