Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45dc535
commit 90d27af
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 */ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ use std; | |
mod bitv; | ||
mod bool; | ||
mod box; | ||
mod c_vec; | ||
mod char; | ||
mod comm; | ||
mod deque; | ||
|