Skip to content

Commit a4a341c

Browse files
committed
Add comparators to 'Number' module
1 parent a65375f commit a4a341c

File tree

3 files changed

+166
-4
lines changed

3 files changed

+166
-4
lines changed

lib/Number.trp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
(** In Troupe, there is not per-se an `int` type. Rather, there is a `number` and some integral
22
* operations on them which treats them as if they were 32 bit signed integers. *)
33

4-
let (** Largest (safe) possible integral value. Anything larger than this cannot represent an
4+
let (*--- Constants ---*)
5+
6+
(** Largest (safe) possible integral value. Anything larger than this cannot represent an
57
* increment of 1.
68
*
79
* NOTE: Value copied from the JavaScript documentation for `Number.MAX_SAFE_INTEGER`. *)
@@ -34,6 +36,8 @@ let (** Largest (safe) possible integral value. Anything larger than this cannot
3436
* - inf
3537
*)
3638

39+
(*--- Operations ---*)
40+
3741
(** Returns the absolute value of x *)
3842
fun abs x = if x < 0 then -x else x
3943

@@ -69,6 +73,8 @@ let (** Largest (safe) possible integral value. Anything larger than this cannot
6973
*)
7074
val sqrt = sqrt
7175

76+
(*--- Integer ---*)
77+
7278
(** Returns `x` with the fractional parts removed. *)
7379
fun isInt x = floor x = x
7480

@@ -79,6 +85,25 @@ let (** Largest (safe) possible integral value. Anything larger than this cannot
7985
* are capped. *)
8086
fun toInt32 x = (max (min (maxInt32, x), minInt32)) orb 0
8187

88+
(*--- Comparators ---*)
89+
90+
(** Returns `true` if `x` is smaller than `y`. *)
91+
fun eq x y = x = y
92+
93+
(** Returns `true` if `x` is smaller than `y`. *)
94+
fun lt x y = x < y
95+
96+
(** Returns `true` if `x` is smaller than or equal to `y`. *)
97+
fun le x y = x <= y
98+
99+
(** Returns `true` if `x` is greater than `y`. *)
100+
fun gt x y = x > y
101+
102+
(** Returns `true` if `x` is greater than or equal to `y`. *)
103+
fun ge x y = x >= y
104+
105+
(*--- String Formatting ---*)
106+
82107
(** Returns the string representing the given `number`.
83108
*
84109
* TODO: Move out of the TCB?
@@ -110,6 +135,11 @@ let (** Largest (safe) possible integral value. Anything larger than this cannot
110135
isInt,
111136
toInt,
112137
toInt32,
138+
eq,
139+
lt,
140+
le,
141+
gt,
142+
ge,
113143
toString,
114144
fromString
115145
}

tests/lib/Number.golden

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
2025-09-19T11:39:50.589Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
1+
2025-10-24T09:49:37.065Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
22
begin Number
33
begin maxInt / minInt
44
[ TEST ] it maxInt++ = maxInt [ PASS ] it maxInt++ = maxInt
@@ -84,6 +84,72 @@
8484
[ TEST ] it truncates 2^33-1 [ PASS ] it truncates 2^33-1
8585
[ TEST ] it truncates -2^33-1 [ PASS ] it truncates -2^33-1
8686
end 
87+
begin eq
88+
[ TEST ] it accepts 0 0 [ PASS ] it accepts 0 0
89+
[ TEST ] it accepts 1 1 [ PASS ] it accepts 1 1
90+
[ TEST ] it accepts -1 -1 [ PASS ] it accepts -1 -1
91+
[ TEST ] it accepts 42 42 [ PASS ] it accepts 42 42
92+
[ TEST ] it accepts 1/2 2/4 [ PASS ] it accepts 1/2 2/4
93+
[ TEST ] it rejects 0 1 [ PASS ] it rejects 0 1
94+
[ TEST ] it rejects 1 0 [ PASS ] it rejects 1 0
95+
[ TEST ] it rejects 0 -1 [ PASS ] it rejects 0 -1
96+
[ TEST ] it rejects -1 0 [ PASS ] it rejects -1 0
97+
[ TEST ] it rejects 2 1 [ PASS ] it rejects 2 1
98+
[ TEST ] it rejects 2 1 [ PASS ] it rejects 2 1
99+
[ TEST ] it rejects 42 12 [ PASS ] it rejects 42 12
100+
end 
101+
begin lt
102+
[ TEST ] it accepts 0 1 [ PASS ] it accepts 0 1
103+
[ TEST ] it accepts 1 2 [ PASS ] it accepts 1 2
104+
[ TEST ] it accepts -1 0 [ PASS ] it accepts -1 0
105+
[ TEST ] it accepts 12 42 [ PASS ] it accepts 12 42
106+
[ TEST ] it accepts 0 1/2 [ PASS ] it accepts 0 1/2
107+
[ TEST ] it rejects 0 0 [ PASS ] it rejects 0 0
108+
[ TEST ] it rejects 42 42 [ PASS ] it rejects 42 42
109+
[ TEST ] it rejects 1 0 [ PASS ] it rejects 1 0
110+
[ TEST ] it rejects 2 1 [ PASS ] it rejects 2 1
111+
[ TEST ] it rejects 0 -1 [ PASS ] it rejects 0 -1
112+
[ TEST ] it rejects 42 12 [ PASS ] it rejects 42 12
113+
end 
114+
begin le
115+
[ TEST ] it accepts 0 1 [ PASS ] it accepts 0 1
116+
[ TEST ] it accepts 1 2 [ PASS ] it accepts 1 2
117+
[ TEST ] it accepts -1 0 [ PASS ] it accepts -1 0
118+
[ TEST ] it accepts 12 42 [ PASS ] it accepts 12 42
119+
[ TEST ] it accepts 0 1/2 [ PASS ] it accepts 0 1/2
120+
[ TEST ] it accepts 0 0 [ PASS ] it accepts 0 0
121+
[ TEST ] it accepts 42 42 [ PASS ] it accepts 42 42
122+
[ TEST ] it rejects 1 0 [ PASS ] it rejects 1 0
123+
[ TEST ] it rejects 2 1 [ PASS ] it rejects 2 1
124+
[ TEST ] it rejects 0 -1 [ PASS ] it rejects 0 -1
125+
[ TEST ] it rejects 42 12 [ PASS ] it rejects 42 12
126+
end 
127+
begin gt
128+
[ TEST ] it rejects 0 1 [ PASS ] it rejects 0 1
129+
[ TEST ] it rejects 1 2 [ PASS ] it rejects 1 2
130+
[ TEST ] it rejects -1 0 [ PASS ] it rejects -1 0
131+
[ TEST ] it rejects 12 42 [ PASS ] it rejects 12 42
132+
[ TEST ] it rejects 0 1/2 [ PASS ] it rejects 0 1/2
133+
[ TEST ] it rejects 0 0 [ PASS ] it rejects 0 0
134+
[ TEST ] it rejects 42 42 [ PASS ] it rejects 42 42
135+
[ TEST ] it accepts 1 0 [ PASS ] it accepts 1 0
136+
[ TEST ] it accepts 2 1 [ PASS ] it accepts 2 1
137+
[ TEST ] it accepts 0 -1 [ PASS ] it accepts 0 -1
138+
[ TEST ] it accepts 42 12 [ PASS ] it accepts 42 12
139+
end 
140+
begin ge
141+
[ TEST ] it rejects 0 1 [ PASS ] it rejects 0 1
142+
[ TEST ] it rejects 1 2 [ PASS ] it rejects 1 2
143+
[ TEST ] it rejects -1 0 [ PASS ] it rejects -1 0
144+
[ TEST ] it rejects 12 42 [ PASS ] it rejects 12 42
145+
[ TEST ] it rejects 0 1/2 [ PASS ] it rejects 0 1/2
146+
[ TEST ] it accepts 0 0 [ PASS ] it accepts 0 0
147+
[ TEST ] it accepts 42 42 [ PASS ] it accepts 42 42
148+
[ TEST ] it accepts 1 0 [ PASS ] it accepts 1 0
149+
[ TEST ] it accepts 2 1 [ PASS ] it accepts 2 1
150+
[ TEST ] it accepts 0 -1 [ PASS ] it accepts 0 -1
151+
[ TEST ] it accepts 42 12 [ PASS ] it accepts 42 12
152+
end 
87153
begin toString
88154
[ TEST ] it returns '0' for 0 [ PASS ] it returns '0' for 0
89155
[ TEST ] it returns '0' for 0/2 [ PASS ] it returns '0' for 0/2
@@ -100,6 +166,6 @@
100166
end 
101167
end 
102168

103-
Total: 72
104-
Passes: 72
169+
Total: 128
170+
Passes: 128
105171
>>> Main thread finished with value: true@{}%{}

tests/lib/Number.trp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,72 @@ let val tests = Unit.group "Number" [
8686
, Unit.it "truncates 2^33-1" (Unit.isEq Number.maxInt32 (Number.toInt32 (2*2*2*(1 << 30))))
8787
, Unit.it "truncates -2^33-1" (Unit.isEq Number.minInt32 (Number.toInt32 (-(2*2*2*(1 << 30) - 1))))
8888
],
89+
Unit.group "eq" [
90+
Unit.it "accepts 0 0" (Unit.isTrue (Number.eq 0 0))
91+
, Unit.it "accepts 1 1" (Unit.isTrue (Number.eq 1 1))
92+
, Unit.it "accepts -1 -1" (Unit.isTrue (Number.eq (-1) (-1)))
93+
, Unit.it "accepts 42 42" (Unit.isTrue (Number.eq 42 42))
94+
, Unit.it "accepts 1/2 2/4" (Unit.isTrue (Number.eq (1/2) (2/4)))
95+
, Unit.it "rejects 0 1" (Unit.isFalse (Number.eq 0 1))
96+
, Unit.it "rejects 1 0" (Unit.isFalse (Number.eq 1 0))
97+
, Unit.it "rejects 0 -1" (Unit.isFalse (Number.eq 0 (-1)))
98+
, Unit.it "rejects -1 0" (Unit.isFalse (Number.eq (-1) 0))
99+
, Unit.it "rejects 2 1" (Unit.isFalse (Number.eq 2 1))
100+
, Unit.it "rejects 2 1" (Unit.isFalse (Number.eq 2 1))
101+
, Unit.it "rejects 42 12" (Unit.isFalse (Number.eq 42 12))
102+
],
103+
Unit.group "lt" [
104+
Unit.it "accepts 0 1" (Unit.isTrue (Number.lt 0 1))
105+
, Unit.it "accepts 1 2" (Unit.isTrue (Number.lt 1 2))
106+
, Unit.it "accepts -1 0" (Unit.isTrue (Number.lt (-1) 0))
107+
, Unit.it "accepts 12 42" (Unit.isTrue (Number.lt 12 42))
108+
, Unit.it "accepts 0 1/2" (Unit.isTrue (Number.lt 0 (1/2)))
109+
, Unit.it "rejects 0 0" (Unit.isFalse (Number.lt 0 0))
110+
, Unit.it "rejects 42 42" (Unit.isFalse (Number.lt 42 42))
111+
, Unit.it "rejects 1 0" (Unit.isFalse (Number.lt 1 0))
112+
, Unit.it "rejects 2 1" (Unit.isFalse (Number.lt 2 1))
113+
, Unit.it "rejects 0 -1" (Unit.isFalse (Number.lt 1 (-1)))
114+
, Unit.it "rejects 42 12" (Unit.isFalse (Number.lt 42 12))
115+
],
116+
Unit.group "le" [
117+
Unit.it "accepts 0 1" (Unit.isTrue (Number.le 0 1))
118+
, Unit.it "accepts 1 2" (Unit.isTrue (Number.le 1 2))
119+
, Unit.it "accepts -1 0" (Unit.isTrue (Number.le (-1) 0))
120+
, Unit.it "accepts 12 42" (Unit.isTrue (Number.le 12 42))
121+
, Unit.it "accepts 0 1/2" (Unit.isTrue (Number.le 0 (1/2)))
122+
, Unit.it "accepts 0 0" (Unit.isTrue (Number.le 0 0))
123+
, Unit.it "accepts 42 42" (Unit.isTrue (Number.le 42 42))
124+
, Unit.it "rejects 1 0" (Unit.isFalse (Number.le 1 0))
125+
, Unit.it "rejects 2 1" (Unit.isFalse (Number.le 2 1))
126+
, Unit.it "rejects 0 -1" (Unit.isFalse (Number.le 1 (-1)))
127+
, Unit.it "rejects 42 12" (Unit.isFalse (Number.le 42 12))
128+
],
129+
Unit.group "gt" [
130+
Unit.it "rejects 0 1" (Unit.isFalse (Number.gt 0 1))
131+
, Unit.it "rejects 1 2" (Unit.isFalse (Number.gt 1 2))
132+
, Unit.it "rejects -1 0" (Unit.isFalse (Number.gt (-1) 0))
133+
, Unit.it "rejects 12 42" (Unit.isFalse (Number.gt 12 42))
134+
, Unit.it "rejects 0 1/2" (Unit.isFalse (Number.gt 0 (1/2)))
135+
, Unit.it "rejects 0 0" (Unit.isFalse (Number.gt 0 0))
136+
, Unit.it "rejects 42 42" (Unit.isFalse (Number.gt 42 42))
137+
, Unit.it "accepts 1 0" (Unit.isTrue (Number.gt 1 0))
138+
, Unit.it "accepts 2 1" (Unit.isTrue (Number.gt 2 1))
139+
, Unit.it "accepts 0 -1" (Unit.isTrue (Number.gt 1 (-1)))
140+
, Unit.it "accepts 42 12" (Unit.isTrue (Number.gt 42 12))
141+
],
142+
Unit.group "ge" [
143+
Unit.it "rejects 0 1" (Unit.isFalse (Number.ge 0 1))
144+
, Unit.it "rejects 1 2" (Unit.isFalse (Number.ge 1 2))
145+
, Unit.it "rejects -1 0" (Unit.isFalse (Number.ge (-1) 0))
146+
, Unit.it "rejects 12 42" (Unit.isFalse (Number.ge 12 42))
147+
, Unit.it "rejects 0 1/2" (Unit.isFalse (Number.ge 0 (1/2)))
148+
, Unit.it "accepts 0 0" (Unit.isTrue (Number.ge 0 0))
149+
, Unit.it "accepts 42 42" (Unit.isTrue (Number.ge 42 42))
150+
, Unit.it "accepts 1 0" (Unit.isTrue (Number.ge 1 0))
151+
, Unit.it "accepts 2 1" (Unit.isTrue (Number.ge 2 1))
152+
, Unit.it "accepts 0 -1" (Unit.isTrue (Number.ge 1 (-1)))
153+
, Unit.it "accepts 42 12" (Unit.isTrue (Number.ge 42 12))
154+
],
89155
Unit.group "toString" [
90156
Unit.it "returns '0' for 0" (Unit.isEq "0" (Number.toString 0))
91157
, Unit.it "returns '0' for 0/2" (Unit.isEq "0" (Number.toString (0/2)))

0 commit comments

Comments
 (0)