Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm: fix some bugs wrt. comparisons, ordering #2999

Merged
merged 3 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions internal/wasm/sdk/test/e2e/exceptions.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# Exception Format is <test name>: <reason>
"baseandvirtualdocs/base/virtual: conflicts": "document merge conflict - https://github.com/open-policy-agent/opa/issues/2926"
"strings/concat: set": "test result order is not consistent - https://github.com/open-policy-agent/opa/issues/2924"
"strings/format_int": "rounding error - https://github.com/open-policy-agent/opa/issues/2923"
"strings/format_int: ref dest": "rounding error - https://github.com/open-policy-agent/opa/issues/2923"
"strings/format_int: ref dest (2)": "rounding error - https://github.com/open-policy-agent/opa/issues/2923"
"strings/format_int: undefined": "rounding error - https://github.com/open-policy-agent/opa/issues/2923"
"withkeyword/with virtual doc specific index": "with target conflict issue - https://github.com/open-policy-agent/opa/issues/2922"
"withkeyword/with virtual doc not specific index": "with target conflict issue - https://github.com/open-policy-agent/opa/issues/2922"
"withkeyword/with virtual doc exact value": "with target conflict issue - https://github.com/open-policy-agent/opa/issues/2922"
Expand Down
84 changes: 3 additions & 81 deletions test/cases/testdata/arithmetic/test-arithmetic-0813.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,17 @@ cases:
- 2
- 3
- 4
b:
v1: hello
v2: goodbye
c:
- x:
- true
- false
- foo
"y":
- null
- 3.14159
z:
p: true
q: false
d:
e:
- bar
- baz
f:
- xs:
- 1
ys:
- 2
- xs:
- 2
ys:
- 3
g:
a:
- 1
- 0
- 0
- 0
b:
- 0
- 2
- 0
- 0
c:
- 0
- 0
- 0
- 4
h:
- - 1
- 2
- 3
- - 2
- 3
- 4
l:
- a: bob
b: -1
c:
- 1
- 2
- 3
- 4
- a: alice
b: 1
c:
- 2
- 3
- 4
- 5
d: null
m: []
numbers:
- "1"
- "2"
- "3"
- "4"
strings:
bar: 2
baz: 3
foo: 1
three: 3
modules:
- |
package generated
package test

p[z] {
data.a[i] = x
__local0__ = i / x
y = __local0__
y = i / x
round(y, z)
}
note: arithmetic/divide+round
query: data.generated.p = x
query: data.test.p = x
sort_bindings: true
want_result:
- x:
Expand Down
104 changes: 17 additions & 87 deletions test/cases/testdata/strings/test-strings-0883.yaml
Original file line number Diff line number Diff line change
@@ -1,95 +1,25 @@
cases:
- data:
a:
- 1
- 2
- 3
- 4
b:
v1: hello
v2: goodbye
c:
- x:
- true
- false
- foo
"y":
- null
- 3.14159
z:
p: true
q: false
d:
e:
- bar
- baz
f:
- xs:
- 1
ys:
- 2
- xs:
- 2
ys:
- 3
g:
a:
- 1
- 0
- 0
- 0
b:
- 0
- 2
- 0
- 0
c:
- 0
- 0
- 0
- 4
h:
- - 1
- 2
- 3
- - 2
- 3
- 4
l:
- a: bob
b: -1
c:
- 1
- 2
- 3
- 4
- a: alice
b: 1
c:
- 2
- 3
- 4
- 5
d: null
m: []
numbers:
- "1"
- "2"
- "3"
- "4"
strings:
bar: 2
baz: 3
foo: 1
three: 3
modules:
- |
package generated
package test

