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-2013 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 #include <UnitTest++.h>
4 #include "cddefines.h"
5 
6 namespace {
7 
8  struct LongIntFixture
9  {
11  LongIntFixture()
12  {
13  p = auto_vec<long>( new long[10] );
14  for( int i=0; i < 10; ++i )
15  p[i] = i;
16  }
17  ~LongIntFixture() {}
18  auto_vec<long> myfunc()
19  {
20  auto_vec<long> a( new long[10] );
21  for( int i=0; i < 10; ++i )
22  a[i] = 2*i;
23  return a;
24  }
25  };
26 
27  struct MyClassFixture
28  {
29  struct Class
30  {
31  long n;
32  Class() { n = 23; }
33  ~Class() {}
34  };
36  MyClassFixture()
37  {
38  p = auto_vec<Class>( new Class[10] );
39  }
40  ~MyClassFixture() {}
41  auto_vec<Class> myfunc()
42  {
43  auto_vec<Class> a( new Class[10] );
44  for( int i=0; i < 10; ++i )
45  a[i].n = i+17;
46  return a;
47  }
48  auto_vec<Class> myfunc2()
49  {
50  return auto_vec<Class>();
51  }
52  };
53 
54  TEST_FIXTURE(LongIntFixture,TestConstructBasic)
55  {
56  // test the basic constructor
58  CHECK( q.get() == NULL );
59  q = auto_vec<long>( new long[5] );
60 
61  // note that q is not used below and valgrind
62  // should check whether the memory in q is freed
63 
64  // this tests assigning an auto_vec to another
65  auto_vec<long> r( p );
66  CHECK( p.get() == NULL );
67  CHECK( r.data() != NULL );
68  // alternative way of doing the same
70  t = r;
71  CHECK( r.get() == NULL );
72  CHECK( t.data() != NULL );
73  // finally, returning a vector as function result!
74  t = myfunc();
75  // this checks operator[]
76  for( int i=0; i < 10; ++i )
77  CHECK_EQUAL(2*i,t[i]);
78  // construct straight from function result
79  auto_vec<long> c( myfunc() );
80  for( int i=0; i < 10; ++i )
81  CHECK_EQUAL(2*i,c[i]);
82  }
83 
84  // now repeat the tests with a class that has a constructor
85  TEST_FIXTURE(MyClassFixture,TestConstructClass)
86  {
88  CHECK( q.get() == NULL );
89  q = auto_vec<Class>( new Class[5] );
90  // check whether the constructor was executed
91  for( int i=0; i < 5; ++i )
92  CHECK_EQUAL(23,q[i].n);
93  auto_vec<Class> r( p );
94  CHECK( p.get() == NULL );
95  CHECK( r.data() != NULL );
97  t = q;
98  CHECK( q.get() == NULL );
99  CHECK( t.data() != NULL );
100  t = myfunc();
101  for( int i=0; i < 10; ++i )
102  CHECK_EQUAL(i+17,t[i].n);
103  auto_vec<Class> a,b,c;
104  a = b = c = t;
105  CHECK( b.get() == NULL );
106  CHECK( c.get() == NULL );
107  CHECK( t.get() == NULL );
108  for( int i=0; i < 10; ++i )
109  CHECK_EQUAL(i+17,a[i].n);
110  a.reset();
111  CHECK( a.get() == NULL );
112  a = myfunc2();
113  CHECK( a.get() == NULL );
114  a = auto_vec<Class>();
115  CHECK( a.get() == NULL );
116  }
117 
118 }
void reset(element_type *p=NULL)
Definition: cddefines.h:1189
element_type * get() const
Definition: cddefines.h:1174