Merge pull request #4542 from akohlmey/add-json-lib

Integrate header-only JSON library
This commit is contained in:
Axel Kohlmeyer
2025-04-17 13:56:16 -04:00
committed by GitHub
9 changed files with 24895 additions and 0 deletions

View File

@ -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)

View File

@ -0,0 +1,78 @@
/* ----------------------------------------------------------------------
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 "gmock/gmock.h"
#include "gtest/gtest.h"
#include <exception>
#include <string>
#include "json.h"
using namespace LAMMPS_NS;
using ::testing::Eq;
#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);
}