arrays_mem_check.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Macros
10 *****************************************************************************/
11 
12 #define ECL_MEM_CHECK_ARRAYS // Make sure we use mem checking arrays, not regular
13 
14 /*****************************************************************************
15 ** Includes
16 *****************************************************************************/
17 
18 #include <iostream>
19 #include <gtest/gtest.h>
20 #include "../../include/ecl/containers/array.hpp"
21 
22 /*****************************************************************************
23 ** Using
24 *****************************************************************************/
25 
26 using ecl::Array;
30 
31 /*****************************************************************************
32 ** Tests
33 *****************************************************************************/
34 
35 TEST(ArrayMemCheckTests,constructors) {
37  EXPECT_EQ(3,array1[0]);
38  EXPECT_EQ(3,array1[1]);
39  EXPECT_EQ(3,array1[2]);
40  EXPECT_EQ(3,array1[3]);
42  EXPECT_EQ(4,array2[0]);
43  EXPECT_EQ(4,array2[1]);
44  EXPECT_EQ(4,array2[2]);
45  EXPECT_EQ(4,array2[3]);
47  EXPECT_EQ(3,array3[0]);
48  EXPECT_EQ(3,array3[1]);
49  EXPECT_EQ(3,array3[2]);
50  EXPECT_EQ(3,array3[3]);
51 // std::cout << "Constant array [fixed] : " << array1 << std::endl;
52  Array<int> darray1 = Array<int>::Constant(4,3);
53  EXPECT_EQ(3,darray1[0]);
54  EXPECT_EQ(3,darray1[1]);
55  EXPECT_EQ(3,darray1[2]);
56  EXPECT_EQ(3,darray1[3]);
57 // std::cout << "Constant array [dynamic] : " << darray1 << std::endl;
58 }
59 
60 TEST(ArrayMemCheckTests,copyConstructors) {
61 
63  Array<int> darray1 = Array<int>::Constant(4,3);
64 
65  Array<int,4> array2(array1);
66  Array<int> darray2(darray1);
67 
68  EXPECT_EQ(3,array2[0]);
69  EXPECT_EQ(3,array2[1]);
70  EXPECT_EQ(3,array2[2]);
71  EXPECT_EQ(3,array2[3]);
72 
73  EXPECT_EQ(3,darray2[0]);
74  EXPECT_EQ(3,darray2[1]);
75  EXPECT_EQ(3,darray2[2]);
76  EXPECT_EQ(3,darray2[3]);
77 }
78 
79 TEST(ArrayMemCheckTests,blueprintAssignment) {
80 
81  Array<int,4> array;
82  array = Array<int,4>::Constant(3);
83 
84  EXPECT_EQ(3,array[0]);
85  EXPECT_EQ(3,array[1]);
86  EXPECT_EQ(3,array[2]);
87  EXPECT_EQ(3,array[3]);
88 }
89 TEST(ArrayMemCheckTests,commaInitialisation) {
90 
91  Array<int,4> array;
92  Array<int> darray(4);
93  array << 1,2,3,4;
94  darray << 1,2,3,4;
95 
96  EXPECT_EQ(1,array[0]);
97  EXPECT_EQ(2,array[1]);
98  EXPECT_EQ(3,array[2]);
99  EXPECT_EQ(4,array[3]);
100 
101  EXPECT_EQ(1,darray[0]);
102  EXPECT_EQ(2,darray[1]);
103  EXPECT_EQ(3,darray[2]);
104  EXPECT_EQ(4,darray[3]);
105 }
106 TEST(ArrayMemCheckTests,Accessors) {
107 
108  int j;
109  Array<int,4> array;
110  Array<int> darray(4);
111  array << 1,2,3,4;
112  darray << 1,2,3,4;
113 
114  j = array[2];
115  EXPECT_EQ(3,j);
116  j = array.at(1);
117  EXPECT_EQ(2,j);
118  j = darray[2];
119  EXPECT_EQ(3,j);
120  j = darray.at(1);
121  EXPECT_EQ(2,j);
122 }
123 
124 TEST(ArrayMemCheckTests,rangeChecking) {
125 
127  Array<int> darray = Array<int>::Constant(4,3);
128  bool result = false;
129  try {
130  array.at(10);
131  } catch ( StandardException &e ) {
132  result = true;
133 // std::cout << "Fixed:\n" << e.what() << std::endl;
134  }
135  EXPECT_TRUE(result);
136  result = false;
137  try {
138  darray.at(10);
139  } catch ( StandardException &e ) {
140  result = true;
141 // std::cout << "Dynamic:\n" << e.what() << std::endl;
142  }
143  EXPECT_TRUE(result);
144 }
145 
146 TEST(ArrayMemCheckTests,iterators) {
147 
149  Array<int,4>::iterator iterator;
150  for ( iterator = array.begin(); iterator != array.end(); ++iterator ) {
151  EXPECT_EQ(3,*iterator);
152  }
153 }
154 
155 TEST(ArrayMemCheckTests,resize) {
156  Array<int> darray = Array<int>::Constant(4,3);
157  darray.resize(10);
158  darray << 0,1,2,3,4,5,6,7,8,9;
159  int i = 0;
160  Array<int>::iterator iterator;
161  for ( iterator = darray.begin(); iterator != darray.end(); ++iterator ) {
162  EXPECT_EQ(i,*iterator);
163  ++i;
164  }
165  darray.clear();
166 }
167 
168 TEST(ArrayMemCheckTests,bufferRuns) {
169  int *istart = NULL;
170  Array<int,4> array_ok; array_ok << 1, 2, 3, 4;
171  Array<int,4> array_under; array_under << 1, 2, 3, 4;
172  Array<int,4> array_over; array_over << 1, 2, 3, 4;
173 
174 // std::cout << "Fixed Arrays" << std::endl;
175 // std::cout << "Array_under/over/ok: " << array_ok << std::endl;
176 //
177 
178 // std::cout << "Underrunning array_under." << std::endl;
179  istart = &array_under[0];
180  for ( unsigned int i = 0; i < 3; ++i ) {
181  *(istart-i) = i;
182  }
183 
184 // std::cout << "Overrunning array_over." << std::endl;
185  istart = &array_over[0];
186  for ( unsigned int i = 0; i < array_over.size() + 1; ++i ) {
187  *(istart+i) = i;
188  }
189 //
190 // std::cout << "Buffer results: " << std::endl;
191  EXPECT_TRUE(array_under.bufferUnderRun());
192  EXPECT_FALSE(array_under.bufferOverRun());
193 
194  EXPECT_TRUE(array_over.bufferOverRun());
195  EXPECT_FALSE(array_over.bufferUnderRun());
196 
197  EXPECT_FALSE(array_ok.bufferOverRun());
198  EXPECT_FALSE(array_ok.bufferUnderRun());
199 
200 // std::cout << "Dynamic Arrays" << std::endl;
201 // std::cout << std::endl;
202 //
203  Array<int> darray_ok(4); darray_ok << 1, 2, 3, 4;
204  Array<int> darray_under(4); darray_under << 1, 2, 3, 4;
205  Array<int> darray_over(4); darray_over << 1, 2, 3, 4;
206 
207 // std::cout << "Array_under/over/ok: " << darray_ok << std::endl;
208 //
209 // std::cout << "Underrunning array_under." << std::endl;
210  istart = &darray_under[0];
211  for ( unsigned int i = 0; i < darray_under.size()+1; ++i ) {
212  *(istart+i-1) = i;
213  }
214 //
215 // std::cout << "Overrunning array_over." << std::endl;
216  istart = &darray_over[0];
217  for ( unsigned int i = 0; i < darray_over.size()+1; ++i ) {
218  *(istart+i) = i;
219  }
220 
221  EXPECT_TRUE(darray_under.bufferUnderRun());
222  EXPECT_FALSE(darray_under.bufferOverRun());
223 
224  EXPECT_TRUE(darray_over.bufferOverRun());
225  EXPECT_FALSE(darray_over.bufferUnderRun());
226 
227  EXPECT_FALSE(darray_ok.bufferOverRun());
228  EXPECT_FALSE(darray_ok.bufferUnderRun());
229 }
230 
231 /*****************************************************************************
232 ** Main program
233 *****************************************************************************/
234 
235 int main(int argc, char **argv) {
236 
237  testing::InitGoogleTest(&argc,argv);
238  return RUN_ALL_TESTS();
239 }
240 
TEST
TEST(ArrayMemCheckTests, constructors)
Definition: arrays_mem_check.cpp:35
ecl::blueprints::ConstantArray
Blueprint for instantiating/configuring an array filled with a constant.
Definition: array_no_mem_check.hpp:61
ecl::Array::at
reference at(size_type i)
Definition: array_no_mem_check.hpp:315
ecl::StandardException
ecl::Array::begin
iterator begin()
Definition: array_no_mem_check.hpp:196
ecl::Array::end
iterator end()
Definition: array_no_mem_check.hpp:206
ecl::Array
Fixed size container with a few bells and whistles.
Definition: array_no_mem_check.hpp:112
main
int main(int argc, char **argv)
Definition: arrays_mem_check.cpp:235
ecl::Array::size
static size_type size()
Definition: array_no_mem_check.hpp:348
ecl::blueprints::ArrayFactory
BluePrint factory for the Array class.
Definition: array_no_mem_check.hpp:59


ecl_containers
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:34