Skip to content

Commit 5944543

Browse files
committed
add support files for new even optional parameter
1 parent 02e9054 commit 5944543

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 2.3.0 (Next)
22
- Add `truncate` function.
3+
- Add `even` optional parameter to `round` function.
34

45
### 2.2.2
56
- Update `exp`, `mod`, and `sqrt` functions' input parameter type from `float64` to `number`.

examples/functions/round/function.tf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ provider::stdlib::round(1.8)
88

99
# Return the rounding of 1.5:
1010
provider::stdlib::round(1.5)
11-
# result => 2
11+
# result => 2
12+
13+
# Return the even tiebreak rounding of 2.5:
14+
provider::stdlib::round(2.5, true)
15+
# result => 2
16+
17+
# Return the normal tiebreak rounding of 2.5:
18+
provider::stdlib::round(2.5, false)
19+
# result => 3
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Return the sorted list of integers:
2-
provider::stdlib::sort_list_string([0, 4, -10, 8])
2+
provider::stdlib::sort_list_number([0, 4, -10, 8])
33
# result => [-10, 0, 4, 8]
44

55
# Return the sorted list of floats:
6-
provider::stdlib::sort_list_string([9.0, 45.5, 123.4, -0.5])
6+
provider::stdlib::sort_list_number([9.0, 45.5, 123.4, -0.5])
77
# result => [-0.5, 9.0, 45.5, 123.4]

stdlib/number/round.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (*roundFunction) Definition(_ context.Context, _ function.DefinitionRequest
3838
},
3939
VariadicParameter: function.BoolParameter{
4040
Name: "even",
41-
MarkdownDescription: "If true then round to the nearest even integer instead. Specifying `false` is the same as not providing an input value for this parameter (default behavior).",
41+
MarkdownDescription: "If true then round 'ties' (i.e. `.5`) to the nearest even integer instead. Specifying `false` is the same as not providing an input value for this parameter (default behavior).",
4242
},
4343
Return: function.Int64Return{},
4444
}

stdlib/number/round_test.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,47 @@ func TestRoundFunction(test *testing.T) {
1919
testCases := util.TestCases{
2020
"round-down": {
2121
Request: function.RunRequest{
22-
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(1.2)}),
22+
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(1.2), types.TupleValueMust([]attr.Type{}, []attr.Value{})}),
2323
},
2424
Expected: function.RunResponse{
2525
Result: function.NewResultData(types.Int64Value(1)),
2626
},
2727
},
2828
"round-up": {
2929
Request: function.RunRequest{
30-
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(1.8)}),
30+
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(1.8), types.TupleValueMust([]attr.Type{}, []attr.Value{})}),
3131
},
3232
Expected: function.RunResponse{
3333
Result: function.NewResultData(types.Int64Value(2)),
3434
},
3535
},
3636
"round-half": {
3737
Request: function.RunRequest{
38-
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(1.5)}),
38+
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(1.5), types.TupleValueMust([]attr.Type{}, []attr.Value{})}),
3939
},
4040
Expected: function.RunResponse{
4141
Result: function.NewResultData(types.Int64Value(2)),
4242
},
4343
},
44+
"round-half-even-true": {
45+
Request: function.RunRequest{
46+
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(2.5), types.TupleValueMust([]attr.Type{types.BoolType}, []attr.Value{types.BoolValue(true)})}),
47+
},
48+
Expected: function.RunResponse{
49+
Result: function.NewResultData(types.Int64Value(2)),
50+
},
51+
},
52+
"round-half-even-false": {
53+
Request: function.RunRequest{
54+
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(2.5), types.TupleValueMust([]attr.Type{types.BoolType}, []attr.Value{types.BoolValue(false)})}),
55+
},
56+
Expected: function.RunResponse{
57+
Result: function.NewResultData(types.Int64Value(3)),
58+
},
59+
},
4460
"beyond-upper-limit": {
4561
Request: function.RunRequest{
46-
Arguments: function.NewArgumentsData([]attr.Value{types.NumberValue(func() *big.Float { f := new(big.Float); f.SetString("1e+310"); return f }())}),
62+
Arguments: function.NewArgumentsData([]attr.Value{types.NumberValue(func() *big.Float { f := new(big.Float); f.SetString("1e+310"); return f }()), types.TupleValueMust([]attr.Type{}, []attr.Value{})}),
4763
},
4864
Expected: function.RunResponse{
4965
Result: resultData,
@@ -52,7 +68,7 @@ func TestRoundFunction(test *testing.T) {
5268
},
5369
"overflow": {
5470
Request: function.RunRequest{
55-
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(9.223372036854777e+18)}), // math.MaxInt64 + 1
71+
Arguments: function.NewArgumentsData([]attr.Value{types.Float64Value(9.223372036854777e+18), types.TupleValueMust([]attr.Type{}, []attr.Value{})}), // math.MaxInt64 + 1
5672
},
5773
Expected: function.RunResponse{
5874
Result: resultData,

0 commit comments

Comments
 (0)