libstd: Move std tests into libstd

This commit is contained in:
Brian Anderson 2012-01-17 19:05:07 -08:00
parent 17bf4b0e1b
commit 6e27b27cf8
51 changed files with 2926 additions and 3032 deletions

View File

@ -55,10 +55,6 @@ ifdef VERBOSE
CTEST_TESTARGS += --verbose
endif
# The standard library test crate
STDTEST_CRATE := $(S)src/test/stdtest/stdtest.rc
STDTEST_INPUTS := $(wildcard $(S)src/test/stdtest/*rs)
# Run the compiletest runner itself under valgrind
ifdef CTEST_VALGRIND
CFG_RUN_CTEST=$(call CFG_RUN_TEST,$(2),$(3))
@ -188,7 +184,7 @@ check-stage$(1)-T-$(2)-H-$(3)-core-dummy: \
# Rules for the standard library test runner
$(3)/test/stdtest.stage$(1)-$(2)$$(X): \
$$(STDTEST_CRATE) $$(STDTEST_INPUTS) \
$$(STDLIB_CRATE) $$(STDLIB_INPUTS) \
$$(SREQ$(1)_T_$(2)_H_$(3))
@$$(call E, compile_and_link: $$@)
$$(STAGE$(1)_T_$(2)_H_$(3)) -o $$@ $$< --test

View File

@ -305,6 +305,291 @@ fn eq_vec(v0: t, v1: [uint]) -> bool {
ret true;
}
#[cfg(test)]
mod tests {
#[test]
fn test_0_elements() {
let act;
let exp;
act = create(0u, false);
exp = vec::init_elt::<uint>(0u, 0u);
assert (eq_vec(act, exp));
}
#[test]
fn test_1_element() {
let act;
act = create(1u, false);
assert (eq_vec(act, [0u]));
act = create(1u, true);
assert (eq_vec(act, [1u]));
}
#[test]
fn test_10_elements() {
let act;
// all 0
act = create(10u, false);
assert (eq_vec(act, [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u]));
// all 1
act = create(10u, true);
assert (eq_vec(act, [1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = create(10u, false);
set(act, 0u, true);
set(act, 1u, true);
set(act, 2u, true);
set(act, 3u, true);
set(act, 4u, true);
assert (eq_vec(act, [1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = create(10u, false);
set(act, 5u, true);
set(act, 6u, true);
set(act, 7u, true);
set(act, 8u, true);
set(act, 9u, true);
assert (eq_vec(act, [0u, 0u, 0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = create(10u, false);
set(act, 0u, true);
set(act, 3u, true);
set(act, 6u, true);
set(act, 9u, true);
assert (eq_vec(act, [1u, 0u, 0u, 1u, 0u, 0u, 1u, 0u, 0u, 1u]));
}
#[test]
fn test_31_elements() {
let act;
// all 0
act = create(31u, false);
assert (eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u]));
// all 1
act = create(31u, true);
assert (eq_vec(act,
[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u]));
// mixed
act = create(31u, false);
set(act, 0u, true);
set(act, 1u, true);
set(act, 2u, true);
set(act, 3u, true);
set(act, 4u, true);
set(act, 5u, true);
set(act, 6u, true);
set(act, 7u, true);
assert (eq_vec(act,
[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u]));
// mixed
act = create(31u, false);
set(act, 16u, true);
set(act, 17u, true);
set(act, 18u, true);
set(act, 19u, true);
set(act, 20u, true);
set(act, 21u, true);
set(act, 22u, true);
set(act, 23u, true);
assert (eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u,
0u, 0u, 0u, 0u, 0u]));
// mixed
act = create(31u, false);
set(act, 24u, true);
set(act, 25u, true);
set(act, 26u, true);
set(act, 27u, true);
set(act, 28u, true);
set(act, 29u, true);
set(act, 30u, true);
assert (eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
1u, 1u, 1u, 1u, 1u]));
// mixed
act = create(31u, false);
set(act, 3u, true);
set(act, 17u, true);
set(act, 30u, true);
assert (eq_vec(act,
[0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 1u]));
}
#[test]
fn test_32_elements() {
let act;
// all 0
act = create(32u, false);
assert (eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u]));
// all 1
act = create(32u, true);
assert (eq_vec(act,
[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = create(32u, false);
set(act, 0u, true);
set(act, 1u, true);
set(act, 2u, true);
set(act, 3u, true);
set(act, 4u, true);
set(act, 5u, true);
set(act, 6u, true);
set(act, 7u, true);
assert (eq_vec(act,
[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = create(32u, false);
set(act, 16u, true);
set(act, 17u, true);
set(act, 18u, true);
set(act, 19u, true);
set(act, 20u, true);
set(act, 21u, true);
set(act, 22u, true);
set(act, 23u, true);
assert (eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = create(32u, false);
set(act, 24u, true);
set(act, 25u, true);
set(act, 26u, true);
set(act, 27u, true);
set(act, 28u, true);
set(act, 29u, true);
set(act, 30u, true);
set(act, 31u, true);
assert (eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = create(32u, false);
set(act, 3u, true);
set(act, 17u, true);
set(act, 30u, true);
set(act, 31u, true);
assert (eq_vec(act,
[0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 1u, 1u]));
}
#[test]
fn test_33_elements() {
let act;
// all 0
act = create(33u, false);
assert (eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u]));
// all 1
act = create(33u, true);
assert (eq_vec(act,
[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = create(33u, false);
set(act, 0u, true);
set(act, 1u, true);
set(act, 2u, true);
set(act, 3u, true);
set(act, 4u, true);
set(act, 5u, true);
set(act, 6u, true);
set(act, 7u, true);
assert (eq_vec(act,
[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = create(33u, false);
set(act, 16u, true);
set(act, 17u, true);
set(act, 18u, true);
set(act, 19u, true);
set(act, 20u, true);
set(act, 21u, true);
set(act, 22u, true);
set(act, 23u, true);
assert (eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = create(33u, false);
set(act, 24u, true);
set(act, 25u, true);
set(act, 26u, true);
set(act, 27u, true);
set(act, 28u, true);
set(act, 29u, true);
set(act, 30u, true);
set(act, 31u, true);
assert (eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 0u]));
// mixed
act = create(33u, false);
set(act, 3u, true);
set(act, 17u, true);
set(act, 30u, true);
set(act, 31u, true);
set(act, 32u, true);
assert (eq_vec(act,
[0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 1u, 1u, 1u]));
}
}
//
// Local Variables:
// mode: rust

View File

@ -148,3 +148,64 @@ Returns a pointer to the first element of the vector
unsafe fn ptr<T>(t: t<T>) -> *mutable T {
ret (*t).base;
}
#[cfg(test)]
mod tests {
import ctypes::*;
#[nolink]
#[abi = "cdecl"]
native mod libc {
fn malloc(n: size_t) -> *mutable u8;
fn free(m: *mutable u8);
}
fn malloc(n: size_t) -> t<u8> {
let mem = libc::malloc(n);
assert mem as int != 0;
ret unsafe { create_with_dtor(mem, n, bind libc::free(mem)) };
}
#[test]
fn test_basic() {
let cv = malloc(16u);
set(cv, 3u, 8u8);
set(cv, 4u, 9u8);
assert get(cv, 3u) == 8u8;
assert get(cv, 4u) == 9u8;
assert len(cv) == 16u;
}
#[test]
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_overrun_get() {
let cv = malloc(16u);
get(cv, 17u);
}
#[test]
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_overrun_set() {
let cv = malloc(16u);
set(cv, 17u, 0u8);
}
#[test]
fn test_and_I_mean_it() {
let cv = malloc(16u);
let p = unsafe { ptr(cv) };
set(cv, 0u, 32u8);
set(cv, 1u, 33u8);
assert unsafe { *p } == 32u8;
set(cv, 2u, 34u8); /* safety */
}
}

View File

@ -131,3 +131,200 @@ fn create<T: copy>() -> t<T> {
};
repr as t::<T>
}
#[cfg(test)]
mod tests {
#[test]
fn test_simple() {
let d: deque::t<int> = deque::create::<int>();
assert (d.size() == 0u);
d.add_front(17);
d.add_front(42);
d.add_back(137);
assert (d.size() == 3u);
d.add_back(137);
assert (d.size() == 4u);
log(debug, d.peek_front());
assert (d.peek_front() == 42);
log(debug, d.peek_back());
assert (d.peek_back() == 137);
let i: int = d.pop_front();
log(debug, i);
assert (i == 42);
i = d.pop_back();
log(debug, i);
assert (i == 137);
i = d.pop_back();
log(debug, i);
assert (i == 137);
i = d.pop_back();
log(debug, i);
assert (i == 17);
assert (d.size() == 0u);
d.add_back(3);
assert (d.size() == 1u);
d.add_front(2);
assert (d.size() == 2u);
d.add_back(4);
assert (d.size() == 3u);
d.add_front(1);
assert (d.size() == 4u);
log(debug, d.get(0));
log(debug, d.get(1));
log(debug, d.get(2));
log(debug, d.get(3));
assert (d.get(0) == 1);
assert (d.get(1) == 2);
assert (d.get(2) == 3);
assert (d.get(3) == 4);
}
fn test_boxes(a: @int, b: @int, c: @int, d: @int) {
let deq: deque::t<@int> = deque::create::<@int>();
assert (deq.size() == 0u);
deq.add_front(a);
deq.add_front(b);
deq.add_back(c);
assert (deq.size() == 3u);
deq.add_back(d);
assert (deq.size() == 4u);
assert (deq.peek_front() == b);
assert (deq.peek_back() == d);
assert (deq.pop_front() == b);
assert (deq.pop_back() == d);
assert (deq.pop_back() == c);
assert (deq.pop_back() == a);
assert (deq.size() == 0u);
deq.add_back(c);
assert (deq.size() == 1u);
deq.add_front(b);
assert (deq.size() == 2u);
deq.add_back(d);
assert (deq.size() == 3u);
deq.add_front(a);
assert (deq.size() == 4u);
assert (deq.get(0) == a);
assert (deq.get(1) == b);
assert (deq.get(2) == c);
assert (deq.get(3) == d);
}
type eqfn<T> = fn@(T, T) -> bool;
fn test_parameterized<T: copy>(e: eqfn<T>, a: T, b: T, c: T, d: T) {
let deq: deque::t<T> = deque::create::<T>();
assert (deq.size() == 0u);
deq.add_front(a);
deq.add_front(b);
deq.add_back(c);
assert (deq.size() == 3u);
deq.add_back(d);
assert (deq.size() == 4u);
assert (e(deq.peek_front(), b));
assert (e(deq.peek_back(), d));
assert (e(deq.pop_front(), b));
assert (e(deq.pop_back(), d));
assert (e(deq.pop_back(), c));
assert (e(deq.pop_back(), a));
assert (deq.size() == 0u);
deq.add_back(c);
assert (deq.size() == 1u);
deq.add_front(b);
assert (deq.size() == 2u);
deq.add_back(d);
assert (deq.size() == 3u);
deq.add_front(a);
assert (deq.size() == 4u);
assert (e(deq.get(0), a));
assert (e(deq.get(1), b));
assert (e(deq.get(2), c));
assert (e(deq.get(3), d));
}
tag taggy { one(int); two(int, int); three(int, int, int); }
tag taggypar<T> {
onepar(int); twopar(int, int); threepar(int, int, int);
}
type reccy = {x: int, y: int, t: taggy};
#[test]
fn test() {
fn inteq(&&a: int, &&b: int) -> bool { ret a == b; }
fn intboxeq(&&a: @int, &&b: @int) -> bool { ret a == b; }
fn taggyeq(a: taggy, b: taggy) -> bool {
alt a {
one(a1) { alt b { one(b1) { ret a1 == b1; } _ { ret false; } } }
two(a1, a2) {
alt b {
two(b1, b2) { ret a1 == b1 && a2 == b2; }
_ { ret false; }
}
}
three(a1, a2, a3) {
alt b {
three(b1, b2, b3) { ret a1 == b1 && a2 == b2 && a3 == b3; }
_ { ret false; }
}
}
}
}
fn taggypareq<T>(a: taggypar<T>, b: taggypar<T>) -> bool {
alt a {
onepar::<T>(a1) {
alt b { onepar::<T>(b1) { ret a1 == b1; } _ { ret false; } }
}
twopar::<T>(a1, a2) {
alt b {
twopar::<T>(b1, b2) { ret a1 == b1 && a2 == b2; }
_ { ret false; }
}
}
threepar::<T>(a1, a2, a3) {
alt b {
threepar::<T>(b1, b2, b3) {
ret a1 == b1 && a2 == b2 && a3 == b3;
}
_ { ret false; }
}
}
}
}
fn reccyeq(a: reccy, b: reccy) -> bool {
ret a.x == b.x && a.y == b.y && taggyeq(a.t, b.t);
}
#debug("*** test boxes");
test_boxes(@5, @72, @64, @175);
#debug("*** end test boxes");
#debug("test parameterized: int");
let eq1: eqfn<int> = inteq;
test_parameterized::<int>(eq1, 5, 72, 64, 175);
#debug("*** test parameterized: @int");
let eq2: eqfn<@int> = intboxeq;
test_parameterized::<@int>(eq2, @5, @72, @64, @175);
#debug("*** end test parameterized @int");
#debug("test parameterized: taggy");
let eq3: eqfn<taggy> = taggyeq;
test_parameterized::<taggy>(eq3, one(1), two(1, 2), three(1, 2, 3),
two(17, 42));
#debug("*** test parameterized: taggypar<int>");
let eq4: eqfn<taggypar<int>> = bind taggypareq::<int>(_, _);
test_parameterized::<taggypar<int>>(eq4, onepar::<int>(1),
twopar::<int>(1, 2),
threepar::<int>(1, 2, 3),
twopar::<int>(17, 42));
#debug("*** end test parameterized: taggypar::<int>");
#debug("*** test parameterized: reccy");
let reccy1: reccy = {x: 1, y: 2, t: one(1)};
let reccy2: reccy = {x: 345, y: 2, t: two(1, 2)};
let reccy3: reccy = {x: 1, y: 777, t: three(1, 2, 3)};
let reccy4: reccy = {x: 19, y: 252, t: two(17, 42)};
let eq5: eqfn<reccy> = reccyeq;
test_parameterized::<reccy>(eq5, reccy1, reccy2, reccy3, reccy4);
#debug("*** end test parameterized: reccy");
#debug("*** done");
}
}

View File

