8#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
9#include <doctest/doctest.h>
11#include "include/ModelArray.hpp"
15TEST_SUITE_BEGIN(
"ModelArray");
17TEST_CASE(
"Two dimensional data access test")
19 ModelArray::MultiDim dims2 = {15, 25};
23 ModelArray check1d = ModelArray::TwoDField();
25 REQUIRE(check1d.nDimensions() == 2);
29 for (
size_t i = 0; i < dims2[0] * dims2[1]; ++i) {
36 REQUIRE(check1d(x+1, y) - check1d(x, y) == 1);
38 REQUIRE(check1d(x, y+1) - check1d(x, y) == dims2[0]);
40 REQUIRE(check1d(dims2[0]-1, dims2[1]-1) == dims2[0] * dims2[1] - 1);
44TEST_CASE(
"Higher dimensional indexing")
47 size_t arrayLen = dimLen * dimLen * dimLen * dimLen;
48 ModelArray::MultiDim dims4 = {dimLen, dimLen, dimLen, dimLen};
51 ModelArray check4d = ModelArray::FourDField();
53 REQUIRE(check4d.nDimensions() == 4);
54 REQUIRE(check4d.size() == dimLen * dimLen * dimLen * dimLen);
56 for (
size_t i = 0; i < check4d.size(); ++i) {
61 REQUIRE(check4d(4, 7, 2, 5) == 5274);
64 for (
size_t i = 0; i < check4d.size(); ++i) {
67 REQUIRE(check4d(4, 7, 2, 5) == 0);
69 double* data =
new double[arrayLen];
70 for (
size_t i = 0; i < arrayLen; ++i) {
74 check4d.setData(data);
76 REQUIRE(check4d(4, 7, 2, 5) == 5274);
79 for (
size_t i = 0; i < check4d.size(); ++i) {
82 REQUIRE(check4d(4, 7, 2, 5) == 0);
84 std::vector<double> vData = std::vector<double>(arrayLen);
85 for (
size_t i = 0; i < arrayLen; ++i) {
89 check4d.setData(vData.data());
91 REQUIRE(check4d(4, 7, 2, 5) == 5274);
94 REQUIRE(check4d[{5, 7, 2, 5}] == 5275);
98TEST_CASE(
"Higher dimensional indexing 2")
100 ModelArray::MultiDim dims4 = {3, 5, 7, 11};
101 size_t totalSize = dims4[0] * dims4[1] * dims4[2] * dims4[3];
104 FourDField primorial = ModelArray::FourDField();
106 REQUIRE(primorial.nDimensions() == 4);
107 REQUIRE(primorial.size() == totalSize);
109 for (
size_t i = 0; i < primorial.size(); ++i) {
118 size_t target = i + dims4[0] * (j + dims4[1] * (k + dims4[2] * (l)));
120 REQUIRE(primorial[target] == target);
122 REQUIRE(primorial(i, j, k, l) == target);
128TEST_CASE(
"Copy constructor and copy assignment operator")
133 ModelArray src = ModelArray::TwoDField();
134 for (
int i = 0; i < n * n; ++i) {
139 ModelArray copyConstructor(src);
140 REQUIRE(copyConstructor(2, 3) == src(2, 3));
143 ModelArray copyAssignment = ModelArray::TwoDField();
144 copyAssignment = src;
145 REQUIRE(copyAssignment(2, 3) == src(2, 3));
150TEST_CASE(
"Instance setDimensions sets instance dimensions")
152 ZUField uu = ModelArray::ZUField();
153 ModelArray::MultiDim udim = {5, 5};
154 uu.setDimensions(udim);
155 REQUIRE(uu.size() == udim[0] * udim[1]);
156 REQUIRE(uu.nDimensions() == 2);
157 REQUIRE(uu.dimensions() == udim);
161TEST_CASE(
"Arithmetic tests")
172 OneDField sum = lhs + rhs;
173 REQUIRE(sum[0] == 12.);
174 REQUIRE(sum[1] == 5.);
175 OneDField difference = lhs - rhs;
176 REQUIRE(difference[0] == 6.);
177 REQUIRE(difference[1] == 15.);
178 OneDField product = lhs * rhs;
179 REQUIRE(product[0] == 27.);
180 REQUIRE(product[1] == -50.);
181 OneDField quotient = lhs / rhs;
182 REQUIRE(quotient[0] == 3.);
183 REQUIRE(quotient[1] == -2.);
184 OneDField negative = -rhs;
185 REQUIRE(negative[0] == -3);
186 REQUIRE(negative[1] == 5);
191 REQUIRE(sum[0] == 12);
192 REQUIRE(sum[1] == 13);
194 REQUIRE(sum[0] == 7);
195 REQUIRE(sum[1] == -1);
196 difference = lhs - three;
197 REQUIRE(difference[0] == 6);
198 REQUIRE(difference[1] == 7);
199 difference = four - rhs;
200 REQUIRE(difference[0] == 1);
201 REQUIRE(difference[1] == 9);
202 product = lhs * three;
203 REQUIRE(product[0] == 27);
204 REQUIRE(product[1] == 30);
205 product = four * rhs;
206 REQUIRE(product[0] == 12);
207 REQUIRE(product[1] == -20);
208 quotient = lhs / three;
209 REQUIRE(quotient[0] == 3);
210 REQUIRE(quotient[1] == (10. / 3.));
211 quotient = four / rhs;
212 REQUIRE(quotient[0] == (4. / 3.));
213 REQUIRE(quotient[1] == (4. / -5.));
217 REQUIRE(lhs[0] == 12);
218 REQUIRE(lhs[1] == 5);
220 REQUIRE(lhs[0] == 9);
221 REQUIRE(lhs[1] == 10);
223 REQUIRE(lhs[0] == 27);
224 REQUIRE(lhs[1] == -50);
226 REQUIRE(lhs[0] == 9);
227 REQUIRE(lhs[1] == 10);
230 REQUIRE(lhs[0] == 12);
231 REQUIRE(lhs[1] == 13);
233 REQUIRE(lhs[0] == 8);
234 REQUIRE(lhs[1] == 9);
236 REQUIRE(lhs[0] == 24);
237 REQUIRE(lhs[1] == 27);
239 REQUIRE(lhs[0] == 6);
240 REQUIRE(lhs[1] == 6.75);
242 OneDField fill = ModelArray::OneDField();
243 double filldub = 5.2354;
246 REQUIRE(fill[0] == filldub);
247 REQUIRE(fill[1] == filldub);
252TEST_CASE(
"Location from index")
254 const size_t nx = 31;
255 const size_t ny = 37;
256 const size_t nz = 41;
265 REQUIRE(loc[0] == x);
266 REQUIRE(loc[1] == y);
267 REQUIRE(loc[2] == z);
272TEST_CASE(
"zIndexAndLayer")
274 const size_t nx = 29;
275 const size_t ny = 23;
276 const size_t nz = 11;
280 ThreeDField threeD(ModelArray::Type::THREED);
285 for (
size_t i = 0; i < nx; ++i) {
286 for (
size_t j = 0; j < ny; ++j) {
287 for (
size_t k = 0; k < nz; ++k) {
288 threeD(i, j, k) = k + mul * (j + mul * (i));
297 REQUIRE(threeD.zIndexAndLayer(ind, z) == threeD(x, y, z));
size_t indexFromLocation(const MultiDim &loc) const
Returns the index for a given set of multi-dimensional location for this array's type.
static void setDimensions(Type, const MultiDim &)
Sets the number and size of the dimensions of a specified type of ModelArray.
MultiDim locationFromIndex(size_t index) const
Returns the multi-dimensional location for a given index for this array's type.