Skip to content

Commit

Permalink
FIX: allow comparison of char! with integer!
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Apr 2, 2021
1 parent 57b315b commit 4b2edac
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/core/n-math.c
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ enum {SINE, COSINE, TANGENT};
SET_MONEY(a, int_to_deci(VAL_INT64(a)));
goto compare;
}
else if (tb == REB_INTEGER) // special negative?, zero?, ...
else if (tb == REB_INTEGER || tb == REB_CHAR) // special negative?, zero?, ...
goto compare;
break;

Expand Down Expand Up @@ -621,8 +621,13 @@ enum {SINE, COSINE, TANGENT};
case REB_TAG:
if (ANY_STR(b)) goto compare;
break;
}

case REB_CHAR:
//o: using sepparated case as I don't want to allow comparison
//o: with not integer numbers (money, decimal, percent)
if (tb == REB_INTEGER)
goto compare;
}
if (strictness == 0 || strictness == 1) return FALSE;
//if (strictness >= 2)
Trap2(RE_INVALID_COMPARE, Of_Type(a), Of_Type(b));
Expand Down
1 change: 1 addition & 0 deletions src/tests/run-tests.r3
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dt [ ;- delta time
%units/bincode-test.r3
%units/bitset-test.r3
%units/codecs-test.r3
%units/compare-test.r3
%units/compress-test.r3
%units/conditional-test.r3
%units/crash-test.r3
Expand Down
65 changes: 65 additions & 0 deletions src/tests/units/compare-test.r3
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
Rebol [
Title: "Rebol3 compare test script"
Author: "Oldes, Peter W A Wood"
File: %compare-test.r3
Tabs: 4
Needs: [%../quick-test-module.r3]
]

~~~start-file~~~ "COMPARE"

===start-group=== "char!"
--test-- "char! = ..."
--assert #"a" = #"a"
--assert #"a" = 97

--test-- "char! <> ..."
--assert #"a" <> #"b"
--assert #"a" <> 98
--assert #"a" <> 97.0
--assert #"a" <> $97
--assert #"a" <> 97%
--assert #"a" <> "a"

--test-- "char! < ..."
--assert #"a" < #"b"
--assert #"a" < 98

--test-- "char! > ..."
--assert #"b" > #"a"
--assert #"a" > 96

--test-- "char! invalid compare"
--assert all [error? e: try [#"a" < 98.0] e/id = 'invalid-compare]
--assert all [error? e: try [#"a" < $98 ] e/id = 'invalid-compare]
--assert all [error? e: try [#"a" < 98% ] e/id = 'invalid-compare]
--assert all [error? e: try [#"a" < "a" ] e/id = 'invalid-compare]
--assert all [error? e: try [#"a" < 1x1 ] e/id = 'invalid-compare]

===end-group===

===start-group=== "integer!"
--test-- "integer! = ..."
--assert 97 = 97
--assert 97 = 97.0
--assert 97 = 9700%
--assert 97 = #"a"

--test-- "integer! < ..."
--assert 97 < 98
--assert 97 < 97.1
--assert 97 < 9701%
--assert 97 < #"b"

--test-- "integer! > ..."
--assert 97 > 96
--assert 97 > 96.0
--assert 97 > 9600%
--assert 98 > #"a"

--test-- "integer! invalid compare"
--assert all [error? e: try [90 < "a" ] e/id = 'invalid-compare]
--assert all [error? e: try [90 < 1x1 ] e/id = 'invalid-compare]
===end-group===

~~~end-file~~~

0 comments on commit 4b2edac

Please sign in to comment.