Skip to content

Commit df959a5

Browse files
committed
FIX: improved doc-string for round/to action and added a few related unit tests
Fixes: metaeducation/rebol-issues#1470
1 parent 6992326 commit df959a5

File tree

5 files changed

+120
-5
lines changed

5 files changed

+120
-5
lines changed

src/boot/actions.r

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ round: action [
9494
{Rounds a numeric value; halves round up (away from zero) by default.}
9595
value [number! pair! money! time!] "The value to round"
9696
/to "Return the nearest multiple of the scale parameter"
97-
scale [number! money! time!] "Must be a non-zero value"
97+
scale [number! money! time!] "Must be a non-zero value (result will be of this type)"
9898
/even "Halves round toward even results"
9999
/down "Round toward zero, ignoring discarded digits. (truncate)"
100100
/half-down "Halves round toward zero"

src/tests/run-tests.r3

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ dt [ ;- delta time
1313
wrap load %units/enbase-test.r3
1414
wrap load %units/map-test.r3
1515
wrap load %units/integer-test.r3
16+
wrap load %units/decimal-test.r3
17+
wrap load %units/money-test.r3
1618
wrap load %units/power-test.r3
1719
wrap load %units/mezz-crypt-test.r3
1820
wrap load %units/rc4-test.r3

src/tests/units/decimal-test.r3

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Rebol [
2+
Title: "Rebol3 decimal test script"
3+
Author: "Oldes, Peter W A Wood"
4+
File: %decimal-test.r3
5+
Tabs: 4
6+
Needs: [%../quick-test-module.r3]
7+
]
8+
9+
10+
~~~start-file~~~ "decimal"
11+
12+
===start-group=== "round"
13+
--test-- "round"
14+
--assert 1.0 = round 1.4999
15+
--assert 2.0 = round 1.5
16+
--assert -2.0 = round -1.5
17+
18+
--test-- "round/to (decimal)"
19+
--assert 1.375 = round/to 1.333 .125
20+
--assert 1.33 = round/to 1.333 .01
21+
22+
--test-- "round/to (integer)"
23+
--assert 1 = round/to 0.5 1
24+
--assert 0 = round/to 0.499 1
25+
--assert integer? round/to 0.5 1
26+
27+
--test-- "round/to (money)"
28+
--assert $1 = round/to 0.5 $1
29+
--assert $0 = round/to 0.499 $1
30+
--assert money? round/to 0.5 $1
31+
32+
--test-- "round/down"
33+
--assert 1.0 = round/down 1.999
34+
--assert -1.0 = round/down -1.999
35+
36+
--test-- "round/even"
37+
--assert 2.0 = round/even 1.5
38+
--assert -2.0 = round/even -1.5
39+
40+
--test-- "round/half-down"
41+
--assert 1.0 = round/half-down 1.5
42+
--assert -1.0 = round/half-down -1.5
43+
44+
--test-- "round/floor"
45+
--assert 1.0 = round/floor 1.999
46+
--assert -2.0 = round/floor -1.0000001
47+
48+
--test-- "round/ceiling"
49+
--assert 2.0 = round/ceiling 1.0000001
50+
--assert -1.0 = round/ceiling -1.999
51+
52+
--test-- "round/half-ceiling"
53+
--assert 2.0 = round/half-ceiling 1.5
54+
--assert -1.0 = round/half-ceiling -1.5
55+
56+
===end-group===
57+
58+
~~~end-file~~~

src/tests/units/integer-test.r3

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
Rebol [
22
Title: "Rebol3 integer test script"
33
Author: "Oldes, Peter W A Wood"
4-
File: %enbase-test.r3
4+
File: %integer-test.r3
55
Tabs: 4
66
Needs: [%../quick-test-module.r3]
77
]
88

9-
;; These supplement the bulk of the integer tests which are automatically
10-
;; generated.
11-
129
~~~start-file~~~ "integer"
1310

1411
===start-group=== "shift op!"

src/tests/units/money-test.r3

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Rebol [
2+
Title: "Rebol3 money test script"
3+
Author: "Oldes, Peter W A Wood"
4+
File: %money-test.r3
5+
Tabs: 4
6+
Needs: [%../quick-test-module.r3]
7+
]
8+
9+
10+
~~~start-file~~~ "money"
11+
12+
===start-group=== "round"
13+
--test-- "round"
14+
--assert $1 = round $1.4999
15+
--assert $2 = round $1.5
16+
--assert -$2 = round -$1.5
17+
18+
--test-- "round/to"
19+
--assert $1.375 = round/to $1.333 $.125
20+
--assert $1.33 = round/to $1.333 $.01
21+
--assert $1 = round/to $0.5 $1
22+
--assert $0 = round/to $0.499 $1
23+
24+
--test-- "round/to (decimal)"
25+
--assert 1.375 = round/to $1.333 .125
26+
--assert 1.33 = round/to $1.333 .01
27+
28+
--test-- "round/to (integer)"
29+
--assert 1 = round/to $0.5 1
30+
--assert 0 = round/to $0.499 1
31+
32+
--test-- "round/down"
33+
--assert $1 = round/down $1.999
34+
--assert -$1 = round/down -$1.999
35+
36+
--test-- "round/even"
37+
--assert $2 = round/even $1.5
38+
--assert -$2 = round/even -$1.5
39+
40+
--test-- "round/half-down"
41+
--assert $1 = round/half-down $1.5
42+
--assert -$1 = round/half-down -$1.5
43+
44+
--test-- "round/floor"
45+
--assert $1 = round/floor $1.999
46+
--assert -$2 = round/floor -$1.0000001
47+
48+
--test-- "round/ceiling"
49+
--assert $2 = round/ceiling $1.0000001
50+
--assert -$1 = round/ceiling -$1.999
51+
52+
--test-- "round/half-ceiling"
53+
--assert $2 = round/half-ceiling $1.5
54+
--assert -$1 = round/half-ceiling -$1.5
55+
56+
===end-group===
57+
58+
~~~end-file~~~

0 commit comments

Comments
 (0)