From 4553108035b5eb4f82a7dd432bad1b97b97aed11 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Tue, 15 Apr 2025 22:11:44 -0400 Subject: [PATCH] add minimal unit test for JSON class --- unittest/utils/CMakeLists.txt | 4 ++ unittest/utils/test_json.cpp | 77 +++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 unittest/utils/test_json.cpp diff --git a/unittest/utils/CMakeLists.txt b/unittest/utils/CMakeLists.txt index c277a0353b..8c09a1b1c7 100644 --- a/unittest/utils/CMakeLists.txt +++ b/unittest/utils/CMakeLists.txt @@ -49,6 +49,10 @@ add_executable(test_fmtlib test_fmtlib.cpp) target_link_libraries(test_fmtlib PRIVATE lammps GTest::GMockMain) add_test(NAME FmtLib COMMAND test_fmtlib) +add_executable(test_json test_json.cpp) +target_link_libraries(test_json PRIVATE lammps GTest::GMockMain) +add_test(NAME JSON COMMAND test_json) + add_executable(test_math_eigen_impl test_math_eigen_impl.cpp) target_include_directories(test_math_eigen_impl PRIVATE ${LAMMPS_SOURCE_DIR}) add_test(NAME MathEigen COMMAND test_math_eigen_impl 10 5) diff --git a/unittest/utils/test_json.cpp b/unittest/utils/test_json.cpp new file mode 100644 index 0000000000..e8cab895c3 --- /dev/null +++ b/unittest/utils/test_json.cpp @@ -0,0 +1,77 @@ +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + https://www.lammps.org/, Sandia National Laboratories + LAMMPS Development team: developers@lammps.org + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +#include "lmptype.h" +#include "nlohmann/json.hpp" +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include +#include + +using namespace LAMMPS_NS; +using ::testing::Eq; +using json = ::nlohmann_lmp::json; + +#define STRINGIFY(val) XSTR(val) +#define XSTR(val) #val + +// this tests a subset of the JSON class that is most relevant to LAMMPS + +TEST(JSON, namespace) +{ + std::string expected = "nlohmann_lmp::json_abi"; + expected += "_v" STRINGIFY(NLOHMANN_JSON_VERSION_MAJOR); + expected += "_" STRINGIFY(NLOHMANN_JSON_VERSION_MINOR); + expected += "_" STRINGIFY(NLOHMANN_JSON_VERSION_PATCH); + + const std::string ns{STRINGIFY(NLOHMANN_JSON_NAMESPACE)}; + ASSERT_THAT(expected, Eq(ns)); +} + +TEST(JSON, serialize_deserialize) +{ + json j1; + j1["pi"] = 3.141; + j1["happy"] = true; + j1["name"] = "Niels"; + j1["nothing"] = nullptr; + + std::string expected = "{\"happy\":true,\"name\":\"Niels\",\"nothing\":null,\"pi\":3.141}"; + std::string dumped = j1.dump(); + ASSERT_THAT(expected, Eq(dumped)); + + json j2 = json::parse(expected); + ASSERT_TRUE(j1 == j2); +} + +TEST(JSON, init_vs_incremental) +{ + json j1; + j1["pi"] = 3.141; + j1["happy"] = true; + j1["name"] = "Niels"; + j1["nothing"] = nullptr; + j1["answer"]["everything"] = 42; + j1["list"] = {1, 0, 2}; + j1["object"] = {{"currency", "USD"}, {"value", 42.99}}; + + json j2 = {{"pi", 3.141}, + {"happy", true}, + {"name", "Niels"}, + {"nothing", nullptr}, + {"answer", {{"everything", 42}}}, + {"list", {1, 0, 2}}, + {"object", {{"currency", "USD"}, {"value", 42.99}}}}; + + ASSERT_TRUE(j1 == j2); +}