arrays.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Includes
10 *****************************************************************************/
11 
12 #include <iostream>
13 #include <gtest/gtest.h>
14 #include "../../include/ecl/containers/array.hpp"
15 
16 /*****************************************************************************
17 ** Using
18 *****************************************************************************/
19 
20 using ecl::Array;
25 
26 /*****************************************************************************
27 ** Tests
28 *****************************************************************************/
29 
30 TEST(ArrayMemCheckTests,constructors) {
32  EXPECT_EQ(3,array1[0]);
33  EXPECT_EQ(3,array1[1]);
34  EXPECT_EQ(3,array1[2]);
35  EXPECT_EQ(3,array1[3]);
37  EXPECT_EQ(4,array2[0]);
38  EXPECT_EQ(4,array2[1]);
39  EXPECT_EQ(4,array2[2]);
40  EXPECT_EQ(4,array2[3]);
42  EXPECT_EQ(3,array3[0]);
43  EXPECT_EQ(3,array3[1]);
44  EXPECT_EQ(3,array3[2]);
45  EXPECT_EQ(3,array3[3]);
46 // std::cout << "Constant array [fixed] : " << array1 << std::endl;
47  Array<int> darray1 = Array<int>::Constant(4,3);
48  EXPECT_EQ(3,darray1[0]);
49  EXPECT_EQ(3,darray1[1]);
50  EXPECT_EQ(3,darray1[2]);
51  EXPECT_EQ(3,darray1[3]);
52 // std::cout << "Constant array [dynamic] : " << darray1 << std::endl;
53 }
54 
55 TEST(ArrayMemCheckTests,copyConstructors) {
56 
58  Array<int> darray1 = Array<int>::Constant(4,3);
59 
60  Array<int,4> array2(array1);
61  Array<int> darray2(darray1);
62 
63  EXPECT_EQ(3,array2[0]);
64  EXPECT_EQ(3,array2[1]);
65  EXPECT_EQ(3,array2[2]);
66  EXPECT_EQ(3,array2[3]);
67 
68  EXPECT_EQ(3,darray2[0]);
69  EXPECT_EQ(3,darray2[1]);
70  EXPECT_EQ(3,darray2[2]);
71  EXPECT_EQ(3,darray2[3]);
72 }
73 
74 TEST(ArrayMemCheckTests,blueprintAssignment) {
75 
76  Array<int,4> array;
77  array = Array<int,4>::Constant(3);
78 
79  EXPECT_EQ(3,array[0]);
80  EXPECT_EQ(3,array[1]);
81  EXPECT_EQ(3,array[2]);
82  EXPECT_EQ(3,array[3]);
83 }
84 TEST(ArrayMemCheckTests,commaInitialisation) {
85 
86  Array<int,4> array;
87  Array<int> darray(4);
88  array << 1,2,3,4;
89  darray << 1,2,3,4;
90 
91  EXPECT_EQ(1,array[0]);
92  EXPECT_EQ(2,array[1]);
93  EXPECT_EQ(3,array[2]);
94  EXPECT_EQ(4,array[3]);
95 
96  EXPECT_EQ(1,darray[0]);
97  EXPECT_EQ(2,darray[1]);
98  EXPECT_EQ(3,darray[2]);
99  EXPECT_EQ(4,darray[3]);
100 }
101 TEST(ArrayMemCheckTests,Accessors) {
102 
103  int j;
104  Array<int,4> array;
105  Array<int> darray(4);
106  array << 1,2,3,4;
107  darray << 1,2,3,4;
108 
109  j = array[2];
110  EXPECT_EQ(3,j);
111  j = array.at(1);
112  EXPECT_EQ(2,j);
113  j = darray[2];
114  EXPECT_EQ(3,j);
115  j = darray.at(1);
116  EXPECT_EQ(2,j);
117 }
118 
119 TEST(ArrayMemCheckTests,rangeChecking) {
120 
122  Array<int> darray = Array<int>::Constant(4,3);
123  bool result = false;
124  try {
125  array.at(10);
126  } catch ( StandardException &e ) {
127  result = true;
128 // std::cout << "Fixed:\n" << e.what() << std::endl;
129  }
130  EXPECT_TRUE(result);
131  result = false;
132  try {
133  darray.at(10);
134  } catch ( StandardException &e ) {
135  result = true;
136 // std::cout << "Dynamic:\n" << e.what() << std::endl;
137  }
138  EXPECT_TRUE(result);
139 }
140 
141 TEST(ArrayMemCheckTests,iterators) {
142 
144  Array<int,4>::iterator iterator;
145  for ( iterator = array.begin(); iterator != array.end(); ++iterator ) {
146  EXPECT_EQ(3,*iterator);
147  }
148 }
149 
150 TEST(ArrayMemCheckTests,resize) {
151  Array<int> darray = Array<int>::Constant(4,3);
152  darray.resize(10);
153  darray << 0,1,2,3,4,5,6,7,8,9;
154  int i = 0;
155  Array<int>::iterator iterator;
156  for ( iterator = darray.begin(); iterator != darray.end(); ++iterator ) {
157  EXPECT_EQ(i,*iterator);
158  ++i;
159  }
160  darray.clear();
161 }
162 
163 TEST(ArrayMemCheckTests,concepts) {
164  typedef Array<char,6> ByteArray;
166  SUCCEED();
167  // ecl_compile_time_concept_check(ContainerConcept<Array<char,6>>); // The macro won't let you do this (macro function syntax problem, not a c++ problem)
168 }
169 
170 /*****************************************************************************
171 ** Main program
172 *****************************************************************************/
173 
174 int main(int argc, char **argv) {
175 
176  testing::InitGoogleTest(&argc,argv);
177  return RUN_ALL_TESTS();
178 }
ecl_compile_time_concept_check
#define ecl_compile_time_concept_check(Model)
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
TEST
TEST(ArrayMemCheckTests, constructors)
Definition: arrays.cpp:30
main
int main(int argc, char **argv)
Definition: arrays.cpp:174
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::ContainerConcept
ecl::Array
Fixed size container with a few bells and whistles.
Definition: array_no_mem_check.hpp:112
ByteArray
Array< char, 3 > ByteArray
Definition: container_converters.cpp:47
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