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

Document overflow behavior, add tests. #130

Merged
merged 2 commits into from
Jun 25, 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
6 changes: 6 additions & 0 deletions doc/langdef.md
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,12 @@ values. The ordering operators obey the usual algebraic properties, i.e. `e1 <=
e2` gives the same result as `!(e1 > e2)` as well as `(e1 < e2) || (e1 == e2)`
when the expressions involved do not have side effects.

### Overflow
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a test to ensure negating the minimum int value will overflow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks, good catch. In cel-go it wraps back to itself, but for consistency we should expect an error.


Arithmetic operations raise an error when the results exceed the range of the
integer type (int, uint) or the timestamp or duration type. An error is also
raised for conversions which exceed the range of the target type.

### Timezones

Timezones are expressed in the following grammar:
Expand Down
12 changes: 8 additions & 4 deletions tests/simple/testdata/integer_math.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ section {
eval_error: {
errors: { message: "return error for overflow" }
}
## Current behavior:
## value: { int64_value: -9223372036854775808 }
}
test {
name: "int64_overflow_negative"
Expand All @@ -200,8 +198,14 @@ section {
eval_error: {
errors: { message: "return error for overflow" }
}
## Current behavior:
## value: { int64_value: 9223372036854775807 }
}
test {
name: "int64_min_negate"
description: "Negated LLONG_MIN is not representable."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra L in LONG?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Gabriel was using standard C constants, where "long" might only be 32-bits, so it's for "long long min", guaranteeing at least 64 bits. Long long could be bigger than that, but C's flexibility for integer sizes is mostly theoretical these days - at least outside the embedded market.

expr: "-(-9223372036854775808)"
eval_error: {
errors: { message: "return error for overflow" }
}
}
test {
name: "uint64_overflow_positive"
Expand Down
64 changes: 64 additions & 0 deletions tests/simple/testdata/timestamps.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,67 @@ section {
value: { int64_value: 3730 }
}
}
section {
name: "timestamp_range"
description: "Tests for out-of-range operations on timestamps."
test {
name: "from_string_under"
expr: "timestamp('0000-01-01T00:00:00Z')"
eval_error {
errors { message: "range" }
}
}
test {
name: "from_string_over"
expr: "timestamp('10000-01-01T00:00:00Z')"
eval_error {
errors { message: "range" }
}
}
test {
name: "add_duration_under"
expr: "timestamp('0001-01-01T00:00:00Z') - duration('10s')"
eval_error {
errors { message: "range" }
}
}
test {
name: "add_duration_over"
expr: "timestamp('9999-12-31T23:59:59Z') + duration('10s')"
eval_error {
errors { message: "range" }
}
}
}
section {
name: "duration_range"
description: "Tests for out-of-range operations on durations."
test {
name: "from_string_under"
expr: "duration('-320000000000s')"
eval_error {
errors { message: "range" }
}
}
test {
name: "from_string_over"
expr: "duration('320000000000s')"
eval_error {
errors { message: "range" }
}
}
test {
name: "add_under"
expr: "duration('-200000000000s') + duration('-200000000000s')"
eval_error {
errors { message: "range" }
}
}
test {
name: "add_over"
expr: "duration('200000000000s') + duration('200000000000s')"
eval_error {
errors { message: "range" }
}
}
}