mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:27:42 +00:00
LibSQL: Implement converting float and tuple values to a boolean
This commit is contained in:
parent
8fab99e920
commit
e13c96157c
3 changed files with 32 additions and 2 deletions
|
@ -169,7 +169,18 @@ TEST_CASE(float_value)
|
||||||
EXPECT(v.to_int().has_value());
|
EXPECT(v.to_int().has_value());
|
||||||
EXPECT_EQ(v.to_int().value(), 3);
|
EXPECT_EQ(v.to_int().value(), 3);
|
||||||
EXPECT_EQ(v.to_string(), "3.14");
|
EXPECT_EQ(v.to_string(), "3.14");
|
||||||
EXPECT(!v.to_bool().has_value());
|
EXPECT(v.to_bool().has_value());
|
||||||
|
EXPECT(v.to_bool().value());
|
||||||
|
|
||||||
|
v = 0.0;
|
||||||
|
EXPECT(!v.is_null());
|
||||||
|
EXPECT(v.to_double().has_value());
|
||||||
|
EXPECT(v.to_double().value() < NumericLimits<double>().epsilon());
|
||||||
|
EXPECT(v.to_int().has_value());
|
||||||
|
EXPECT_EQ(v.to_int().value(), 0);
|
||||||
|
EXPECT_EQ(v.to_string(), "0");
|
||||||
|
EXPECT(v.to_bool().has_value());
|
||||||
|
EXPECT(!v.to_bool().value());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
SQL::Value v(3.14);
|
SQL::Value v(3.14);
|
||||||
|
|
|
@ -728,6 +728,11 @@ Optional<int> FloatImpl::to_int() const
|
||||||
return static_cast<int>(round(value()));
|
return static_cast<int>(round(value()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<bool> FloatImpl::to_bool() const
|
||||||
|
{
|
||||||
|
return fabs(value()) > NumericLimits<double>::epsilon();
|
||||||
|
}
|
||||||
|
|
||||||
Optional<double> FloatImpl::to_double() const
|
Optional<double> FloatImpl::to_double() const
|
||||||
{
|
{
|
||||||
return value();
|
return value();
|
||||||
|
@ -999,6 +1004,19 @@ int TupleImpl::compare(Value const& other) const
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Optional<bool> TupleImpl::to_bool() const
|
||||||
|
{
|
||||||
|
for (auto const& value : value()) {
|
||||||
|
auto as_bool = Value(value).to_bool();
|
||||||
|
if (!as_bool.has_value())
|
||||||
|
return {};
|
||||||
|
if (!as_bool.value())
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void TupleImpl::serialize(Serializer& serializer) const
|
void TupleImpl::serialize(Serializer& serializer) const
|
||||||
{
|
{
|
||||||
serializer.serialize<TupleDescriptor>(*m_descriptor);
|
serializer.serialize<TupleDescriptor>(*m_descriptor);
|
||||||
|
|
|
@ -159,7 +159,7 @@ public:
|
||||||
[[nodiscard]] String to_string() const;
|
[[nodiscard]] String to_string() const;
|
||||||
[[nodiscard]] Optional<int> to_int() const;
|
[[nodiscard]] Optional<int> to_int() const;
|
||||||
[[nodiscard]] Optional<double> to_double() const;
|
[[nodiscard]] Optional<double> to_double() const;
|
||||||
[[nodiscard]] static Optional<bool> to_bool() { return {}; }
|
[[nodiscard]] Optional<bool> to_bool() const;
|
||||||
[[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
|
[[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
|
||||||
void assign(Value const&);
|
void assign(Value const&);
|
||||||
void assign_string(String const&);
|
void assign_string(String const&);
|
||||||
|
@ -250,6 +250,7 @@ public:
|
||||||
[[nodiscard]] size_t length() const;
|
[[nodiscard]] size_t length() const;
|
||||||
[[nodiscard]] bool can_cast(Value const&) const;
|
[[nodiscard]] bool can_cast(Value const&) const;
|
||||||
[[nodiscard]] int compare(Value const& other) const;
|
[[nodiscard]] int compare(Value const& other) const;
|
||||||
|
[[nodiscard]] Optional<bool> to_bool() const;
|
||||||
|
|
||||||
virtual bool validate_before_assignment(Vector<Value> const&) override;
|
virtual bool validate_before_assignment(Vector<Value> const&) override;
|
||||||
virtual bool validate(BaseTypeImpl const&) override;
|
virtual bool validate(BaseTypeImpl const&) override;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue