cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TestFlexArr.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2022 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 #include "cdstd.h"
4 #include <UnitTest++.h>
5 #include "cddefines.h"
6 #include "container_classes.h"
7 
8 namespace {
9 
10  struct LongIntFixtureBlank
11  {
13  LongIntFixtureBlank() {}
14  ~LongIntFixtureBlank() {}
15  };
16 
17  template<bool lgBC>
18  struct LongIntFixtureGeneric
19  {
21  LongIntFixtureGeneric()
22  {
23  arr.alloc(5,10);
24  for( int i=5; i < 10; ++i )
25  arr[i] = i;
26  }
27  ~LongIntFixtureGeneric() {}
28  };
29 
30  typedef LongIntFixtureGeneric<false> LongIntFixture;
31  typedef LongIntFixtureGeneric<true> LongIntFixtureBC;
32 
33  struct LongIntFixtureFill
34  {
36  LongIntFixtureFill()
37  {
38  arr.alloc(5,10);
39  for( int i=5; i < 10; ++i )
40  arr[i] = i;
41  }
42  ~LongIntFixtureFill() {}
43  void myfill()
44  {
45  long i = 0;
47  for( p=arr.begin(); p != arr.end(); ++p )
48  *p = ++i;
49  }
50  };
51 
52  struct RealNumFixture
53  {
55  RealNumFixture()
56  {
57  arr.alloc(5,10);
58  for( int i=5; i < 10; ++i )
59  arr[i] = i;
60  }
61  ~RealNumFixture() {}
62  };
63 
64  struct DoubleFixture
65  {
67  DoubleFixture()
68  {
69  arr.alloc(5,10);
70  for( int i=5; i < 10; ++i )
71  arr[i] = i;
72  }
73  ~DoubleFixture() {}
74  };
75 
76  struct StructWithConstructorFixture
77  {
78  struct a
79  {
80  long n;
81  a() { n = 23; }
82  ~a() {}
83  };
85  StructWithConstructorFixture()
86  {
87  arr.alloc(7,12);
88  }
89  ~StructWithConstructorFixture() {}
90  };
91 
92  struct TestAllocFixture
93  {
94  TestAllocFixture() {}
95  ~TestAllocFixture() {}
96  long mytest()
97  {
98  flex_arr<long,true> a(2,20);
99 
101 
102  a.zero();
103 
104  long res = 0;
105  for (int i=2; i<20; ++i)
106  {
107  p = a.ptr(i);
108  res += ( *p != 0 );
109  }
110 
111  return res;
112  }
113  };
114 
115  TEST_FIXTURE(LongIntFixture,TestZero)
116  {
117  arr.zero();
118  for( int i=5; i < 10; ++i )
119  CHECK_EQUAL(0,arr[i]);
120  arr.clear();
121  arr.zero();
122  }
123 
124  TEST_FIXTURE(LongIntFixture,TestInvalidate1)
125  {
126  arr.invalidate();
127  for( int i=5; i < 10; ++i )
128  CHECK_EQUAL(-1,arr[i]);
129  arr.clear();
130  arr.invalidate();
131  }
132 
133  TEST_FIXTURE(RealNumFixture,TestInvalidate2)
134  {
135  arr.invalidate();
136  for( int i=5; i < 10; ++i )
137  CHECK(isnan(arr[i]));
138  arr.clear();
139  arr.invalidate();
140  }
141 
142  TEST_FIXTURE(DoubleFixture,TestInvalidate3)
143  {
144  arr.invalidate();
145  for( int i=5; i < 10; ++i )
146  CHECK(isnan(arr[i]));
147  arr.clear();
148  arr.invalidate();
149  }
150 
151  TEST_FIXTURE(LongIntFixtureBC,TestAccess1)
152  {
153  for( int i=5; i < 10; ++i )
154  CHECK_EQUAL(i,arr[i]);
155  CHECK_THROW(arr[4],out_of_range);
156  CHECK_THROW(arr[10],out_of_range);
157  const flex_arr<long,true>* carr = &arr;
158  for( int i=5; i < 10; ++i )
159  CHECK_EQUAL(i,(*carr)[i]);
160  CHECK_THROW((*carr)[4],out_of_range);
161  CHECK_THROW((*carr)[10],out_of_range);
162  }
163 
164  TEST_FIXTURE(LongIntFixtureBC,TestAccess2)
165  {
166  for( int i=5; i < 10; ++i )
167  CHECK_EQUAL(i,arr[i]);
168  CHECK_THROW(arr[4],out_of_range);
169  CHECK_THROW(arr[10],out_of_range);
170  const flex_arr<long,true>* carr = &arr;
171  for( int i=5; i < 10; ++i )
172  CHECK_EQUAL(i,(*carr)[i]);
173  CHECK_THROW((*carr)[4],out_of_range);
174  CHECK_THROW((*carr)[10],out_of_range);
175  }
176 
177  TEST_FIXTURE(LongIntFixture,TestAccess3)
178  {
179  for( int i=5; i < 10; ++i )
180  CHECK_EQUAL(i,*arr.ptr(i));
181  const flex_arr<long,false>* carr = &arr;
182  for( int i=5; i < 10; ++i )
183  CHECK_EQUAL(i,*carr->ptr(i));
184  }
185 
186  TEST_FIXTURE(LongIntFixtureBC,TestAccess4)
187  {
188  for( int i=5; i < 10; ++i )
189  CHECK_EQUAL(i,*arr.ptr(i));
190  CHECK_THROW(*arr.ptr(4),out_of_range);
191  CHECK_THROW(*arr.ptr(10),out_of_range);
192  const flex_arr<long,true>* carr = &arr;
193  for( int i=5; i < 10; ++i )
194  CHECK_EQUAL(i,*carr->ptr(i));
195  CHECK_THROW(*carr->ptr(4),out_of_range);
196  CHECK_THROW(*carr->ptr(10),out_of_range);
197  }
198 
199  TEST_FIXTURE(LongIntFixture,TestAccess5)
200  {
201  for( int i=5; i < 10; ++i )
202  CHECK_EQUAL(i,arr.at(i));
203  CHECK_THROW(arr.at(4),out_of_range);
204  CHECK_THROW(arr.at(10),out_of_range);
205  const flex_arr<long,false>* carr = &arr;
206  for( int i=5; i < 10; ++i )
207  CHECK_EQUAL(i,carr->at(i));
208  CHECK_THROW(carr->at(4),out_of_range);
209  CHECK_THROW(carr->at(10),out_of_range);
210  }
211 
212  // test the state_do function
213  TEST_FIXTURE(LongIntFixture,TestStateDo)
214  {
215  const char *fnam = "tfa.872GqS";
216  FILE *io = open_data( fnam, "wb" );
217  CHECK( io != NULL );
218  arr.state_do( io, false ); // dump state
219  fclose( io );
220  arr.invalidate(); // trash contents
221  io = open_data( fnam, "rb", AS_LOCAL_ONLY );
222  CHECK( io != NULL );
223  arr.state_do( io, true ); // restore state
224  fclose( io );
225  remove(fnam);
226  CHECK_EQUAL(7,arr[7]);
227  }
228 
229  TEST_FIXTURE(LongIntFixtureBC,TestBoundaries)
230  {
231  CHECK( arr.begin() == arr.ptr(5) );
232  CHECK( arr.end() == arr.ptr(10) );
233  CHECK( &arr.front() == &arr[5] );
234  CHECK( &arr.back() == &arr[9] );
235  CHECK( arr.data() == &arr[5] );
236  CHECK( arr.ptr0() == &arr[5]-5 );
237  const flex_arr<long,true>* carr = &arr;
238  CHECK( carr->begin() == arr.ptr(5) );
239  CHECK( carr->end() == arr.ptr(10) );
240  CHECK( &carr->front() == &arr[5] );
241  CHECK( &carr->back() == &arr[9] );
242  CHECK( carr->data() == &arr[5] );
243  CHECK( carr->ptr0() == &arr[5]-5 );
244  }
245 
246  TEST_FIXTURE(LongIntFixtureFill,TestRealloc)
247  {
248  myfill();
249  for( int i=5; i < 10; ++i )
250  CHECK_EQUAL( i-4, arr[i] );
251  long *pold = arr.data();
252  arr.realloc( 20 );
253  long *pnew = arr.data();
254  CHECK( pold != pnew );
255  for( int i=5; i < 10; ++i )
256  CHECK_EQUAL( i-4, arr[i] );
257  }
258 
259  TEST_FIXTURE(LongIntFixtureBlank,TestAllocationWithoutReservation)
260  {
261  CHECK_EQUAL(0L,arr.size());
262  CHECK_EQUAL(0L,arr.capacity());
263  CHECK( arr.empty() );
264  arr.alloc(5,10);
265  CHECK_EQUAL(5L,arr.size());
266  CHECK_EQUAL(5L,arr.capacity());
267  CHECK( !arr.empty() );
268  arr.realloc(30);
269  CHECK_EQUAL(25L,arr.size());
270  CHECK_EQUAL(25L,arr.capacity());
271  CHECK( !arr.empty() );
272  arr.realloc(10);
273  CHECK_EQUAL(5L,arr.size());
274  CHECK_EQUAL(25L,arr.capacity());
275  CHECK( !arr.empty() );
276  arr.realloc(0);
277  CHECK_EQUAL(0L,arr.size());
278  CHECK_EQUAL(25L,arr.capacity());
279  CHECK( arr.empty() );
280  arr.clear();
281  CHECK_EQUAL(0L,arr.size());
282  CHECK_EQUAL(0L,arr.capacity());
283  CHECK( arr.empty() );
284  arr.alloc(-5,12);
285  CHECK_EQUAL(17L,arr.size());
286  CHECK_EQUAL(17L,arr.capacity());
287  CHECK( !arr.empty() );
288  arr.clear();
289  CHECK_EQUAL(0L,arr.size());
290  CHECK_EQUAL(0L,arr.capacity());
291  CHECK( arr.empty() );
292  arr.alloc(-5,-12);
293  CHECK_EQUAL(0L,arr.size());
294  CHECK_EQUAL(0L,arr.capacity());
295  CHECK( arr.empty() );
296  arr.realloc(30);
297  CHECK_EQUAL(35L,arr.size());
298  CHECK_EQUAL(35L,arr.capacity());
299  CHECK( !arr.empty() );
300  }
301 
302  TEST_FIXTURE(LongIntFixtureBlank,TestAllocationWithReservation)
303  {
304  arr.reserve(100);
305  CHECK_EQUAL(0L,arr.size());
306  CHECK_EQUAL(100L,arr.capacity());
307  CHECK( arr.empty() );
308  arr.alloc(5,10);
309  CHECK_EQUAL(5L,arr.size());
310  CHECK_EQUAL(100L,arr.capacity());
311  CHECK( !arr.empty() );
312  arr.realloc(30);
313  CHECK_EQUAL(25L,arr.size());
314  CHECK_EQUAL(100L,arr.capacity());
315  CHECK( !arr.empty() );
316  arr.realloc(200);
317  CHECK_EQUAL(195L,arr.size());
318  CHECK_EQUAL(195L,arr.capacity());
319  CHECK( !arr.empty() );
320  arr.reserve(5);
321  CHECK_EQUAL(0L,arr.size());
322  CHECK_EQUAL(5L,arr.capacity());
323  CHECK( arr.empty() );
324  arr.alloc(-5,-10);
325  CHECK_EQUAL(0L,arr.size());
326  CHECK_EQUAL(5L,arr.capacity());
327  CHECK( arr.empty() );
328  arr.reserve(-5);
329  CHECK_EQUAL(0L,arr.size());
330  CHECK_EQUAL(0L,arr.capacity());
331  CHECK( arr.empty() );
332  arr.reserve(10);
333  CHECK_EQUAL(0L,arr.size());
334  CHECK_EQUAL(10L,arr.capacity());
335  CHECK( arr.empty() );
336  arr.alloc(-10,10);
337  CHECK_EQUAL(20L,arr.size());
338  CHECK_EQUAL(20L,arr.capacity());
339  CHECK( !arr.empty() );
340  }
341 
342  TEST_FIXTURE(StructWithConstructorFixture,TestAllocationWithConstructor)
343  {
344  for( int i=7; i < 12; ++i )
345  CHECK_EQUAL(23,arr[i].n);
346  }
347 
348  // check whether the variant form for allocating works correctly
349  // this also tests p_iterator in bounds-checking mode
350  TEST_FIXTURE(TestAllocFixture,TestVariantAlloc)
351  {
352  CHECK_EQUAL(0,mytest());
353  }
354 
355  TEST_FIXTURE(LongIntFixtureBC,TestCopyOperator)
356  {
357  flex_arr<long,true> arr2(12,234);
358  CHECK( arr.size() != arr2.size() );
359  arr2.zero();
360  CHECK_EQUAL(0,arr2[114]);
361  arr2 = arr;
362  CHECK( arr.size() == arr2.size() );
363  // check that copies are distinct
364  CHECK( &arr[5] != &arr2[5] );
365  for (int i=5; i<10; ++i)
366  CHECK_EQUAL(i,arr2[i]);
367 
368  CHECK_THROW(arr2[4],out_of_range);
369  CHECK_THROW(arr2[10],out_of_range);
370 
371  // is it safe to copy to oneself?
372  flex_arr<long,true>* copy = &arr2;
373  arr2 = *copy;
374  // have the contents been preserved?
375  CHECK_EQUAL(9,arr2[9]);
376 
377  // now copy using constructor
378  flex_arr<long,true> arr3 = arr;
379  CHECK( arr.size() == arr3.size() );
380  // check that copies are distinct
381  CHECK( &arr[5] != &arr3[5] );
382  for (int i=5; i<10; ++i)
383  CHECK_EQUAL(i,arr3[i]);
384 
385  arr.clear();
386  arr2 = arr;
387  CHECK_EQUAL(0L,arr2.size());
388  arr2.reserve( 100 );
389  }
390 
391 }
FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
Definition: cpu.cpp:765
pntr - interface class to replace normal pointers
size_type size() const
#define isnan
Definition: cddefines.h:659
void alloc(size_type begin, size_type end)
iterator ptr(size_type i)