Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TwoDArray_UnitTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Teuchos_TwoDArray.hpp"
12 
13 
14 
15 namespace Teuchos{
16 
18  TwoDArray<int> simpleArray(2,2);
19  simpleArray(0,0) =1;
20  simpleArray(0,1) =2;
21  simpleArray(1,0) =3;
22  simpleArray(1,1) =4;
23  return simpleArray;
24 }
25 
26 
30 TEUCHOS_UNIT_TEST(Teuchos_TwoDArrays, simpleTest){
31  TwoDArray<int> simpleArray(3,2);
32  simpleArray[0][0] =1;
33  simpleArray[0][1] =2;
34  simpleArray[1][0] =3;
35  simpleArray[1][1] =4;
36  simpleArray[2][0] =5;
37  simpleArray[2][1] =6;
38 
39  TEST_EQUALITY_CONST(simpleArray[0][0],1)
40  TEST_EQUALITY_CONST(simpleArray[0][1],2)
41  TEST_EQUALITY_CONST(simpleArray[1][0],3)
42  TEST_EQUALITY_CONST(simpleArray[1][1],4)
43  TEST_EQUALITY_CONST(simpleArray[2][0],5)
44  TEST_EQUALITY_CONST(simpleArray[2][1],6)
45 
46  TwoDArray<int> const& simpleArrayConst = simpleArray;
47 
48  TEST_EQUALITY_CONST(simpleArrayConst[0][0],1)
49  TEST_EQUALITY_CONST(simpleArrayConst[0][1],2)
50  TEST_EQUALITY_CONST(simpleArrayConst[1][0],3)
51  TEST_EQUALITY_CONST(simpleArrayConst[1][1],4)
52  TEST_EQUALITY_CONST(simpleArrayConst[2][0],5)
53  TEST_EQUALITY_CONST(simpleArrayConst[2][1],6)
54 
55  TEST_EQUALITY_CONST(simpleArray(0,0),1)
56  TEST_EQUALITY_CONST(simpleArray(0,1),2)
57  TEST_EQUALITY_CONST(simpleArray(1,0),3)
58  TEST_EQUALITY_CONST(simpleArray(1,1),4)
59  TEST_EQUALITY_CONST(simpleArray(2,0),5)
60  TEST_EQUALITY_CONST(simpleArray(2,1),6)
61 
62  TEST_EQUALITY_CONST(simpleArray.getNumRows(), 3)
63  TEST_EQUALITY_CONST(simpleArray.getNumCols(), 2)
64  Array<int> oneDArray = tuple<int>(1,2,3,4,5,6);
65  TEST_COMPARE_ARRAYS(oneDArray, simpleArray.getDataArray())
66 
67 }
68 
69 TEUCHOS_UNIT_TEST(Teuchos_TwoDArrays, stringFunctions){
70  TwoDArray<int> simpleArray = getSimpleTestTwoDArray();
71  std::string stringRep = TwoDArray<int>::toString(simpleArray);
72  TwoDArray<int> convertedArray = TwoDArray<int>::fromString(stringRep);
73  TEST_EQUALITY(simpleArray, convertedArray)
74 
75  std::string badStringRep = "4x4:{1.0,1.0}";
78 }
79 
80 TEUCHOS_UNIT_TEST(Teuchos_TwoDArrays, emptyTest){
81  TwoDArray<int> emptyArray;
82  TEST_EQUALITY_CONST(emptyArray.getNumRows(), 0)
83  TEST_EQUALITY_CONST(emptyArray.getNumCols(), 0)
84  TEST_EQUALITY_CONST(emptyArray.getDataArray().size(), 0)
85  TEST_ASSERT(emptyArray.isEmpty());
86 }
87 
88 TEUCHOS_UNIT_TEST(Teuchos_TwoDArrays, streamTests){
89  TwoDArray<int> simpleArray = getSimpleTestTwoDArray();
90  std::stringstream ss;
91  ss << simpleArray;
92  TwoDArray<int> readArray;
93  std::istringstream instream(ss.str());
94  instream >> readArray;
95  TEST_EQUALITY(simpleArray, readArray);
96 }
97 
98 TEUCHOS_UNIT_TEST(Teuchos_TwoDArray, clearTest){
99  TwoDArray<int> simpleArray = getSimpleTestTwoDArray();
100 
101  simpleArray.clear();
102  TEST_ASSERT(simpleArray.isEmpty());
103 }
104 
105 TEUCHOS_UNIT_TEST(Teuchos_TwoDArray, resizeTest){
106  TwoDArray<int> simpleArray = getSimpleTestTwoDArray();
107 
108  simpleArray.resizeRows(4);
109  TEST_EQUALITY_CONST(simpleArray.getNumRows(), 4);
110  TEST_EQUALITY_CONST(simpleArray.getNumCols(), 2);
111  TEST_EQUALITY_CONST(simpleArray(3,1), 0);
112  TEST_EQUALITY_CONST(simpleArray(1,1), 4);
113 
114  simpleArray.resizeRows(2);
115  TEST_EQUALITY_CONST(simpleArray.getNumRows(), 2);
116  TEST_EQUALITY_CONST(simpleArray.getNumCols(), 2);
117 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
118  TEST_THROW(simpleArray(3,1), RangeError);
119 #endif
120  TEST_EQUALITY_CONST(simpleArray(1,1), 4);
121 
122  simpleArray.resizeCols(4);
123  TEST_EQUALITY_CONST(simpleArray.getNumCols(), 4);
124  TEST_EQUALITY_CONST(simpleArray.getNumRows(), 2);
125  TEST_EQUALITY_CONST(simpleArray(1,3), 0);
126  TEST_EQUALITY_CONST(simpleArray(1,1), 4);
127 
128  simpleArray.resizeCols(2);
129  TEST_EQUALITY_CONST(simpleArray.getNumCols(), 2);
130  TEST_EQUALITY_CONST(simpleArray.getNumRows(), 2);
131 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
132  TEST_THROW(simpleArray(1,3), RangeError);
133 #endif
134  TEST_EQUALITY_CONST(simpleArray(1,1), 4);
135 
136 }
137 
138 TEUCHOS_UNIT_TEST(Teuchos_TwoDArray, symmetryTest){
139  TwoDArray<int> simpleArray = getSimpleTestTwoDArray();
140  TEST_ASSERT(!simpleArray.isSymmetrical());
141  simpleArray.setSymmetrical(true);
142  TEST_ASSERT(simpleArray.isSymmetrical());
143 
144 }
145 
146 TEUCHOS_UNIT_TEST(Teuchos_TwoDArray, symmetrySerialization){
147  TwoDArray<int> simpleArray = getSimpleTestTwoDArray();
148  simpleArray.setSymmetrical(true);
149  std::string arrayString = TwoDArray<int>::toString(simpleArray);
150  TwoDArray<int> readIn = TwoDArray<int>::fromString(arrayString);
151  TEST_ASSERT(readIn.isSymmetrical());
152 }
153 
154 
155 } //namespace Teuchos
156 
A thin wrapper around the Teuchos Array class that allows for 2 dimensional arrays.
#define TEST_ASSERT(v1)
Assert the given statement is true.
const Array< T > & getDataArray() const
Returns the 1D array that is backing this TwoDArray.
void setSymmetrical(bool symmetrical)
Sets whether or not the the TwoDArray should be interpurted as symetric.
void clear()
delets all the entries from the TwoDArray
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
static std::string toString(const TwoDArray< T > array)
Converts a given TwoDArray to a valid string representation.
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
A thin wrapper around the Array class which causes it to be interpreted as a 2D Array.
TwoDArray< int > getSimpleTestTwoDArray()
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
size_type getNumCols() const
returns the number of columns in the TwoDArray.
void resizeCols(size_type numberOfCols)
Changes the number of rows in the matrix.
void resizeRows(size_type numberOfRows)
Changes the number of rows in the matrix.
size_type getNumRows() const
returns the number of rows in the TwoDArray.
#define TEST_COMPARE_ARRAYS(a1, a2)
Assert that a1.size()==a2.size() and a[i]==b[i], i=0....
size_type size() const
Range error exception class.
static TwoDArray< T > fromString(const std::string &string)
Converts a valid string to it&#39;s corresponding TwoDArray.
bool isSymmetrical() const
A simple flag indicating whether or not this TwoDArray should be interpurted as symmetrical.