std: clean up tests (mostly unused unsafe blocks)

This commit is contained in:
Alex Crichton 2013-04-18 20:40:53 -04:00
parent 98dfeb173f
commit cd982ad3f7
4 changed files with 108 additions and 124 deletions

View file

@ -159,8 +159,8 @@ mod tests {
assert!(mem as int != 0);
return unsafe { c_vec_with_dtor(mem as *mut u8, n as uint,
|| unsafe { free(mem) }) };
return c_vec_with_dtor(mem as *mut u8, n as uint,
|| unsafe { free(mem) });
}
}
@ -196,7 +196,7 @@ mod tests {
#[test]
fn test_and_I_mean_it() {
let cv = malloc(16u as size_t);
let p = unsafe { ptr(cv) };
let p = ptr(cv);
set(cv, 0u, 32u8);
set(cv, 1u, 33u8);

View file

@ -1287,18 +1287,16 @@ mod tests {
node::Content(x) => {
let str = @mut ~"";
fn aux(str: @mut ~str, node: @node::Node) {
unsafe {
match (*node) {
node::Leaf(x) => {
*str += str::slice(
*x.content, x.byte_offset,
x.byte_offset + x.byte_len).to_owned();
}
node::Concat(ref x) => {
aux(str, x.left);
aux(str, x.right);
}
}
match (*node) {
node::Leaf(x) => {
*str += str::slice(
*x.content, x.byte_offset,
x.byte_offset + x.byte_len).to_owned();
}
node::Concat(ref x) => {
aux(str, x.left);
aux(str, x.right);
}
}
}
aux(str, x);

View file

@ -224,36 +224,32 @@ struct AhData {
#[cfg(test)]
fn impl_uv_iotask_async(iotask: &IoTask) {
unsafe {
let async_handle = ll::async_t();
let ah_ptr = ptr::addr_of(&async_handle);
let (exit_po, exit_ch) = stream::<()>();
let ah_data = AhData {
iotask: iotask.clone(),
exit_ch: SharedChan::new(exit_ch)
};
let ah_data_ptr: *AhData = unsafe {
ptr::to_unsafe_ptr(&ah_data)
};
debug!("about to interact");
do interact(iotask) |loop_ptr| {
unsafe {
debug!("interacting");
ll::async_init(loop_ptr, ah_ptr, async_handle_cb);
ll::set_data_for_uv_handle(
ah_ptr, ah_data_ptr as *libc::c_void);
ll::async_send(ah_ptr);
}
};
debug!("waiting for async close");
exit_po.recv();
}
let async_handle = ll::async_t();
let ah_ptr = ptr::addr_of(&async_handle);
let (exit_po, exit_ch) = stream::<()>();
let ah_data = AhData {
iotask: iotask.clone(),
exit_ch: SharedChan::new(exit_ch)
};
let ah_data_ptr: *AhData = ptr::to_unsafe_ptr(&ah_data);
debug!("about to interact");
do interact(iotask) |loop_ptr| {
unsafe {
debug!("interacting");
ll::async_init(loop_ptr, ah_ptr, async_handle_cb);
ll::set_data_for_uv_handle(
ah_ptr, ah_data_ptr as *libc::c_void);
ll::async_send(ah_ptr);
}
};
debug!("waiting for async close");
exit_po.recv();
}
// this fn documents the bear minimum neccesary to roll your own
// high_level_loop
#[cfg(test)]
unsafe fn spawn_test_loop(exit_ch: ~Chan<()>) -> IoTask {
fn spawn_test_loop(exit_ch: ~Chan<()>) -> IoTask {
let (iotask_port, iotask_ch) = stream::<IoTask>();
do task::spawn_sched(task::ManualThreads(1u)) {
debug!("about to run a test loop");
@ -265,9 +261,7 @@ unsafe fn spawn_test_loop(exit_ch: ~Chan<()>) -> IoTask {
#[cfg(test)]
extern fn lifetime_handle_close(handle: *libc::c_void) {
unsafe {
debug!("lifetime_handle_close ptr %?", handle);
}
debug!("lifetime_handle_close ptr %?", handle);
}
#[cfg(test)]
@ -279,38 +273,36 @@ extern fn lifetime_async_callback(handle: *libc::c_void,
#[test]
fn test_uv_iotask_async() {
unsafe {
let (exit_po, exit_ch) = stream::<()>();
let iotask = &spawn_test_loop(~exit_ch);
let (exit_po, exit_ch) = stream::<()>();
let iotask = &spawn_test_loop(~exit_ch);
debug!("spawned iotask");
debug!("spawned iotask");
// using this handle to manage the lifetime of the
// high_level_loop, as it will exit the first time one of
// the impl_uv_hl_async() is cleaned up with no one ref'd
// handles on the loop (Which can happen under
// race-condition type situations.. this ensures that the
// loop lives until, at least, all of the
// impl_uv_hl_async() runs have been called, at least.
let (work_exit_po, work_exit_ch) = stream::<()>();
let work_exit_ch = SharedChan::new(work_exit_ch);
for iter::repeat(7u) {
let iotask_clone = iotask.clone();
let work_exit_ch_clone = work_exit_ch.clone();
do task::spawn_sched(task::ManualThreads(1u)) {
debug!("async");
impl_uv_iotask_async(&iotask_clone);
debug!("done async");
work_exit_ch_clone.send(());
};
// using this handle to manage the lifetime of the
// high_level_loop, as it will exit the first time one of
// the impl_uv_hl_async() is cleaned up with no one ref'd
// handles on the loop (Which can happen under
// race-condition type situations.. this ensures that the
// loop lives until, at least, all of the
// impl_uv_hl_async() runs have been called, at least.
let (work_exit_po, work_exit_ch) = stream::<()>();
let work_exit_ch = SharedChan::new(work_exit_ch);
for iter::repeat(7u) {
let iotask_clone = iotask.clone();
let work_exit_ch_clone = work_exit_ch.clone();
do task::spawn_sched(task::ManualThreads(1u)) {
debug!("async");
impl_uv_iotask_async(&iotask_clone);
debug!("done async");
work_exit_ch_clone.send(());
};
for iter::repeat(7u) {
debug!("waiting");
work_exit_po.recv();
};
debug!(~"sending teardown_loop msg..");
exit(iotask);
exit_po.recv();
debug!(~"after recv on exit_po.. exiting..");
}
};
for iter::repeat(7u) {
debug!("waiting");
work_exit_po.recv();
};
debug!(~"sending teardown_loop msg..");
exit(iotask);
exit_po.recv();
debug!(~"after recv on exit_po.. exiting..");
}

View file

@ -1422,10 +1422,8 @@ mod test {
}
extern fn server_after_close_cb(handle: *libc::c_void) {
unsafe {
debug!("SERVER server stream closed, should exit. h: %?",
handle);
}
debug!("SERVER server stream closed, should exit. h: %?",
handle);
}
extern fn client_stream_after_close_cb(handle: *libc::c_void) {
@ -1709,48 +1707,46 @@ mod test {
// this is the impl for a test that is (maybe) ran on a
// per-platform/arch basis below
pub fn impl_uv_tcp_server_and_request() {
unsafe {
let bind_ip = ~"0.0.0.0";
let request_ip = ~"127.0.0.1";
let port = 8886;
let kill_server_msg = ~"does a dog have buddha nature?";
let server_resp_msg = ~"mu!";
let (client_port, client_chan) = stream::<~str>();
let client_chan = SharedChan::new(client_chan);
let (server_port, server_chan) = stream::<~str>();
let server_chan = SharedChan::new(server_chan);
let bind_ip = ~"0.0.0.0";
let request_ip = ~"127.0.0.1";
let port = 8886;
let kill_server_msg = ~"does a dog have buddha nature?";
let server_resp_msg = ~"mu!";
let (client_port, client_chan) = stream::<~str>();
let client_chan = SharedChan::new(client_chan);
let (server_port, server_chan) = stream::<~str>();
let server_chan = SharedChan::new(server_chan);
let (continue_port, continue_chan) = stream::<bool>();
let continue_chan = SharedChan::new(continue_chan);
let (continue_port, continue_chan) = stream::<bool>();
let continue_chan = SharedChan::new(continue_chan);
let kill_server_msg_copy = copy kill_server_msg;
let server_resp_msg_copy = copy server_resp_msg;
do task::spawn_sched(task::ManualThreads(1)) {
impl_uv_tcp_server(bind_ip, port,
copy kill_server_msg_copy,
copy server_resp_msg_copy,
server_chan.clone(),
continue_chan.clone());
};
let kill_server_msg_copy = copy kill_server_msg;
let server_resp_msg_copy = copy server_resp_msg;
do task::spawn_sched(task::ManualThreads(1)) {
impl_uv_tcp_server(bind_ip, port,
copy kill_server_msg_copy,
copy server_resp_msg_copy,
server_chan.clone(),
continue_chan.clone());
};
// block until the server up is.. possibly a race?
debug!(~"before receiving on server continue_port");
continue_port.recv();
debug!(~"received on continue port, set up tcp client");
// block until the server up is.. possibly a race?
debug!(~"before receiving on server continue_port");
continue_port.recv();
debug!(~"received on continue port, set up tcp client");
let kill_server_msg_copy = copy kill_server_msg;
do task::spawn_sched(task::ManualThreads(1u)) {
impl_uv_tcp_request(request_ip, port,
kill_server_msg_copy,
client_chan.clone());
};
let kill_server_msg_copy = copy kill_server_msg;
do task::spawn_sched(task::ManualThreads(1u)) {
impl_uv_tcp_request(request_ip, port,
kill_server_msg_copy,
client_chan.clone());
};
let msg_from_client = server_port.recv();
let msg_from_server = client_port.recv();
let msg_from_client = server_port.recv();
let msg_from_server = client_port.recv();
assert!(str::contains(msg_from_client, kill_server_msg));
assert!(str::contains(msg_from_server, server_resp_msg));
}
assert!(str::contains(msg_from_client, kill_server_msg));
assert!(str::contains(msg_from_server, server_resp_msg));
}
// FIXME don't run on fbsd or linux 32 bit(#2064)
@ -1784,17 +1780,15 @@ mod test {
fn struct_size_check_common<TStruct>(t_name: ~str,
foreign_size: libc::c_uint) {
unsafe {
let rust_size = sys::size_of::<TStruct>();
let sizes_match = foreign_size as uint == rust_size;
if !sizes_match {
let output = fmt!(
"STRUCT_SIZE FAILURE: %s -- actual: %u expected: %u",
t_name, rust_size, foreign_size as uint);
debug!(output);
}
assert!(sizes_match);
let rust_size = sys::size_of::<TStruct>();
let sizes_match = foreign_size as uint == rust_size;
if !sizes_match {
let output = fmt!(
"STRUCT_SIZE FAILURE: %s -- actual: %u expected: %u",
t_name, rust_size, foreign_size as uint);
debug!(output);
}
assert!(sizes_match);
}
// struct size tests