@ -220,6 +220,156 @@ A trit of `v` (`both` and `none` are both coalesced into `trit::unknown`)
*/
fn to_trit(v: t) -> tri::t { v & (v ^ not(v)) }
#[cfg(test)]
mod tests {
fn eq1(a: four::t, b: four::t) -> bool { four::eq(a , b) }
fn ne1(a: four::t, b: four::t) -> bool { four::ne(a , b) }
fn eq2(a: four::t, b: four::t) -> bool { eq1( a, b ) && eq1( b, a ) }
#[test]
fn test_four_req_eq() {
four::all_values { |a|
four::all_values { |b|
assert if a == b { eq1( a, b ) } else { ne1( a, b ) };
}
}
}
#[test]
fn test_four_and_symmetry() {
four::all_values { |a|
four::all_values { |b|
assert eq1( four::and(a ,b), four::and(b, a) );
}
}
}
#[test]
fn test_four_xor_symmetry() {
four::all_values { |a|
four::all_values { |b|
assert eq1( four::and(a ,b), four::and(b, a) );
}
}
}
#[test]
fn test_four_or_symmetry() {
four::all_values { |a|
four::all_values { |b|
assert eq1( four::or(a ,b), four::or(b, a) );
}
}
}
fn to_tup(v: four::t) -> (bool, bool) {
alt v {
0u8 { (false, false) }
1u8 { (false, true) }
2u8 { (true, false) }
3u8 { (true, true) }
}
}
#[test]
fn test_four_not() {
four::all_values { |a|
let (x, y) = to_tup(a);
assert to_tup(four::not(a)) == (y, x);
};
}
#[test]
fn test_four_and() {
four::all_values { |a|
four::all_values { |b|
let (y1, x1) = to_tup(a);
let (y2, x2) = to_tup(b);
let (y3, x3) = to_tup(four::and(a, b));
assert (x3, y3) == (x1 && x2, y1 || y2);
}
};
}
#[test]
fn test_four_or() {
four::all_values { |a|
four::all_values { |b|
let (y1, x1) = to_tup(a);
let (y2, x2) = to_tup(b);
let (y3, x3) = to_tup(four::or(a, b));
assert (x3, y3) == (x1 || x2, y1 && y2);
}
};
}
#[test]
fn test_four_implies() {
four::all_values { |a|
four::all_values { |b|
let (_, x1) = to_tup(a);
let (y2, x2) = to_tup(b);
let (y3, x3) = to_tup(four::implies(a, b));
assert (x3, y3) == (!x1 || x2, x1 && y2);
}
};
}
#[test]
fn test_four_is_true() {
assert !four::is_true(four::none);
assert !four::is_true(four::false);
assert four::is_true(four::true);
assert four::is_true(four::both);
}
#[test]
fn test_four_is_false() {
assert four::is_false(four::none);
assert four::is_false(four::false);
assert !four::is_false(four::true);
assert !four::is_false(four::both);
}
#[test]
fn test_four_from_str() {
four::all_values { |v|
assert eq1( v, four::from_str(four::to_str(v)) );
}
}
#[test]
fn test_four_to_str() {
assert four::to_str(four::none) == "none";
assert four::to_str(four::false) == "false";
assert four::to_str(four::true) == "true" ;
assert four::to_str(four::both) == "both";
}
#[test]
fn test_four_to_tri() {
assert tri::eq( four::to_trit(four::true), tri::true );
assert tri::eq( four::to_trit(four::false), tri::false );
assert tri::eq( four::to_trit(four::none), tri::unknown );
log(debug, four::to_trit(four::both));
assert tri::eq( four::to_trit(four::both), tri::unknown );
}
#[test]
fn test_four_to_bit() {
four::all_values { |v|
assert four::to_bit(v) ==
if four::is_true(v) { 1u8 } else { 0u8 };
}
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -450,6 +450,291 @@ fn homedir() -> option<path> {
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_connect() {
let slash = fs::path_sep();
log(error, fs::connect("a", "b"));
assert (fs::connect("a", "b") == "a" + slash + "b");
assert (fs::connect("a" + slash, "b") == "a" + slash + "b");
}
// Issue #712
#[test]
fn test_list_dir_no_invalid_memory_access() { fs::list_dir("."); }
#[test]
fn list_dir() {
let dirs = fs::list_dir(".");
// Just assuming that we've got some contents in the current directory
assert (vec::len(dirs) > 0u);
for dir in dirs { log(debug, dir); }
}
#[test]
fn path_is_dir() {
assert (fs::path_is_dir("."));
assert (!fs::path_is_dir("test/stdtest/fs.rs"));
}
#[test]
fn path_exists() {
assert (fs::path_exists("."));
assert (!fs::path_exists("test/nonexistent-bogus-path"));
}
fn ps() -> str {
fs::path_sep()
}
fn aps() -> str {
"/"
}
#[test]
fn split1() {
let actual = fs::split("a" + ps() + "b");
let expected = ["a", "b"];
assert actual == expected;
}
#[test]
fn split2() {
let actual = fs::split("a" + aps() + "b");
let expected = ["a", "b"];
assert actual == expected;
}
#[test]
fn split3() {
let actual = fs::split(ps() + "a" + ps() + "b");
let expected = ["a", "b"];
assert actual == expected;
}
#[test]
fn split4() {
let actual = fs::split("a" + ps() + "b" + aps() + "c");
let expected = ["a", "b", "c"];
assert actual == expected;
}
#[test]
fn normalize1() {
let actual = fs::normalize("a/b/..");
let expected = "a";
assert actual == expected;
}
#[test]
fn normalize2() {
let actual = fs::normalize("/a/b/..");
let expected = "/a";
assert actual == expected;
}
#[test]
fn normalize3() {
let actual = fs::normalize("a/../b");
let expected = "b";
assert actual == expected;
}
#[test]
fn normalize4() {
let actual = fs::normalize("/a/../b");
let expected = "/b";
assert actual == expected;
}
#[test]
fn normalize5() {
let actual = fs::normalize("a/.");
let expected = "a";
assert actual == expected;
}
#[test]
fn normalize6() {
let actual = fs::normalize("a/./b/");
let expected = "a/b/";
assert actual == expected;
}
#[test]
fn normalize7() {
let actual = fs::normalize("a/..");
let expected = ".";
assert actual == expected;
}
#[test]
fn normalize8() {
let actual = fs::normalize("../../..");
let expected = "../../..";
assert actual == expected;
}
#[test]
fn normalize9() {
let actual = fs::normalize("a/b/../../..");
let expected = "..";
assert actual == expected;
}
#[test]
fn normalize10() {
let actual = fs::normalize("/a/b/c/../d/./../../e/");
let expected = "/a/e/";
log(error, actual);
assert actual == expected;
}
#[test]
fn normalize11() {
let actual = fs::normalize("/a/..");
let expected = "/";
assert actual == expected;
}
#[test]
#[cfg(target_os = "win32")]
fn normalize12() {
let actual = fs::normalize("C:/whatever");
let expected = "C:/whatever";
log(error, actual);
assert actual == expected;
}
#[test]
#[cfg(target_os = "win32")]
fn path_is_absolute_win32() {
assert fs::path_is_absolute("C:/whatever");
}
#[test]
fn splitext_empty() {
let (base, ext) = fs::splitext("");
assert base == "";
assert ext == "";
}
#[test]
fn splitext_ext() {
let (base, ext) = fs::splitext("grum.exe");
assert base == "grum";
assert ext == ".exe";
}
#[test]
fn splitext_noext() {
let (base, ext) = fs::splitext("grum");
assert base == "grum";
assert ext == "";
}
#[test]
fn splitext_dotfile() {
let (base, ext) = fs::splitext(".grum");
assert base == ".grum";
assert ext == "";
}
#[test]
fn splitext_path_ext() {
let (base, ext) = fs::splitext("oh/grum.exe");
assert base == "oh/grum";
assert ext == ".exe";
}
#[test]
fn splitext_path_noext() {
let (base, ext) = fs::splitext("oh/grum");
assert base == "oh/grum";
assert ext == "";
}
#[test]
fn splitext_dot_in_path() {
let (base, ext) = fs::splitext("oh.my/grum");
assert base == "oh.my/grum";
assert ext == "";
}
#[test]
fn splitext_nobasename() {
let (base, ext) = fs::splitext("oh.my/");
assert base == "oh.my/";
assert ext == "";
}
#[test]
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
fn homedir() {
import getenv = generic_os::getenv;
import setenv = generic_os::setenv;
let oldhome = getenv("HOME");
setenv("HOME", "/home/MountainView");
assert fs::homedir() == some("/home/MountainView");
setenv("HOME", "");
assert fs::homedir() == none;
option::may(oldhome, {|s| setenv("HOME", s)});
}
#[test]
#[cfg(target_os = "win32")]
fn homedir() {
import getenv = generic_os::getenv;
import setenv = generic_os::setenv;
let oldhome = getenv("HOME");
let olduserprofile = getenv("USERPROFILE");
setenv("HOME", "");
setenv("USERPROFILE", "");
assert fs::homedir() == none;
setenv("HOME", "/home/MountainView");
assert fs::homedir() == some("/home/MountainView");
setenv("HOME", "");
setenv("USERPROFILE", "/home/MountainView");
assert fs::homedir() == some("/home/MountainView");
setenv("USERPROFILE", "/home/MountainView");
assert fs::homedir() == some("/home/MountainView");
setenv("HOME", "/home/MountainView");
setenv("USERPROFILE", "/home/PaloAlto");
assert fs::homedir() == some("/home/MountainView");
option::may(oldhome, {|s| setenv("HOME", s)});
option::may(olduserprofile, {|s| setenv("USERPROFILE", s)});
}
}
#[test]
fn test() {
assert (!fs::path_is_absolute("test-path"));
log(debug, "Current working directory: " + os::getcwd());
log(debug, fs::make_absolute("test-path"));
log(debug, fs::make_absolute("/usr/bin"));
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -93,6 +93,56 @@ fn setenv(n: str, v: str) {
});
}
#[cfg(test)]
mod tests {
#[test]
#[ignore(reason = "fails periodically on mac")]
fn test_setenv() {
// NB: Each test of setenv needs to use different variable names or
// the tests will not be threadsafe
setenv("NAME1", "VALUE");
assert (getenv("NAME1") == option::some("VALUE"));
}
#[test]
#[ignore(reason = "fails periodically on mac")]
fn test_setenv_overwrite() {
setenv("NAME2", "1");
setenv("NAME2", "2");
assert (getenv("NAME2") == option::some("2"));
}
// Windows GetEnvironmentVariable requires some extra work to make sure
// the buffer the variable is copied into is the right size
#[test]
#[ignore(reason = "fails periodically on mac")]
fn test_getenv_big() {
let s = "";
let i = 0;
while i < 100 { s += "aaaaaaaaaa"; i += 1; }
setenv("test_getenv_big", s);
log(debug, s);
assert (getenv("test_getenv_big") == option::some(s));
}
#[test]
fn test_get_exe_path() {
let path = os::get_exe_path();
assert option::is_some(path);
let path = option::get(path);
log(debug, path);
// Hard to test this function
if os::target_os() != "win32" {
assert str::starts_with(path, fs::path_sep());
} else {
assert path[1] == ':' as u8;
}
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -375,6 +375,479 @@ fn opt_default(m: match, nm: str, def: str) -> option::t<str> {
if vec::len::<optval>(vals) == 0u { ret none::<str>; }
ret alt vals[0] { val(s) { some::<str>(s) } _ { some::<str>(def) } }
}
#[cfg(test)]
mod tests {
import opt = getopts;
import result::{err, ok};
tag fail_type {
argument_missing_;
unrecognized_option_;
option_missing_;
option_duplicated_;
unexpected_argument_;
}
fn check_fail_type(f: fail_, ft: fail_type) {
alt f {
argument_missing(_) { assert (ft == argument_missing_); }
unrecognized_option(_) { assert (ft == unrecognized_option_); }
option_missing(_) { assert (ft == option_missing_); }
option_duplicated(_) { assert (ft == option_duplicated_); }
unexpected_argument(_) { assert (ft == unexpected_argument_); }
_ { fail; }
}
}
// Tests for reqopt
#[test]
fn test_reqopt_long() {
let args = ["--test=20"];
let opts = [reqopt("test")];
let rs = getopts(args, opts);
alt rs {
ok(m) {
assert (opt_present(m, "test"));
assert (opt_str(m, "test") == "20");
}
_ { fail; }
}
}
#[test]
fn test_reqopt_long_missing() {
let args = ["blah"];
let opts = [reqopt("test")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_missing_); }
_ { fail; }
}
}
#[test]
fn test_reqopt_long_no_arg() {
let args = ["--test"];
let opts = [reqopt("test")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, argument_missing_); }
_ { fail; }
}
}
#[test]
fn test_reqopt_long_multi() {
let args = ["--test=20", "--test=30"];
let opts = [reqopt("test")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_duplicated_); }
_ { fail; }
}
}
#[test]
fn test_reqopt_short() {
let args = ["-t", "20"];
let opts = [reqopt("t")];
let rs = getopts(args, opts);
alt rs {
ok(m) {
assert (opt_present(m, "t"));
assert (opt_str(m, "t") == "20");
}
_ { fail; }
}
}
#[test]
fn test_reqopt_short_missing() {
let args = ["blah"];
let opts = [reqopt("t")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_missing_); }
_ { fail; }
}
}
#[test]
fn test_reqopt_short_no_arg() {
let args = ["-t"];
let opts = [reqopt("t")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, argument_missing_); }
_ { fail; }
}
}
#[test]
fn test_reqopt_short_multi() {
let args = ["-t", "20", "-t", "30"];
let opts = [reqopt("t")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_duplicated_); }
_ { fail; }
}
}
// Tests for optopt
#[test]
fn test_optopt_long() {
let args = ["--test=20"];
let opts = [optopt("test")];
let rs = getopts(args, opts);
alt rs {
ok(m) {
assert (opt_present(m, "test"));
assert (opt_str(m, "test") == "20");
}
_ { fail; }
}
}
#[test]
fn test_optopt_long_missing() {
let args = ["blah"];
let opts = [optopt("test")];
let rs = getopts(args, opts);
alt rs {
ok(m) { assert (!opt_present(m, "test")); }
_ { fail; }
}
}
#[test]
fn test_optopt_long_no_arg() {
let args = ["--test"];
let opts = [optopt("test")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, argument_missing_); }
_ { fail; }
}
}
#[test]
fn test_optopt_long_multi() {
let args = ["--test=20", "--test=30"];
let opts = [optopt("test")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_duplicated_); }
_ { fail; }
}
}
#[test]
fn test_optopt_short() {
let args = ["-t", "20"];
let opts = [optopt("t")];
let rs = getopts(args, opts);
alt rs {
ok(m) {
assert (opt_present(m, "t"));
assert (opt_str(m, "t") == "20");
}
_ { fail; }
}
}
#[test]
fn test_optopt_short_missing() {
let args = ["blah"];
let opts = [optopt("t")];
let rs = getopts(args, opts);
alt rs {
ok(m) { assert (!opt_present(m, "t")); }
_ { fail; }
}
}
#[test]
fn test_optopt_short_no_arg() {
let args = ["-t"];
let opts = [optopt("t")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, argument_missing_); }
_ { fail; }
}
}
#[test]
fn test_optopt_short_multi() {
let args = ["-t", "20", "-t", "30"];
let opts = [optopt("t")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_duplicated_); }
_ { fail; }
}
}
// Tests for optflag
#[test]
fn test_optflag_long() {
let args = ["--test"];
let opts = [optflag("test")];
let rs = getopts(args, opts);
alt rs {
ok(m) { assert (opt_present(m, "test")); }
_ { fail; }
}
}
#[test]
fn test_optflag_long_missing() {
let args = ["blah"];
let opts = [optflag("test")];
let rs = getopts(args, opts);
alt rs {
ok(m) { assert (!opt_present(m, "test")); }
_ { fail; }
}
}
#[test]
fn test_optflag_long_arg() {
let args = ["--test=20"];
let opts = [optflag("test")];
let rs = getopts(args, opts);
alt rs {
err(f) {
log(error, fail_str(f));
check_fail_type(f, unexpected_argument_);
}
_ { fail; }
}
}
#[test]
fn test_optflag_long_multi() {
let args = ["--test", "--test"];
let opts = [optflag("test")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_duplicated_); }
_ { fail; }
}
}
#[test]
fn test_optflag_short() {
let args = ["-t"];
let opts = [optflag("t")];
let rs = getopts(args, opts);
alt rs {
ok(m) { assert (opt_present(m, "t")); }
_ { fail; }
}
}
#[test]
fn test_optflag_short_missing() {
let args = ["blah"];
let opts = [optflag("t")];
let rs = getopts(args, opts);
alt rs {
ok(m) { assert (!opt_present(m, "t")); }
_ { fail; }
}
}
#[test]
fn test_optflag_short_arg() {
let args = ["-t", "20"];
let opts = [optflag("t")];
let rs = getopts(args, opts);
alt rs {
ok(m) {
// The next variable after the flag is just a free argument
assert (m.free[0] == "20");
}
_ { fail; }
}
}
#[test]
fn test_optflag_short_multi() {
let args = ["-t", "-t"];
let opts = [optflag("t")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_duplicated_); }
_ { fail; }
}
}
// Tests for optmulti
#[test]
fn test_optmulti_long() {
let args = ["--test=20"];
let opts = [optmulti("test")];
let rs = getopts(args, opts);
alt rs {
ok(m) {
assert (opt_present(m, "test"));
assert (opt_str(m, "test") == "20");
}
_ { fail; }
}
}
#[test]
fn test_optmulti_long_missing() {
let args = ["blah"];
let opts = [optmulti("test")];
let rs = getopts(args, opts);
alt rs {
ok(m) { assert (!opt_present(m, "test")); }
_ { fail; }
}
}
#[test]
fn test_optmulti_long_no_arg() {
let args = ["--test"];
let opts = [optmulti("test")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, argument_missing_); }
_ { fail; }
}
}
#[test]
fn test_optmulti_long_multi() {
let args = ["--test=20", "--test=30"];
let opts = [optmulti("test")];
let rs = getopts(args, opts);
alt rs {
ok(m) {
assert (opt_present(m, "test"));
assert (opt_str(m, "test") == "20");
assert (opt_strs(m, "test")[0] == "20");
assert (opt_strs(m, "test")[1] == "30");
}
_ { fail; }
}
}
#[test]
fn test_optmulti_short() {
let args = ["-t", "20"];
let opts = [optmulti("t")];
let rs = getopts(args, opts);
alt rs {
ok(m) {
assert (opt_present(m, "t"));
assert (opt_str(m, "t") == "20");
}
_ { fail; }
}
}
#[test]
fn test_optmulti_short_missing() {
let args = ["blah"];
let opts = [optmulti("t")];
let rs = getopts(args, opts);
alt rs {
ok(m) { assert (!opt_present(m, "t")); }
_ { fail; }
}
}
#[test]
fn test_optmulti_short_no_arg() {
let args = ["-t"];
let opts = [optmulti("t")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, argument_missing_); }
_ { fail; }
}
}
#[test]
fn test_optmulti_short_multi() {
let args = ["-t", "20", "-t", "30"];
let opts = [optmulti("t")];
let rs = getopts(args, opts);
alt rs {
ok(m) {
assert (opt_present(m, "t"));
assert (opt_str(m, "t") == "20");
assert (opt_strs(m, "t")[0] == "20");
assert (opt_strs(m, "t")[1] == "30");
}
_ { fail; }
}
}
#[test]
fn test_unrecognized_option_long() {
let args = ["--untest"];
let opts = [optmulti("t")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, unrecognized_option_); }
_ { fail; }
}
}
#[test]
fn test_unrecognized_option_short() {
let args = ["-t"];
let opts = [optmulti("test")];
let rs = getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, unrecognized_option_); }
_ { fail; }
}
}
#[test]
fn test_combined() {
let args =
["prog", "free1", "-s", "20", "free2", "--flag", "--long=30",
"-f", "-m", "40", "-m", "50", "-n", "-A B", "-n", "-60 70"];
let opts =
[optopt("s"), optflag("flag"), reqopt("long"),
optflag("f"), optmulti("m"), optmulti("n"),
optopt("notpresent")];
let rs = getopts(args, opts);
alt rs {
ok(m) {
assert (m.free[0] == "prog");
assert (m.free[1] == "free1");
assert (opt_str(m, "s") == "20");
assert (m.free[2] == "free2");
assert (opt_present(m, "flag"));
assert (opt_str(m, "long") == "30");
assert (opt_present(m, "f"));
assert (opt_strs(m, "m")[0] == "40");
assert (opt_strs(m, "m")[1] == "50");
assert (opt_strs(m, "n")[0] == "-A B");
assert (opt_strs(m, "n")[1] == "-60 70");
assert (!opt_present(m, "notpresent"));
}
_ { fail; }
}
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -561,6 +561,105 @@ mod fsync {
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_simple() {
let tmpfile: str = "tmp/lib-io-test-simple.tmp";
log(debug, tmpfile);
let frood: str = "A hoopy frood who really knows where his towel is.";
log(debug, frood);
{
let out: io::writer =
result::get(
io::file_writer(tmpfile, [io::create, io::truncate]));
out.write_str(frood);
}
let inp: io::reader = result::get(io::file_reader(tmpfile));
let frood2: str = inp.read_c_str();
log(debug, frood2);
assert (str::eq(frood, frood2));
}
#[test]
fn test_readchars_empty() {
let inp : io::reader = io::string_reader("");
let res : [char] = inp.read_chars(128u);
assert(vec::len(res) == 0u);
}
#[test]
fn test_readchars_wide() {
let wide_test = "生锈的汤匙切肉汤hello生锈的汤匙切肉汤";
let ivals : [int] = [
29983, 38152, 30340, 27748,
21273, 20999, 32905, 27748,
104, 101, 108, 108, 111,
29983, 38152, 30340, 27748,
21273, 20999, 32905, 27748];
fn check_read_ln(len : uint, s: str, ivals: [int]) {
let inp : io::reader = io::string_reader(s);
let res : [char] = inp.read_chars(len);
if (len <= vec::len(ivals)) {
assert(vec::len(res) == len);
}
assert(vec::slice(ivals, 0u, vec::len(res)) ==
vec::map(res, {|x| x as int}));
}
let i = 0u;
while i < 8u {
check_read_ln(i, wide_test, ivals);
i += 1u;
}
// check a long read for good measure
check_read_ln(128u, wide_test, ivals);
}
#[test]
fn test_readchar() {
let inp : io::reader = io::string_reader("");
let res : char = inp.read_char();
assert(res as int == 29983);
}
#[test]
fn test_readchar_empty() {
let inp : io::reader = io::string_reader("");
let res : char = inp.read_char();
assert(res as int == -1);
}
#[test]
fn file_reader_not_exist() {
alt io::file_reader("not a file") {
result::err(e) {
assert e == "error opening not a file";
}
result::ok(_) { fail; }
}
}
#[test]
fn file_writer_bad_name() {
alt io::file_writer("?/?", []) {
result::err(e) {
assert e == "error opening ?/?";
}
result::ok(_) { fail; }
}
}
#[test]
fn buffered_file_writer_bad_name() {
alt io::buffered_file_writer("?/?") {
result::err(e) {
assert e == "error opening ?/?";
}
result::ok(_) { fail; }
}
}
}
//
// Local Variables:

View File

@ -268,3 +268,58 @@ fn from_str(s: str) -> option::t<json> {
let (j, _) = from_str_helper(s);
j
}
#[cfg(test)]
mod tests {
#[test]
fn test_from_str_null() {
assert(from_str("null") == some(null));
}
#[test]
fn test_from_str_num() {
assert(from_str("3") == some(num(3f)));
assert(from_str("3.1") == some(num(3.1f)));
assert(from_str("-1.2") == some(num(-1.2f)));
assert(from_str(".4") == some(num(0.4f)));
}
#[test]
fn test_from_str_str() {
assert(from_str("\"foo\"") == some(string("foo")));
assert(from_str("\"\\\"\"") == some(string("\"")));
assert(from_str("\"lol") == none);
}
#[test]
fn test_from_str_bool() {
assert(from_str("true") == some(boolean(true)));
assert(from_str("false") == some(boolean(false)));
assert(from_str("truz") == none);
}
#[test]
fn test_from_str_list() {
assert(from_str("[]") == some(list(@[])));
assert(from_str("[true]") == some(list(@[boolean(true)])));
assert(from_str("[null]") == some(list(@[null])));
assert(from_str("[3, 1]") == some(list(@[num(3f), num(1f)])));
assert(from_str("[2, [4, 1]]") ==
some(list(@[num(2f), list(@[num(4f), num(1f)])])));
assert(from_str("[2, ]") == none);
assert(from_str("[5, ") == none);
assert(from_str("[6 7]") == none);
assert(from_str("[3") == none);
}
#[test]
fn test_from_str_dict() {
assert(from_str("{}") != none);
assert(from_str("{\"a\": 3}") != none);
assert(from_str("{\"a\": null}") != none);
assert(from_str("{\"a\": }") == none);
assert(from_str("{\"a\" }") == none);
assert(from_str("{\"a\"") == none);
assert(from_str("{") == none);
}
}

View File

@ -183,6 +183,118 @@ fn iter<T>(l: list<T>, f: block(T)) {
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_is_empty() {
let empty : list::list<int> = from_vec([]);
let full1 = from_vec([1]);
let full2 = from_vec(['r', 'u']);
assert is_empty(empty);
assert !is_empty(full1);
assert !is_empty(full2);
assert !is_not_empty(empty);
assert is_not_empty(full1);
assert is_not_empty(full2);
}
#[test]
fn test_from_vec() {
let l = from_vec([0, 1, 2]);
check is_not_empty(l);
assert (head(l) == 0);
let tail_l = tail(l);
check is_not_empty(tail_l);
assert (head(tail_l) == 1);
let tail_tail_l = tail(tail_l);
check is_not_empty(tail_tail_l);
assert (head(tail_tail_l) == 2);
}
#[test]
fn test_from_vec_empty() {
let empty : list::list<int> = from_vec([]);
assert (empty == list::nil::<int>);
}
#[test]
fn test_from_vec_mut() {
let l = from_vec([mutable 0, 1, 2]);
check is_not_empty(l);
assert (head(l) == 0);
let tail_l = tail(l);
check is_not_empty(tail_l);
assert (head(tail_l) == 1);
let tail_tail_l = tail(tail_l);
check is_not_empty(tail_tail_l);
assert (head(tail_tail_l) == 2);
}
#[test]
fn test_foldl() {
fn add(&&a: uint, &&b: int) -> uint { ret a + (b as uint); }
let l = from_vec([0, 1, 2, 3, 4]);
let empty = list::nil::<int>;
assert (list::foldl(l, 0u, add) == 10u);
assert (list::foldl(empty, 0u, add) == 0u);
}
#[test]
fn test_foldl2() {
fn sub(&&a: int, &&b: int) -> int {
a - b
}
let l = from_vec([1, 2, 3, 4]);
assert (list::foldl(l, 0, sub) == -10);
}
#[test]
fn test_find_success() {
fn match(&&i: int) -> option::t<int> {
ret if i == 2 { option::some(i) } else { option::none::<int> };
}
let l = from_vec([0, 1, 2]);
assert (list::find(l, match) == option::some(2));
}
#[test]
fn test_find_fail() {
fn match(&&_i: int) -> option::t<int> { ret option::none::<int>; }
let l = from_vec([0, 1, 2]);
let empty = list::nil::<int>;
assert (list::find(l, match) == option::none::<int>);
assert (list::find(empty, match) == option::none::<int>);
}
#[test]
fn test_has() {
let l = from_vec([5, 8, 6]);
let empty = list::nil::<int>;
assert (list::has(l, 5));
assert (!list::has(l, 7));
assert (list::has(l, 8));
assert (!list::has(empty, 5));
}
#[test]
fn test_len() {
let l = from_vec([0, 1, 2]);
let empty = list::nil::<int>;
assert (list::len(l) == 3u);
assert (list::len(empty) == 0u);
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -388,3 +388,247 @@ Function: set_add
Convenience function for adding keys to a hashmap with nil type keys
*/
fn set_add<K>(set: set<K>, key: K) -> bool { ret set.insert(key, ()); }
#[cfg(test)]
mod tests {
#[test]
fn test_simple() {
#debug("*** starting test_simple");
fn eq_uint(&&x: uint, &&y: uint) -> bool { ret x == y; }
fn uint_id(&&x: uint) -> uint { x }
let hasher_uint: map::hashfn<uint> = uint_id;
let eqer_uint: map::eqfn<uint> = eq_uint;
let hasher_str: map::hashfn<str> = str::hash;
let eqer_str: map::eqfn<str> = str::eq;
#debug("uint -> uint");
let hm_uu: map::hashmap<uint, uint> =
map::mk_hashmap::<uint, uint>(hasher_uint, eqer_uint);
assert (hm_uu.insert(10u, 12u));
assert (hm_uu.insert(11u, 13u));
assert (hm_uu.insert(12u, 14u));
assert (hm_uu.get(11u) == 13u);
assert (hm_uu.get(12u) == 14u);
assert (hm_uu.get(10u) == 12u);
assert (!hm_uu.insert(12u, 14u));
assert (hm_uu.get(12u) == 14u);
assert (!hm_uu.insert(12u, 12u));
assert (hm_uu.get(12u) == 12u);
let ten: str = "ten";
let eleven: str = "eleven";
let twelve: str = "twelve";
#debug("str -> uint");
let hm_su: map::hashmap<str, uint> =
map::mk_hashmap::<str, uint>(hasher_str, eqer_str);
assert (hm_su.insert("ten", 12u));
assert (hm_su.insert(eleven, 13u));
assert (hm_su.insert("twelve", 14u));
assert (hm_su.get(eleven) == 13u);
assert (hm_su.get("eleven") == 13u);
assert (hm_su.get("twelve") == 14u);
assert (hm_su.get("ten") == 12u);
assert (!hm_su.insert("twelve", 14u));
assert (hm_su.get("twelve") == 14u);
assert (!hm_su.insert("twelve", 12u));
assert (hm_su.get("twelve") == 12u);
#debug("uint -> str");
let hm_us: map::hashmap<uint, str> =
map::mk_hashmap::<uint, str>(hasher_uint, eqer_uint);
assert (hm_us.insert(10u, "twelve"));
assert (hm_us.insert(11u, "thirteen"));
assert (hm_us.insert(12u, "fourteen"));
assert (str::eq(hm_us.get(11u), "thirteen"));
assert (str::eq(hm_us.get(12u), "fourteen"));
assert (str::eq(hm_us.get(10u), "twelve"));
assert (!hm_us.insert(12u, "fourteen"));
assert (str::eq(hm_us.get(12u), "fourteen"));
assert (!hm_us.insert(12u, "twelve"));
assert (str::eq(hm_us.get(12u), "twelve"));
#debug("str -> str");
let hm_ss: map::hashmap<str, str> =
map::mk_hashmap::<str, str>(hasher_str, eqer_str);
assert (hm_ss.insert(ten, "twelve"));
assert (hm_ss.insert(eleven, "thirteen"));
assert (hm_ss.insert(twelve, "fourteen"));
assert (str::eq(hm_ss.get("eleven"), "thirteen"));
assert (str::eq(hm_ss.get("twelve"), "fourteen"));
assert (str::eq(hm_ss.get("ten"), "twelve"));
assert (!hm_ss.insert("twelve", "fourteen"));
assert (str::eq(hm_ss.get("twelve"), "fourteen"));
assert (!hm_ss.insert("twelve", "twelve"));
assert (str::eq(hm_ss.get("twelve"), "twelve"));
#debug("*** finished test_simple");
}
/**
* Force map growth
*/
#[test]
fn test_growth() {
#debug("*** starting test_growth");
let num_to_insert: uint = 64u;
fn eq_uint(&&x: uint, &&y: uint) -> bool { ret x == y; }
fn uint_id(&&x: uint) -> uint { x }
#debug("uint -> uint");
let hasher_uint: map::hashfn<uint> = uint_id;
let eqer_uint: map::eqfn<uint> = eq_uint;
let hm_uu: map::hashmap<uint, uint> =
map::mk_hashmap::<uint, uint>(hasher_uint, eqer_uint);
let i: uint = 0u;
while i < num_to_insert {
assert (hm_uu.insert(i, i * i));
#debug("inserting %u -> %u", i, i*i);
i += 1u;
}
#debug("-----");
i = 0u;
while i < num_to_insert {
#debug("get(%u) = %u", i, hm_uu.get(i));
assert (hm_uu.get(i) == i * i);
i += 1u;
}
assert (hm_uu.insert(num_to_insert, 17u));
assert (hm_uu.get(num_to_insert) == 17u);
#debug("-----");
i = 0u;
while i < num_to_insert {
#debug("get(%u) = %u", i, hm_uu.get(i));
assert (hm_uu.get(i) == i * i);
i += 1u;
}
#debug("str -> str");
let hasher_str: map::hashfn<str> = str::hash;
let eqer_str: map::eqfn<str> = str::eq;
let hm_ss: map::hashmap<str, str> =
map::mk_hashmap::<str, str>(hasher_str, eqer_str);
i = 0u;
while i < num_to_insert {
assert hm_ss.insert(uint::to_str(i, 2u), uint::to_str(i * i, 2u));
#debug("inserting \"%s\" -> \"%s\"",
uint::to_str(i, 2u),
uint::to_str(i*i, 2u));
i += 1u;
}
#debug("-----");
i = 0u;
while i < num_to_insert {
#debug("get(\"%s\") = \"%s\"",
uint::to_str(i, 2u),
hm_ss.get(uint::to_str(i, 2u)));
assert (str::eq(hm_ss.get(uint::to_str(i, 2u)),
uint::to_str(i * i, 2u)));
i += 1u;
}
assert (hm_ss.insert(uint::to_str(num_to_insert, 2u),
uint::to_str(17u, 2u)));
assert (str::eq(hm_ss.get(uint::to_str(num_to_insert, 2u)),
uint::to_str(17u, 2u)));
#debug("-----");
i = 0u;
while i < num_to_insert {
#debug("get(\"%s\") = \"%s\"",
uint::to_str(i, 2u),
hm_ss.get(uint::to_str(i, 2u)));
assert (str::eq(hm_ss.get(uint::to_str(i, 2u)),
uint::to_str(i * i, 2u)));
i += 1u;
}
#debug("*** finished test_growth");
}
#[test]
fn test_removal() {
#debug("*** starting test_removal");
let num_to_insert: uint = 64u;
fn eq(&&x: uint, &&y: uint) -> bool { ret x == y; }
fn hash(&&u: uint) -> uint {
// This hash function intentionally causes collisions between
// consecutive integer pairs.
ret u / 2u * 2u;
}
assert (hash(0u) == hash(1u));
assert (hash(2u) == hash(3u));
assert (hash(0u) != hash(2u));
let hasher: map::hashfn<uint> = hash;
let eqer: map::eqfn<uint> = eq;
let hm: map::hashmap<uint, uint> =
map::mk_hashmap::<uint, uint>(hasher, eqer);
let i: uint = 0u;
while i < num_to_insert {
assert (hm.insert(i, i * i));
#debug("inserting %u -> %u", i, i*i);
i += 1u;
}
assert (hm.size() == num_to_insert);
#debug("-----");
#debug("removing evens");
i = 0u;
while i < num_to_insert {
let v = hm.remove(i);
alt v {
option::some(u) { assert (u == i * i); }
option::none. { fail; }
}
i += 2u;
}
assert (hm.size() == num_to_insert / 2u);
#debug("-----");
i = 1u;
while i < num_to_insert {
#debug("get(%u) = %u", i, hm.get(i));
assert (hm.get(i) == i * i);
i += 2u;
}
#debug("-----");
i = 1u;
while i < num_to_insert {
#debug("get(%u) = %u", i, hm.get(i));
assert (hm.get(i) == i * i);
i += 2u;
}
#debug("-----");
i = 0u;
while i < num_to_insert {
assert (hm.insert(i, i * i));
#debug("inserting %u -> %u", i, i*i);
i += 2u;
}
assert (hm.size() == num_to_insert);
#debug("-----");
i = 0u;
while i < num_to_insert {
#debug("get(%u) = %u", i, hm.get(i));
assert (hm.get(i) == i * i);
i += 1u;
}
#debug("-----");
assert (hm.size() == num_to_insert);
i = 0u;
while i < num_to_insert {
#debug("get(%u) = %u", i, hm.get(i));
assert (hm.get(i) == i * i);
i += 1u;
}
#debug("*** finished test_removal");
}
#[test]
fn test_contains_key() {
let key = "k";
let map = map::mk_hashmap::<str, str>(str::hash, str::eq);
assert (!map.contains_key(key));
map.insert(key, "val");
assert (map.contains_key(key));
}
#[test]
fn test_find() {
let key = "k";
let map = map::mk_hashmap::<str, str>(str::hash, str::eq);
assert (option::is_none(map.find(key)));
map.insert(key, "val");
assert (option::get(map.find(key)) == "val");
}
}

