arrays_mem_check.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Macros
00010 *****************************************************************************/
00011 
00012 #define ECL_MEM_CHECK_ARRAYS // Make sure we use mem checking arrays, not regular
00013 
00014 /*****************************************************************************
00015 ** Includes
00016 *****************************************************************************/
00017 
00018 #include <iostream>
00019 #include <gtest/gtest.h>
00020 #include "../../include/ecl/containers/array.hpp"
00021 
00022 /*****************************************************************************
00023 ** Using
00024 *****************************************************************************/
00025 
00026 using ecl::Array;
00027 using ecl::blueprints::ArrayFactory;
00028 using ecl::blueprints::ConstantArray;
00029 using ecl::StandardException;
00030 
00031 /*****************************************************************************
00032 ** Tests
00033 *****************************************************************************/
00034 
00035 TEST(ArrayMemCheckTests,constructors) {
00036     Array<int,4> array1 = Array<int,4>::Constant(3);
00037     EXPECT_EQ(3,array1[0]);
00038     EXPECT_EQ(3,array1[1]);
00039     EXPECT_EQ(3,array1[2]);
00040     EXPECT_EQ(3,array1[3]);
00041     Array<int,4> array2 = ArrayFactory<int,4>::Constant(4);
00042     EXPECT_EQ(4,array2[0]);
00043     EXPECT_EQ(4,array2[1]);
00044     EXPECT_EQ(4,array2[2]);
00045     EXPECT_EQ(4,array2[3]);
00046     Array<int,4> array3 = ConstantArray<int,4>(3);
00047     EXPECT_EQ(3,array3[0]);
00048     EXPECT_EQ(3,array3[1]);
00049     EXPECT_EQ(3,array3[2]);
00050     EXPECT_EQ(3,array3[3]);
00051 //    std::cout << "Constant array [fixed]  : " << array1 << std::endl;
00052     Array<int> darray1 = Array<int>::Constant(4,3);
00053     EXPECT_EQ(3,darray1[0]);
00054     EXPECT_EQ(3,darray1[1]);
00055     EXPECT_EQ(3,darray1[2]);
00056     EXPECT_EQ(3,darray1[3]);
00057 //    std::cout << "Constant array [dynamic]  : " << darray1 << std::endl;
00058 }
00059 
00060 TEST(ArrayMemCheckTests,copyConstructors) {
00061 
00062     Array<int,4> array1 = Array<int,4>::Constant(3);
00063     Array<int> darray1 = Array<int>::Constant(4,3);
00064 
00065     Array<int,4> array2(array1);
00066         Array<int> darray2(darray1);
00067 
00068     EXPECT_EQ(3,array2[0]);
00069     EXPECT_EQ(3,array2[1]);
00070     EXPECT_EQ(3,array2[2]);
00071     EXPECT_EQ(3,array2[3]);
00072 
00073     EXPECT_EQ(3,darray2[0]);
00074     EXPECT_EQ(3,darray2[1]);
00075     EXPECT_EQ(3,darray2[2]);
00076     EXPECT_EQ(3,darray2[3]);
00077 }
00078 
00079 TEST(ArrayMemCheckTests,blueprintAssignment) {
00080 
00081         Array<int,4> array;
00082         array = Array<int,4>::Constant(3);
00083 
00084     EXPECT_EQ(3,array[0]);
00085     EXPECT_EQ(3,array[1]);
00086     EXPECT_EQ(3,array[2]);
00087     EXPECT_EQ(3,array[3]);
00088 }
00089 TEST(ArrayMemCheckTests,commaInitialisation) {
00090 
00091         Array<int,4> array;
00092     Array<int> darray(4);
00093         array << 1,2,3,4;
00094         darray << 1,2,3,4;
00095 
00096     EXPECT_EQ(1,array[0]);
00097     EXPECT_EQ(2,array[1]);
00098     EXPECT_EQ(3,array[2]);
00099     EXPECT_EQ(4,array[3]);
00100 
00101     EXPECT_EQ(1,darray[0]);
00102     EXPECT_EQ(2,darray[1]);
00103     EXPECT_EQ(3,darray[2]);
00104     EXPECT_EQ(4,darray[3]);
00105 }
00106 TEST(ArrayMemCheckTests,Accessors) {
00107 
00108         int j;
00109         Array<int,4> array;
00110     Array<int> darray(4);
00111         array << 1,2,3,4;
00112         darray << 1,2,3,4;
00113 
00114         j = array[2];
00115         EXPECT_EQ(3,j);
00116         j = array.at(1);
00117         EXPECT_EQ(2,j);
00118         j = darray[2];
00119         EXPECT_EQ(3,j);
00120         j = darray.at(1);
00121         EXPECT_EQ(2,j);
00122 }
00123 
00124 TEST(ArrayMemCheckTests,rangeChecking) {
00125 
00126         Array<int,4> array = Array<int,4>::Constant(3);
00127     Array<int> darray = Array<int>::Constant(4,3);
00128         bool result = false;
00129         try {
00130         array.at(10);
00131     } catch ( StandardException &e ) {
00132         result = true;
00133 //        std::cout << "Fixed:\n" << e.what() << std::endl;
00134     }
00135     EXPECT_TRUE(result);
00136     result = false;
00137     try {
00138         darray.at(10);
00139     } catch ( StandardException &e ) {
00140         result = true;
00141 //        std::cout << "Dynamic:\n" << e.what() << std::endl;
00142     }
00143     EXPECT_TRUE(result);
00144 }
00145 
00146 TEST(ArrayMemCheckTests,iterators) {
00147 
00148     Array<int,4> array = Array<int,4>::Constant(3);
00149         Array<int,4>::iterator iterator;
00150         for ( iterator = array.begin(); iterator != array.end(); ++iterator ) {
00151                 EXPECT_EQ(3,*iterator);
00152         }
00153 }
00154 
00155 TEST(ArrayMemCheckTests,resize) {
00156     Array<int> darray = Array<int>::Constant(4,3);
00157     darray.resize(10);
00158     darray << 0,1,2,3,4,5,6,7,8,9;
00159     int i = 0;
00160     Array<int>::iterator iterator;
00161         for ( iterator = darray.begin(); iterator != darray.end(); ++iterator ) {
00162                 EXPECT_EQ(i,*iterator);
00163                 ++i;
00164         }
00165         darray.clear();
00166 }
00167 
00168 TEST(ArrayMemCheckTests,bufferRuns) {
00169     int *istart = NULL;
00170     Array<int,4> array_ok; array_ok << 1, 2, 3, 4;
00171     Array<int,4> array_under; array_under << 1, 2, 3, 4;
00172     Array<int,4> array_over; array_over << 1, 2, 3, 4;
00173 
00174 //    std::cout << "Fixed Arrays" << std::endl;
00175 //    std::cout << "Array_under/over/ok: " << array_ok << std::endl;
00176 //
00177 
00178 //    std::cout << "Underrunning array_under." << std::endl;
00179     istart = &array_under[0];
00180     for ( unsigned int i = 0; i < 3; ++i ) {
00181         *(istart-i) = i;
00182     }
00183 
00184 //    std::cout << "Overrunning array_over." << std::endl;
00185     istart = &array_over[0];
00186     for ( unsigned int i = 0; i < array_over.size() + 1; ++i ) {
00187         *(istart+i) = i;
00188     }
00189 //
00190 //    std::cout << "Buffer results: " << std::endl;
00191     EXPECT_TRUE(array_under.bufferUnderRun());
00192     EXPECT_FALSE(array_under.bufferOverRun());
00193 
00194     EXPECT_TRUE(array_over.bufferOverRun());
00195     EXPECT_FALSE(array_over.bufferUnderRun());
00196 
00197     EXPECT_FALSE(array_ok.bufferOverRun());
00198     EXPECT_FALSE(array_ok.bufferUnderRun());
00199 
00200 //    std::cout << "Dynamic Arrays" << std::endl;
00201 //    std::cout << std::endl;
00202 //
00203     Array<int> darray_ok(4); darray_ok << 1, 2, 3, 4;
00204     Array<int> darray_under(4); darray_under << 1, 2, 3, 4;
00205     Array<int> darray_over(4); darray_over << 1, 2, 3, 4;
00206 
00207 //    std::cout << "Array_under/over/ok: " << darray_ok << std::endl;
00208 //
00209 //    std::cout << "Underrunning array_under." << std::endl;
00210     istart = &darray_under[0];
00211     for ( unsigned int i = 0; i < darray_under.size()+1; ++i ) {
00212         *(istart+i-1) = i;
00213     }
00214 //
00215 //    std::cout << "Overrunning array_over." << std::endl;
00216     istart = &darray_over[0];
00217     for ( unsigned int i = 0; i < darray_over.size()+1; ++i ) {
00218         *(istart+i) = i;
00219     }
00220 
00221     EXPECT_TRUE(darray_under.bufferUnderRun());
00222     EXPECT_FALSE(darray_under.bufferOverRun());
00223 
00224     EXPECT_TRUE(darray_over.bufferOverRun());
00225     EXPECT_FALSE(darray_over.bufferUnderRun());
00226 
00227     EXPECT_FALSE(darray_ok.bufferOverRun());
00228     EXPECT_FALSE(darray_ok.bufferUnderRun());
00229 }
00230 
00231 /*****************************************************************************
00232 ** Main program
00233 *****************************************************************************/
00234 
00235 int main(int argc, char **argv) {
00236 
00237     testing::InitGoogleTest(&argc,argv);
00238     return RUN_ALL_TESTS();
00239 }
00240 


ecl_containers
Author(s): Daniel Stonier
autogenerated on Mon Jul 3 2017 02:21:42