From 6456a8d7f0122a486d6597bae7e53bb2a7251773 Mon Sep 17 00:00:00 2001 From: Jim Larson Date: Wed, 24 Jun 2020 13:40:33 -0700 Subject: [PATCH 1/2] Document overflow behavior, add tests. --- doc/langdef.md | 6 ++ tests/simple/testdata/timestamps.textproto | 64 ++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/doc/langdef.md b/doc/langdef.md index 1f9ac013..ad3b9a64 100644 --- a/doc/langdef.md +++ b/doc/langdef.md @@ -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: diff --git a/tests/simple/testdata/timestamps.textproto b/tests/simple/testdata/timestamps.textproto index bbbcfede..d810ad79 100644 --- a/tests/simple/testdata/timestamps.textproto +++ b/tests/simple/testdata/timestamps.textproto @@ -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" } + } + } +} From 099c004c3a71a03e72fb531c5f950a18128d0752 Mon Sep 17 00:00:00 2001 From: Jim Larson Date: Wed, 24 Jun 2020 16:58:59 -0700 Subject: [PATCH 2/2] Add test for negating smallest signed int. --- tests/simple/testdata/integer_math.textproto | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/simple/testdata/integer_math.textproto b/tests/simple/testdata/integer_math.textproto index 5c54b338..4662ac0b 100644 --- a/tests/simple/testdata/integer_math.textproto +++ b/tests/simple/testdata/integer_math.textproto @@ -190,8 +190,6 @@ section { eval_error: { errors: { message: "return error for overflow" } } - ## Current behavior: - ## value: { int64_value: -9223372036854775808 } } test { name: "int64_overflow_negative" @@ -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"