#include #include #include "Array2T.h" using namespace std; void PlaceTest(); void CopyTest(); void PreCopyTest( Array2T a, Array2T b); Array2T MakeRandomArray(); bool CompareArrays(const Array2T & a, const Array2T &b); int main() { CopyTest(); CopyTest(); CopyTest(); PlaceTest(); return 0; } void PlaceTest() { cout << endl; size_t rows{10}, cols{5}; Array2T data(rows, cols); size_t r, c; for(r = 0; r < data.Rows(); ++r) { for(c = 0; c < data.Cols(); ++c) { data[r,c] = r + c * 100; } } for(r = 0; r < data.Rows(); ++r) { for(c = 0; c < data.Cols(); ++c) { cout << setw(4) << data[r,c]; } cout << endl; } return; } // idea, force a copy constructor call before the compare void PreCopyTest( Array2T a, Array2T b){ CompareArrays(a, b); } void CopyTest() { cout << "In Copy Test" << endl; // test assignment operator sort of Array2T randomArray = MakeRandomArray(); // test copy constructor, sort of. Array2T copy1(randomArray); Array2T copy2; Array2T copy3(100,100); // test assignment operator copy2 = randomArray; copy3 = copy2; CompareArrays(randomArray, copy1); CompareArrays(randomArray, copy2); CompareArrays(copy1, copy2); CompareArrays(copy3, copy1); CompareArrays(copy3, randomArray); PreCopyTest(copy1, copy2); // hope this doesn't mess anything up. randomArray = randomArray; CompareArrays(randomArray, copy1); // now try a = b = c = d; copy2 = copy1 = randomArray = MakeRandomArray(); CompareArrays(randomArray, copy1); CompareArrays(randomArray, copy2); CompareArrays(copy1, copy2); cout << "Done with Copy Test " << endl; return; } Array2T MakeRandomArray(){ size_t rows, cols; rows = rand() % 10 + 2; cols = rows + 2 + rand() % 3; Array2T rv(rows,cols); for (rows = 0; rows < rv.Rows(); ++rows) { for(cols = 0; cols < rv.Cols(); ++cols) { rv[rows,cols] = rand() % 99 + 1; } } return rv; } bool CompareArrays(const Array2T & a, const Array2T & b){ if (a.Rows() != b.Rows() ) { cerr << "In Compare Arrays, the arrays have different row sizes" << endl; return false; } if (a.Cols() != b.Cols() ) { cerr << "In Compare Arrays, the arrays have different column sizes" << endl; return false; } size_t r,c; for(r = 0; r < a.Rows(); ++r) { for (c = 0; c < a.Cols(); ++c) { int x = a[r,c]; int y = b[r,c]; if (x != y) { cerr << "In Compare Arrays, elementes at (" << r << ", " << c << " are not the same " << endl; return false; } } } return true; }