forked from tmolteno/necpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafe_array_tb.cpp
More file actions
130 lines (95 loc) · 2.98 KB
/
Copy pathsafe_array_tb.cpp
File metadata and controls
130 lines (95 loc) · 2.98 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
/*
Copyright (C) 2015 Timothy C.A. Molteno
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "safe_array.h"
TEST_CASE( "Resizing Array", "[safe_array]") {
SECTION( "Capacity is bigger or equal to size" ) {
safe_array<float> v( 5 );
REQUIRE( v.size() == 5 );
REQUIRE( v.capacity() >= 5 );
}
SECTION( "resizing bigger changes size and capacity" ) {
safe_array<float> v( 5 );
v.resize( 10 );
REQUIRE( v.size() == 10 );
REQUIRE( v.capacity() >= 10 );
}
SECTION( "resizing smaller changes size but not capacity" ) {
safe_array<float> v( 5 );
v.resize( 0 );
REQUIRE( v.size() == 0 );
REQUIRE( v.capacity() >= 5 );
}
}
TEST_CASE( "Indexing Array", "[safe_array]") {
safe_array<float> v( 5 );
SECTION( "Filling works" ) {
v.fill(0,5,5);
for (int i=0;i<5;i++)
REQUIRE( v[i] == 5 );
}
SECTION( "Negative Indices" ) {
REQUIRE_THROWS( v[-1] );
}
SECTION( "Out of Bound Indices" ) {
REQUIRE_THROWS( v[5] );
}
}
TEST_CASE( "Filling Array", "[safe_array]") {
safe_array<float> v( 5 );
v.fill(0,5,5);
for (int i=0;i<5;i++)
REQUIRE( v[i] == 5 );
SECTION( "Filling from the Middle" ) {
v.resize( 10 );
v.fill(5,5,0);
for (int i=0;i<5;i++)
REQUIRE( v[i] == 5 );
for (int i=5;i<10;i++)
REQUIRE( v[i] == 0 );
}
}
TEST_CASE( "Segments", "[safe_array]") {
safe_array<int> v( 5 );
for (int i=0;i<5;i++)
v[i] = i;
const safe_array<int>& f = v.segment(0,3);
REQUIRE(f.size() == 4);
for (int i=0;i<5;i++)
REQUIRE( v[i] == i );
for (int i=0;i<3;i++)
REQUIRE( f[i] == i );
SECTION( "modifying segment" ) {
v[2] = 1;
REQUIRE( f[2] == 1 );
REQUIRE( v[2] == 1 );
}
SECTION( "Zero length segment" ) {
const safe_array<int>& g = v.segment(1,1);
REQUIRE( g[0] == 1 );
}
}
TEST_CASE( "2D", "[safe_array]") {
safe_matrix<int> v( 5,5 );
for (int i=0;i<5;i++)
for (int j=0;j<5;j++)
v(i,j) = i*j;
REQUIRE(v.size() == 25);
REQUIRE(v(1,1) == 1);
REQUIRE(v(4,1) == 4);
REQUIRE_THROWS(v(5,1));
REQUIRE_THROWS(v(5,0));
}