View File

@ -54,3 +54,13 @@ fn parse_addr(ip: str) -> ip_addr {
for i in parts { if i > 255u { fail "Invalid IP Address part."; } }
ipv4(parts[0] as u8, parts[1] as u8, parts[2] as u8, parts[3] as u8)
}
#[test]
fn test_format_ip() {
assert (net::format_addr(net::ipv4(127u8, 0u8, 0u8, 1u8)) == "127.0.0.1")
}
#[test]
fn test_parse_ip() {
assert (net::parse_addr("127.0.0.1") == net::ipv4(127u8, 0u8, 0u8, 1u8));
}

View File

@ -94,6 +94,46 @@ fn mk_rng() -> rng {
}
@rand_res(rustrt::rand_new()) as rng
}
#[cfg(test)]
mod tests {
#[test]
fn test() {
let r1: rand::rng = rand::mk_rng();
log(debug, r1.next());
log(debug, r1.next());
{
let r2 = rand::mk_rng();
log(debug, r1.next());
log(debug, r2.next());
log(debug, r1.next());
log(debug, r1.next());
log(debug, r2.next());
log(debug, r2.next());
log(debug, r1.next());
log(debug, r1.next());
log(debug, r1.next());
log(debug, r2.next());
log(debug, r2.next());
log(debug, r2.next());
}
log(debug, r1.next());
log(debug, r1.next());
}
#[test]
fn genstr() {
let r: rand::rng = rand::mk_rng();
log(debug, r.gen_str(10u));
log(debug, r.gen_str(10u));
log(debug, r.gen_str(10u));
assert(str::char_len(r.gen_str(10u)) == 10u);
assert(str::char_len(r.gen_str(16u)) == 16u);
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -1332,3 +1332,166 @@ mod node {
}
}
#[cfg(test)]
mod tests {
//Utility function, used for sanity check
fn rope_to_string(r: rope) -> str {
alt(r) {
node::empty. { ret "" }
node::content(x) {
let str = @mutable "";
fn aux(str: @mutable str, node: @node::node) {
alt(*node) {
node::leaf(x) {
*str += str::substr(
*x.content, x.byte_offset, x.byte_len);
}
node::concat(x) {
aux(str, x.left);
aux(str, x.right);
}
}
}
aux(str, x);
ret *str
}
}
}
#[test]
fn trivial() {
assert char_len(empty()) == 0u;
assert byte_len(empty()) == 0u;
}
#[test]
fn of_string1() {
let sample = @"0123456789ABCDE";
let r = of_str(sample);
assert char_len(r) == str::char_len(*sample);
assert rope_to_string(r) == *sample;
}
#[test]
fn of_string2() {
let buf = @ mutable "1234567890";
let i = 0;
while i < 10 { *buf = *buf + *buf; i+=1;}
let sample = @*buf;
let r = of_str(sample);
assert char_len(r) == str::char_len(*sample);
assert rope_to_string(r) == *sample;
let string_iter = 0u;
let string_len = str::byte_len(*sample);
let rope_iter = iterator::char::start(r);
let equal = true;
let pos = 0u;
while equal {
alt(node::char_iterator::next(rope_iter)) {
option::none. {
if string_iter < string_len {
equal = false;
} break; }
option::some(c) {
let {ch, next} = str::char_range_at(*sample, string_iter);
string_iter = next;
if ch != c { equal = false; break; }
}
}
pos += 1u;
}
assert equal;
}
#[test]
fn iter1() {
let buf = @ mutable "1234567890";
let i = 0;
while i < 10 { *buf = *buf + *buf; i+=1;}
let sample = @*buf;
let r = of_str(sample);
let len = 0u;
let it = iterator::char::start(r);
while true {
alt(node::char_iterator::next(it)) {
option::none. { break; }
option::some(_) { len += 1u; }
}
}
assert len == str::char_len(*sample);
}
#[test]
fn bal1() {
let init = @ "1234567890";
let buf = @ mutable * init;
let i = 0;
while i < 8 { *buf = *buf + *buf; i+=1;}
let sample = @*buf;
let r1 = of_str(sample);
let r2 = of_str(init);
i = 0;
while i < 8 { r2 = append_rope(r2, r2); i+= 1;}
assert eq(r1, r2);
let r3 = bal(r2);
assert char_len(r1) == char_len(r3);
assert eq(r1, r3);
}
#[test]
fn char_at1() {
//Generate a large rope
let r = of_str(@ "123456789");
uint::range(0u, 10u){|_i|
r = append_rope(r, r);
}
//Copy it in the slowest possible way
let r2 = empty();
uint::range(0u, char_len(r)){|i|
r2 = append_char(r2, char_at(r, i));
}
assert eq(r, r2);
let r3 = empty();
uint::range(0u, char_len(r)){|i|
r3 = prepend_char(r3, char_at(r, char_len(r) - i - 1u));
}
assert eq(r, r3);
//Additional sanity checks
let balr = bal(r);
let bal2 = bal(r2);
let bal3 = bal(r3);
assert eq(r, balr);
assert eq(r, bal2);
assert eq(r, bal3);
assert eq(r2, r3);
assert eq(bal2, bal3);
}
#[test]
fn concat1() {
//Generate a reasonable rope
let chunk = of_str(@ "123456789");
let r = empty();
uint::range(0u, 10u){|_i|
r = append_rope(r, chunk);
}
//Same rope, obtained with rope::concat
let r2 = concat(vec::init_elt(chunk, 10u));
assert eq(r, r2);
}
}

View File

@ -294,6 +294,75 @@ fn waitpid(pid: pid_t) -> int {
}
}
#[cfg(test)]
mod tests {
import io::writer_util;
import ctypes::fd_t;
// Regression test for memory leaks
#[ignore(cfg(target_os = "win32"))] // FIXME
fn test_leaks() {
run::run_program("echo", []);
run::start_program("echo", []);
run::program_output("echo", []);
}
#[test]
fn test_pipes() {
let pipe_in = os::pipe();
let pipe_out = os::pipe();
let pipe_err = os::pipe();
let pid =
run::spawn_process(
"cat", [], pipe_in.in, pipe_out.out, pipe_err.out);
os::close(pipe_in.in);
os::close(pipe_out.out);
os::close(pipe_err.out);
if pid == -1i32 { fail; }
let expected = "test";
writeclose(pipe_in.out, expected);
let actual = readclose(pipe_out.in);
readclose(pipe_err.in);
os::waitpid(pid);
log(debug, expected);
log(debug, actual);
assert (expected == actual);
fn writeclose(fd: fd_t, s: str) {
#error("writeclose %d, %s", fd as int, s);
let writer = io::fd_writer(fd, false);
writer.write_str(s);
os::close(fd);
}
fn readclose(fd: fd_t) -> str {
// Copied from run::program_output
let file = os::fd_FILE(fd);
let reader = io::FILE_reader(file, false);
let buf = "";
while !reader.eof() {
let bytes = reader.read_bytes(4096u);
buf += str::unsafe_from_bytes(bytes);
}
os::fclose(file);
ret buf;
}
}
#[test]
fn waitpid() {
let pid = run::spawn_process("false", [], 0i32, 0i32, 0i32);
let status = run::waitpid(pid);
assert status == 1;
}
}
// Local Variables:
// mode: rust
// fill-column: 78;