p = x {
concat(",", {"1", "2", "3"}, x)
# Sets are unordered, so the output is not guaranteed.
# These are theoretically possible:
possibilities = {
"1,2,3",
"2,3,1",
"3,1,2",
"3,2,1",
"2,1,3",
"1,3,2"
}

p {
x := concat(",", {"1", "2", "3"})
possibilities[x]
}
note: 'strings/concat: set'
query: data.generated.p = x
query: data.test.p = x
want_result:
- x: 1,2,3
- x: true
6 changes: 6 additions & 0 deletions test/wasm/assets/018_builtins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ cases:
- note: round built-in
query: round(1.4,x)
want_result: [{'x': 1}]
- note: round built-in ("halfs up")
query: round(1.5,x)
want_result: [{'x': 2}]
- note: round built-in ("halfs up not to even")
query: round(2.5,x)
want_result: [{'x': 3}]
- note: plus built-in
query: plus(1,1,x)
want_result: [{'x': 2}]
Expand Down
1 change: 1 addition & 0 deletions wasm/src/mpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static void init(void)

mpd_maxcontext(&max_ctx);
max_ctx.traps = 0;
max_ctx.round = MPD_ROUND_HALF_UP; // .5 always rounded up

one = mpd_qnew();

Expand Down
20 changes: 11 additions & 9 deletions wasm/src/strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,24 +209,26 @@ opa_value *opa_strings_format_int(opa_value *a, opa_value *b)

mpd_t *i = mpd_qnew();
uint32_t status = 0;

mpd_qround_to_intx(i, input, mpd_max_ctx(), &status);
if (status & ~MPD_Rounded)
mpd_qtrunc(i, input, mpd_max_ctx(), &status);
if (status != 0)
{
opa_abort("strings: invalid rounding");
opa_abort("strings: truncate failed");
}

opa_value *n = opa_bf_to_number(i);
opa_number_try_int(opa_cast_number(n), &v);
int32_t w = mpd_qget_i32(i, &status);
if (status != 0)
{
opa_abort("strings: get uint failed");
}

char *str = opa_malloc(21); // enough for int_t (with sign).

if (v < 0)
if (w < 0)
{
str[0] = '-';
snprintf(&str[1], 21, format, -v);
snprintf(&str[1], 21, format, -w);
} else {
snprintf(str, 21, format, v);
snprintf(str, 21, format, w);
}

return opa_string_allocated(str, opa_strlen(str));
Expand Down
4 changes: 4 additions & 0 deletions wasm/tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,8 @@ void test_arithmetic(void)
test("round -1.4 (float)", opa_number_as_float(opa_cast_number(opa_arith_round(opa_number_float(-1.4)))) == -1);
test("round 1.5 (float)", opa_number_as_float(opa_cast_number(opa_arith_round(opa_number_float(1.5)))) == 2);
test("round -1.5 (float)", opa_number_as_float(opa_cast_number(opa_arith_round(opa_number_float(-1.5)))) == -2);
test("round 2.5 (float)", opa_number_as_float(opa_cast_number(opa_arith_round(opa_number_float(2.5)))) == 3);
test("round -2.5 (float)", opa_number_as_float(opa_cast_number(opa_arith_round(opa_number_float(-2.5)))) == -3);
test("plus 1+2", opa_number_as_float(opa_cast_number(opa_arith_plus(opa_number_float(1), opa_number_float(2)))) == 3);
test("minus 3-2", opa_number_as_float(opa_cast_number(opa_arith_minus(opa_number_float(3), opa_number_float(2)))) == 1);

Expand Down Expand Up @@ -1794,6 +1796,8 @@ void test_strings(void)
test("format_int/16_0", opa_value_compare(opa_strings_format_int(opa_number_float(0), opa_number_int(16)), opa_string_terminated("0")) == 0);
test("format_int/16_1", opa_value_compare(opa_strings_format_int(opa_number_float(1), opa_number_int(16)), opa_string_terminated("1")) == 0);
test("format_int/16_-1", opa_value_compare(opa_strings_format_int(opa_number_float(-1), opa_number_int(16)), opa_string_terminated("-1")) == 0);
test("format_int/16_15.5", opa_value_compare(opa_strings_format_int(opa_number_float(15.5), opa_number_int(16)), opa_string_terminated("f")) == 0);
test("format_int/16_-15.5", opa_value_compare(opa_strings_format_int(opa_number_float(-15.5), opa_number_int(16)), opa_string_terminated("-f")) == 0);
test("format_int/16_16", opa_value_compare(opa_strings_format_int(opa_number_float(16), opa_number_int(16)), opa_string_terminated("10")) == 0);
test("format_int/16_31", opa_value_compare(opa_strings_format_int(opa_number_float(31), opa_number_int(16)), opa_string_terminated("1f")) == 0);

Expand Down