Skip to content

Commit

Permalink
Add unit test for MultivarJson
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek committed Oct 6, 2023
1 parent 811cd01 commit 1e0a5f3
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 6 deletions.
1 change: 1 addition & 0 deletions Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ if (tests)
src/test/basics/join_test.cpp
src/test/basics/mulDiv_test.cpp
src/test/basics/tagged_integer_test.cpp
src/test/basics/MultivarJson_test.cpp
#[===============================[
test sources:
subdir: beast
Expand Down
15 changes: 9 additions & 6 deletions src/ripple/basics/MultivarJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,27 @@
#include <ripple/json/json_value.h>

#include <array>
#include <concepts>
#include <cstdlib>
#include <type_traits>

namespace ripple {
template <std::size_t size>
template <std::size_t Size>
struct MultivarJson
{
std::array<Json::Value, size> val;
std::array<Json::Value, Size> val;
constexpr static std::size_t size = Size;

Json::Value const&
select(auto selector) const
select(auto&& selector) const
requires std::same_as<std::size_t, decltype(selector())>
{
static_assert(std::is_same_v<decltype(selector()), std::size_t>);
return val[selector()];
}

void
set(const char* key, auto const& v)
set(const char* key,
auto const&
v) requires std::constructible_from<Json::Value, decltype(v)>
{
for (auto& a : this->val)
a[key] = v;
Expand Down
241 changes: 241 additions & 0 deletions src/test/basics/MultivarJson_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github0.com/ripple/rippled
Copyright (c) 2023 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#include <ripple/basics/MultivarJson.h>
#include <ripple/beast/unit_test.h>
#include "ripple/beast/unit_test/suite.hpp"
#include "ripple/json/json_value.h"
#include <cstdint>
#include <limits>
#include <optional>
#include <type_traits>

namespace ripple {
namespace test {

struct MultivarJson_test : beast::unit_test::suite
{
void
run() override
{
constexpr static Json::StaticString string1("string1");
static Json::Value const str1{string1};

static Json::Value const obj1{[]() {
Json::Value obj1;
obj1["one"] = 1;
return obj1;
}()};

static Json::Value const jsonNull{};

MultivarJson<3> const subject({str1, obj1});
static_assert(sizeof(subject) == sizeof(subject.val));
static_assert(subject.size == subject.val.size());
static_assert(
std::is_same_v<decltype(subject.val), std::array<Json::Value, 3>>);

BEAST_EXPECT(subject.val.size() == 3);
BEAST_EXPECT(
(subject.val == std::array<Json::Value, 3>{str1, obj1, jsonNull}));
BEAST_EXPECT(
(MultivarJson<3>({obj1, str1}).val ==
std::array<Json::Value, 3>{obj1, str1, jsonNull}));
BEAST_EXPECT(
(MultivarJson<3>({jsonNull, obj1, str1}).val ==
std::array<Json::Value, 3>{jsonNull, obj1, str1}));

{
testcase("default copy construction / assignment");

MultivarJson<3> x{subject};

BEAST_EXPECT(x.val.size() == subject.val.size());
BEAST_EXPECT(x.val[0] == subject.val[0]);
BEAST_EXPECT(x.val[1] == subject.val[1]);
BEAST_EXPECT(x.val[2] == subject.val[2]);
BEAST_EXPECT(x.val == subject.val);
BEAST_EXPECT(&x.val[0] != &subject.val[0]);
BEAST_EXPECT(&x.val[1] != &subject.val[1]);
BEAST_EXPECT(&x.val[2] != &subject.val[2]);

MultivarJson<3> y;
BEAST_EXPECT((y.val == std::array<Json::Value, 3>{}));
y = subject;
BEAST_EXPECT(y.val == subject.val);
BEAST_EXPECT(&y.val[0] != &subject.val[0]);
BEAST_EXPECT(&y.val[1] != &subject.val[1]);
BEAST_EXPECT(&y.val[2] != &subject.val[2]);

y = std::move(x);
BEAST_EXPECT(y.val == subject.val);
BEAST_EXPECT(&y.val[0] != &subject.val[0]);
BEAST_EXPECT(&y.val[1] != &subject.val[1]);
BEAST_EXPECT(&y.val[2] != &subject.val[2]);
}

{
testcase("select");

BEAST_EXPECT(
subject.select([]() -> std::size_t { return 0; }) == str1);
BEAST_EXPECT(
subject.select([]() -> std::size_t { return 1; }) == obj1);
BEAST_EXPECT(
subject.select([]() -> std::size_t { return 2; }) == jsonNull);

// Tests of requires clause - these are expected to match
static_assert([](auto&& v) {
return requires
{
v.select([]() -> std::size_t {});
};
}(subject));
static_assert([](auto&& v) {
return requires
{
v.select([]() constexpr->std::size_t { return 0; });
};
}(subject));
static_assert([](auto&& v) {
return requires
{
v.select([]() mutable -> std::size_t {});
};
}(subject));

// Tests of requires clause - these are expected NOT to match
static_assert([](auto&& a) {
return !requires
{
subject.select([]() -> int { return 0; });
};
}(subject));
static_assert([](auto&& v) {
return !requires
{
v.select([]() -> void {});
};
}(subject));
static_assert([](auto&& v) {
return !requires
{
v.select([]() -> bool {});
};
}(subject));
}

{
struct foo_t final
{
};
testcase("set");

auto x = MultivarJson<2>{{Json::objectValue, Json::objectValue}};
x.set("name1", 42);
BEAST_EXPECT(x.val[0].isMember("name1"));
BEAST_EXPECT(x.val[1].isMember("name1"));
BEAST_EXPECT(x.val[0]["name1"].isInt());
BEAST_EXPECT(x.val[1]["name1"].isInt());
BEAST_EXPECT(x.val[0]["name1"].asInt() == 42);
BEAST_EXPECT(x.val[1]["name1"].asInt() == 42);

x.set("name2", "bar");
BEAST_EXPECT(x.val[0].isMember("name2"));
BEAST_EXPECT(x.val[1].isMember("name2"));
BEAST_EXPECT(x.val[0]["name2"].isString());
BEAST_EXPECT(x.val[1]["name2"].isString());
BEAST_EXPECT(x.val[0]["name2"].asString() == "bar");
BEAST_EXPECT(x.val[1]["name2"].asString() == "bar");

// Tests of requires clause - these are expected to match
static_assert([](auto&& v) {
return requires
{
v.set("name", Json::nullValue);
};
}(x));
static_assert([](auto&& v) {
return requires
{
v.set("name", "value");
};
}(x));
static_assert([](auto&& v) {
return requires
{
v.set("name", true);
};
}(x));
static_assert([](auto&& v) {
return requires
{
v.set("name", 42);
};
}(x));

// Tests of requires clause - these are expected NOT to match
static_assert([](auto&& v) {
return !requires
{
v.set("name", foo_t{});
};
}(x));
static_assert([](auto&& v) {
return !requires
{
v.set("name", std::nullopt);
};
}(x));
}

{
testcase("apiVersionSelector");

static_assert(MultiApiJson::size == 2);
static MultiApiJson x{{obj1, str1}};

static_assert(
std::is_same_v<decltype(apiVersionSelector(1)()), std::size_t>);
static_assert([](auto&& v) {
return requires
{
v.select(apiVersionSelector(1));
};
}(x));

BEAST_EXPECT(x.select(apiVersionSelector(0)) == obj1);
BEAST_EXPECT(x.select(apiVersionSelector(2)) == str1);

static_assert(
apiVersionSelector(std::numeric_limits<int>::min())() == 0);
static_assert(apiVersionSelector(0)() == 0);
static_assert(apiVersionSelector(1)() == 0);
static_assert(apiVersionSelector(2)() == 1);
static_assert(apiVersionSelector(3)() == 1);
static_assert(
apiVersionSelector(std::numeric_limits<int>::max())() == 1);
}
}
};

BEAST_DEFINE_TESTSUITE(MultivarJson, ripple_basics, ripple);

} // namespace test
} // namespace ripple

0 comments on commit 1e0a5f3

Please sign in to comment.