View File

@ -287,6 +287,102 @@ fn mk_sha1() -> sha1 {
ret sh;
}
#[cfg(test)]
mod tests {
#[test]
fn test() {
type test = {input: str, output: [u8]};
fn a_million_letter_a() -> str {
let i = 0;
let rs = "";
while i < 100000 { rs += "aaaaaaaaaa"; i += 1; }
ret rs;
}
// Test messages from FIPS 180-1
let fips_180_1_tests: [test] =
[{input: "abc",
output:
[0xA9u8, 0x99u8, 0x3Eu8, 0x36u8,
0x47u8, 0x06u8, 0x81u8, 0x6Au8,
0xBAu8, 0x3Eu8, 0x25u8, 0x71u8,
0x78u8, 0x50u8, 0xC2u8, 0x6Cu8,
0x9Cu8, 0xD0u8, 0xD8u8, 0x9Du8]},
{input:
"abcdbcdecdefdefgefghfghighij" +
"hijkijkljklmklmnlmnomnopnopq",
output:
[0x84u8, 0x98u8, 0x3Eu8, 0x44u8,
0x1Cu8, 0x3Bu8, 0xD2u8, 0x6Eu8,
0xBAu8, 0xAEu8, 0x4Au8, 0xA1u8,
0xF9u8, 0x51u8, 0x29u8, 0xE5u8,
0xE5u8, 0x46u8, 0x70u8, 0xF1u8]},
{input: a_million_letter_a(),
output:
[0x34u8, 0xAAu8, 0x97u8, 0x3Cu8,
0xD4u8, 0xC4u8, 0xDAu8, 0xA4u8,
0xF6u8, 0x1Eu8, 0xEBu8, 0x2Bu8,
0xDBu8, 0xADu8, 0x27u8, 0x31u8,
0x65u8, 0x34u8, 0x01u8, 0x6Fu8]}];
// Examples from wikipedia
let wikipedia_tests: [test] =
[{input: "The quick brown fox jumps over the lazy dog",
output:
[0x2fu8, 0xd4u8, 0xe1u8, 0xc6u8,
0x7au8, 0x2du8, 0x28u8, 0xfcu8,
0xedu8, 0x84u8, 0x9eu8, 0xe1u8,
0xbbu8, 0x76u8, 0xe7u8, 0x39u8,
0x1bu8, 0x93u8, 0xebu8, 0x12u8]},
{input: "The quick brown fox jumps over the lazy cog",
output:
[0xdeu8, 0x9fu8, 0x2cu8, 0x7fu8,
0xd2u8, 0x5eu8, 0x1bu8, 0x3au8,
0xfau8, 0xd3u8, 0xe8u8, 0x5au8,
0x0bu8, 0xd1u8, 0x7du8, 0x9bu8,
0x10u8, 0x0du8, 0xb4u8, 0xb3u8]}];
let tests = fips_180_1_tests + wikipedia_tests;
fn check_vec_eq(v0: [u8], v1: [u8]) {
assert (vec::len::<u8>(v0) == vec::len::<u8>(v1));
let len = vec::len::<u8>(v0);
let i = 0u;
while i < len {
let a = v0[i];
let b = v1[i];
assert (a == b);
i += 1u;
}
}
// Test that it works when accepting the message all at once
let sh = sha1::mk_sha1();
for t: test in tests {
sh.input_str(t.input);
let out = sh.result();
check_vec_eq(t.output, out);
sh.reset();
}
// Test that it works when accepting the message in pieces
for t: test in tests {
let len = str::byte_len(t.input);
let left = len;
while left > 0u {
let take = (left + 1u) / 2u;
sh.input_str(str::substr(t.input, len - left, take));
left = left - take;
}
let out = sh.result();
check_vec_eq(t.output, out);
sh.reset();
}
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -157,6 +157,150 @@ fn quick_sort3<T: copy>(compare_func_lt: lteq<T>, compare_func_eq: lteq<T>,
(len::<T>(arr) as int) - 1);
}
#[cfg(test)]
mod test_qsort3 {
fn check_sort(v1: [mutable int], v2: [mutable int]) {
let len = vec::len::<int>(v1);
fn lt(&&a: int, &&b: int) -> bool { ret a < b; }
fn equal(&&a: int, &&b: int) -> bool { ret a == b; }
let f1 = lt;
let f2 = equal;
quick_sort3::<int>(f1, f2, v1);
let i = 0u;
while i < len {
log(debug, v2[i]);
assert (v2[i] == v1[i]);
i += 1u;
}
}
#[test]
fn test() {
{
let v1 = [mutable 3, 7, 4, 5, 2, 9, 5, 8];
let v2 = [mutable 2, 3, 4, 5, 5, 7, 8, 9];
check_sort(v1, v2);
}
{
let v1 = [mutable 1, 1, 1];
let v2 = [mutable 1, 1, 1];
check_sort(v1, v2);
}
{
let v1: [mutable int] = [mutable];
let v2: [mutable int] = [mutable];
check_sort(v1, v2);
}
{ let v1 = [mutable 9]; let v2 = [mutable 9]; check_sort(v1, v2); }
{
let v1 = [mutable 9, 3, 3, 3, 9];
let v2 = [mutable 3, 3, 3, 9, 9];
check_sort(v1, v2);
}
}
}
#[cfg(test)]
mod test_qsort {
fn check_sort(v1: [mutable int], v2: [mutable int]) {
let len = vec::len::<int>(v1);
fn ltequal(&&a: int, &&b: int) -> bool { ret a <= b; }
let f = ltequal;
quick_sort::<int>(f, v1);
let i = 0u;
while i < len {
log(debug, v2[i]);
assert (v2[i] == v1[i]);
i += 1u;
}
}
#[test]
fn test() {
{
let v1 = [mutable 3, 7, 4, 5, 2, 9, 5, 8];
let v2 = [mutable 2, 3, 4, 5, 5, 7, 8, 9];
check_sort(v1, v2);
}
{
let v1 = [mutable 1, 1, 1];
let v2 = [mutable 1, 1, 1];
check_sort(v1, v2);
}
{
let v1: [mutable int] = [mutable];
let v2: [mutable int] = [mutable];
check_sort(v1, v2);
}
{ let v1 = [mutable 9]; let v2 = [mutable 9]; check_sort(v1, v2); }
{
let v1 = [mutable 9, 3, 3, 3, 9];
let v2 = [mutable 3, 3, 3, 9, 9];
check_sort(v1, v2);
}
}
// Regression test for #750
#[test]
fn test_simple() {
let names = [mutable 2, 1, 3];
let expected = [1, 2, 3];
fn lteq(&&a: int, &&b: int) -> bool { int::le(a, b) }
sort::quick_sort(lteq, names);
let immut_names = vec::from_mut(names);
// Silly, but what else can we do?
check (vec::same_length(expected, immut_names));
let pairs = vec::zip(expected, immut_names);
for (a, b) in pairs { #debug("%d %d", a, b); assert (a == b); }
}
}
#[cfg(test)]
mod tests {
fn check_sort(v1: [int], v2: [int]) {
let len = vec::len::<int>(v1);
fn lteq(&&a: int, &&b: int) -> bool { ret a <= b; }
let f = lteq;
let v3 = merge_sort::<int>(f, v1);
let i = 0u;
while i < len {
log(debug, v3[i]);
assert (v3[i] == v2[i]);
i += 1u;
}
}
#[test]
fn test() {
{
let v1 = [3, 7, 4, 5, 2, 9, 5, 8];
let v2 = [2, 3, 4, 5, 5, 7, 8, 9];
check_sort(v1, v2);
}
{ let v1 = [1, 1, 1]; let v2 = [1, 1, 1]; check_sort(v1, v2); }
{ let v1: [int] = []; let v2: [int] = []; check_sort(v1, v2); }
{ let v1 = [9]; let v2 = [9]; check_sort(v1, v2); }
{
let v1 = [9, 3, 3, 3, 9];
let v2 = [3, 3, 3, 9, 9];
check_sort(v1, v2);
}
}
#[test]
fn test_merge_sort_mutable() {
fn lteq(&&a: int, &&b: int) -> bool { ret a <= b; }
let v1 = [mutable 3, 2, 1];
let v2 = merge_sort(lteq, v1);
assert v2 == [1, 2, 3];
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -24,3 +24,15 @@ fn mkdtemp(prefix: str, suffix: str) -> option::t<str> {
}
ret none;
}
#[test]
fn test_mkdtemp() {
let r = mkdtemp("./", "foobar");
alt r {
some(p) {
fs::remove_dir(p);
assert(str::ends_with(p, "foobar"));
}
_ { assert(false); }
}
}

View File

@ -343,6 +343,135 @@ fn configure_test_task() {
task::unsupervise();
}
#[cfg(test)]
mod tests {
#[test]
fn do_not_run_ignored_tests() {
fn f() { fail; }
let desc = {
name: "whatever",
fn: f,
ignore: true,
should_fail: false
};
let future = test::run_test(desc, test::default_test_to_task);
let result = future.wait();
assert result != test::tr_ok;
}
#[test]
fn ignored_tests_result_in_ignored() {
fn f() { }
let desc = {
name: "whatever",
fn: f,
ignore: true,
should_fail: false
};
let res = test::run_test(desc, test::default_test_to_task).wait();
assert (res == test::tr_ignored);
}
#[test]
#[ignore(cfg(target_os = "win32"))]
fn test_should_fail() {
fn f() { fail; }
let desc = {
name: "whatever",
fn: f,
ignore: false,
should_fail: true
};
let res = test::run_test(desc, test::default_test_to_task).wait();
assert res == test::tr_ok;
}
#[test]
fn test_should_fail_but_succeeds() {
fn f() { }
let desc = {
name: "whatever",
fn: f,
ignore: false,
should_fail: true
};
let res = test::run_test(desc, test::default_test_to_task).wait();
assert res == test::tr_failed;
}
#[test]
fn first_free_arg_should_be_a_filter() {
let args = ["progname", "filter"];
check (vec::is_not_empty(args));
let opts = alt test::parse_opts(args) { either::left(o) { o } };
assert (str::eq("filter", option::get(opts.filter)));
}
#[test]
fn parse_ignored_flag() {
let args = ["progname", "filter", "--ignored"];
check (vec::is_not_empty(args));
let opts = alt test::parse_opts(args) { either::left(o) { o } };
assert (opts.run_ignored);
}
#[test]
fn filter_for_ignored_option() {
// When we run ignored tests the test filter should filter out all the
// unignored tests and flip the ignore flag on the rest to false
let opts = {filter: option::none, run_ignored: true};
let tests =
[{name: "1", fn: fn@() { }, ignore: true, should_fail: false},
{name: "2", fn: fn@() { }, ignore: false, should_fail: false}];
let filtered = test::filter_tests(opts, tests);
assert (vec::len(filtered) == 1u);
assert (filtered[0].name == "1");
assert (filtered[0].ignore == false);
}
#[test]
fn sort_tests() {
let opts = {filter: option::none, run_ignored: false};
let names =
["sha1::test", "int::test_to_str", "int::test_pow",
"test::do_not_run_ignored_tests",
"test::ignored_tests_result_in_ignored",
"test::first_free_arg_should_be_a_filter",
"test::parse_ignored_flag", "test::filter_for_ignored_option",
"test::sort_tests"];
let tests =
{
let testfn = fn@() { };
let tests = [];
for name: str in names {
let test = {name: name, fn: testfn, ignore: false,
should_fail: false};
tests += [test];
}
tests
};
let filtered = test::filter_tests(opts, tests);
let expected =
["int::test_pow", "int::test_to_str", "sha1::test",
"test::do_not_run_ignored_tests", "test::filter_for_ignored_option",
"test::first_free_arg_should_be_a_filter",
"test::ignored_tests_result_in_ignored", "test::parse_ignored_flag",
"test::sort_tests"];
check (vec::same_length(expected, filtered));
let pairs = vec::zip(expected, filtered);
for (a, b) in pairs { assert (a == b.name); }
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -94,3 +94,65 @@ fn traverse<K, V>(m: treemap<K, V>, f: block(K, V)) {
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn init_treemap() { let _m = init::<int, int>(); }
#[test]
fn insert_one() { let m = init(); insert(m, 1, 2); }
#[test]
fn insert_two() { let m = init(); insert(m, 1, 2); insert(m, 3, 4); }
#[test]
fn insert_find() {
let m = init();
insert(m, 1, 2);
assert (find(m, 1) == some(2));
}
#[test]
fn find_empty() {
let m = init::<int, int>(); assert (find(m, 1) == none);
}
#[test]
fn find_not_found() {
let m = init();
insert(m, 1, 2);
assert (find(m, 2) == none);
}
#[test]
fn traverse_in_order() {
let m = init();
insert(m, 3, ());
insert(m, 0, ());
insert(m, 4, ());
insert(m, 2, ());
insert(m, 1, ());
let n = @mutable 0;
fn t(n: @mutable int, &&k: int, &&_v: ()) {
assert (*n == k); *n += 1;
}
traverse(m, bind t(n, _, _));
}
#[test]
fn u8_map() {
let m = init();
let k1 = str::bytes("foo");
let k2 = str::bytes("bar");
insert(m, k1, "foo");
insert(m, k2, "bar");
assert (find(m, k2) == some("bar"));
assert (find(m, k1) == some("foo"));
}
}

View File

@ -179,6 +179,124 @@ An u8 whose first bit is set if `if_true(v)` holds
*/
fn to_bit(v: t) -> u8 { v & b0 }
#[cfg(test)]
mod tests {
pure fn eq1(a: tri::t, b: tri::t) -> bool { tri::eq(a , b) }
pure fn ne1(a: tri::t, b: tri::t) -> bool { tri::ne(a , b) }
pure fn eq2(a: tri::t, b: tri::t) -> bool { eq1( a, b ) && eq1( b, a ) }
#[test]
fn test_eq2() {
tri::all_values { |a|
tri::all_values { |b|
assert if a == b { eq1( a, b ) } else { ne1( a, b ) }
}
}
}
#[test]
fn test_tri_and_symmetry() {
tri::all_values { |a|
tri::all_values { |b|
assert eq1( tri::and(a ,b), tri::and(b, a) );
}
}
}
#[test]
fn test_tri_or_symmetry() {
tri::all_values { |a|
tri::all_values { |b|
assert eq1( tri::or(a ,b), tri::or(b, a) );
}
}
}
#[test]
fn test_tri_xor_symmetry() {
tri::all_values { |a|
tri::all_values { |b|
assert eq1( tri::xor(a ,b), tri::xor(b, a) );
}
}
}
#[test]
fn test_tri_not() {
assert eq2( tri::not(tri::true), tri::false);
assert eq2( tri::not(tri::unknown), tri::unknown);
assert eq2( tri::not(tri::false), tri::true);
}
#[test]
fn test_tri_and() {
assert eq2( tri::and(tri::true, tri::true), tri::true);
assert eq2( tri::and(tri::true, tri::false), tri::false);
assert eq2( tri::and(tri::true, tri::unknown), tri::unknown);
assert eq2( tri::and(tri::false, tri::false), tri::false);
assert eq2( tri::and(tri::false, tri::unknown), tri::false);
assert eq2( tri::and(tri::unknown, tri::unknown), tri::unknown);
}
#[test]
fn test_tri_or() {
assert eq2( tri::or(tri::true, tri::true), tri::true);
assert eq2( tri::or(tri::true, tri::false), tri::true);
assert eq2( tri::or(tri::true, tri::unknown), tri::true);
assert eq2( tri::or(tri::false, tri::false), tri::false);
assert eq2( tri::or(tri::false, tri::unknown), tri::unknown);
assert eq2( tri::or(tri::unknown, tri::unknown), tri::unknown);
}
#[test]
fn test_tri_xor() {
assert eq2( tri::xor(tri::true, tri::true), tri::false);
assert eq2( tri::xor(tri::false, tri::false), tri::false);
assert eq2( tri::xor(tri::true, tri::false), tri::true);
assert eq2( tri::xor(tri::true, tri::unknown), tri::unknown);
assert eq2( tri::xor(tri::false, tri::unknown), tri::unknown);
assert eq2( tri::xor(tri::unknown, tri::unknown), tri::unknown);
}
#[test]
fn test_tri_implies() {
assert eq2( tri::implies(tri::false, tri::false), tri::true);
assert eq2( tri::implies(tri::false, tri::unknown), tri::true);
assert eq2( tri::implies(tri::false, tri::true), tri::true);
assert eq2( tri::implies(tri::unknown, tri::false), tri::unknown);
assert eq2( tri::implies(tri::unknown, tri::unknown), tri::unknown);
assert eq2( tri::implies(tri::unknown, tri::true), tri::true);
assert eq2( tri::implies(tri::true, tri::false), tri::false);
assert eq2( tri::implies(tri::true, tri::unknown), tri::unknown);
assert eq2( tri::implies(tri::true, tri::true), tri::true);
}
#[test]
fn test_tri_from_str() {
tri::all_values { |v|
assert eq2( v, tri::from_str(tri::to_str(v)));
}
}
#[test]
fn test_tri_to_str() {
assert tri::to_str(tri::false) == "false";
assert tri::to_str(tri::unknown) == "unknown";
assert tri::to_str(tri::true) == "true";
}
#[test]
fn test_tri_to_bit() {
tri::all_values { |v|
assert tri::to_bit(v) == if tri::is_true(v) { 1u8 } else { 0u8 };
}
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;

View File

@ -207,3 +207,30 @@ pure fn is_upper(c: char) -> bool {
ret icu::libicu::u_isupper(c) == icu::TRUE;
}
#[cfg(test)]
mod tests {
#[test]
fn test_is_digit() {
assert (unicode::icu::is_digit('0'));
assert (!unicode::icu::is_digit('m'));
}
#[test]
fn test_is_lower() {
assert (unicode::icu::is_lower('m'));
assert (!unicode::icu::is_lower('M'));
}
#[test]
fn test_is_space() {
assert (unicode::icu::is_space(' '));
assert (!unicode::icu::is_space('m'));
}
#[test]
fn test_is_upper() {
assert (unicode::icu::is_upper('M'));
assert (!unicode::icu::is_upper('m'));
}
}

View File

@ -153,3 +153,47 @@ fn idle_new() -> idle_t {
fields: handle_fields_new()
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_sanity_check() {
sanity_check();
}
// From test-ref.c
mod test_ref {
#[test]
fn ref() {
let loop = loop_new();
run(loop);
loop_delete(loop);
}
#[test]
fn idle_ref() {
let loop = loop_new();
let h = idle_new();
idle_init(loop, ptr::addr_of(h));
idle_start(ptr::addr_of(h), ptr::null());
unref(loop);
run(loop);
loop_delete(loop);
}
#[test]
fn async_ref() {
/*
let loop = loop_new();
let h = async_new();
async_init(loop, ptr::addr_of(h), ptr::null());
unref(loop);
run(loop);
loop_delete(loop);
*/
}
}
}

View File

@ -1,287 +0,0 @@
import core::*;
use std;
import vec;
import std::bitv;
#[test]
fn test_0_elements() {
let act;
let exp;
act = bitv::create(0u, false);
exp = vec::init_elt::<uint>(0u, 0u);
assert (bitv::eq_vec(act, exp));
}
#[test]
fn test_1_element() {
let act;
act = bitv::create(1u, false);
assert (bitv::eq_vec(act, [0u]));
act = bitv::create(1u, true);
assert (bitv::eq_vec(act, [1u]));
}
#[test]
fn test_10_elements() {
let act;
// all 0
act = bitv::create(10u, false);
assert (bitv::eq_vec(act, [0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u]));
// all 1
act = bitv::create(10u, true);
assert (bitv::eq_vec(act, [1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = bitv::create(10u, false);
bitv::set(act, 0u, true);
bitv::set(act, 1u, true);
bitv::set(act, 2u, true);
bitv::set(act, 3u, true);
bitv::set(act, 4u, true);
assert (bitv::eq_vec(act, [1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = bitv::create(10u, false);
bitv::set(act, 5u, true);
bitv::set(act, 6u, true);
bitv::set(act, 7u, true);
bitv::set(act, 8u, true);
bitv::set(act, 9u, true);
assert (bitv::eq_vec(act, [0u, 0u, 0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = bitv::create(10u, false);
bitv::set(act, 0u, true);
bitv::set(act, 3u, true);
bitv::set(act, 6u, true);
bitv::set(act, 9u, true);
assert (bitv::eq_vec(act, [1u, 0u, 0u, 1u, 0u, 0u, 1u, 0u, 0u, 1u]));
}
#[test]
fn test_31_elements() {
let act;
// all 0
act = bitv::create(31u, false);
assert (bitv::eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u]));
// all 1
act = bitv::create(31u, true);
assert (bitv::eq_vec(act,
[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u]));
// mixed
act = bitv::create(31u, false);
bitv::set(act, 0u, true);
bitv::set(act, 1u, true);
bitv::set(act, 2u, true);
bitv::set(act, 3u, true);
bitv::set(act, 4u, true);
bitv::set(act, 5u, true);
bitv::set(act, 6u, true);
bitv::set(act, 7u, true);
assert (bitv::eq_vec(act,
[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u]));
// mixed
act = bitv::create(31u, false);
bitv::set(act, 16u, true);
bitv::set(act, 17u, true);
bitv::set(act, 18u, true);
bitv::set(act, 19u, true);
bitv::set(act, 20u, true);
bitv::set(act, 21u, true);
bitv::set(act, 22u, true);
bitv::set(act, 23u, true);
assert (bitv::eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u,
0u, 0u, 0u, 0u, 0u]));
// mixed
act = bitv::create(31u, false);
bitv::set(act, 24u, true);
bitv::set(act, 25u, true);
bitv::set(act, 26u, true);
bitv::set(act, 27u, true);
bitv::set(act, 28u, true);
bitv::set(act, 29u, true);
bitv::set(act, 30u, true);
assert (bitv::eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
1u, 1u, 1u, 1u, 1u]));
// mixed
act = bitv::create(31u, false);
bitv::set(act, 3u, true);
bitv::set(act, 17u, true);
bitv::set(act, 30u, true);
assert (bitv::eq_vec(act,
[0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 1u]));
}
#[test]
fn test_32_elements() {
let act;
// all 0
act = bitv::create(32u, false);
assert (bitv::eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u]));
// all 1
act = bitv::create(32u, true);
assert (bitv::eq_vec(act,
[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = bitv::create(32u, false);
bitv::set(act, 0u, true);
bitv::set(act, 1u, true);
bitv::set(act, 2u, true);
bitv::set(act, 3u, true);
bitv::set(act, 4u, true);
bitv::set(act, 5u, true);
bitv::set(act, 6u, true);
bitv::set(act, 7u, true);
assert (bitv::eq_vec(act,
[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = bitv::create(32u, false);
bitv::set(act, 16u, true);
bitv::set(act, 17u, true);
bitv::set(act, 18u, true);
bitv::set(act, 19u, true);
bitv::set(act, 20u, true);
bitv::set(act, 21u, true);
bitv::set(act, 22u, true);
bitv::set(act, 23u, true);
assert (bitv::eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = bitv::create(32u, false);
bitv::set(act, 24u, true);
bitv::set(act, 25u, true);
bitv::set(act, 26u, true);
bitv::set(act, 27u, true);
bitv::set(act, 28u, true);
bitv::set(act, 29u, true);
bitv::set(act, 30u, true);
bitv::set(act, 31u, true);
assert (bitv::eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = bitv::create(32u, false);
bitv::set(act, 3u, true);
bitv::set(act, 17u, true);
bitv::set(act, 30u, true);
bitv::set(act, 31u, true);
assert (bitv::eq_vec(act,
[0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 1u, 1u]));
}
#[test]
fn test_33_elements() {
let act;
// all 0
act = bitv::create(33u, false);
assert (bitv::eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u]));
// all 1
act = bitv::create(33u, true);
assert (bitv::eq_vec(act,
[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = bitv::create(33u, false);
bitv::set(act, 0u, true);
bitv::set(act, 1u, true);
bitv::set(act, 2u, true);
bitv::set(act, 3u, true);
bitv::set(act, 4u, true);
bitv::set(act, 5u, true);
bitv::set(act, 6u, true);
bitv::set(act, 7u, true);
assert (bitv::eq_vec(act,
[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = bitv::create(33u, false);
bitv::set(act, 16u, true);
bitv::set(act, 17u, true);
bitv::set(act, 18u, true);
bitv::set(act, 19u, true);
bitv::set(act, 20u, true);
bitv::set(act, 21u, true);
bitv::set(act, 22u, true);
bitv::set(act, 23u, true);
assert (bitv::eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = bitv::create(33u, false);
bitv::set(act, 24u, true);
bitv::set(act, 25u, true);
bitv::set(act, 26u, true);
bitv::set(act, 27u, true);
bitv::set(act, 28u, true);
bitv::set(act, 29u, true);
bitv::set(act, 30u, true);
bitv::set(act, 31u, true);
assert (bitv::eq_vec(act,
[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 0u]));
// mixed
act = bitv::create(33u, false);
bitv::set(act, 3u, true);
bitv::set(act, 17u, true);
bitv::set(act, 30u, true);
bitv::set(act, 31u, true);
bitv::set(act, 32u, true);
assert (bitv::eq_vec(act,
[0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 1u, 1u, 1u]));
}

View File

@ -1,61 +0,0 @@
import core::*;
// -*- rust -*-
use std;
import std::c_vec::*;
import ctypes::*;
#[nolink]
#[abi = "cdecl"]
native mod libc {
fn malloc(n: size_t) -> *mutable u8;
fn free(m: *mutable u8);
}
fn malloc(n: size_t) -> t<u8> {
let mem = libc::malloc(n);
assert mem as int != 0;
ret unsafe { create_with_dtor(mem, n, bind libc::free(mem)) };
}
#[test]
fn test_basic() {
let cv = malloc(16u);
set(cv, 3u, 8u8);
set(cv, 4u, 9u8);
assert get(cv, 3u) == 8u8;
assert get(cv, 4u) == 9u8;
assert len(cv) == 16u;
}
#[test]
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_overrun_get() {
let cv = malloc(16u);
get(cv, 17u);
}
#[test]
#[should_fail]
#[ignore(cfg(target_os = "win32"))]
fn test_overrun_set() {
let cv = malloc(16u);
set(cv, 17u, 0u8);
}
#[test]
fn test_and_I_mean_it() {
let cv = malloc(16u);
let p = unsafe { ptr(cv) };
set(cv, 0u, 32u8);
set(cv, 1u, 33u8);
assert unsafe { *p } == 32u8;
set(cv, 2u, 34u8); /* safety */
}

View File

@ -1,197 +0,0 @@
import core::*;
// -*- rust -*-
use std;
import std::deque;
#[test]
fn test_simple() {
let d: deque::t<int> = deque::create::<int>();
assert (d.size() == 0u);
d.add_front(17);
d.add_front(42);
d.add_back(137);
assert (d.size() == 3u);
d.add_back(137);
assert (d.size() == 4u);
log(debug, d.peek_front());
assert (d.peek_front() == 42);
log(debug, d.peek_back());
assert (d.peek_back() == 137);
let i: int = d.pop_front();
log(debug, i);
assert (i == 42);
i = d.pop_back();
log(debug, i);
assert (i == 137);
i = d.pop_back();
log(debug, i);
assert (i == 137);
i = d.pop_back();
log(debug, i);
assert (i == 17);
assert (d.size() == 0u);
d.add_back(3);
assert (d.size() == 1u);
d.add_front(2);
assert (d.size() == 2u);
d.add_back(4);
assert (d.size() == 3u);
d.add_front(1);
assert (d.size() == 4u);
log(debug, d.get(0));
log(debug, d.get(1));
log(debug, d.get(2));
log(debug, d.get(3));
assert (d.get(0) == 1);
assert (d.get(1) == 2);
assert (d.get(2) == 3);
assert (d.get(3) == 4);
}
fn test_boxes(a: @int, b: @int, c: @int, d: @int) {
let deq: deque::t<@int> = deque::create::<@int>();
assert (deq.size() == 0u);
deq.add_front(a);
deq.add_front(b);
deq.add_back(c);
assert (deq.size() == 3u);
deq.add_back(d);
assert (deq.size() == 4u);
assert (deq.peek_front() == b);
assert (deq.peek_back() == d);
assert (deq.pop_front() == b);
assert (deq.pop_back() == d);
assert (deq.pop_back() == c);
assert (deq.pop_back() == a);
assert (deq.size() == 0u);
deq.add_back(c);
assert (deq.size() == 1u);
deq.add_front(b);
assert (deq.size() == 2u);
deq.add_back(d);
assert (deq.size() == 3u);
deq.add_front(a);
assert (deq.size() == 4u);
assert (deq.get(0) == a);
assert (deq.get(1) == b);
assert (deq.get(2) == c);
assert (deq.get(3) == d);
}
type eqfn<T> = fn@(T, T) -> bool;
fn test_parameterized<T: copy>(e: eqfn<T>, a: T, b: T, c: T, d: T) {
let deq: deque::t<T> = deque::create::<T>();
assert (deq.size() == 0u);
deq.add_front(a);
deq.add_front(b);
deq.add_back(c);
assert (deq.size() == 3u);
deq.add_back(d);
assert (deq.size() == 4u);
assert (e(deq.peek_front(), b));
assert (e(deq.peek_back(), d));
assert (e(deq.pop_front(), b));
assert (e(deq.pop_back(), d));
assert (e(deq.pop_back(), c));
assert (e(deq.pop_back(), a));
assert (deq.size() == 0u);
deq.add_back(c);
assert (deq.size() == 1u);
deq.add_front(b);
assert (deq.size() == 2u);
deq.add_back(d);
assert (deq.size() == 3u);
deq.add_front(a);
assert (deq.size() == 4u);
assert (e(deq.get(0), a));
assert (e(deq.get(1), b));
assert (e(deq.get(2), c));
assert (e(deq.get(3), d));
}
tag taggy { one(int); two(int, int); three(int, int, int); }
tag taggypar<T> { onepar(int); twopar(int, int); threepar(int, int, int); }
type reccy = {x: int, y: int, t: taggy};
#[test]
fn test() {
fn inteq(&&a: int, &&b: int) -> bool { ret a == b; }
fn intboxeq(&&a: @int, &&b: @int) -> bool { ret a == b; }
fn taggyeq(a: taggy, b: taggy) -> bool {
alt a {
one(a1) { alt b { one(b1) { ret a1 == b1; } _ { ret false; } } }
two(a1, a2) {
alt b {
two(b1, b2) { ret a1 == b1 && a2 == b2; }
_ { ret false; }
}
}
three(a1, a2, a3) {
alt b {
three(b1, b2, b3) { ret a1 == b1 && a2 == b2 && a3 == b3; }
_ { ret false; }
}
}
}
}
fn taggypareq<T>(a: taggypar<T>, b: taggypar<T>) -> bool {
alt a {
onepar::<T>(a1) {
alt b { onepar::<T>(b1) { ret a1 == b1; } _ { ret false; } }
}
twopar::<T>(a1, a2) {
alt b {
twopar::<T>(b1, b2) { ret a1 == b1 && a2 == b2; }
_ { ret false; }
}
}
threepar::<T>(a1, a2, a3) {
alt b {
threepar::<T>(b1, b2, b3) {
ret a1 == b1 && a2 == b2 && a3 == b3;
}
_ { ret false; }
}
}
}
}
fn reccyeq(a: reccy, b: reccy) -> bool {
ret a.x == b.x && a.y == b.y && taggyeq(a.t, b.t);
}
#debug("*** test boxes");
test_boxes(@5, @72, @64, @175);
#debug("*** end test boxes");
#debug("test parameterized: int");
let eq1: eqfn<int> = inteq;
test_parameterized::<int>(eq1, 5, 72, 64, 175);
#debug("*** test parameterized: @int");
let eq2: eqfn<@int> = intboxeq;
test_parameterized::<@int>(eq2, @5, @72, @64, @175);
#debug("*** end test parameterized @int");
#debug("test parameterized: taggy");
let eq3: eqfn<taggy> = taggyeq;
test_parameterized::<taggy>(eq3, one(1), two(1, 2), three(1, 2, 3),
two(17, 42));
#debug("*** test parameterized: taggypar<int>");
let eq4: eqfn<taggypar<int>> = bind taggypareq::<int>(_, _);
test_parameterized::<taggypar<int>>(eq4, onepar::<int>(1),
twopar::<int>(1, 2),
threepar::<int>(1, 2, 3),
twopar::<int>(17, 42));
#debug("*** end test parameterized: taggypar::<int>");
#debug("*** test parameterized: reccy");
let reccy1: reccy = {x: 1, y: 2, t: one(1)};
let reccy2: reccy = {x: 345, y: 2, t: two(1, 2)};
let reccy3: reccy = {x: 1, y: 777, t: three(1, 2, 3)};
let reccy4: reccy = {x: 19, y: 252, t: two(17, 42)};
let eq5: eqfn<reccy> = reccyeq;
test_parameterized::<reccy>(eq5, reccy1, reccy2, reccy3, reccy4);
#debug("*** end test parameterized: reccy");
#debug("*** done");
}

View File

@ -1,151 +0,0 @@
import core::*;
use std;
import std::tri;
import std::four;
fn eq1(a: four::t, b: four::t) -> bool { four::eq(a , b) }
fn ne1(a: four::t, b: four::t) -> bool { four::ne(a , b) }
fn eq2(a: four::t, b: four::t) -> bool { eq1( a, b ) && eq1( b, a ) }
#[test]
fn test_four_req_eq() {
four::all_values { |a|
four::all_values { |b|
assert if a == b { eq1( a, b ) } else { ne1( a, b ) };
}
}
}
#[test]
fn test_four_and_symmetry() {
four::all_values { |a|
four::all_values { |b|
assert eq1( four::and(a ,b), four::and(b, a) );
}
}
}
#[test]
fn test_four_xor_symmetry() {
four::all_values { |a|
four::all_values { |b|
assert eq1( four::and(a ,b), four::and(b, a) );
}
}
}
#[test]
fn test_four_or_symmetry() {
four::all_values { |a|
four::all_values { |b|
assert eq1( four::or(a ,b), four::or(b, a) );
}
}
}
fn to_tup(v: four::t) -> (bool, bool) {
alt v {
0u8 { (false, false) }
1u8 { (false, true) }
2u8 { (true, false) }
3u8 { (true, true) }
}
}
#[test]
fn test_four_not() {
four::all_values { |a|
let (x, y) = to_tup(a);
assert to_tup(four::not(a)) == (y, x);
};
}
#[test]
fn test_four_and() {
four::all_values { |a|
four::all_values { |b|
let (y1, x1) = to_tup(a);
let (y2, x2) = to_tup(b);
let (y3, x3) = to_tup(four::and(a, b));
assert (x3, y3) == (x1 && x2, y1 || y2);
}
};
}
#[test]
fn test_four_or() {
four::all_values { |a|
four::all_values { |b|
let (y1, x1) = to_tup(a);
let (y2, x2) = to_tup(b);
let (y3, x3) = to_tup(four::or(a, b));
assert (x3, y3) == (x1 || x2, y1 && y2);
}
};
}
#[test]
fn test_four_implies() {
four::all_values { |a|
four::all_values { |b|
let (_, x1) = to_tup(a);
let (y2, x2) = to_tup(b);
let (y3, x3) = to_tup(four::implies(a, b));
assert (x3, y3) == (!x1 || x2, x1 && y2);
}
};
}
#[test]
fn test_four_is_true() {
assert !four::is_true(four::none);
assert !four::is_true(four::false);
assert four::is_true(four::true);
assert four::is_true(four::both);
}
#[test]
fn test_four_is_false() {
assert four::is_false(four::none);
assert four::is_false(four::false);
assert !four::is_false(four::true);
assert !four::is_false(four::both);
}
#[test]
fn test_four_from_str() {
four::all_values { |v|
assert eq1( v, four::from_str(four::to_str(v)) );
}
}
#[test]
fn test_four_to_str() {
assert four::to_str(four::none) == "none";
assert four::to_str(four::false) == "false";
assert four::to_str(four::true) == "true" ;
assert four::to_str(four::both) == "both";
}
#[test]
fn test_four_to_tri() {
assert tri::eq( four::to_trit(four::true), tri::true );
assert tri::eq( four::to_trit(four::false), tri::false );
assert tri::eq( four::to_trit(four::none), tri::unknown );
log(debug, four::to_trit(four::both));
assert tri::eq( four::to_trit(four::both), tri::unknown );
}
#[test]
fn test_four_to_bit() {
four::all_values { |v|
assert four::to_bit(v) == if four::is_true(v) { 1u8 } else { 0u8 };
}
}

View File

@ -1,275 +0,0 @@
import core::*;
use std;
import std::fs;
import vec;
#[test]
fn test_connect() {
let slash = fs::path_sep();
log(error, fs::connect("a", "b"));
assert (fs::connect("a", "b") == "a" + slash + "b");
assert (fs::connect("a" + slash, "b") == "a" + slash + "b");
}
// Issue #712
#[test]
fn test_list_dir_no_invalid_memory_access() { fs::list_dir("."); }
#[test]
fn list_dir() {
let dirs = fs::list_dir(".");
// Just assuming that we've got some contents in the current directory
assert (vec::len(dirs) > 0u);
for dir in dirs { log(debug, dir); }
}
#[test]
fn path_is_dir() {
assert (fs::path_is_dir("."));
assert (!fs::path_is_dir("test/stdtest/fs.rs"));
}
#[test]
fn path_exists() {
assert (fs::path_exists("."));
assert (!fs::path_exists("test/nonexistent-bogus-path"));
}
fn ps() -> str {
fs::path_sep()
}
fn aps() -> str {
"/"
}
#[test]
fn split1() {
let actual = fs::split("a" + ps() + "b");
let expected = ["a", "b"];
assert actual == expected;
}
#[test]
fn split2() {
let actual = fs::split("a" + aps() + "b");
let expected = ["a", "b"];
assert actual == expected;
}
#[test]
fn split3() {
let actual = fs::split(ps() + "a" + ps() + "b");
let expected = ["a", "b"];
assert actual == expected;
}
#[test]
fn split4() {
let actual = fs::split("a" + ps() + "b" + aps() + "c");
let expected = ["a", "b", "c"];
assert actual == expected;
}
#[test]
fn normalize1() {
let actual = fs::normalize("a/b/..");
let expected = "a";
assert actual == expected;
}
#[test]
fn normalize2() {
let actual = fs::normalize("/a/b/..");
let expected = "/a";
assert actual == expected;
}
#[test]
fn normalize3() {
let actual = fs::normalize("a/../b");
let expected = "b";
assert actual == expected;
}
#[test]
fn normalize4() {
let actual = fs::normalize("/a/../b");
let expected = "/b";
assert actual == expected;
}
#[test]
fn normalize5() {
let actual = fs::normalize("a/.");
let expected = "a";
assert actual == expected;
}
#[test]
fn normalize6() {
let actual = fs::normalize("a/./b/");
let expected = "a/b/";
assert actual == expected;
}
#[test]
fn normalize7() {
let actual = fs::normalize("a/..");
let expected = ".";
assert actual == expected;
}
#[test]
fn normalize8() {
let actual = fs::normalize("../../..");
let expected = "../../..";
assert actual == expected;
}
#[test]
fn normalize9() {
let actual = fs::normalize("a/b/../../..");
let expected = "..";
assert actual == expected;
}
#[test]
fn normalize10() {
let actual = fs::normalize("/a/b/c/../d/./../../e/");
let expected = "/a/e/";
log(error, actual);
assert actual == expected;
}
#[test]
fn normalize11() {
let actual = fs::normalize("/a/..");
let expected = "/";
assert actual == expected;
}
#[test]
#[cfg(target_os = "win32")]
fn normalize12() {
let actual = fs::normalize("C:/whatever");
let expected = "C:/whatever";
log(error, actual);
assert actual == expected;
}
#[test]
#[cfg(target_os = "win32")]
fn path_is_absolute_win32() {
assert fs::path_is_absolute("C:/whatever");
}
#[test]
fn splitext_empty() {
let (base, ext) = fs::splitext("");
assert base == "";
assert ext == "";
}
#[test]
fn splitext_ext() {
let (base, ext) = fs::splitext("grum.exe");
assert base == "grum";
assert ext == ".exe";
}
#[test]
fn splitext_noext() {
let (base, ext) = fs::splitext("grum");
assert base == "grum";
assert ext == "";
}
#[test]
fn splitext_dotfile() {
let (base, ext) = fs::splitext(".grum");
assert base == ".grum";
assert ext == "";
}
#[test]
fn splitext_path_ext() {
let (base, ext) = fs::splitext("oh/grum.exe");
assert base == "oh/grum";
assert ext == ".exe";
}
#[test]
fn splitext_path_noext() {
let (base, ext) = fs::splitext("oh/grum");
assert base == "oh/grum";
assert ext == "";
}
#[test]
fn splitext_dot_in_path() {
let (base, ext) = fs::splitext("oh.my/grum");
assert base == "oh.my/grum";
assert ext == "";
}
#[test]
fn splitext_nobasename() {
let (base, ext) = fs::splitext("oh.my/");
assert base == "oh.my/";
assert ext == "";
}
#[test]
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
fn homedir() {
import getenv = std::generic_os::getenv;
import setenv = std::generic_os::setenv;
let oldhome = getenv("HOME");
setenv("HOME", "/home/MountainView");
assert fs::homedir() == some("/home/MountainView");
setenv("HOME", "");
assert fs::homedir() == none;
option::may(oldhome, {|s| setenv("HOME", s)});
}
#[test]
#[cfg(target_os = "win32")]
fn homedir() {
import getenv = std::generic_os::getenv;
import setenv = std::generic_os::setenv;
let oldhome = getenv("HOME");
let olduserprofile = getenv("USERPROFILE");
setenv("HOME", "");
setenv("USERPROFILE", "");
assert fs::homedir() == none;
setenv("HOME", "/home/MountainView");
assert fs::homedir() == some("/home/MountainView");
setenv("HOME", "");
setenv("USERPROFILE", "/home/MountainView");
assert fs::homedir() == some("/home/MountainView");
setenv("USERPROFILE", "/home/MountainView");
assert fs::homedir() == some("/home/MountainView");
setenv("HOME", "/home/MountainView");
setenv("USERPROFILE", "/home/PaloAlto");
assert fs::homedir() == some("/home/MountainView");
option::may(oldhome, {|s| setenv("HOME", s)});
option::may(olduserprofile, {|s| setenv("USERPROFILE", s)});
}

View File

@ -1,471 +0,0 @@
import core::*;
use std;
import opt = std::getopts;
import result::{err, ok};
tag fail_type {
argument_missing;
unrecognized_option;
option_missing;
option_duplicated;
unexpected_argument;
}
fn check_fail_type(f: opt::fail_, ft: fail_type) {
alt f {
opt::argument_missing(_) { assert (ft == argument_missing); }
opt::unrecognized_option(_) { assert (ft == unrecognized_option); }
opt::option_missing(_) { assert (ft == option_missing); }
opt::option_duplicated(_) { assert (ft == option_duplicated); }
opt::unexpected_argument(_) { assert (ft == unexpected_argument); }
_ { fail; }
}
}
// Tests for reqopt
#[test]
fn test_reqopt_long() {
let args = ["--test=20"];
let opts = [opt::reqopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) {
assert (opt::opt_present(m, "test"));
assert (opt::opt_str(m, "test") == "20");
}
_ { fail; }
}
}
#[test]
fn test_reqopt_long_missing() {
let args = ["blah"];
let opts = [opt::reqopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_missing); }
_ { fail; }
}
}
#[test]
fn test_reqopt_long_no_arg() {
let args = ["--test"];
let opts = [opt::reqopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, argument_missing); }
_ { fail; }
}
}
#[test]
fn test_reqopt_long_multi() {
let args = ["--test=20", "--test=30"];
let opts = [opt::reqopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_duplicated); }
_ { fail; }
}
}
#[test]
fn test_reqopt_short() {
let args = ["-t", "20"];
let opts = [opt::reqopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) {
assert (opt::opt_present(m, "t"));
assert (opt::opt_str(m, "t") == "20");
}
_ { fail; }
}
}
#[test]
fn test_reqopt_short_missing() {
let args = ["blah"];
let opts = [opt::reqopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_missing); }
_ { fail; }
}
}
#[test]
fn test_reqopt_short_no_arg() {
let args = ["-t"];
let opts = [opt::reqopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, argument_missing); }
_ { fail; }
}
}
#[test]
fn test_reqopt_short_multi() {
let args = ["-t", "20", "-t", "30"];
let opts = [opt::reqopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_duplicated); }
_ { fail; }
}
}
// Tests for optopt
#[test]
fn test_optopt_long() {
let args = ["--test=20"];
let opts = [opt::optopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) {
assert (opt::opt_present(m, "test"));
assert (opt::opt_str(m, "test") == "20");
}
_ { fail; }
}
}
#[test]
fn test_optopt_long_missing() {
let args = ["blah"];
let opts = [opt::optopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) { assert (!opt::opt_present(m, "test")); }
_ { fail; }
}
}
#[test]
fn test_optopt_long_no_arg() {
let args = ["--test"];
let opts = [opt::optopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, argument_missing); }
_ { fail; }
}
}
#[test]
fn test_optopt_long_multi() {
let args = ["--test=20", "--test=30"];
let opts = [opt::optopt("test")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_duplicated); }
_ { fail; }
}
}
#[test]
fn test_optopt_short() {
let args = ["-t", "20"];
let opts = [opt::optopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) {
assert (opt::opt_present(m, "t"));
assert (opt::opt_str(m, "t") == "20");
}
_ { fail; }
}
}
#[test]
fn test_optopt_short_missing() {
let args = ["blah"];
let opts = [opt::optopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) { assert (!opt::opt_present(m, "t")); }
_ { fail; }
}
}
#[test]
fn test_optopt_short_no_arg() {
let args = ["-t"];
let opts = [opt::optopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, argument_missing); }
_ { fail; }
}
}
#[test]
fn test_optopt_short_multi() {
let args = ["-t", "20", "-t", "30"];
let opts = [opt::optopt("t")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_duplicated); }
_ { fail; }
}
}
// Tests for optflag
#[test]
fn test_optflag_long() {
let args = ["--test"];
let opts = [opt::optflag("test")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) { assert (opt::opt_present(m, "test")); }
_ { fail; }
}
}
#[test]
fn test_optflag_long_missing() {
let args = ["blah"];
let opts = [opt::optflag("test")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) { assert (!opt::opt_present(m, "test")); }
_ { fail; }
}
}
#[test]
fn test_optflag_long_arg() {
let args = ["--test=20"];
let opts = [opt::optflag("test")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) {
log(error, opt::fail_str(f));
check_fail_type(f, unexpected_argument);
}
_ { fail; }
}
}
#[test]
fn test_optflag_long_multi() {
let args = ["--test", "--test"];
let opts = [opt::optflag("test")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_duplicated); }
_ { fail; }
}
}
#[test]
fn test_optflag_short() {
let args = ["-t"];
let opts = [opt::optflag("t")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) { assert (opt::opt_present(m, "t")); }
_ { fail; }
}
}
#[test]
fn test_optflag_short_missing() {
let args = ["blah"];
let opts = [opt::optflag("t")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) { assert (!opt::opt_present(m, "t")); }
_ { fail; }
}
}
#[test]
fn test_optflag_short_arg() {
let args = ["-t", "20"];
let opts = [opt::optflag("t")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) {
// The next variable after the flag is just a free argument
assert (m.free[0] == "20");
}
_ { fail; }
}
}
#[test]
fn test_optflag_short_multi() {
let args = ["-t", "-t"];
let opts = [opt::optflag("t")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, option_duplicated); }
_ { fail; }
}
}
// Tests for optmulti
#[test]
fn test_optmulti_long() {
let args = ["--test=20"];
let opts = [opt::optmulti("test")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) {
assert (opt::opt_present(m, "test"));
assert (opt::opt_str(m, "test") == "20");
}
_ { fail; }
}
}
#[test]
fn test_optmulti_long_missing() {
let args = ["blah"];
let opts = [opt::optmulti("test")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) { assert (!opt::opt_present(m, "test")); }
_ { fail; }
}
}
#[test]
fn test_optmulti_long_no_arg() {
let args = ["--test"];
let opts = [opt::optmulti("test")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, argument_missing); }
_ { fail; }
}
}
#[test]
fn test_optmulti_long_multi() {
let args = ["--test=20", "--test=30"];
let opts = [opt::optmulti("test")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) {
assert (opt::opt_present(m, "test"));
assert (opt::opt_str(m, "test") == "20");
assert (opt::opt_strs(m, "test")[0] == "20");
assert (opt::opt_strs(m, "test")[1] == "30");
}
_ { fail; }
}
}
#[test]
fn test_optmulti_short() {
let args = ["-t", "20"];
let opts = [opt::optmulti("t")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) {
assert (opt::opt_present(m, "t"));
assert (opt::opt_str(m, "t") == "20");
}
_ { fail; }
}
}
#[test]
fn test_optmulti_short_missing() {
let args = ["blah"];
let opts = [opt::optmulti("t")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) { assert (!opt::opt_present(m, "t")); }
_ { fail; }
}
}
#[test]
fn test_optmulti_short_no_arg() {
let args = ["-t"];
let opts = [opt::optmulti("t")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, argument_missing); }
_ { fail; }
}
}
#[test]
fn test_optmulti_short_multi() {
let args = ["-t", "20", "-t", "30"];
let opts = [opt::optmulti("t")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) {
assert (opt::opt_present(m, "t"));
assert (opt::opt_str(m, "t") == "20");
assert (opt::opt_strs(m, "t")[0] == "20");
assert (opt::opt_strs(m, "t")[1] == "30");
}
_ { fail; }
}
}
#[test]
fn test_unrecognized_option_long() {
let args = ["--untest"];
let opts = [opt::optmulti("t")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, unrecognized_option); }
_ { fail; }
}
}
#[test]
fn test_unrecognized_option_short() {
let args = ["-t"];
let opts = [opt::optmulti("test")];
let rs = opt::getopts(args, opts);
alt rs {
err(f) { check_fail_type(f, unrecognized_option); }
_ { fail; }
}
}
#[test]
fn test_combined() {
let args =
["prog", "free1", "-s", "20", "free2", "--flag", "--long=30", "-f",
"-m", "40", "-m", "50", "-n", "-A B", "-n", "-60 70"];
let opts =
[opt::optopt("s"), opt::optflag("flag"), opt::reqopt("long"),
opt::optflag("f"), opt::optmulti("m"), opt::optmulti("n"),
opt::optopt("notpresent")];
let rs = opt::getopts(args, opts);
alt rs {
ok(m) {
assert (m.free[0] == "prog");
assert (m.free[1] == "free1");
assert (opt::opt_str(m, "s") == "20");
assert (m.free[2] == "free2");
assert (opt::opt_present(m, "flag"));
assert (opt::opt_str(m, "long") == "30");
assert (opt::opt_present(m, "f"));
assert (opt::opt_strs(m, "m")[0] == "40");
assert (opt::opt_strs(m, "m")[1] == "50");
assert (opt::opt_strs(m, "n")[0] == "-A B");
assert (opt::opt_strs(m, "n")[1] == "-60 70");
assert (!opt::opt_present(m, "notpresent"));
}
_ { fail; }
}
}

