Skip to content

Commit 17bf4b0

Browse files
committed
libcore: Move core tests into libcore
1 parent de4053f commit 17bf4b0

35 files changed

+1990
-2068
lines changed

src/libcore/bool.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ fn all_values(blk: block(v: t)) {
8585
)]
8686
pure fn to_bit(v: t) -> u8 { if v { 1u8 } else { 0u8 } }
8787

88+
#[test]
89+
fn test_bool_from_str() {
90+
all_values { |v|
91+
assert v == from_str(bool::to_str(v))
92+
}
93+
}
94+
95+
#[test]
96+
fn test_bool_to_str() {
97+
assert to_str(false) == "false";
98+
assert to_str(true) == "true";
99+
}
100+
101+
#[test]
102+
fn test_bool_to_bit() {
103+
all_values { |v|
104+
assert to_bit(v) == if is_true(v) { 1u8 } else { 0u8 };
105+
}
106+
}
107+
88108
// Local Variables:
89109
// mode: rust;
90110
// fill-column: 78;

src/libcore/box.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@ pure fn ptr_eq<T>(a: @T, b: @T) -> bool {
1111
ret a_ptr == b_ptr;
1212
}
1313
}
14+
15+
#[test]
16+
fn test() {
17+
let x = @3;
18+
let y = @3;
19+
assert (ptr_eq::<int>(x, x));
20+
assert (ptr_eq::<int>(y, y));
21+
assert (!ptr_eq::<int>(x, y));
22+
assert (!ptr_eq::<int>(y, x));
23+
}

src/libcore/char.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,77 @@ pure fn cmp(a: char, b: char) -> int {
147147
else if b < a { 1 }
148148
else { 0 }
149149
}
150+
151+
#[test]
152+
fn test_is_lowercase() {
153+
assert is_lowercase('a');
154+
assert is_lowercase('ö');
155+
assert is_lowercase('ß');
156+
assert !is_lowercase('Ü');
157+
assert !is_lowercase('P');
158+
}
159+
160+
#[test]
161+
fn test_is_uppercase() {
162+
assert !is_uppercase('h');
163+
assert !is_uppercase('ä');
164+
assert !is_uppercase('ß');
165+
assert is_uppercase('Ö');
166+
assert is_uppercase('T');
167+
}
168+
169+
#[test]
170+
fn test_is_whitespace() {
171+
assert is_whitespace(' ');
172+
assert is_whitespace('\u2007');
173+
assert is_whitespace('\t');
174+
assert is_whitespace('\n');
175+
176+
assert !is_whitespace('a');
177+
assert !is_whitespace('_');
178+
assert !is_whitespace('\u0000');
179+
}
180+
181+
#[test]
182+
fn test_to_digit() {
183+
assert (to_digit('0') == 0u8);
184+
assert (to_digit('1') == 1u8);
185+
assert (to_digit('2') == 2u8);
186+
assert (to_digit('9') == 9u8);
187+
assert (to_digit('a') == 10u8);
188+
assert (to_digit('A') == 10u8);
189+
assert (to_digit('b') == 11u8);
190+
assert (to_digit('B') == 11u8);
191+
assert (to_digit('z') == 35u8);
192+
assert (to_digit('Z') == 35u8);
193+
}
194+
195+
#[test]
196+
#[should_fail]
197+
#[ignore(cfg(target_os = "win32"))]
198+
fn test_to_digit_fail_1() {
199+
to_digit(' ');
200+
}
201+
202+
#[test]
203+
#[should_fail]
204+
#[ignore(cfg(target_os = "win32"))]
205+
fn test_to_digit_fail_2() {
206+
to_digit('$');
207+
}
208+
209+
#[test]
210+
fn test_to_lower() {
211+
assert (to_lower('H') == 'h');
212+
assert (to_lower('e') == 'e');
213+
//assert (to_lower('Ö') == 'ö');
214+
assert (to_lower('ß') == 'ß');
215+
}
216+
217+
#[test]
218+
fn test_to_upper() {
219+
assert (to_upper('l') == 'L');
220+
assert (to_upper('Q') == 'Q');
221+
//assert (to_upper('ü') == 'Ü');
222+
assert (to_upper('ß') == 'ß');
223+
}

