add nf7::Value::operator==
This commit is contained in:
parent
ca16c6948a
commit
1bcbd786f5
@ -23,10 +23,14 @@ namespace nf7 {
|
|||||||
|
|
||||||
class Value final {
|
class Value final {
|
||||||
public:
|
public:
|
||||||
struct Null { };
|
|
||||||
using Integer = int64_t;
|
using Integer = int64_t;
|
||||||
using Real = double;
|
using Real = double;
|
||||||
|
|
||||||
|
class Null {
|
||||||
|
public:
|
||||||
|
bool operator==(const Null&) const noexcept { return true; }
|
||||||
|
};
|
||||||
|
|
||||||
class Buffer final {
|
class Buffer final {
|
||||||
public:
|
public:
|
||||||
Buffer() = default;
|
Buffer() = default;
|
||||||
@ -48,6 +52,10 @@ class Value final {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool operator==(const Buffer& other) const noexcept {
|
||||||
|
return size_ == other.size_ && buf_ == other.buf_;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T = uint8_t>
|
template <typename T = uint8_t>
|
||||||
std::span<const T> span() const noexcept {
|
std::span<const T> span() const noexcept {
|
||||||
return {begin<T>(), end<T>()};
|
return {begin<T>(), end<T>()};
|
||||||
@ -111,6 +119,10 @@ class Value final {
|
|||||||
return itr->second;
|
return itr->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool operator==(const Object& other) const noexcept {
|
||||||
|
return size_ == other.size_ && pairs_ == other.pairs_;
|
||||||
|
}
|
||||||
|
|
||||||
const Value& at(
|
const Value& at(
|
||||||
uint64_t index, const Value& def = {}) const noexcept {
|
uint64_t index, const Value& def = {}) const noexcept {
|
||||||
return index < size_? pairs_[index].second: def;
|
return index < size_? pairs_[index].second: def;
|
||||||
@ -215,11 +227,16 @@ class Value final {
|
|||||||
Value(Object&& v) noexcept : var_(std::move(v)) { }
|
Value(Object&& v) noexcept : var_(std::move(v)) { }
|
||||||
Value(const Object& v) noexcept : var_(v) { }
|
Value(const Object& v) noexcept : var_(v) { }
|
||||||
|
|
||||||
|
public:
|
||||||
Value(const Value&) = default;
|
Value(const Value&) = default;
|
||||||
Value(Value&&) = default;
|
Value(Value&&) = default;
|
||||||
Value& operator=(const Value&) = default;
|
Value& operator=(const Value&) = default;
|
||||||
Value& operator=(Value&&) = default;
|
Value& operator=(Value&&) = default;
|
||||||
|
|
||||||
|
bool operator==(const Value& other) const noexcept {
|
||||||
|
return var_ == other.var_;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const T& as(
|
const T& as(
|
||||||
|
@ -19,6 +19,15 @@ TEST(Value, NullAsInvalid) {
|
|||||||
EXPECT_THROW(v.as<nf7::Value::Buffer>(), nf7::Exception);
|
EXPECT_THROW(v.as<nf7::Value::Buffer>(), nf7::Exception);
|
||||||
EXPECT_THROW(v.as<nf7::Value::Object>(), nf7::Exception);
|
EXPECT_THROW(v.as<nf7::Value::Object>(), nf7::Exception);
|
||||||
}
|
}
|
||||||
|
TEST(Value, NullEqual) {
|
||||||
|
EXPECT_EQ(nf7::Value::MakeNull(), nf7::Value::MakeNull());
|
||||||
|
}
|
||||||
|
TEST(Value, NullNotEqual) {
|
||||||
|
EXPECT_NE(nf7::Value::MakeNull(), nf7::Value::MakeInteger(0));
|
||||||
|
EXPECT_NE(nf7::Value::MakeNull(), nf7::Value::MakeReal(0));
|
||||||
|
EXPECT_NE(nf7::Value::MakeNull(), nf7::Value::MakeBuffer<uint8_t>({}));
|
||||||
|
EXPECT_NE(nf7::Value::MakeNull(), nf7::Value::MakeObject({}));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(Value, IntegerAsInteger) {
|
TEST(Value, IntegerAsInteger) {
|
||||||
const auto v = nf7::Value::MakeInteger(777);
|
const auto v = nf7::Value::MakeInteger(777);
|
||||||
@ -48,6 +57,16 @@ TEST(Value, IntegerAsInvalidNum) {
|
|||||||
EXPECT_THROW(v.num<int8_t>(), nf7::Exception);
|
EXPECT_THROW(v.num<int8_t>(), nf7::Exception);
|
||||||
EXPECT_THROW(v.num<int8_t>(int8_t {77}), nf7::Exception);
|
EXPECT_THROW(v.num<int8_t>(int8_t {77}), nf7::Exception);
|
||||||
}
|
}
|
||||||
|
TEST(Value, IntegerEqual) {
|
||||||
|
EXPECT_EQ(nf7::Value::MakeInteger(666), nf7::Value::MakeInteger(666));
|
||||||
|
}
|
||||||
|
TEST(Value, IntegerNotEqual) {
|
||||||
|
EXPECT_NE(nf7::Value::MakeInteger(666), nf7::Value::MakeInteger(777));
|
||||||
|
EXPECT_NE(nf7::Value::MakeInteger(666), nf7::Value::MakeNull());
|
||||||
|
EXPECT_NE(nf7::Value::MakeInteger(666), nf7::Value::MakeReal(0));
|
||||||
|
EXPECT_NE(nf7::Value::MakeInteger(666), nf7::Value::MakeBuffer<uint8_t>({}));
|
||||||
|
EXPECT_NE(nf7::Value::MakeInteger(666), nf7::Value::MakeObject({}));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(Value, RealAsReal) {
|
TEST(Value, RealAsReal) {
|
||||||
const auto v = nf7::Value::MakeReal(777);
|
const auto v = nf7::Value::MakeReal(777);
|
||||||
@ -77,6 +96,16 @@ TEST(Value, RealAsInvalidNum) {
|
|||||||
EXPECT_THROW(v.num<int8_t>(), nf7::Exception);
|
EXPECT_THROW(v.num<int8_t>(), nf7::Exception);
|
||||||
EXPECT_THROW(v.num<int8_t>(int8_t {77}), nf7::Exception);
|
EXPECT_THROW(v.num<int8_t>(int8_t {77}), nf7::Exception);
|
||||||
}
|
}
|
||||||
|
TEST(Value, RealEqual) {
|
||||||
|
EXPECT_EQ(nf7::Value::MakeReal(0.5), nf7::Value::MakeReal(0.5));
|
||||||
|
}
|
||||||
|
TEST(Value, RealNotEqual) {
|
||||||
|
EXPECT_NE(nf7::Value::MakeReal(1), nf7::Value::MakeReal(0.5));
|
||||||
|
EXPECT_NE(nf7::Value::MakeReal(1), nf7::Value::MakeNull());
|
||||||
|
EXPECT_NE(nf7::Value::MakeReal(1), nf7::Value::MakeInteger(1));
|
||||||
|
EXPECT_NE(nf7::Value::MakeReal(1), nf7::Value::MakeBuffer<uint8_t>({}));
|
||||||
|
EXPECT_NE(nf7::Value::MakeReal(1), nf7::Value::MakeObject({}));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(Value, BufferAsBuffer) {
|
TEST(Value, BufferAsBuffer) {
|
||||||
const auto v = nf7::Value::MakeBuffer<uint8_t>({});
|
const auto v = nf7::Value::MakeBuffer<uint8_t>({});
|
||||||
@ -93,6 +122,18 @@ TEST(Value, BufferAsInvalid) {
|
|||||||
EXPECT_THROW(v.as<nf7::Value::Real>(), nf7::Exception);
|
EXPECT_THROW(v.as<nf7::Value::Real>(), nf7::Exception);
|
||||||
EXPECT_THROW(v.as<nf7::Value::Object>(), nf7::Exception);
|
EXPECT_THROW(v.as<nf7::Value::Object>(), nf7::Exception);
|
||||||
}
|
}
|
||||||
|
TEST(Value, BufferEqual) {
|
||||||
|
const auto v = nf7::Value::MakeBuffer<uint8_t>({});
|
||||||
|
EXPECT_EQ(v, v);
|
||||||
|
}
|
||||||
|
TEST(Value, BufferNotEqual) {
|
||||||
|
EXPECT_NE(nf7::Value::MakeBuffer<uint8_t>({}), nf7::Value::MakeNull());
|
||||||
|
EXPECT_NE(nf7::Value::MakeBuffer<uint8_t>({}), nf7::Value::MakeInteger(0));
|
||||||
|
EXPECT_NE(nf7::Value::MakeBuffer<uint8_t>({}), nf7::Value::MakeReal(0));
|
||||||
|
EXPECT_NE(nf7::Value::MakeBuffer<uint8_t>({}),
|
||||||
|
nf7::Value::MakeBuffer<uint8_t>({}));
|
||||||
|
EXPECT_NE(nf7::Value::MakeBuffer<uint8_t>({}), nf7::Value::MakeObject({}));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(Value, ObjectAsObject) {
|
TEST(Value, ObjectAsObject) {
|
||||||
const auto v = nf7::Value::MakeObject({});
|
const auto v = nf7::Value::MakeObject({});
|
||||||
@ -109,6 +150,17 @@ TEST(Value, ObjectAsInvalid) {
|
|||||||
EXPECT_THROW(v.as<nf7::Value::Real>(), nf7::Exception);
|
EXPECT_THROW(v.as<nf7::Value::Real>(), nf7::Exception);
|
||||||
EXPECT_THROW(v.as<nf7::Value::Buffer>(), nf7::Exception);
|
EXPECT_THROW(v.as<nf7::Value::Buffer>(), nf7::Exception);
|
||||||
}
|
}
|
||||||
|
TEST(Value, ObjectEqual) {
|
||||||
|
const auto v = nf7::Value::MakeObject({});
|
||||||
|
EXPECT_EQ(v, v);
|
||||||
|
}
|
||||||
|
TEST(Value, ObjectNotEqual) {
|
||||||
|
EXPECT_NE(nf7::Value::MakeObject({}), nf7::Value::MakeNull());
|
||||||
|
EXPECT_NE(nf7::Value::MakeObject({}), nf7::Value::MakeInteger(0));
|
||||||
|
EXPECT_NE(nf7::Value::MakeObject({}), nf7::Value::MakeReal(0));
|
||||||
|
EXPECT_NE(nf7::Value::MakeObject({}), nf7::Value::MakeBuffer<uint8_t>({}));
|
||||||
|
EXPECT_NE(nf7::Value::MakeObject({}), nf7::Value::MakeObject({}));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(Value_Buffer, Make) {
|
TEST(Value_Buffer, Make) {
|
||||||
const auto value = nf7::Value::MakeBuffer<uint8_t>({1, 2, 3, 4});
|
const auto value = nf7::Value::MakeBuffer<uint8_t>({1, 2, 3, 4});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user