View File

@ -1,103 +0,0 @@
import core::*;
// -*- rust -*-
use std;
import std::io;
import io::{writer_util, reader_util};
import str;
import result;
#[test]
fn test_simple() {
let tmpfile: str = "tmp/lib-io-test-simple.tmp";
log(debug, tmpfile);
let frood: str = "A hoopy frood who really knows where his towel is.";
log(debug, frood);
{
let out: io::writer =
result::get(io::file_writer(tmpfile, [io::create, io::truncate]));
out.write_str(frood);
}
let inp: io::reader = result::get(io::file_reader(tmpfile));
let frood2: str = inp.read_c_str();
log(debug, frood2);
assert (str::eq(frood, frood2));
}
#[test]
fn test_readchars_empty() {
let inp : io::reader = io::string_reader("");
let res : [char] = inp.read_chars(128u);
assert(vec::len(res) == 0u);
}
#[test]
fn test_readchars_wide() {
let wide_test = "生锈的汤匙切肉汤hello生锈的汤匙切肉汤";
let ivals : [int] = [
29983, 38152, 30340, 27748,
21273, 20999, 32905, 27748,
104, 101, 108, 108, 111,
29983, 38152, 30340, 27748,
21273, 20999, 32905, 27748];
fn check_read_ln(len : uint, s: str, ivals: [int]) {
let inp : io::reader = io::string_reader(s);
let res : [char] = inp.read_chars(len);
if (len <= vec::len(ivals)) {
assert(vec::len(res) == len);
}
assert(vec::slice(ivals, 0u, vec::len(res)) ==
vec::map(res, {|x| x as int}));
}
let i = 0u;
while i < 8u {
check_read_ln(i, wide_test, ivals);
i += 1u;
}
// check a long read for good measure
check_read_ln(128u, wide_test, ivals);
}
#[test]
fn test_readchar() {
let inp : io::reader = io::string_reader("");
let res : char = inp.read_char();
assert(res as int == 29983);
}
#[test]
fn test_readchar_empty() {
let inp : io::reader = io::string_reader("");
let res : char = inp.read_char();
assert(res as int == -1);
}
#[test]
fn file_reader_not_exist() {
alt io::file_reader("not a file") {
result::err(e) {
assert e == "error opening not a file";
}
result::ok(_) { fail; }
}
}
#[test]
fn file_writer_bad_name() {
alt io::file_writer("?/?", []) {
result::err(e) {
assert e == "error opening ?/?";
}
result::ok(_) { fail; }
}
}
#[test]
fn buffered_file_writer_bad_name() {
alt io::buffered_file_writer("?/?") {
result::err(e) {
assert e == "error opening ?/?";
}
result::ok(_) { fail; }
}
}