src/libcore/comm.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,46 @@ fn recv_<T: send>(p: *rustrt::rust_port) -> T {
156156
fn chan<T: send>(p: port<T>) -> chan<T> {
157157
chan_t(task::get_task(), rustrt::get_port_id(***p))
158158
}
159+
160+
#[test]
161+
fn create_port_and_chan() { let p = port::<int>(); chan(p); }
162+
163+
#[test]
164+
fn send_int() {
165+
let p = port::<int>();
166+
let c = chan(p);
167+
send(c, 22);
168+
}
169+
170+
#[test]
171+
fn send_recv_fn() {
172+
let p = port::<int>();
173+
let c = chan::<int>(p);
174+
send(c, 42);
175+
assert (recv(p) == 42);
176+
}
177+
178+
#[test]
179+
fn send_recv_fn_infer() {
180+
let p = port();
181+
let c = chan(p);
182+
send(c, 42);
183+
assert (recv(p) == 42);
184+
}
185+
186+
#[test]
187+
fn chan_chan_infer() {
188+
let p = port(), p2 = port::<int>();
189+
let c = chan(p);
190+
send(c, chan(p2));
191+
recv(p);
192+
}
193+
194+
#[test]
195+
fn chan_chan() {
196+
let p = port::<chan<int>>(), p2 = port::<int>();
197+
let c = chan(p);
198+
send(c, chan(p2));
199+
recv(p);
200+
}
201+

src/libcore/either.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,96 @@ pure fn is_right<T, U>(eith: t<T, U>) -> bool {
130130
// buffer-file-coding-system: utf-8-unix
131131
// End:
132132
//
133+
134+
#[test]
135+
fn test_either_left() {
136+
let val = left(10);
137+
fn f_left(&&x: int) -> bool { x == 10 }
138+
fn f_right(&&_x: uint) -> bool { false }
139+
assert (either(f_left, f_right, val));
140+
}
141+
142+
#[test]
143+
fn test_either_right() {
144+
let val = right(10u);
145+
fn f_left(&&_x: int) -> bool { false }
146+
fn f_right(&&x: uint) -> bool { x == 10u }
147+
assert (either(f_left, f_right, val));
148+
}
149+
150+
#[test]
151+
fn test_lefts() {
152+
let input = [left(10), right(11), left(12), right(13), left(14)];
153+
let result = lefts(input);
154+
assert (result == [10, 12, 14]);
155+
}
156+
157+
#[test]
158+
fn test_lefts_none() {
159+
let input: [t<int, int>] = [right(10), right(10)];
160+
let result = lefts(input);
161+
assert (vec::len(result) == 0u);
162+
}
163+
164+
#[test]
165+
fn test_lefts_empty() {
166+
let input: [t<int, int>] = [];
167+
let result = lefts(input);
168+
assert (vec::len(result) == 0u);
169+
}
170+
171+
#[test]
172+
fn test_rights() {
173+
let input = [left(10), right(11), left(12), right(13), left(14)];
174+
let result = rights(input);
175+
assert (result == [11, 13]);
176+
}
177+
178+
#[test]
179+
fn test_rights_none() {
180+
let input: [t<int, int>] = [left(10), left(10)];
181+
let result = rights(input);
182+
assert (vec::len(result) == 0u);
183+
}
184+
185+
#[test]
186+
fn test_rights_empty() {
187+
let input: [t<int, int>] = [];
188+
let result = rights(input);
189+
assert (vec::len(result) == 0u);
190+
}
191+
192+
#[test]
193+
fn test_partition() {
194+
let input = [left(10), right(11), left(12), right(13), left(14)];
195+
let result = partition(input);
196+
assert (result.lefts[0] == 10);
197+
assert (result.lefts[1] == 12);
198+
assert (result.lefts[2] == 14);
199+
assert (result.rights[0] == 11);
200+
assert (result.rights[1] == 13);
201+
}
202+
203+
#[test]
204+
fn test_partition_no_lefts() {
205+
let input: [t<int, int>] = [right(10), right(11)];
206+
let result = partition(input);
207+
assert (vec::len(result.lefts) == 0u);
208+
assert (vec::len(result.rights) == 2u);
209+
}
210+
211+
#[test]
212+
fn test_partition_no_rights() {
213+
let input: [t<int, int>] = [left(10), left(11)];
214+
let result = partition(input);
215+
assert (vec::len(result.lefts) == 2u);
216+
assert (vec::len(result.rights) == 0u);
217+
}
218+
219+
#[test]
220+
fn test_partition_empty() {
221+
let input: [t<int, int>] = [];
222+
let result = partition(input);
223+
assert (vec::len(result.lefts) == 0u);
224+
assert (vec::len(result.rights) == 0u);
225+
}

src/libcore/float.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,87 @@ fn pow_uint_to_uint_as_float(x: uint, pow: uint) -> float {
281281
}
282282

283283

284+
#[test]
285+
fn test_from_str() {
286+
assert ( from_str("3") == 3. );
287+
assert ( from_str(" 3 ") == 3. );
288+
assert ( from_str("3.14") == 3.14 );
289+
assert ( from_str("+3.14") == 3.14 );
290+
assert ( from_str("-3.14") == -3.14 );
291+
assert ( from_str("2.5E10") == 25000000000. );
292+
assert ( from_str("2.5e10") == 25000000000. );
293+
assert ( from_str("25000000000.E-10") == 2.5 );
294+
assert ( from_str("") == 0. );
295+
assert ( from_str(".") == 0. );
296+
assert ( from_str(".e1") == 0. );
297+
assert ( from_str(".e-1") == 0. );
298+
assert ( from_str("5.") == 5. );
299+
assert ( from_str(".5") == 0.5 );
300+
assert ( from_str("0.5") == 0.5 );
301+
assert ( from_str("0.5 ") == 0.5 );
302+
assert ( from_str(" 0.5 ") == 0.5 );
303+
assert ( from_str(" -.5 ") == -0.5 );
304+
assert ( from_str(" -.5 ") == -0.5 );
305+
assert ( from_str(" -5 ") == -5. );
306+
307+
assert ( is_NaN(from_str("x")) );
308+
assert ( from_str(" ") == 0. );
309+
assert ( from_str(" ") == 0. );
310+
assert ( from_str(" 0.5") == 0.5 );
311+
assert ( from_str(" 0.5 ") == 0.5 );
312+
assert ( from_str(" .1 ") == 0.1 );
313+
assert ( is_NaN(from_str("e")) );
314+
assert ( is_NaN(from_str("E")) );
315+
assert ( is_NaN(from_str("E1")) );
316+
assert ( is_NaN(from_str("1e1e1")) );
317+
assert ( is_NaN(from_str("1e1.1")) );
318+
assert ( is_NaN(from_str("1e1-1")) );
319+
}
320+
321+
#[test]
322+
fn test_positive() {
323+
assert(is_positive(infinity));
324+
assert(is_positive(1.));
325+
assert(is_positive(0.));
326+
assert(!is_positive(-1.));
327+
assert(!is_positive(neg_infinity));
328+
assert(!is_positive(1./neg_infinity));
329+
assert(!is_positive(NaN));
330+
}
331+
332+
#[test]
333+
fn test_negative() {
334+
assert(!is_negative(infinity));
335+
assert(!is_negative(1.));
336+
assert(!is_negative(0.));
337+
assert(is_negative(-1.));
338+
assert(is_negative(neg_infinity));
339+
assert(is_negative(1./neg_infinity));
340+
assert(!is_negative(NaN));
341+
}
342+
343+
#[test]
344+
fn test_nonpositive() {
345+
assert(!is_nonpositive(infinity));
346+
assert(!is_nonpositive(1.));
347+
assert(!is_nonpositive(0.));
348+
assert(is_nonpositive(-1.));
349+
assert(is_nonpositive(neg_infinity));
350+
assert(is_nonpositive(1./neg_infinity));
351+
assert(!is_nonpositive(NaN));
352+
}
353+
354+
#[test]
355+
fn test_nonnegative() {
356+
assert(is_nonnegative(infinity));
357+
assert(is_nonnegative(1.));
358+
assert(is_nonnegative(0.));
359+
assert(!is_nonnegative(-1.));
360+
assert(!is_nonnegative(neg_infinity));
361+
assert(!is_nonnegative(1./neg_infinity));
362+
assert(!is_nonnegative(NaN));
363+
}
364+
284365
//
285366
// Local Variables:
286367
// mode: rust

0 commit comments

Comments
 (0)