Skip to content

Commit

Permalink
CHANGE: don't allow to conversion from logic! to integer!, `mon…
Browse files Browse the repository at this point in the history
…ey!` and `percent!`

related to: Oldes/Rebol-issues#1018
  • Loading branch information
Oldes committed Dec 8, 2020
1 parent bf649c2 commit d07af99
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/core/t-decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ REBOOL almost_equal(REBDEC a, REBDEC b, REBCNT max_diff) {
goto setDec;

case REB_LOGIC:
if (action != A_MAKE) goto err_make;
d1 = VAL_LOGIC(val) ? 1.0 : 0.0;
goto setDec;

Expand All @@ -377,7 +378,7 @@ REBOOL almost_equal(REBDEC a, REBDEC b, REBCNT max_diff) {
if (type == REB_PERCENT) break;
goto setDec;
}
Trap_Make(type, val);
goto err_make;
}

case REB_BINARY:
Expand Down Expand Up @@ -413,8 +414,10 @@ REBOOL almost_equal(REBDEC a, REBDEC b, REBCNT max_diff) {
exp--, d1 *= 10.0, Check_Overflow(d1);
while (exp <= -1)
exp++, d1 /= 10.0;
} else
} else {
err_make:
Trap_Make(type, val);
}
}

if (type == REB_PERCENT) d1 /= 100.0;
Expand Down
5 changes: 3 additions & 2 deletions src/core/t-money.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
const REBYTE *end;
str = Qualify_String(arg, 36, 0, FALSE);
VAL_DECI(D_RET) = string_to_deci(str, &end);
if (end == str || *end != 0) Trap_Make(REB_MONEY, arg);
if (end == str || *end != 0) goto err;
break;
}

Expand All @@ -228,8 +228,9 @@
break;

case REB_LOGIC:
if(action != A_MAKE) goto err;
equal = !VAL_LOGIC(arg);
// case REB_NONE: // 'equal defaults to 1
// case REB_NONE: // 'equal defaults to 1 // removed by design
VAL_DECI(D_RET) = int_to_deci(equal ? 0 : 1);
break;

Expand Down
60 changes: 59 additions & 1 deletion src/tests/units/make-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,44 @@ Rebol [
===end-group===


===start-group=== "make decimal!"
===start-group=== "make/to integer"
--test-- "to integer! logic!"
;@@ https://github.com/Oldes/Rebol-issues/issues/1018
--assert all [
error? e: try [to integer! true]
e/id = 'bad-make-arg
]
--assert all [
error? e: try [to integer! false]
e/id = 'bad-make-arg
]
--test-- "make integer! logic!"
;@@ https://github.com/Oldes/Rebol-issues/issues/1018
--assert 1 = make integer! true
--assert 0 = make integer! false
--assert 1 = make 42 true
--assert 0 = make 42 false
--assert error? try [to integer! true]
--assert error? try [to integer! false]

===end-group===

===start-group=== "make/to decimal!"
--test-- "to decimal! issue!"
;@@ https://github.com/Oldes/Rebol-issues/issues/1130
--assert all [
error? e: try [to decimal! #FF]
e/id = 'bad-make-arg
]
--test-- "make/to decimal! logic!"
;@@ https://github.com/Oldes/Rebol-issues/issues/1018
--assert 1.0 = make decimal! true
--assert 0.0 = make decimal! false
--assert 1.0 = make 0.0 true
--assert 0.0 = make 0.0 false
--assert error? try [to decimal! true]
--assert error? try [to decimal! false]

===end-group===


Expand All @@ -64,6 +95,32 @@ Rebol [
error? e: try [to money! #FF]
e/id = 'bad-make-arg
]
--test-- "make/to money! logic!"
;@@ https://github.com/Oldes/Rebol-issues/issues/1018
--assert $1 = make money! true
--assert $0 = make money! false
--assert $1 = make $111 true
--assert $0 = make $111 false
--assert error? try [to money! true]
--assert error? try [to money! false]
===end-group===


===start-group=== "make percent!"
--test-- "to percent! issue!"
;@@ https://github.com/Oldes/Rebol-issues/issues/1130
--assert all [
error? e: try [to percent! #FF]
e/id = 'bad-make-arg
]
--test-- "make/to percent! logic!"
;@@ https://github.com/Oldes/Rebol-issues/issues/1018
--assert 100% = make percent! true
--assert 0% = make percent! false
--assert 100% = make 50% true
--assert 0% = make 50% false
--assert error? try [to percent! true]
--assert error? try [to percent! false]
===end-group===


Expand All @@ -85,6 +142,7 @@ Rebol [

===start-group=== "make special"
--test-- "make types from none!"
;@@ https://github.com/Oldes/Rebol-issues/issues/1018
;@@ https://github.com/Oldes/Rebol-issues/issues/1041
--assert error? try [make end! none]
--assert not error? try [make unset! none]
Expand Down

0 comments on commit d07af99

Please sign in to comment.