1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
Module: int
*/
/*
Const: max_value
The maximum value of an integer
*/
// FIXME: Find another way to access the machine word size in a const expr
#[cfg(target_arch="x86")]
const max_value: int = (-1 << 31)-1;
#[cfg(target_arch="x86_64")]
const max_value: int = (-1 << 63)-1;
/*
Const: min_value
The minumum value of an integer
*/
#[cfg(target_arch="x86")]
const min_value: int = -1 << 31;
#[cfg(target_arch="x86_64")]
const min_value: int = -1 << 63;
/* Function: add */
pure fn add(x: int, y: int) -> int { ret x + y; }
/* Function: sub */
pure fn sub(x: int, y: int) -> int { ret x - y; }
/* Function: mul */
pure fn mul(x: int, y: int) -> int { ret x * y; }
/* Function: div */
pure fn div(x: int, y: int) -> int { ret x / y; }
/* Function: rem */
pure fn rem(x: int, y: int) -> int { ret x % y; }
/* Predicate: lt */
pure fn lt(x: int, y: int) -> bool { ret x < y; }
/* Predicate: le */
pure fn le(x: int, y: int) -> bool { ret x <= y; }
/* Predicate: eq */
pure fn eq(x: int, y: int) -> bool { ret x == y; }
/* Predicate: ne */
pure fn ne(x: int, y: int) -> bool { ret x != y; }
/* Predicate: ge */
pure fn ge(x: int, y: int) -> bool { ret x >= y; }
/* Predicate: gt */
pure fn gt(x: int, y: int) -> bool { ret x > y; }
/* Predicate: positive */
pure fn positive(x: int) -> bool { ret x > 0; }
/* Predicate: negative */
pure fn negative(x: int) -> bool { ret x < 0; }
/* Predicate: nonpositive */
pure fn nonpositive(x: int) -> bool { ret x <= 0; }
/* Predicate: nonnegative */
pure fn nonnegative(x: int) -> bool { ret x >= 0; }
// FIXME: Make sure this works with negative integers.
/*
Function: hash
Produce a uint suitable for use in a hash table
*/
fn hash(x: int) -> uint { ret x as uint; }
/*
Function: range
Iterate over the range [`lo`..`hi`)
*/
fn range(lo: int, hi: int, it: block(int)) {
let i = lo;
while i < hi { it(i); i += 1; }
}
/*
Function: parse_buf
Parse a buffer of bytes
Parameters:
buf - A byte buffer
radix - The base of the number
Failure:
buf must not be empty
*/
fn parse_buf(buf: [u8], radix: uint) -> int {
if vec::len::<u8>(buf) == 0u {
#error("parse_buf(): buf is empty");
fail;
}
let i = vec::len::<u8>(buf) - 1u;
let start = 0u;
let power = 1;
if buf[0] == ('-' as u8) {
power = -1;
start = 1u;
}
let n = 0;
while true {
let digit = char::to_digit(buf[i] as char);
if (digit as uint) >= radix {
fail;
}
n += (digit as int) * power;
power *= radix as int;
if i <= start { ret n; }
i -= 1u;
}
fail;
}
/*
Function: from_str
Parse a string to an int
Failure:
s must not be empty
*/
fn from_str(s: str) -> int { parse_buf(str::bytes(s), 10u) }
/*
Function: to_str
Convert to a string in a given base
*/
fn to_str(n: int, radix: uint) -> str {
assert (0u < radix && radix <= 16u);
ret if n < 0 {
"-" + uint::to_str(-n as uint, radix)
} else { uint::to_str(n as uint, radix) };
}
/*
Function: str
Convert to a string
*/
fn str(i: int) -> str { ret to_str(i, 10u); }
/*
Function: pow
Returns `base` raised to the power of `exponent`
*/
fn pow(base: int, exponent: uint) -> int {
if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
if base == 0 { ret 0; }
let my_pow = exponent;
let acc = 1;
let multiplier = base;
while(my_pow > 0u) {
if my_pow % 2u == 1u {
acc *= multiplier;
}
my_pow /= 2u;
multiplier *= multiplier;
}
ret acc;
}
#[test]
fn test_from_str() {
assert(from_str("0") == 0);
assert(from_str("3") == 3);
assert(from_str("10") == 10);
assert(from_str("123456789") == 123456789);
assert(from_str("00100") == 100);
assert(from_str("-1") == -1);
assert(from_str("-3") == -3);
assert(from_str("-10") == -10);
assert(from_str("-123456789") == -123456789);
assert(from_str("-00100") == -100);
}
#[test]
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_from_str_fail_1() {
from_str(" ");
}
#[test]
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_from_str_fail_2() {
from_str("x");
}
#[test]
fn test_parse_buf() {
import str::bytes;
assert (parse_buf(bytes("123"), 10u) == 123);
assert (parse_buf(bytes("1001"), 2u) == 9);
assert (parse_buf(bytes("123"), 8u) == 83);
assert (parse_buf(bytes("123"), 16u) == 291);
assert (parse_buf(bytes("ffff"), 16u) == 65535);
assert (parse_buf(bytes("FFFF"), 16u) == 65535);
assert (parse_buf(bytes("z"), 36u) == 35);
assert (parse_buf(bytes("Z"), 36u) == 35);
assert (parse_buf(bytes("-123"), 10u) == -123);
assert (parse_buf(bytes("-1001"), 2u) == -9);
assert (parse_buf(bytes("-123"), 8u) == -83);
assert (parse_buf(bytes("-123"), 16u) == -291);
assert (parse_buf(bytes("-ffff"), 16u) == -65535);
assert (parse_buf(bytes("-FFFF"), 16u) == -65535);
assert (parse_buf(bytes("-z"), 36u) == -35);
assert (parse_buf(bytes("-Z"), 36u) == -35);
}
#[test]
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_parse_buf_fail_1() {
parse_buf(str::bytes("Z"), 35u);
}
#[test]
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_parse_buf_fail_2() {
parse_buf(str::bytes("-9"), 2u);
}
#[test]
fn test_to_str() {
import str::eq;
assert (eq(to_str(0, 10u), "0"));
assert (eq(to_str(1, 10u), "1"));
assert (eq(to_str(-1, 10u), "-1"));
assert (eq(to_str(255, 16u), "ff"));
assert (eq(to_str(100, 10u), "100"));
}
#[test]
fn test_pow() {
assert (pow(0, 0u) == 1);
assert (pow(0, 1u) == 0);
assert (pow(0, 2u) == 0);
assert (pow(-1, 0u) == 1);
assert (pow(1, 0u) == 1);
assert (pow(-3, 2u) == 9);
assert (pow(-3, 3u) == -27);
assert (pow(4, 9u) == 262144);
}
#[test]
fn test_overflows() {
assert (max_value > 0);
assert (min_value <= 0);
assert (min_value + max_value + 1 == 0);
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
|