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

@@ -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 */
}
}