Skip to content

Commit bea37de

Browse files
committed
Test edge cases of snapshot attribute
1 parent 62a4f81 commit bea37de

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

test/JSONTest.cpp

Lines changed: 83 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,84 @@ 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+
auto testRead = [](std::vector<size_t> const &requiredIterations) {
228+
Series readSeries(file, Access::READ_ONLY);
229+
size_t counter = 0;
230+
for (auto const &iteration : readSeries.readIterations())
231+
{
232+
REQUIRE(iteration.iterationIndex == requiredIterations[counter++]);
233+
}
234+
REQUIRE(counter == requiredIterations.size());
235+
};
236+
testRead(std::vector<size_t>{0});
237+
238+
{
239+
nlohmann::json series;
240+
{
241+
std::fstream fstream;
242+
fstream.open(file, std::ios_base::in);
243+
fstream >> series;
244+
}
245+
series["data"]["attributes"].erase("snapshot");
246+
auto &snapshot = series["data"]["attributes"]["snapshot"];
247+
snapshot["datatype"] = "VEC_ULONG";
248+
snapshot["value"] = std::vector{1, 2, 3, 4, 5};
249+
{
250+
std::fstream fstream;
251+
fstream.open(file, std::ios_base::out | std::ios_base::trunc);
252+
fstream << series;
253+
}
254+
}
255+
256+
testRead(std::vector<size_t>{1, 2, 3, 4, 5});
257+
}

0 commit comments

Comments
 (0)