Add overload of operator< for mgp::Value (#1090)
This commit is contained in:
parent
609b9a20f1
commit
76ca019f31
@ -1131,6 +1131,8 @@ class Value {
|
||||
/// @exception std::runtime_error Unknown value type.
|
||||
bool operator!=(const Value &other) const;
|
||||
|
||||
bool operator<(const Value &other) const;
|
||||
|
||||
private:
|
||||
mgp_value *ptr_;
|
||||
};
|
||||
@ -3257,6 +3259,44 @@ inline bool Value::operator==(const Value &other) const { return util::ValuesEqu
|
||||
|
||||
inline bool Value::operator!=(const Value &other) const { return !(*this == other); }
|
||||
|
||||
inline bool Value::operator<(const Value &other) const {
|
||||
const mgp::Type &type = Type();
|
||||
if (type != other.Type() && !(IsNumeric() && other.IsNumeric())) {
|
||||
throw ValueException("Values have to be of the same type");
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case Type::Null:
|
||||
throw ValueException("Cannot compare Null types");
|
||||
case Type::Bool:
|
||||
return ValueBool() < other.ValueBool();
|
||||
case Type::Int:
|
||||
return ValueNumeric() < other.ValueNumeric();
|
||||
case Type::Double:
|
||||
return ValueNumeric() < other.ValueNumeric();
|
||||
case Type::String:
|
||||
return ValueString() < other.ValueString();
|
||||
case Type::Node:
|
||||
return ValueNode() < other.ValueNode();
|
||||
case Type::Relationship:
|
||||
return ValueRelationship() < other.ValueRelationship();
|
||||
case Type::Date:
|
||||
return ValueDate() < other.ValueDate();
|
||||
case Type::LocalTime:
|
||||
return ValueLocalTime() < other.ValueLocalTime();
|
||||
case Type::LocalDateTime:
|
||||
return ValueLocalDateTime() < other.ValueLocalDateTime();
|
||||
case Type::Duration:
|
||||
return ValueDuration() < other.ValueDuration();
|
||||
case Type::Path:
|
||||
case Type::List:
|
||||
case Type::Map:
|
||||
throw ValueException("Operator < is not defined for this Path, List or Map data type");
|
||||
default:
|
||||
throw ValueException("Undefined behaviour");
|
||||
}
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &os, const mgp::Type &type) {
|
||||
switch (type) {
|
||||
case mgp::Type::Null:
|
||||
@ -3292,6 +3332,7 @@ inline std::ostream &operator<<(std::ostream &os, const mgp::Type &type) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* #endregion */
|
||||
|
||||
/* #region Record */
|
||||
|
@ -473,6 +473,29 @@ TYPED_TEST(CppApiTestFixture, TestNodeProperties) {
|
||||
ASSERT_EQ(node_1.GetProperty("b").ValueString(), "b");
|
||||
}
|
||||
|
||||
|
||||
TYPED_TEST(CppApiTestFixture, TestValueOperatorLessThan) {
|
||||
const int64_t int1 = 3;
|
||||
const int64_t int2 = 4;
|
||||
const double double1 = 3.5;
|
||||
const mgp::List list1 = mgp::List();
|
||||
const mgp::Map map1 = mgp::Map();
|
||||
const mgp::Value int_test1 = mgp::Value(int1);
|
||||
const mgp::Value int_test2 = mgp::Value(int2);
|
||||
const mgp::Value double_test1 = mgp::Value(double1);
|
||||
const mgp::Value list_test = mgp::Value(list1);
|
||||
const mgp::Value map_test = mgp::Value(map1);
|
||||
|
||||
ASSERT_TRUE(int_test1 < int_test2);
|
||||
ASSERT_TRUE(double_test1 < int_test2);
|
||||
|
||||
const std::string string1 = "string";
|
||||
const mgp::Value string_test1 = mgp::Value(string1);
|
||||
|
||||
ASSERT_THROW(int_test1 < string_test1, mgp::ValueException);
|
||||
ASSERT_THROW(list_test < map_test, mgp::ValueException);
|
||||
ASSERT_THROW(list_test < list_test, mgp::ValueException);
|
||||
}
|
||||
TYPED_TEST(CppApiTestFixture, TestNumberEquality) {
|
||||
mgp::Value double_1{1.0};
|
||||
mgp::Value int_1{static_cast<int64_t>(1)};
|
||||
@ -480,6 +503,7 @@ TYPED_TEST(CppApiTestFixture, TestNumberEquality) {
|
||||
mgp::Value double_2{2.01};
|
||||
mgp::Value int_2{static_cast<int64_t>(2)};
|
||||
ASSERT_FALSE(double_2 == int_2);
|
||||
|
||||
}
|
||||
|
||||
TYPED_TEST(CppApiTestFixture, TestTypeOperatorStream) {
|
||||
|
Loading…
Reference in New Issue
Block a user