Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

libstd: Move std tests into std

  • Loading branch information...
commit 0ad4631397c23090a3dc6f6c5f857aaa45ec8e4a 1 parent 0c92423
@brson authored
Showing with 2,926 additions and 3,032 deletions.
  1. +1 −5 mk/tests.mk
  2. +285 −0 src/libstd/bitv.rs
  3. +61 −0 src/libstd/c_vec.rs
  4. +197 −0 src/libstd/deque.rs
  5. +150 −0 src/libstd/four.rs
  6. +285 −0 src/libstd/fs.rs
  7. +50 −0 src/libstd/generic_os.rs
  8. +473 −0 src/libstd/getopts.rs
  9. +99 −0 src/libstd/io.rs
  10. +55 −0 src/libstd/json.rs
  11. +112 −0 src/libstd/list.rs
  12. +244 −0 src/libstd/map.rs
  13. +10 −0 src/libstd/net.rs
  14. +40 −0 src/libstd/rand.rs
  15. +163 −0 src/libstd/rope.rs
  16. +69 −0 src/libstd/run_program.rs
  17. +96 −0 src/libstd/sha1.rs
  18. +144 −0 src/libstd/sort.rs
  19. +12 −0 src/libstd/tempfile.rs
  20. +129 −0 src/libstd/test.rs
  21. +62 −0 src/libstd/treemap.rs
  22. +118 −0 src/libstd/tri.rs
  23. +27 −0 src/libstd/unicode.rs
  24. +44 −0 src/libstd/uv.rs
  25. +0 −287 src/test/stdtest/bitv.rs
  26. +0 −61 src/test/stdtest/c_vec.rs
  27. +0 −197 src/test/stdtest/deque.rs
  28. +0 −151 src/test/stdtest/four.rs
  29. +0 −275 src/test/stdtest/fs.rs
  30. +0 −471 src/test/stdtest/getopts.rs
  31. +0 −103 src/test/stdtest/io.rs
  32. +0 −58 src/test/stdtest/json.rs
  33. +0 −114 src/test/stdtest/list.rs
  34. +0 −249 src/test/stdtest/map.rs
  35. +0 −14 src/test/stdtest/net.rs
  36. +0 −58 src/test/stdtest/os.rs
  37. +0 −18 src/test/stdtest/path.rs
  38. +0 −71 src/test/stdtest/qsort.rs
  39. +0 −50 src/test/stdtest/qsort3.rs
  40. +0 −40 src/test/stdtest/rand.rs
  41. +0 −166 src/test/stdtest/rope.rs
  42. +0 −70 src/test/stdtest/run.rs
  43. +0 −95 src/test/stdtest/sha1.rs
  44. +0 −41 src/test/stdtest/sort.rs
  45. +0 −40 src/test/stdtest/stdtest.rc
  46. +0 −20 src/test/stdtest/tempfile.rs
  47. +0 −119 src/test/stdtest/test.rs
  48. +0 −62 src/test/stdtest/treemap.rs
  49. +0 −119 src/test/stdtest/tri.rs
  50. +0 −30 src/test/stdtest/unicode.rs
  51. +0 −48 src/test/stdtest/uv.rs
View
6 mk/tests.mk
@@ -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
285 src/libstd/bitv.rs
@@ -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
61 src/libstd/c_vec.rs
@@ -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
197 src/libstd/deque.rs
@@ -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
150 src/libstd/four.rs
@@ -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
285 src/libstd/fs.rs
@@ -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
50 src/libstd/generic_os.rs
@@ -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
473 src/libstd/getopts.rs
@@ -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
99 src/libstd/io.rs
@@ -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
55 src/libstd/json.rs
@@ -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
112 src/libstd/list.rs
@@ -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
244 src/libstd/map.rs
@@ -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
10 src/libstd/net.rs
@@ -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
40 src/libstd/rand.rs
@@ -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());