cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TestAutoVec.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2017 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 
7 namespace {
8 
9  struct LongIntFixture
10  {
12  LongIntFixture()
13  {
14  p = auto_vec<long>( new long[10] );
15  for( int i=0; i < 10; ++i )
16  p[i] = i;
17  }
18  ~LongIntFixture() {}
19  auto_vec<long> myfunc()
20  {
21  auto_vec<long> a( new long[10] );
22  for( int i=0; i < 10; ++i )
23  a[i] = 2*i;
24  return a;
25  }
26  };
27 
28  struct MyClassFixture
29  {
30  struct Class
31  {
32  long n;
33  Class() { n = 23; }
34  ~Class() {}
35  };
37  MyClassFixture()
38  {
39  p = auto_vec<Class>( new Class[10] );
40  }
41  ~MyClassFixture() {}
42  auto_vec<Class> myfunc()
43  {
44  auto_vec<Class> a( new Class[10] );
45  for( int i=0; i < 10; ++i )
46  a[i].n = i+17;
47  return a;
48  }
49  auto_vec<Class> myfunc2()
50  {
51  return auto_vec<Class>();
52  }
53  };
54 
55  TEST_FIXTURE(LongIntFixture,TestConstructBasic)
56  {
57  // test the basic constructor
59  CHECK( q.get() == NULL );
60  q = auto_vec<long>( new long[5] );
61 
62  // note that q is not used below and valgrind
63  // should check whether the memory in q is freed
64 
65  // this tests assigning an auto_vec to another
66  auto_vec<long> r( p );
67  CHECK( p.get() == NULL );
68  CHECK( r.data() != NULL );
69  // alternative way of doing the same
71  t = r;
72  CHECK( r.get() == NULL );
73  CHECK( t.data() != NULL );
74  // finally, returning a vector as function result!
75  t = myfunc();
76  // this checks operator[]
77  for( int i=0; i < 10; ++i )
78  CHECK_EQUAL(2*i,t[i]);
79  // construct straight from function result
80  auto_vec<long> c( myfunc() );
81  for( int i=0; i < 10; ++i )
82  CHECK_EQUAL(2*i,c[i]);
83  }
84 
85  // now repeat the tests with a class that has a constructor
86  TEST_FIXTURE(MyClassFixture,TestConstructClass)
87  {
89  CHECK( q.get() == NULL );
90  q = auto_vec<Class>( new Class[5] );
91  // check whether the constructor was executed
92  for( int i=0; i < 5; ++i )
93  CHECK_EQUAL(23,q[i].n);
94  auto_vec<Class> r( p );
95  CHECK( p.get() == NULL );
96  CHECK( r.data() != NULL );
98  t = q;
99  CHECK( q.get() == NULL );
100  CHECK( t.data() != NULL );
101  t = myfunc();
102  for( int i=0; i < 10; ++i )
103  CHECK_EQUAL(i+17,t[i].n);
104  auto_vec<Class> a,b,c;
105  a = b = c = t;
106  CHECK( b.get() == NULL );
107  CHECK( c.get() == NULL );
108  CHECK( t.get() == NULL );
109  for( int i=0; i < 10; ++i )
110  CHECK_EQUAL(i+17,a[i].n);
111  a.reset();
112  CHECK( a.get() == NULL );
113  a = myfunc2();
114  CHECK( a.get() == NULL );
115  a = auto_vec<Class>();
116  CHECK( a.get() == NULL );
117  }
118 
119 }
void reset(element_type *p=NULL)
Definition: cddefines.h:1219
element_type * get() const
Definition: cddefines.h:1204