Skip to content

Commit

Permalink
c_vec: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jwise authored and nikomatsakis committed Nov 28, 2011
1 parent 45dc535 commit 90d27af
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/test/stdtest/c_vec.rs
@@ -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 */
}
1 change: 1 addition & 0 deletions src/test/stdtest/stdtest.rc
Expand Up @@ -3,6 +3,7 @@ use std;
mod bitv;
mod bool;
mod box;
mod c_vec;
mod char;
mod comm;
mod deque;
Expand Down

0 comments on commit 90d27af

Please sign in to comment.