Skip to content

Commit

Permalink
Extensive testing
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Dec 13, 2024
1 parent be3543a commit 7f81e62
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/IO/JSON/JSONIOHandlerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,8 @@ void JSONIOHandlerImpl::createDataset(
default:
break;
}
if (parameter.extent.size() == 1 &&
parameter.extent[0] == Dataset::UNDEFINED_EXTENT)
{
dset["data"] = std::vector<int>(0);
}
else
if (parameter.extent.size() != 1 ||
parameter.extent[0] != Dataset::UNDEFINED_EXTENT)
{
// TOML does not support nulls, so initialize with zero
dset["data"] = initializeNDArray(
Expand Down
191 changes: 191 additions & 0 deletions test/JSONTest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "openPMD/auxiliary/JSON.hpp"
#include "openPMD/Error.hpp"
#include "openPMD/auxiliary/JSON_internal.hpp"
#include "openPMD/helper/list_series.hpp"
#include "openPMD/openPMD.hpp"

#include <catch2/catch.hpp>
Expand All @@ -9,6 +10,7 @@
#include <fstream>
#include <sstream>
#include <string>
#include <tuple>
#include <variant>
#include <vector>

Expand Down Expand Up @@ -306,3 +308,192 @@ TEST_CASE("variableBasedModifiedSnapshot", "[auxiliary]")

testRead(std::vector<size_t>{1, 2, 3, 4, 5});
}

namespace auxiliary
{
template <typename Callable, typename AccumulatorTuple>
void test_matrix_impl(Callable &callable, AccumulatorTuple tuple)
{
std::apply(callable, std::move(tuple));
}

template <
typename Callable,
typename AccumulatorTuple,
typename Arg,
typename... Args>
void test_matrix_impl(
Callable &callable,
AccumulatorTuple tuple,
std::vector<Arg> const &arg,
std::vector<Args> const &...args)

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable args is not used.

Check notice

Code scanning / CodeQL

Unused static variable Note test

Static variable args is never read.
{
for (auto &val : arg)
{
test_matrix_impl(
callable, std::tuple_cat(tuple, std::tuple<Arg>{val}), args...);
}
}

template <typename Callable, typename... Args>
void test_matrix(Callable &&callable, std::vector<Args> const &...matrix)
{
test_matrix_impl(callable, std::tuple<>(), matrix...);
}
} // namespace auxiliary

void json_short_modes(

Check warning

Code scanning / CodeQL

Poorly documented large function Warning test

Poorly documented function: fewer than 2% comments for a function of 143 lines.
std::optional<bool> short_attributes,
std::optional<bool> template_datasets,
std::string const &standardVersion,
std::string const &backend,
unsigned int *name_counter)
{
nlohmann::json config = nlohmann::json::object();
if (short_attributes.has_value())
{
config[backend]["attribute"]["mode"] =
*short_attributes ? "short" : "long";
}
if (template_datasets.has_value())
{
config[backend]["dataset"]["mode"] =
*template_datasets ? "template" : "dataset";
}
std::string name = "../samples/json_short_modes/test" +
std::to_string((*name_counter)++) + "." + backend;

auto config_str = [&]() {
std::stringstream res;
res << config;
return res.str();
}();
Series output(name, Access::CREATE, config_str);
output.setOpenPMD(standardVersion);
auto iteration = output.writeIterations()[0];

auto default_configured = iteration.meshes["default_configured"];
Dataset ds1(Datatype::INT, {5});
default_configured.resetDataset(ds1);

auto explicitly_templated = iteration.meshes["explicitly_templated"];
Dataset ds2 = ds1;
ds2.options = backend + R"(.dataset.mode = "template")";
explicitly_templated.resetDataset(ds2);

auto explicitly_not_templated =
iteration.meshes["explicitly_not_templated"];
Dataset ds3 = ds1;
ds3.options = backend + R"(.dataset.mode = "dataset")";
explicitly_not_templated.resetDataset(ds3);

auto undefined_dataset = iteration.meshes["undefined_dataset"];
Dataset d4(Datatype::UNDEFINED, {Dataset::UNDEFINED_EXTENT});
undefined_dataset.resetDataset(d4);

output.close();

bool expect_template_datasets = template_datasets.value_or(false);
bool expect_short_attributes = short_attributes.value_or(
backend == "toml" || standardVersion == "2.0.0");

nlohmann::json resulting_dataset = [&]() {
std::fstream handle;
handle.open(name, std::ios_base::binary | std::ios_base::in);
if (backend == "json")
{
nlohmann::json res;
handle >> res;
return res;
}
else
{
auto toml_val = toml::parse(handle, name);
return json::tomlToJson(toml_val);
}
}();

if (expect_short_attributes)
{
REQUIRE(
resulting_dataset["attributes"]["openPMD"] ==
nlohmann::json::string_t{standardVersion});
REQUIRE(
resulting_dataset["__openPMD_internal"]["attribute_mode"] ==
nlohmann::json::string_t{"short"});
}
else
{
REQUIRE(
resulting_dataset["attributes"]["openPMD"] ==
nlohmann::json{{"datatype", "STRING"}, {"value", standardVersion}});
REQUIRE(
resulting_dataset["__openPMD_internal"]["attribute_mode"] ==
nlohmann::json::string_t{"long"});
}

auto verify_full_dataset = [&](nlohmann::json const &j) {
REQUIRE(j["datatype"] == "INT");
if (backend == "json")
{
nlohmann::json null;
REQUIRE(
j["data"] ==
nlohmann::json::array_t{null, null, null, null, null});
}
else
{
REQUIRE(j["data"] == nlohmann::json::array_t{0, 0, 0, 0, 0});
}
REQUIRE(j.size() == 3);
};
auto verify_template_dataset = [](nlohmann::json const &j) {
REQUIRE(j["datatype"] == "INT");
REQUIRE(j["extent"] == nlohmann::json::array_t{5});
REQUIRE(j.size() == 3);
};

// Undefined datasets write neither `extent` nor `data` key, so they are
// not distinguished between template and nontemplate mode.
REQUIRE(
resulting_dataset["data"]["0"]["meshes"]["undefined_dataset"]
["datatype"] == nlohmann::json::string_t{"UNDEFINED"});
REQUIRE(
resulting_dataset["data"]["0"]["meshes"]["undefined_dataset"].size() ==
2);
if (expect_template_datasets)
{
REQUIRE(
resulting_dataset["__openPMD_internal"]["dataset_mode"] ==
nlohmann::json::string_t{"template"});
verify_template_dataset(
resulting_dataset["data"]["0"]["meshes"]["default_configured"]);
}
else
{
REQUIRE(
resulting_dataset["__openPMD_internal"]["dataset_mode"] ==
nlohmann::json::string_t{"dataset"});
verify_full_dataset(
resulting_dataset["data"]["0"]["meshes"]["default_configured"]);
}
verify_template_dataset(
resulting_dataset["data"]["0"]["meshes"]["explicitly_templated"]);
verify_full_dataset(
resulting_dataset["data"]["0"]["meshes"]["explicitly_not_templated"]);

Series read(name, Access::READ_ONLY);
helper::listSeries(read);
}

TEST_CASE("json_short_modes")

Check notice

Code scanning / CodeQL

Unused static function Note test

Static function C_A_T_C_H_T_E_S_T_6 is unreachable (
autoRegistrar7
must be removed at the same time)
{
unsigned int name_counter = 0;
::auxiliary::test_matrix(
&json_short_modes,
std::vector<std::optional<bool>>{std::nullopt, true, false},
std::vector<std::optional<bool>>{std::nullopt, true, false},
std::vector<std::string>{getStandardDefault(), getStandardMaximum()},
std::vector<std::string>{"json", "toml"},
std::vector<unsigned int *>{&name_counter});
}

0 comments on commit 7f81e62

Please sign in to comment.