arrays.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Includes
00010 *****************************************************************************/
00011 
00012 #include <iostream>
00013 #include <gtest/gtest.h>
00014 #include "../../include/ecl/containers/array.hpp"
00015 
00016 /*****************************************************************************
00017 ** Using
00018 *****************************************************************************/
00019 
00020 using ecl::Array;
00021 using ecl::blueprints::ArrayFactory;
00022 using ecl::blueprints::ConstantArray;
00023 using ecl::StandardException;
00024 using ecl::ContainerConcept;
00025 
00026 /*****************************************************************************
00027 ** Tests
00028 *****************************************************************************/
00029 
00030 TEST(ArrayMemCheckTests,constructors) {
00031     Array<int,4> array1 = Array<int,4>::Constant(3);
00032     EXPECT_EQ(3,array1[0]);
00033     EXPECT_EQ(3,array1[1]);
00034     EXPECT_EQ(3,array1[2]);
00035     EXPECT_EQ(3,array1[3]);
00036     Array<int,4> array2 = ArrayFactory<int,4>::Constant(4);
00037     EXPECT_EQ(4,array2[0]);
00038     EXPECT_EQ(4,array2[1]);
00039     EXPECT_EQ(4,array2[2]);
00040     EXPECT_EQ(4,array2[3]);
00041     Array<int,4> array3 = ConstantArray<int,4>(3);
00042     EXPECT_EQ(3,array3[0]);
00043     EXPECT_EQ(3,array3[1]);
00044     EXPECT_EQ(3,array3[2]);
00045     EXPECT_EQ(3,array3[3]);
00046 //    std::cout << "Constant array [fixed]  : " << array1 << std::endl;
00047     Array<int> darray1 = Array<int>::Constant(4,3);
00048     EXPECT_EQ(3,darray1[0]);
00049     EXPECT_EQ(3,darray1[1]);
00050     EXPECT_EQ(3,darray1[2]);
00051     EXPECT_EQ(3,darray1[3]);
00052 //    std::cout << "Constant array [dynamic]  : " << darray1 << std::endl;
00053 }
00054 
00055 TEST(ArrayMemCheckTests,copyConstructors) {
00056 
00057     Array<int,4> array1 = Array<int,4>::Constant(3);
00058     Array<int> darray1 = Array<int>::Constant(4,3);
00059 
00060     Array<int,4> array2(array1);
00061         Array<int> darray2(darray1);
00062 
00063     EXPECT_EQ(3,array2[0]);
00064     EXPECT_EQ(3,array2[1]);
00065     EXPECT_EQ(3,array2[2]);
00066     EXPECT_EQ(3,array2[3]);
00067 
00068     EXPECT_EQ(3,darray2[0]);
00069     EXPECT_EQ(3,darray2[1]);
00070     EXPECT_EQ(3,darray2[2]);
00071     EXPECT_EQ(3,darray2[3]);
00072 }
00073 
00074 TEST(ArrayMemCheckTests,blueprintAssignment) {
00075 
00076         Array<int,4> array;
00077         array = Array<int,4>::Constant(3);
00078 
00079     EXPECT_EQ(3,array[0]);
00080     EXPECT_EQ(3,array[1]);
00081     EXPECT_EQ(3,array[2]);
00082     EXPECT_EQ(3,array[3]);
00083 }
00084 TEST(ArrayMemCheckTests,commaInitialisation) {
00085 
00086         Array<int,4> array;
00087     Array<int> darray(4);
00088         array << 1,2,3,4;
00089         darray << 1,2,3,4;
00090 
00091     EXPECT_EQ(1,array[0]);
00092     EXPECT_EQ(2,array[1]);
00093     EXPECT_EQ(3,array[2]);
00094     EXPECT_EQ(4,array[3]);
00095 
00096     EXPECT_EQ(1,darray[0]);
00097     EXPECT_EQ(2,darray[1]);
00098     EXPECT_EQ(3,darray[2]);
00099     EXPECT_EQ(4,darray[3]);
00100 }
00101 TEST(ArrayMemCheckTests,Accessors) {
00102 
00103         int j;
00104         Array<int,4> array;
00105     Array<int> darray(4);
00106         array << 1,2,3,4;
00107         darray << 1,2,3,4;
00108 
00109         j = array[2];
00110         EXPECT_EQ(3,j);
00111         j = array.at(1);
00112         EXPECT_EQ(2,j);
00113         j = darray[2];
00114         EXPECT_EQ(3,j);
00115         j = darray.at(1);
00116         EXPECT_EQ(2,j);
00117 }
00118 
00119 TEST(ArrayMemCheckTests,rangeChecking) {
00120 
00121         Array<int,4> array = Array<int,4>::Constant(3);
00122     Array<int> darray = Array<int>::Constant(4,3);
00123         bool result = false;
00124         try {
00125         array.at(10);
00126     } catch ( StandardException &e ) {
00127         result = true;
00128 //        std::cout << "Fixed:\n" << e.what() << std::endl;
00129     }
00130     EXPECT_TRUE(result);
00131     result = false;
00132     try {
00133         darray.at(10);
00134     } catch ( StandardException &e ) {
00135         result = true;
00136 //        std::cout << "Dynamic:\n" << e.what() << std::endl;
00137     }
00138     EXPECT_TRUE(result);
00139 }
00140 
00141 TEST(ArrayMemCheckTests,iterators) {
00142 
00143     Array<int,4> array = Array<int,4>::Constant(3);
00144         Array<int,4>::iterator iterator;
00145         for ( iterator = array.begin(); iterator != array.end(); ++iterator ) {
00146                 EXPECT_EQ(3,*iterator);
00147         }
00148 }
00149 
00150 TEST(ArrayMemCheckTests,resize) {
00151     Array<int> darray = Array<int>::Constant(4,3);
00152     darray.resize(10);
00153     darray << 0,1,2,3,4,5,6,7,8,9;
00154     int i = 0;
00155     Array<int>::iterator iterator;
00156         for ( iterator = darray.begin(); iterator != darray.end(); ++iterator ) {
00157                 EXPECT_EQ(i,*iterator);
00158                 ++i;
00159         }
00160         darray.clear();
00161 }
00162 
00163 TEST(ArrayMemCheckTests,concepts) {
00164         typedef Array<char,6> ByteArray;
00165         ecl_compile_time_concept_check(ContainerConcept<ByteArray>);
00166         SUCCEED();
00167         // 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)
00168 }
00169 
00170 /*****************************************************************************
00171 ** Main program
00172 *****************************************************************************/
00173 
00174 int main(int argc, char **argv) {
00175 
00176     testing::InitGoogleTest(&argc,argv);
00177     return RUN_ALL_TESTS();
00178 }


ecl_containers
Author(s): Daniel Stonier
autogenerated on Sun Oct 5 2014 23:35:46