View File

@ -1,58 +0,0 @@
import core::*;
use std;
import option;
import std::json::*;
import option::{none, some};
#[test]
fn test_from_str_null() {
assert(from_str("null") == some(null));
}
#[test]
fn test_from_str_num() {
assert(from_str("3") == some(num(3f)));
assert(from_str("3.1") == some(num(3.1f)));
assert(from_str("-1.2") == some(num(-1.2f)));
assert(from_str(".4") == some(num(0.4f)));
}
#[test]
fn test_from_str_str() {
assert(from_str("\"foo\"") == some(string("foo")));
assert(from_str("\"\\\"\"") == some(string("\"")));
assert(from_str("\"lol") == none);
}
#[test]
fn test_from_str_bool() {
assert(from_str("true") == some(boolean(true)));
assert(from_str("false") == some(boolean(false)));
assert(from_str("truz") == none);
}
#[test]
fn test_from_str_list() {
assert(from_str("[]") == some(list(@[])));
assert(from_str("[true]") == some(list(@[boolean(true)])));
assert(from_str("[null]") == some(list(@[null])));
assert(from_str("[3, 1]") == some(list(@[num(3f), num(1f)])));
assert(from_str("[2, [4, 1]]") ==
some(list(@[num(2f), list(@[num(4f), num(1f)])])));
assert(from_str("[2, ]") == none);
assert(from_str("[5, ") == none);
assert(from_str("[6 7]") == none);
assert(from_str("[3") == none);
}
#[test]
fn test_from_str_dict() {
assert(from_str("{}") != none);
assert(from_str("{\"a\": 3}") != none);
assert(from_str("{\"a\": null}") != none);
assert(from_str("{\"a\": }") == none);
assert(from_str("{\"a\" }") == none);
assert(from_str("{\"a\"") == none);
assert(from_str("{") == none);
}

View File

