Skip to content

Commit

Permalink
Add JSON output for bitset
Browse files Browse the repository at this point in the history
  • Loading branch information
sethrj committed Jan 8, 2025
1 parent 3eeb6be commit 329b7c5
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 10 deletions.
36 changes: 36 additions & 0 deletions src/corecel/cont/EnumBitsetIO.json.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/cont/EnumBitsetIO.json.hh
//---------------------------------------------------------------------------//
#pragma once

#include <nlohmann/json.hpp>

#include "EnumBitset.hh"
#include "Range.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Write an enum bitset to a JSON file.
*
* \note A \c to_cstring function for the underlying type must be defined.
*/
template<class E>
void to_json(nlohmann::json& j, EnumBitset<E> const& v)
{
j = nlohmann::json::array();
for (auto enum_val : range(E::size_))
{
if (v[enum_val])
{
j.push_back(to_cstring(enum_val));
}
}
}

//---------------------------------------------------------------------------//
} // namespace celeritas
3 changes: 2 additions & 1 deletion test/corecel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ celeritas_add_test(OpaqueId.test.cc)

# Cont
celeritas_add_test(cont/Array.test.cc)
celeritas_add_test(cont/EnumBitset.test.cc)
celeritas_add_test(cont/EnumBitset.test.cc
LINK_LIBRARIES nlohmann_json::nlohmann_json)
celeritas_add_test(cont/InitializedValue.test.cc)
celeritas_add_test(cont/Span.test.cc)
celeritas_add_test(cont/LabelIdMultiMap.test.cc)
Expand Down
43 changes: 42 additions & 1 deletion test/corecel/cont/EnumBitset.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
//---------------------------------------------------------------------------//
//! \file corecel/cont/EnumBitset.test.cc
//---------------------------------------------------------------------------//
#include "corecel/cont/EnumBitset.hh"

#include <climits>

#include "corecel/Types.hh"
#include "corecel/cont/EnumBitset.hh"
#include "corecel/cont/EnumBitsetIO.json.hh"
#include "corecel/io/EnumStringMapper.hh"

#include "celeritas_test.hh"

Expand All @@ -17,6 +20,44 @@ namespace test
{
//---------------------------------------------------------------------------//
// TESTS
//---------------------------------------------------------------------------//
enum class Colors
{
red,
green,
blue,
size_
};

char const* to_cstring(Colors c)
{
static EnumStringMapper<Colors> const get_impl{"red", "green", "blue"};

return get_impl(c);
}

TEST(EnumJson, output)
{
EnumBitset<Colors> c;

{
// Empty set
nlohmann::json out = c;
static char const expected[] = R"json([])json";
EXPECT_JSON_EQ(expected, out.dump());
}

c[Colors::red] = true;
c[Colors::blue] = true;

{
// Two values
nlohmann::json out = c;
static char const expected[] = R"json(["red","blue"])json";
EXPECT_JSON_EQ(expected, out.dump());
}
}

//---------------------------------------------------------------------------//

template<typename T>
Expand Down
14 changes: 7 additions & 7 deletions test/corecel/io/EnumStringMapper.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "corecel/io/EnumStringMapper.hh"

#include "celeritas_test.hh"
// #include "EnumStringMapper.test.hh"

namespace celeritas
{
Expand All @@ -22,12 +21,6 @@ enum class CeleritasLabs
size_
};

enum class InvalidEnum
{
foo,
bar
};

//---------------------------------------------------------------------------//

TEST(EnumStringMapperTest, all)
Expand All @@ -49,6 +42,13 @@ TEST(EnumStringMapperTest, compiler_error)
static EnumStringMapper<CeleritasLabs> const too_short{"argonne", "ornl"};
static EnumStringMapper<CeleritasLabs> const too_long{
"argonne", "ornl", "foo", "bar"};

enum class InvalidEnum
{
foo,
bar
};

static EnumStringMapper<InvalidEnum> const no_size{"foo", "bar"};
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/corecel/math/Quantity.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ TEST(QuantityTest, io)
SCOPED_TRACE("Output");
nlohmann::json out = Dozen{2};
static char const expected[] = R"json([2,"dozen"])json";
EXPECT_EQ(std::string(expected), std::string(out.dump()));
EXPECT_JSON_EQ(expected, out.dump());
}
}

Expand Down

0 comments on commit 329b7c5

Please sign in to comment.