forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_vec.rs
61 lines (48 loc) · 1.01 KB
/
c_vec.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import core::*;
// -*- rust -*-
use std;
import std::c_vec::*;
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 */
}