@ -1,114 +0,0 @@
import core::*;
use std;
import std::list;
import std::list::{from_vec, head, is_empty, is_not_empty, tail};
import option;
#[test]
fn test_is_empty() {
let empty : list::list<int> = from_vec([]);
let full1 = from_vec([1]);
let full2 = from_vec(['r', 'u']);
assert is_empty(empty);
assert !is_empty(full1);
assert !is_empty(full2);
assert !is_not_empty(empty);
assert is_not_empty(full1);
assert is_not_empty(full2);
}
#[test]
fn test_from_vec() {
let l = from_vec([0, 1, 2]);
check is_not_empty(l);
assert (head(l) == 0);
let tail_l = tail(l);
check is_not_empty(tail_l);
assert (head(tail_l) == 1);
let tail_tail_l = tail(tail_l);
check is_not_empty(tail_tail_l);
assert (head(tail_tail_l) == 2);
}
#[test]
fn test_from_vec_empty() {
let empty : list::list<int> = from_vec([]);
assert (empty == list::nil::<int>);
}
#[test]
fn test_from_vec_mut() {
let l = from_vec([mutable 0, 1, 2]);
check is_not_empty(l);
assert (head(l) == 0);
let tail_l = tail(l);
check is_not_empty(tail_l);
assert (head(tail_l) == 1);
let tail_tail_l = tail(tail_l);
check is_not_empty(tail_tail_l);
assert (head(tail_tail_l) == 2);
}
#[test]
fn test_foldl() {
fn add(&&a: uint, &&b: int) -> uint { ret a + (b as uint); }
let l = from_vec([0, 1, 2, 3, 4]);
let empty = list::nil::<int>;
assert (list::foldl(l, 0u, add) == 10u);
assert (list::foldl(empty, 0u, add) == 0u);
}
#[test]
fn test_foldl2() {
fn sub(&&a: int, &&b: int) -> int {
a - b
}
let l = from_vec([1, 2, 3, 4]);
assert (list::foldl(l, 0, sub) == -10);
}
#[test]
fn test_find_success() {
fn match(&&i: int) -> option::t<int> {
ret if i == 2 { option::some(i) } else { option::none::<int> };
}
let l = from_vec([0, 1, 2]);
assert (list::find(l, match) == option::some(2));
}
#[test]
fn test_find_fail() {
fn match(&&_i: int) -> option::t<int> { ret option::none::<int>; }
let l = from_vec([0, 1, 2]);
let empty = list::nil::<int>;
assert (list::find(l, match) == option::none::<int>);
assert (list::find(empty, match) == option::none::<int>);
}
#[test]
fn test_has() {
let l = from_vec([5, 8, 6]);
let empty = list::nil::<int>;
assert (list::has(l, 5));
assert (!list::has(l, 7));
assert (list::has(l, 8));
assert (!list::has(empty, 5));
}
#[test]
fn test_len() {
let l = from_vec([0, 1, 2]);
let empty = list::nil::<int>;
assert (list::len(l) == 3u);
assert (list::len(empty) == 0u);
}

View File

@ -1,249 +0,0 @@
import core::*;
// -*- rust -*-
use std;
import std::map;
import str;
import uint;
import option;
#[test]
fn test_simple() {
#debug("*** starting test_simple");
fn eq_uint(&&x: uint, &&y: uint) -> bool { ret x == y; }
fn uint_id(&&x: uint) -> uint { x }
let hasher_uint: map::hashfn<uint> = uint_id;
let eqer_uint: map::eqfn<uint> = eq_uint;
let hasher_str: map::hashfn<str> = str::hash;
let eqer_str: map::eqfn<str> = str::eq;
#debug("uint -> uint");
let hm_uu: map::hashmap<uint, uint> =
map::mk_hashmap::<uint, uint>(hasher_uint, eqer_uint);
assert (hm_uu.insert(10u, 12u));
assert (hm_uu.insert(11u, 13u));
assert (hm_uu.insert(12u, 14u));
assert (hm_uu.get(11u) == 13u);
assert (hm_uu.get(12u) == 14u);
assert (hm_uu.get(10u) == 12u);
assert (!hm_uu.insert(12u, 14u));
assert (hm_uu.get(12u) == 14u);
assert (!hm_uu.insert(12u, 12u));
assert (hm_uu.get(12u) == 12u);
let ten: str = "ten";
let eleven: str = "eleven";
let twelve: str = "twelve";
#debug("str -> uint");
let hm_su: map::hashmap<str, uint> =
map::mk_hashmap::<str, uint>(hasher_str, eqer_str);
assert (hm_su.insert("ten", 12u));
assert (hm_su.insert(eleven, 13u));
assert (hm_su.insert("twelve", 14u));
assert (hm_su.get(eleven) == 13u);
assert (hm_su.get("eleven") == 13u);
assert (hm_su.get("twelve") == 14u);
assert (hm_su.get("ten") == 12u);
assert (!hm_su.insert("twelve", 14u));
assert (hm_su.get("twelve") == 14u);
assert (!hm_su.insert("twelve", 12u));
assert (hm_su.get("twelve") == 12u);
#debug("uint -> str");
let hm_us: map::hashmap<uint, str> =
map::mk_hashmap::<uint, str>(hasher_uint, eqer_uint);
assert (hm_us.insert(10u, "twelve"));
assert (hm_us.insert(11u, "thirteen"));
assert (hm_us.insert(12u, "fourteen"));
assert (str::eq(hm_us.get(11u), "thirteen"));
assert (str::eq(hm_us.get(12u), "fourteen"));
assert (str::eq(hm_us.get(10u), "twelve"));
assert (!hm_us.insert(12u, "fourteen"));
assert (str::eq(hm_us.get(12u), "fourteen"));
assert (!hm_us.insert(12u, "twelve"));
assert (str::eq(hm_us.get(12u), "twelve"));
#debug("str -> str");
let hm_ss: map::hashmap<str, str> =
map::mk_hashmap::<str, str>(hasher_str, eqer_str);
assert (hm_ss.insert(ten, "twelve"));
assert (hm_ss.insert(eleven, "thirteen"));
assert (hm_ss.insert(twelve, "fourteen"));
assert (str::eq(hm_ss.get("eleven"), "thirteen"));
assert (str::eq(hm_ss.get("twelve"), "fourteen"));
assert (str::eq(hm_ss.get("ten"), "twelve"));
assert (!hm_ss.insert("twelve", "fourteen"));
assert (str::eq(hm_ss.get("twelve"), "fourteen"));
assert (!hm_ss.insert("twelve", "twelve"));
assert (str::eq(hm_ss.get("twelve"), "twelve"));
#debug("*** finished test_simple");
}
/**
* Force map growth
*/
#[test]
fn test_growth() {
#debug("*** starting test_growth");
let num_to_insert: uint = 64u;
fn eq_uint(&&x: uint, &&y: uint) -> bool { ret x == y; }
fn uint_id(&&x: uint) -> uint { x }
#debug("uint -> uint");
let hasher_uint: map::hashfn<uint> = uint_id;
let eqer_uint: map::eqfn<uint> = eq_uint;
let hm_uu: map::hashmap<uint, uint> =
map::mk_hashmap::<uint, uint>(hasher_uint, eqer_uint);
let i: uint = 0u;
while i < num_to_insert {
assert (hm_uu.insert(i, i * i));
#debug("inserting %u -> %u", i, i*i);
i += 1u;
}
#debug("-----");
i = 0u;
while i < num_to_insert {
#debug("get(%u) = %u", i, hm_uu.get(i));
assert (hm_uu.get(i) == i * i);
i += 1u;
}
assert (hm_uu.insert(num_to_insert, 17u));
assert (hm_uu.get(num_to_insert) == 17u);
#debug("-----");
i = 0u;
while i < num_to_insert {
#debug("get(%u) = %u", i, hm_uu.get(i));
assert (hm_uu.get(i) == i * i);
i += 1u;
}
#debug("str -> str");
let hasher_str: map::hashfn<str> = str::hash;
let eqer_str: map::eqfn<str> = str::eq;
let hm_ss: map::hashmap<str, str> =
map::mk_hashmap::<str, str>(hasher_str, eqer_str);
i = 0u;
while i < num_to_insert {
assert (hm_ss.insert(uint::to_str(i, 2u), uint::to_str(i * i, 2u)));
#debug("inserting \"%s\" -> \"%s\"",
uint::to_str(i, 2u),
uint::to_str(i*i, 2u));
i += 1u;
}
#debug("-----");
i = 0u;
while i < num_to_insert {
#debug("get(\"%s\") = \"%s\"",
uint::to_str(i, 2u),
hm_ss.get(uint::to_str(i, 2u)));
assert (str::eq(hm_ss.get(uint::to_str(i, 2u)),
uint::to_str(i * i, 2u)));
i += 1u;
}
assert (hm_ss.insert(uint::to_str(num_to_insert, 2u),
uint::to_str(17u, 2u)));
assert (str::eq(hm_ss.get(uint::to_str(num_to_insert, 2u)),
uint::to_str(17u, 2u)));
#debug("-----");
i = 0u;
while i < num_to_insert {
#debug("get(\"%s\") = \"%s\"",
uint::to_str(i, 2u),
hm_ss.get(uint::to_str(i, 2u)));
assert (str::eq(hm_ss.get(uint::to_str(i, 2u)),
uint::to_str(i * i, 2u)));
i += 1u;
}
#debug("*** finished test_growth");
}
#[test]
fn test_removal() {
#debug("*** starting test_removal");
let num_to_insert: uint = 64u;
fn eq(&&x: uint, &&y: uint) -> bool { ret x == y; }
fn hash(&&u: uint) -> uint {
// This hash function intentionally causes collisions between
// consecutive integer pairs.
ret u / 2u * 2u;
}
assert (hash(0u) == hash(1u));
assert (hash(2u) == hash(3u));
assert (hash(0u) != hash(2u));
let hasher: map::hashfn<uint> = hash;
let eqer: map::eqfn<uint> = eq;
let hm: map::hashmap<uint, uint> =
map::mk_hashmap::<uint, uint>(hasher, eqer);
let i: uint = 0u;
while i < num_to_insert {
assert (hm.insert(i, i * i));
#debug("inserting %u -> %u", i, i*i);
i += 1u;
}
assert (hm.size() == num_to_insert);
#debug("-----");
#debug("removing evens");
i = 0u;
while i < num_to_insert {
let v = hm.remove(i);
alt v {
option::some(u) { assert (u == i * i); }
option::none. { fail; }
}
i += 2u;
}
assert (hm.size() == num_to_insert / 2u);
#debug("-----");
i = 1u;
while i < num_to_insert {
#debug("get(%u) = %u", i, hm.get(i));
assert (hm.get(i) == i * i);
i += 2u;
}
#debug("-----");
i = 1u;
while i < num_to_insert {
#debug("get(%u) = %u", i, hm.get(i));
assert (hm.get(i) == i * i);
i += 2u;
}
#debug("-----");
i = 0u;
while i < num_to_insert {
assert (hm.insert(i, i * i));
#debug("inserting %u -> %u", i, i*i);
i += 2u;
}
assert (hm.size() == num_to_insert);
#debug("-----");
i = 0u;
while i < num_to_insert {
#debug("get(%u) = %u", i, hm.get(i));
assert (hm.get(i) == i * i);
i += 1u;
}
#debug("-----");
assert (hm.size() == num_to_insert);
i = 0u;
while i < num_to_insert {
#debug("get(%u) = %u", i, hm.get(i));
assert (hm.get(i) == i * i);
i += 1u;
}
#debug("*** finished test_removal");
}
#[test]
fn test_contains_key() {
let key = "k";
let map = map::mk_hashmap::<str, str>(str::hash, str::eq);
assert (!map.contains_key(key));
map.insert(key, "val");
assert (map.contains_key(key));
}
#[test]
fn test_find() {
let key = "k";
let map = map::mk_hashmap::<str, str>(str::hash, str::eq);
assert (option::is_none(map.find(key)));
map.insert(key, "val");
assert (option::get(map.find(key)) == "val");
}

View File

@ -1,14 +0,0 @@
import core::*;
use std;
import std::net;
#[test]
fn test_format_ip() {
assert (net::format_addr(net::ipv4(127u8, 0u8, 0u8, 1u8)) == "127.0.0.1")
}
#[test]
fn test_parse_ip() {
assert (net::parse_addr("127.0.0.1") == net::ipv4(127u8, 0u8, 0u8, 1u8));
}

View File

