c_vec: add tests

This commit is contained in:
Joshua Wise 2011-11-26 04:18:31 -05:00 committed by Niko Matsakis
parent 45dc5356df
commit 90d27af42a
2 changed files with 58 additions and 0 deletions

57
src/test/stdtest/c_vec.rs Normal file
View File

@ -0,0 +1,57 @@
// -*- rust -*-
use std;
import std::c_vec::*;
import std::ctypes::*;
#[link_name = ""]
#[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 size(cv) == 16u;
}
#[test]
#[should_fail]
fn test_overrun_get() {
let cv = malloc(16u);
get(cv, 17u);
}
#[test]
#[should_fail]
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

@ -3,6 +3,7 @@ use std;
mod bitv;
mod bool;
mod box;
mod c_vec;
mod char;
mod comm;
mod deque;