Skip to content

Commit e491869

Browse files
committed
Test edge cases of snapshot attribute
1 parent 8dcf776 commit e491869

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

test/JSONTest.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "openPMD/auxiliary/JSON.hpp"
22
#include "openPMD/auxiliary/JSON_internal.hpp"
3+
#include "openPMD/openPMD.hpp"
34

45
#include <catch2/catch.hpp>
56

7+
#include <fstream>
68
#include <variant>
79

810
using namespace openPMD;
@@ -172,3 +174,88 @@ TEST_CASE("json_merging", "auxiliary")
172174
json::merge(defaultVal, overwrite) ==
173175
json::parseOptions(expect, false).config.dump());
174176
}
177+
178+
/*
179+
* This tests two things about the /data/snapshot attribute:
180+
*
181+
* 1) Reading a variable-based series without the snapshot attribute should be
182+
* possible by assuming a default /data/snapshot = 0.
183+
* 2) The snapshot attribute might be a vector of iterations. The Read API
184+
* should then return the same iteration multiple times, with different
185+
* indices.
186+
*
187+
* Such files are currently not created by the openPMD-api (the API currently
188+
* supports creating a variable-based series with a scalar snapshot attribute).
189+
* But the standard will allow both options above, so reading should at least
190+
* be possible.
191+
* This test creates a variable-based JSON series and then uses the nlohmann
192+
* json library to modifiy the resulting series for testing purposes.
193+
*/
194+
TEST_CASE("variableBasedModifiedSnapshot", "[auxiliary]")
195+
{
196+
constexpr auto file = "../samples/variableBasedModifiedSnapshot.json";
197+
{
198+
Series writeSeries(file, Access::CREATE);
199+
writeSeries.setIterationEncoding(IterationEncoding::variableBased);
200+
REQUIRE(
201+
writeSeries.iterationEncoding() ==
202+
IterationEncoding::variableBased);
203+
auto iterations = writeSeries.writeIterations();
204+
auto iteration = iterations[10];
205+
auto E_z = iteration.meshes["E"]["x"];
206+
E_z.resetDataset({Datatype::INT, {1}});
207+
E_z.makeConstant(72);
208+
209+
iteration.close();
210+
}
211+
212+
{
213+
nlohmann::json series;
214+
{
215+
std::fstream fstream;
216+
fstream.open(file, std::ios_base::in);
217+
fstream >> series;
218+
}
219+
series["data"]["attributes"].erase("snapshot");
220+
{
221+
std::fstream fstream;
222+
fstream.open(file, std::ios_base::out | std::ios_base::trunc);
223+
fstream << series;
224+
}
225+
}
226+
227+
/*
228+
* Need generic capture here since the compilers are being
229+
* annoying otherwise.
230+
*/
231+
auto testRead = [&](std::vector<size_t> const &requiredIterations) {
232+
Series readSeries(file, Access::READ_ONLY);
233+
size_t counter = 0;
234+
for (auto const &iteration : readSeries.readIterations())
235+
{
236+
REQUIRE(iteration.iterationIndex == requiredIterations[counter++]);
237+
}
238+
REQUIRE(counter == requiredIterations.size());
239+
};
240+
testRead(std::vector<size_t>{0});
241+
242+
{
243+
nlohmann::json series;
244+
{
245+
std::fstream fstream;
246+
fstream.open(file, std::ios_base::in);
247+
fstream >> series;
248+
}
249+
series["data"]["attributes"].erase("snapshot");
250+
auto &snapshot = series["data"]["attributes"]["snapshot"];
251+
snapshot["datatype"] = "VEC_ULONG";
252+
snapshot["value"] = std::vector{1, 2, 3, 4, 5};
253+
{
254+
std::fstream fstream;
255+
fstream.open(file, std::ios_base::out | std::ios_base::trunc);
256+
fstream << series;
257+
}
258+
}
259+
260+
testRead(std::vector<size_t>{1, 2, 3, 4, 5});
261+
}

0 commit comments

Comments
 (0)