Skip to content

Commit

Permalink
Document overflow behavior, add tests. (google#130)
Browse files Browse the repository at this point in the history
Overflow for int, uint, timestamp, and duration.
  • Loading branch information
JimLarson authored Jun 25, 2020
1 parent 862afab commit e34c5ef
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
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

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."
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" }
}
}
}

0 comments on commit e34c5ef

Please sign in to comment.