Fix datatype and lexical form of result of various numeric functions

- to match SPARQL semantics
This commit is contained in:
Marcel Otto 2018-08-21 03:27:48 +02:00
parent 81cfcff5d7
commit 1c7cad39be
2 changed files with 23 additions and 20 deletions

View file

@ -283,6 +283,7 @@ defmodule RDF.Numeric do
if RDF.Decimal.valid?(literal) do
literal.value
|> xpath_round(precision)
|> to_string()
|> RDF.Decimal.new()
end
end
@ -333,8 +334,8 @@ defmodule RDF.Numeric do
if RDF.Decimal.valid?(literal) do
literal.value
|> D.round(0, (if literal.value.sign == -1, do: :down, else: :up))
|> D.to_integer()
|> RDF.Integer.new()
|> D.to_string()
|> RDF.Decimal.new()
end
end
@ -346,7 +347,8 @@ defmodule RDF.Numeric do
literal.value
|> Float.ceil()
|> trunc()
|> RDF.Integer.new()
|> to_string()
|> RDF.Double.new()
end
end
@ -370,8 +372,8 @@ defmodule RDF.Numeric do
if RDF.Decimal.valid?(literal) do
literal.value
|> D.round(0, (if literal.value.sign == -1, do: :up, else: :down))
|> D.to_integer()
|> RDF.Integer.new()
|> D.to_string()
|> RDF.Decimal.new()
end
end
@ -383,7 +385,8 @@ defmodule RDF.Numeric do
literal.value
|> Float.floor()
|> trunc()
|> RDF.Integer.new()
|> to_string()
|> RDF.Double.new()
end
end

View file

@ -364,9 +364,9 @@ defmodule RDF.NumericTest do
end
test "with xsd:decimal" do
assert RDF.decimal(2.5) |> Numeric.round() == RDF.decimal(3.0)
assert RDF.decimal(2.4999) |> Numeric.round() == RDF.decimal(2.0)
assert RDF.decimal(-2.5) |> Numeric.round() == RDF.decimal(-2.0)
assert RDF.decimal(2.5) |> Numeric.round() == RDF.decimal("3")
assert RDF.decimal(2.4999) |> Numeric.round() == RDF.decimal("2")
assert RDF.decimal(-2.5) |> Numeric.round() == RDF.decimal("-2")
end
test "with invalid numeric literals" do
@ -397,9 +397,9 @@ defmodule RDF.NumericTest do
end
test "with xsd:decimal" do
assert RDF.decimal(1.125) |> Numeric.round(2) == RDF.decimal(1.13)
assert RDF.decimal(2.4999) |> Numeric.round(2) == RDF.decimal(2.5)
assert RDF.decimal(-2.55) |> Numeric.round(1) == RDF.decimal(-2.5)
assert RDF.decimal(1.125) |> Numeric.round(2) == RDF.decimal("1.13")
assert RDF.decimal(2.4999) |> Numeric.round(2) == RDF.decimal("2.50")
assert RDF.decimal(-2.55) |> Numeric.round(1) == RDF.decimal("-2.5")
end
test "with invalid numeric literals" do
@ -416,8 +416,8 @@ defmodule RDF.NumericTest do
end
test "with xsd:double" do
assert RDF.double(10.5) |> Numeric.ceil() == RDF.integer(11)
assert RDF.double(-10.5) |> Numeric.ceil() == RDF.integer(-10)
assert RDF.double(10.5) |> Numeric.ceil() == RDF.double("11")
assert RDF.double(-10.5) |> Numeric.ceil() == RDF.double("-10")
assert RDF.double("INF") |> Numeric.ceil() == RDF.double("INF")
assert RDF.double("-INF") |> Numeric.ceil() == RDF.double("-INF")
@ -425,8 +425,8 @@ defmodule RDF.NumericTest do
end
test "with xsd:decimal" do
assert RDF.decimal(10.5) |> Numeric.ceil() == RDF.integer(11)
assert RDF.decimal(-10.5) |> Numeric.ceil() == RDF.integer(-10)
assert RDF.decimal(10.5) |> Numeric.ceil() == RDF.decimal("11")
assert RDF.decimal(-10.5) |> Numeric.ceil() == RDF.decimal("-10")
end
test "with invalid numeric literals" do
@ -443,8 +443,8 @@ defmodule RDF.NumericTest do
end
test "with xsd:double" do
assert RDF.double(10.5) |> Numeric.floor() == RDF.integer(10)
assert RDF.double(-10.5) |> Numeric.floor() == RDF.integer(-11)
assert RDF.double(10.5) |> Numeric.floor() == RDF.double("10")
assert RDF.double(-10.5) |> Numeric.floor() == RDF.double("-11")
assert RDF.double("INF") |> Numeric.floor() == RDF.double("INF")
assert RDF.double("-INF") |> Numeric.floor() == RDF.double("-INF")
@ -452,8 +452,8 @@ defmodule RDF.NumericTest do
end
test "with xsd:decimal" do
assert RDF.decimal(10.5) |> Numeric.floor() == RDF.integer(10)
assert RDF.decimal(-10.5) |> Numeric.floor() == RDF.integer(-11)
assert RDF.decimal(10.5) |> Numeric.floor() == RDF.decimal("10")
assert RDF.decimal(-10.5) |> Numeric.floor() == RDF.decimal("-11")
end
test "with invalid numeric literals" do