@ -1,58 +0,0 @@
import core::*;
import std::generic_os::setenv;
import std::generic_os::getenv;
import option;
#[test]
#[ignore(reason = "fails periodically on mac")]
fn test_setenv() {
// NB: Each test of setenv needs to use different variable names or the
// tests will not be threadsafe
setenv("NAME1", "VALUE");
assert (getenv("NAME1") == option::some("VALUE"));
}
#[test]
#[ignore(reason = "fails periodically on mac")]
fn test_setenv_overwrite() {
setenv("NAME2", "1");
setenv("NAME2", "2");
assert (getenv("NAME2") == option::some("2"));
}
// Windows GetEnvironmentVariable requires some extra work to make sure
// the buffer the variable is copied into is the right size
#[test]
#[ignore(reason = "fails periodically on mac")]
fn test_getenv_big() {
let s = "";
let i = 0;
while i < 100 { s += "aaaaaaaaaa"; i += 1; }
setenv("test_getenv_big", s);
log(debug, s);
assert (getenv("test_getenv_big") == option::some(s));
}
#[test]
fn get_exe_path() {
let path = std::os::get_exe_path();
assert option::is_some(path);
let path = option::get(path);
log(debug, path);
// Hard to test this function
if std::os::target_os() != "win32" {
assert str::starts_with(path, std::fs::path_sep());
} else {
assert path[1] == ':' as u8;
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:

View File

@ -1,18 +0,0 @@
import core::*;
// Testing a few of the path manipuation functions
use std;
import std::fs;
import std::os;
#[test]
fn test() {
assert (!fs::path_is_absolute("test-path"));
log(debug, "Current working directory: " + os::getcwd());
log(debug, fs::make_absolute("test-path"));
log(debug, fs::make_absolute("/usr/bin"));
}

View File

@ -1,71 +0,0 @@
import core::*;
use std;
import std::sort;
import vec;
import int;
fn check_sort(v1: [mutable int], v2: [mutable int]) {
let len = vec::len::<int>(v1);
fn ltequal(&&a: int, &&b: int) -> bool { ret a <= b; }
let f = ltequal;
std::sort::quick_sort::<int>(f, v1);
let i = 0u;
while i < len {
log(debug, v2[i]);
assert (v2[i] == v1[i]);
i += 1u;
}
}
#[test]
fn test() {
{
let v1 = [mutable 3, 7, 4, 5, 2, 9, 5, 8];
let v2 = [mutable 2, 3, 4, 5, 5, 7, 8, 9];
check_sort(v1, v2);
}
{
let v1 = [mutable 1, 1, 1];
let v2 = [mutable 1, 1, 1];
check_sort(v1, v2);
}
{
let v1: [mutable int] = [mutable];
let v2: [mutable int] = [mutable];
check_sort(v1, v2);
}
{ let v1 = [mutable 9]; let v2 = [mutable 9]; check_sort(v1, v2); }
{
let v1 = [mutable 9, 3, 3, 3, 9];
let v2 = [mutable 3, 3, 3, 9, 9];
check_sort(v1, v2);
}
}
// Regression test for #750
#[test]
fn test_simple() {
let names = [mutable 2, 1, 3];
let expected = [1, 2, 3];
fn lteq(&&a: int, &&b: int) -> bool { int::le(a, b) }
sort::quick_sort(lteq, names);
let immut_names = vec::from_mut(names);
// Silly, but what else can we do?
check (vec::same_length(expected, immut_names));
let pairs = vec::zip(expected, immut_names);
for (a, b) in pairs { #debug("%d %d", a, b); assert (a == b); }
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:

View File

@ -1,50 +0,0 @@
import core::*;
use std;
fn check_sort(v1: [mutable int], v2: [mutable int]) {
let len = vec::len::<int>(v1);
fn lt(&&a: int, &&b: int) -> bool { ret a < b; }
fn equal(&&a: int, &&b: int) -> bool { ret a == b; }
let f1 = lt;
let f2 = equal;
std::sort::quick_sort3::<int>(f1, f2, v1);
let i = 0u;
while i < len {
log(debug, v2[i]);
assert (v2[i] == v1[i]);
i += 1u;
}
}
#[test]
fn test() {
{
let v1 = [mutable 3, 7, 4, 5, 2, 9, 5, 8];
let v2 = [mutable 2, 3, 4, 5, 5, 7, 8, 9];
check_sort(v1, v2);
}
{
let v1 = [mutable 1, 1, 1];
let v2 = [mutable 1, 1, 1];
check_sort(v1, v2);
}
{
let v1: [mutable int] = [mutable];
let v2: [mutable int] = [mutable];
check_sort(v1, v2);
}
{ let v1 = [mutable 9]; let v2 = [mutable 9]; check_sort(v1, v2); }
{
let v1 = [mutable 9, 3, 3, 3, 9];
let v2 = [mutable 3, 3, 3, 9, 9];
check_sort(v1, v2);
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:

View File

@ -1,40 +0,0 @@
import core::*;
// -*- rust -*-
use std;
import std::rand;
import str;
#[test]
fn test() {
let r1: rand::rng = rand::mk_rng();
log(debug, r1.next());
log(debug, r1.next());
{
let r2 = rand::mk_rng();
log(debug, r1.next());
log(debug, r2.next());
log(debug, r1.next());
log(debug, r1.next());
log(debug, r2.next());
log(debug, r2.next());
log(debug, r1.next());
log(debug, r1.next());
log(debug, r1.next());
log(debug, r2.next());
log(debug, r2.next());
log(debug, r2.next());
}
log(debug, r1.next());
log(debug, r1.next());
}
#[test]
fn genstr() {
let r: rand::rng = rand::mk_rng();
log(debug, r.gen_str(10u));
log(debug, r.gen_str(10u));
log(debug, r.gen_str(10u));
assert(str::char_len(r.gen_str(10u)) == 10u);
assert(str::char_len(r.gen_str(16u)) == 16u);
}

View File

@ -1,166 +0,0 @@
import core::*;
import str;
import std::rope::*;
import option;
import uint;
import vec;
//Utility function, used for sanity check
fn rope_to_string(r: rope) -> str {
alt(r) {
node::empty. { ret "" }
node::content(x) {
let str = @mutable "";
fn aux(str: @mutable str, node: @node::node) {
alt(*node) {
node::leaf(x) {
*str += str::substr(*x.content, x.byte_offset, x.byte_len);
}
node::concat(x) {
aux(str, x.left);
aux(str, x.right);
}
}
}
aux(str, x);
ret *str
}
}
}
#[test]
fn trivial() {
assert char_len(empty()) == 0u;
assert byte_len(empty()) == 0u;
}
#[test]
fn of_string1() {
let sample = @"0123456789ABCDE";
let r = of_str(sample);
assert char_len(r) == str::char_len(*sample);
assert rope_to_string(r) == *sample;
}
#[test]
fn of_string2() {
let buf = @ mutable "1234567890";
let i = 0;
while i < 10 { *buf = *buf + *buf; i+=1;}
let sample = @*buf;
let r = of_str(sample);
assert char_len(r) == str::char_len(*sample);
assert rope_to_string(r) == *sample;
let string_iter = 0u;
let string_len = str::byte_len(*sample);
let rope_iter = iterator::char::start(r);
let equal = true;
let pos = 0u;
while equal {
alt(node::char_iterator::next(rope_iter)) {
option::none. {
if string_iter < string_len {
equal = false;
} break; }
option::some(c) {
let {ch, next} = str::char_range_at(*sample, string_iter);
string_iter = next;
if ch != c { equal = false; break; }
}
}
pos += 1u;
}
assert equal;
}
#[test]
fn iter1() {
let buf = @ mutable "1234567890";
let i = 0;
while i < 10 { *buf = *buf + *buf; i+=1;}
let sample = @*buf;
let r = of_str(sample);
let len = 0u;
let it = iterator::char::start(r);
while true {
alt(node::char_iterator::next(it)) {
option::none. { break; }
option::some(_) { len += 1u; }
}
}
assert len == str::char_len(*sample);
}
#[test]
fn bal1() {
let init = @ "1234567890";
let buf = @ mutable * init;
let i = 0;
while i < 8 { *buf = *buf + *buf; i+=1;}
let sample = @*buf;
let r1 = of_str(sample);
let r2 = of_str(init);
i = 0;
while i < 8 { r2 = append_rope(r2, r2); i+= 1;}
assert eq(r1, r2);
let r3 = bal(r2);
assert char_len(r1) == char_len(r3);
assert eq(r1, r3);
}
#[test]
fn char_at1() {
//Generate a large rope
let r = of_str(@ "123456789");
uint::range(0u, 10u){|_i|
r = append_rope(r, r);
}
//Copy it in the slowest possible way
let r2 = empty();
uint::range(0u, char_len(r)){|i|
r2 = append_char(r2, char_at(r, i));
}
assert eq(r, r2);
let r3 = empty();
uint::range(0u, char_len(r)){|i|
r3 = prepend_char(r3, char_at(r, char_len(r) - i - 1u));
}
assert eq(r, r3);
//Additional sanity checks
let balr = bal(r);
let bal2 = bal(r2);
let bal3 = bal(r3);
assert eq(r, balr);
assert eq(r, bal2);
assert eq(r, bal3);
assert eq(r2, r3);
assert eq(bal2, bal3);
}
#[test]
fn concat1() {
//Generate a reasonable rope
let chunk = of_str(@ "123456789");
let r = empty();
uint::range(0u, 10u){|_i|
r = append_rope(r, chunk);
}
//Same rope, obtained with rope::concat
let r2 = concat(vec::init_elt(chunk, 10u));
assert eq(r, r2);
}

View File

@ -1,70 +0,0 @@
import core::*;
use std;
import std::run;
import std::os;
import std::io;
import io::writer_util;
import option;
import str;
import ctypes::fd_t;
// Regression test for memory leaks
#[ignore(cfg(target_os = "win32"))] // FIXME
fn test_leaks() {
run::run_program("echo", []);
run::start_program("echo", []);
run::program_output("echo", []);
}
#[test]
fn test_pipes() {
let pipe_in = os::pipe();
let pipe_out = os::pipe();
let pipe_err = os::pipe();
let pid =
run::spawn_process("cat", [], pipe_in.in, pipe_out.out, pipe_err.out);
os::close(pipe_in.in);
os::close(pipe_out.out);
os::close(pipe_err.out);
if pid == -1i32 { fail; }
let expected = "test";
writeclose(pipe_in.out, expected);
let actual = readclose(pipe_out.in);
readclose(pipe_err.in);
os::waitpid(pid);
log(debug, expected);
log(debug, actual);
assert (expected == actual);
fn writeclose(fd: fd_t, s: str) {
#error("writeclose %d, %s", fd as int, s);
let writer = io::fd_writer(fd, false);
writer.write_str(s);
os::close(fd);
}
fn readclose(fd: fd_t) -> str {
// Copied from run::program_output
let file = os::fd_FILE(fd);
let reader = io::FILE_reader(file, false);
let buf = "";
while !reader.eof() {
let bytes = reader.read_bytes(4096u);
buf += str::unsafe_from_bytes(bytes);
}
os::fclose(file);
ret buf;
}
}
#[test]
fn waitpid() {
let pid = run::spawn_process("false", [], 0i32, 0i32, 0i32);
let status = run::waitpid(pid);
assert status == 1;
}

View File

@ -1,95 +0,0 @@
import core::*;
// -*- rust -*-
use std;
import std::sha1;
import vec;
import str;
#[test]
fn test() {
type test = {input: str, output: [u8]};
fn a_million_letter_a() -> str {
let i = 0;
let rs = "";
while i < 100000 { rs += "aaaaaaaaaa"; i += 1; }
ret rs;
}
// Test messages from FIPS 180-1
let fips_180_1_tests: [test] =
[{input: "abc",
output:
[0xA9u8, 0x99u8, 0x3Eu8, 0x36u8, 0x47u8, 0x06u8, 0x81u8, 0x6Au8,
0xBAu8, 0x3Eu8, 0x25u8, 0x71u8, 0x78u8, 0x50u8, 0xC2u8, 0x6Cu8,
0x9Cu8, 0xD0u8, 0xD8u8, 0x9Du8]},
{input:
"abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq",
output:
[0x84u8, 0x98u8, 0x3Eu8, 0x44u8, 0x1Cu8, 0x3Bu8, 0xD2u8, 0x6Eu8,
0xBAu8, 0xAEu8, 0x4Au8, 0xA1u8, 0xF9u8, 0x51u8, 0x29u8, 0xE5u8,
0xE5u8, 0x46u8, 0x70u8, 0xF1u8]},
{input: a_million_letter_a(),
output:
[0x34u8, 0xAAu8, 0x97u8, 0x3Cu8, 0xD4u8, 0xC4u8, 0xDAu8, 0xA4u8,
0xF6u8, 0x1Eu8, 0xEBu8, 0x2Bu8, 0xDBu8, 0xADu8, 0x27u8, 0x31u8,
0x65u8, 0x34u8, 0x01u8, 0x6Fu8]}];
// Examples from wikipedia
let wikipedia_tests: [test] =
[{input: "The quick brown fox jumps over the lazy dog",
output:
[0x2fu8, 0xd4u8, 0xe1u8, 0xc6u8, 0x7au8, 0x2du8, 0x28u8, 0xfcu8,
0xedu8, 0x84u8, 0x9eu8, 0xe1u8, 0xbbu8, 0x76u8, 0xe7u8, 0x39u8,
0x1bu8, 0x93u8, 0xebu8, 0x12u8]},
{input: "The quick brown fox jumps over the lazy cog",
output:
[0xdeu8, 0x9fu8, 0x2cu8, 0x7fu8, 0xd2u8, 0x5eu8, 0x1bu8, 0x3au8,
0xfau8, 0xd3u8, 0xe8u8, 0x5au8, 0x0bu8, 0xd1u8, 0x7du8, 0x9bu8,
0x10u8, 0x0du8, 0xb4u8, 0xb3u8]}];
let tests = fips_180_1_tests + wikipedia_tests;
fn check_vec_eq(v0: [u8], v1: [u8]) {
assert (vec::len::<u8>(v0) == vec::len::<u8>(v1));
let len = vec::len::<u8>(v0);
let i = 0u;
while i < len {
let a = v0[i];
let b = v1[i];
assert (a == b);
i += 1u;
}
}
// Test that it works when accepting the message all at once
let sh = sha1::mk_sha1();
for t: test in tests {
sh.input_str(t.input);
let out = sh.result();
check_vec_eq(t.output, out);
sh.reset();
}
// Test that it works when accepting the message in pieces
for t: test in tests {
let len = str::byte_len(t.input);
let left = len;
while left > 0u {
let take = (left + 1u) / 2u;
sh.input_str(str::substr(t.input, len - left, take));
left = left - take;
}
let out = sh.result();
check_vec_eq(t.output, out);
sh.reset();
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:

View File

@ -1,41 +0,0 @@
import core::*;
use std;
fn check_sort(v1: [int], v2: [int]) {
let len = vec::len::<int>(v1);
fn lteq(&&a: int, &&b: int) -> bool { ret a <= b; }
let f = lteq;
let v3 = std::sort::merge_sort::<int>(f, v1);
let i = 0u;
while i < len {
log(debug, v3[i]);
assert (v3[i] == v2[i]);
i += 1u;
}
}
#[test]
fn test() {
{
let v1 = [3, 7, 4, 5, 2, 9, 5, 8];
let v2 = [2, 3, 4, 5, 5, 7, 8, 9];
check_sort(v1, v2);
}
{ let v1 = [1, 1, 1]; let v2 = [1, 1, 1]; check_sort(v1, v2); }
{ let v1: [int] = []; let v2: [int] = []; check_sort(v1, v2); }
{ let v1 = [9]; let v2 = [9]; check_sort(v1, v2); }
{
let v1 = [9, 3, 3, 3, 9];
let v2 = [3, 3, 3, 9, 9];
check_sort(v1, v2);
}
}
#[test]
fn test_merge_sort_mutable() {
fn lteq(&&a: int, &&b: int) -> bool { ret a <= b; }
let v1 = [mutable 3, 2, 1];
let v2 = std::sort::merge_sort(lteq, v1);
assert v2 == [1, 2, 3];
}

View File

@ -1,40 +0,0 @@
use std;
mod bitv;
mod c_vec;
mod deque;
mod four;
mod fs;
mod getopts;
mod io;
mod json;
mod list;
mod map;
mod net;
mod os;
mod path;
mod qsort3;
mod qsort;
mod rand;
mod rope;
mod run;
mod sha1;
mod sort;
mod tempfile;
mod test;
mod tri;
mod treemap;
#[cfg(unicode)]
mod unicode;
mod uv;
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:

View File

@ -1,20 +0,0 @@
import core::*;
use std;
import option;
import std::fs;
import option::some;
import str;
import std::tempfile;
#[test]
fn mkdtemp() {
let r = tempfile::mkdtemp("./", "foobar");
alt r {
some(p) {
fs::remove_dir(p);
assert(str::ends_with(p, "foobar"));
}
_ { assert(false); }
}
}

View File

@ -1,119 +0,0 @@
import core::*;
import std::test;
import str;
import option;
import either;
import vec;
#[test]
fn do_not_run_ignored_tests() {
fn f() { fail; }
let desc = {name: "whatever", fn: f, ignore: true, should_fail: false};
let future = test::run_test(desc, test::default_test_to_task);
let result = future.wait();
assert result != test::tr_ok;
}
#[test]
fn ignored_tests_result_in_ignored() {
fn f() { }
let desc = {name: "whatever", fn: f, ignore: true, should_fail: false};
let res = test::run_test(desc, test::default_test_to_task).wait();
assert (res == test::tr_ignored);
}
#[test]
#[ignore(cfg(target_os = "win32"))]
fn test_should_fail() {
fn f() { fail; }
let desc = {name: "whatever", fn: f, ignore: false, should_fail: true};
let res = test::run_test(desc, test::default_test_to_task).wait();
assert res == test::tr_ok;
}
#[test]
fn test_should_fail_but_succeeds() {
fn f() { }
let desc = {name: "whatever", fn: f, ignore: false, should_fail: true};
let res = test::run_test(desc, test::default_test_to_task).wait();
assert res == test::tr_failed;
}
#[test]
fn first_free_arg_should_be_a_filter() {
let args = ["progname", "filter"];
check (vec::is_not_empty(args));
let opts = alt test::parse_opts(args) { either::left(o) { o } };
assert (str::eq("filter", option::get(opts.filter)));
}
#[test]
fn parse_ignored_flag() {
let args = ["progname", "filter", "--ignored"];
check (vec::is_not_empty(args));
let opts = alt test::parse_opts(args) { either::left(o) { o } };
assert (opts.run_ignored);
}
#[test]
fn filter_for_ignored_option() {
// When we run ignored tests the test filter should filter out all the
// unignored tests and flip the ignore flag on the rest to false
let opts = {filter: option::none, run_ignored: true};
let tests =
[{name: "1", fn: fn@() { }, ignore: true, should_fail: false},
{name: "2", fn: fn@() { }, ignore: false, should_fail: false}];
let filtered = test::filter_tests(opts, tests);
assert (vec::len(filtered) == 1u);
assert (filtered[0].name == "1");
assert (filtered[0].ignore == false);
}
#[test]
fn sort_tests() {
let opts = {filter: option::none, run_ignored: false};
let names =
["sha1::test", "int::test_to_str", "int::test_pow",
"test::do_not_run_ignored_tests",
"test::ignored_tests_result_in_ignored",
"test::first_free_arg_should_be_a_filter",
"test::parse_ignored_flag", "test::filter_for_ignored_option",
"test::sort_tests"];
let tests =
{
let testfn = fn@() { };
let tests = [];
for name: str in names {
let test = {name: name, fn: testfn, ignore: false,
should_fail: false};
tests += [test];
}
tests
};
let filtered = test::filter_tests(opts, tests);
let expected =
["int::test_pow", "int::test_to_str", "sha1::test",
"test::do_not_run_ignored_tests", "test::filter_for_ignored_option",
"test::first_free_arg_should_be_a_filter",
"test::ignored_tests_result_in_ignored", "test::parse_ignored_flag",
"test::sort_tests"];
check (vec::same_length(expected, filtered));
let pairs = vec::zip(expected, filtered);
for (a, b) in pairs { assert (a == b.name); }
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:

View File

@ -1,62 +0,0 @@
import core::*;
use std;
import option;
import std::treemap::*;
import option::some;
import option::none;
import str;
#[test]
fn init_treemap() { let _m = init::<int, int>(); }
#[test]
fn insert_one() { let m = init(); insert(m, 1, 2); }
#[test]
fn insert_two() { let m = init(); insert(m, 1, 2); insert(m, 3, 4); }
#[test]
fn insert_find() {
let m = init();
insert(m, 1, 2);
assert (find(m, 1) == some(2));
}
#[test]
fn find_empty() { let m = init::<int, int>(); assert (find(m, 1) == none); }
#[test]
fn find_not_found() {
let m = init();
insert(m, 1, 2);
assert (find(m, 2) == none);
}
#[test]
fn traverse_in_order() {
let m = init();
insert(m, 3, ());
insert(m, 0, ());
insert(m, 4, ());
insert(m, 2, ());
insert(m, 1, ());
let n = @mutable 0;
fn t(n: @mutable int, &&k: int, &&_v: ()) { assert (*n == k); *n += 1; }
traverse(m, bind t(n, _, _));
}
#[test]
fn u8_map() {
let m = init();
let k1 = str::bytes("foo");
let k2 = str::bytes("bar");
insert(m, k1, "foo");
insert(m, k2, "bar");
assert (find(m, k2) == some("bar"));
assert (find(m, k1) == some("foo"));
}

View File

@ -1,119 +0,0 @@
import core::*;
use std;
import std::tri;
pure fn eq1(a: tri::t, b: tri::t) -> bool { tri::eq(a , b) }
pure fn ne1(a: tri::t, b: tri::t) -> bool { tri::ne(a , b) }
pure fn eq2(a: tri::t, b: tri::t) -> bool { eq1( a, b ) && eq1( b, a ) }
#[test]
fn test_eq2() {
tri::all_values { |a|
tri::all_values { |b|
assert if a == b { eq1( a, b ) } else { ne1( a, b ) }
}
}
}
#[test]
fn test_tri_and_symmetry() {
tri::all_values { |a|
tri::all_values { |b|
assert eq1( tri::and(a ,b), tri::and(b, a) );
}
}
}
#[test]
fn test_tri_or_symmetry() {
tri::all_values { |a|
tri::all_values { |b|
assert eq1( tri::or(a ,b), tri::or(b, a) );
}
}
}
#[test]
fn test_tri_xor_symmetry() {
tri::all_values { |a|
tri::all_values { |b|
assert eq1( tri::xor(a ,b), tri::xor(b, a) );
}
}
}
#[test]
fn test_tri_not() {
assert eq2( tri::not(tri::true), tri::false);
assert eq2( tri::not(tri::unknown), tri::unknown);
assert eq2( tri::not(tri::false), tri::true);
}
#[test]
fn test_tri_and() {
assert eq2( tri::and(tri::true, tri::true), tri::true);
assert eq2( tri::and(tri::true, tri::false), tri::false);
assert eq2( tri::and(tri::true, tri::unknown), tri::unknown);
assert eq2( tri::and(tri::false, tri::false), tri::false);
assert eq2( tri::and(tri::false, tri::unknown), tri::false);
assert eq2( tri::and(tri::unknown, tri::unknown), tri::unknown);
}
#[test]
fn test_tri_or() {
assert eq2( tri::or(tri::true, tri::true), tri::true);
assert eq2( tri::or(tri::true, tri::false), tri::true);
assert eq2( tri::or(tri::true, tri::unknown), tri::true);
assert eq2( tri::or(tri::false, tri::false), tri::false);
assert eq2( tri::or(tri::false, tri::unknown), tri::unknown);
assert eq2( tri::or(tri::unknown, tri::unknown), tri::unknown);
}
#[test]
fn test_tri_xor() {
assert eq2( tri::xor(tri::true, tri::true), tri::false);
assert eq2( tri::xor(tri::false, tri::false), tri::false);
assert eq2( tri::xor(tri::true, tri::false), tri::true);
assert eq2( tri::xor(tri::true, tri::unknown), tri::unknown);
assert eq2( tri::xor(tri::false, tri::unknown), tri::unknown);
assert eq2( tri::xor(tri::unknown, tri::unknown), tri::unknown);
}
#[test]
fn test_tri_implies() {
assert eq2( tri::implies(tri::false, tri::false), tri::true);
assert eq2( tri::implies(tri::false, tri::unknown), tri::true);
assert eq2( tri::implies(tri::false, tri::true), tri::true);
assert eq2( tri::implies(tri::unknown, tri::false), tri::unknown);
assert eq2( tri::implies(tri::unknown, tri::unknown), tri::unknown);
assert eq2( tri::implies(tri::unknown, tri::true), tri::true);
assert eq2( tri::implies(tri::true, tri::false), tri::false);
assert eq2( tri::implies(tri::true, tri::unknown), tri::unknown);
assert eq2( tri::implies(tri::true, tri::true), tri::true);
}
#[test]
fn test_tri_from_str() {
tri::all_values { |v|
assert eq2( v, tri::from_str(tri::to_str(v)));
}
}
#[test]
fn test_tri_to_str() {
assert tri::to_str(tri::false) == "false";
assert tri::to_str(tri::unknown) == "unknown";
assert tri::to_str(tri::true) == "true";
}
#[test]
fn test_tri_to_bit() {
tri::all_values { |v|
assert tri::to_bit(v) == if tri::is_true(v) { 1u8 } else { 0u8 };
}
}

View File

@ -1,30 +0,0 @@
import core::*;
use std;
import unicode;
#[test]
fn test_is_digit() {
assert (unicode::icu::is_digit('0'));
assert (!unicode::icu::is_digit('m'));
}
#[test]
fn test_is_lower() {
assert (unicode::icu::is_lower('m'));
assert (!unicode::icu::is_lower('M'));
}
#[test]
fn test_is_space() {
assert (unicode::icu::is_space(' '));
assert (!unicode::icu::is_space('m'));
}
#[test]
fn test_is_upper() {
assert (unicode::icu::is_upper('M'));
assert (!unicode::icu::is_upper('m'));
}

View File

@ -1,48 +0,0 @@
#[cfg(target_os = "linux")];
#[cfg(target_os = "macos")];
#[cfg(target_os = "freebsd")];
import core::*;
import std::uv;
import ptr;
#[test]
fn sanity_check() {
uv::sanity_check();
}
// From test-ref.c
mod test_ref {
#[test]
fn ref() {
let loop = uv::loop_new();
uv::run(loop);
uv::loop_delete(loop);
}
#[test]
fn idle_ref() {
let loop = uv::loop_new();
let h = uv::idle_new();
uv::idle_init(loop, ptr::addr_of(h));
uv::idle_start(ptr::addr_of(h), ptr::null());
uv::unref(loop);
uv::run(loop);
uv::loop_delete(loop);
}
#[test]
fn async_ref() {
/*
let loop = uv::loop_new();
let h = uv::async_new();
uv::async_init(loop, ptr::addr_of(h), ptr::null());
uv::unref(loop);
uv::run(loop);
uv::loop_delete(loop);
*/
}
}