1 |
// $Id$ |
2 |
/* |
3 |
***************************************************************************** |
4 |
* * |
5 |
* COPYRIGHT ACcESS - All Rights Reserved * |
6 |
* * |
7 |
* This software is the property of ACcESS. No part of this code * |
8 |
* may be copied in any form or by any means without the expressed written * |
9 |
* consent of ACcESS. Copying, use or modification of this software * |
10 |
* by any unauthorised person is illegal unless that person has a software * |
11 |
* license agreement with ACcESS. * |
12 |
* * |
13 |
***************************************************************************** |
14 |
*/ |
15 |
|
16 |
#include "escript/Data/DataBlocks2D.h" |
17 |
#include "esysUtils/EsysException.h" |
18 |
|
19 |
#include "DataBlocks2DTestCase.h" |
20 |
|
21 |
#include <iostream> |
22 |
|
23 |
using namespace std; |
24 |
using namespace CppUnitTest; |
25 |
using namespace escript; |
26 |
using namespace esysUtils; |
27 |
|
28 |
void DataBlocks2DTestCase::setUp() { |
29 |
// |
30 |
// This is called before each test is run |
31 |
|
32 |
} |
33 |
|
34 |
void DataBlocks2DTestCase::tearDown() { |
35 |
// |
36 |
// This is called after each test has been run |
37 |
|
38 |
} |
39 |
|
40 |
void DataBlocks2DTestCase::testAll() { |
41 |
|
42 |
cout << endl; |
43 |
|
44 |
cout << "\tTest DataBlocks2D constructor for various dimension values:" << endl; |
45 |
|
46 |
{ |
47 |
cout << "\t\tnumRows = 1, numCols = 1, blockSize = 20." << endl; |
48 |
int numRows=1; |
49 |
int numCols=1; |
50 |
int blockSize=20; |
51 |
DataBlocks2D myData(numRows,numCols,blockSize); |
52 |
int i = numRows-1; |
53 |
int j = numCols-1; |
54 |
assert(myData.index(i,j) == (i*numCols+j)*blockSize); |
55 |
assert(myData.size() == numRows*numCols*blockSize); |
56 |
} |
57 |
|
58 |
{ |
59 |
cout << "\t\tnumRows = 3, numCols = 5, blockSize = 20." << endl; |
60 |
int numRows=3; |
61 |
int numCols=5; |
62 |
int blockSize=20; |
63 |
DataBlocks2D myData(numRows,numCols,blockSize); |
64 |
int i = numRows-1; |
65 |
int j = numCols-1; |
66 |
assert(myData.index(i,j) == (i*numCols+j)*blockSize); |
67 |
assert(myData.size() == numRows*numCols*blockSize); |
68 |
} |
69 |
|
70 |
{ |
71 |
cout << "\t\tnumRows = 3, numCols = 5, blockSize = 1." << endl; |
72 |
int numRows=3; |
73 |
int numCols=5; |
74 |
int blockSize=1; |
75 |
DataBlocks2D myData(numRows,numCols,blockSize); |
76 |
int i = numRows-1; |
77 |
int j = numCols-1; |
78 |
assert(myData.index(i,j) == (i*numCols+j)*blockSize); |
79 |
assert(myData.size() == numRows*numCols*blockSize); |
80 |
} |
81 |
|
82 |
{ |
83 |
cout << "\t\tnumRows = 1, numCols = 1, blockSize = 1." << endl; |
84 |
int numRows=1; |
85 |
int numCols=1; |
86 |
int blockSize=1; |
87 |
DataBlocks2D myData(numRows,numCols,blockSize); |
88 |
int i = numRows-1; |
89 |
int j = numCols-1; |
90 |
assert(myData.index(i,j) == (i*numCols+j)*blockSize); |
91 |
assert(myData.size() == numRows*numCols*blockSize); |
92 |
} |
93 |
|
94 |
{ |
95 |
cout << "\tTest DataBlocks2D.index and DataBlocks2D operator[] for blockSize = 3." << endl; |
96 |
int numRows=10; |
97 |
int numCols=8; |
98 |
int blockSize=3; |
99 |
DataBlocks2D myData(numRows,numCols,blockSize); |
100 |
int val=0; |
101 |
for (int i=0; i<numRows; i++) { |
102 |
for (int j=0; j<numCols; j++) { |
103 |
for (int k=0; k<blockSize; k++) { |
104 |
myData[myData.index(i,j)+k] = val; |
105 |
val++; |
106 |
} |
107 |
} |
108 |
} |
109 |
val=0; |
110 |
for (int i=0; i<numRows; i++) { |
111 |
for (int j=0; j<numCols; j++) { |
112 |
for (int k=0; k<blockSize; k++) { |
113 |
assert(myData[myData.index(i,j)+k] == val); |
114 |
val++; |
115 |
} |
116 |
} |
117 |
} |
118 |
} |
119 |
|
120 |
{ |
121 |
cout << "\tTest DataBlocks2D for blockSize = 0." << endl; |
122 |
// |
123 |
// every attempted access for a 0 size block should cause an exception |
124 |
int numRows=10; |
125 |
int numCols=8; |
126 |
int blockSize=0; |
127 |
int exceptionCount=0; |
128 |
DataBlocks2D myData(numRows,numCols,blockSize); |
129 |
for (int i=0;i<numRows;i++) { |
130 |
for (int j=0;j<numCols;j++) { |
131 |
try { |
132 |
myData[myData.index(i,j)]; |
133 |
assert(false); |
134 |
} |
135 |
catch(EsysException& e) { |
136 |
++exceptionCount; |
137 |
assert(true); |
138 |
} |
139 |
} |
140 |
} |
141 |
assert(exceptionCount == numRows*numCols); |
142 |
} |
143 |
|
144 |
{ |
145 |
cout << "\tTest getNumRows, getNumCols and getBlockSize." << endl; |
146 |
int numRows=1; |
147 |
int numCols=1; |
148 |
int blockSize=1; |
149 |
DataBlocks2D myData(numRows,numCols,blockSize); |
150 |
assert(myData.getNumRows() == numRows); |
151 |
assert(myData.getNumCols() == numCols); |
152 |
assert(myData.getBlockSize() == blockSize); |
153 |
} |
154 |
|
155 |
{ |
156 |
cout << "\tTest resize." << endl; |
157 |
int numRows=1; |
158 |
int numCols=1; |
159 |
int blockSize=1; |
160 |
DataBlocks2D myData; |
161 |
myData.resize(numRows,numCols,blockSize); |
162 |
assert(myData.getNumRows() == numRows); |
163 |
assert(myData.getNumCols() == numCols); |
164 |
assert(myData.getBlockSize() == blockSize); |
165 |
} |
166 |
|
167 |
{ |
168 |
cout << "\tTest = operator, swap, and copy constructor." << endl; |
169 |
DataBlocks2D myData1; |
170 |
DataBlocks2D myData2(1, 1, 1); |
171 |
int val=0; |
172 |
for (int i=0; i<1; i++) { |
173 |
for (int j=0; j<1; j++) { |
174 |
for (int k=0; k<1; k++) { |
175 |
myData2[myData2.index(i,j)+k] = val; |
176 |
val++; |
177 |
} |
178 |
} |
179 |
} |
180 |
myData1 = myData2; |
181 |
for (int i=0; i<myData1.getNumRows(); i++) { |
182 |
for (int j=0; j<myData1.getNumCols(); j++) { |
183 |
assert(myData1(i,j) == myData2(i,j)); |
184 |
} |
185 |
} |
186 |
} |
187 |
|
188 |
{ |
189 |
cout << "\tTest DOASSERT exception." << endl; |
190 |
DataBlocks2D myData; |
191 |
try { |
192 |
myData.index(1,2); |
193 |
assert(false); |
194 |
} |
195 |
catch (EsysException& e) { |
196 |
assert(true); |
197 |
} |
198 |
} |
199 |
|
200 |
} |
201 |
|
202 |
TestSuite* DataBlocks2DTestCase::suite () |
203 |
{ |
204 |
// |
205 |
// create the suite of tests to perform. |
206 |
TestSuite *testSuite = new TestSuite ("DataBlocks2DTestCase"); |
207 |
|
208 |
testSuite->addTest (new TestCaller< DataBlocks2DTestCase>("testAll",&DataBlocks2DTestCase::testAll)); |
209 |
return testSuite; |
210 |
} |