Skip to content

Commit cd982ad

Browse files
committed
std: clean up tests (mostly unused unsafe blocks)
1 parent 98dfeb1 commit cd982ad

File tree

4 files changed

+116
-132
lines changed

4 files changed

+116
-132
lines changed

src/libstd/c_vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ mod tests {
159159

160160
assert!(mem as int != 0);
161161

162-
return unsafe { c_vec_with_dtor(mem as *mut u8, n as uint,
163-
|| unsafe { free(mem) }) };
162+
return c_vec_with_dtor(mem as *mut u8, n as uint,
163+
|| unsafe { free(mem) });
164164
}
165165
}
166166

@@ -196,7 +196,7 @@ mod tests {
196196
#[test]
197197
fn test_and_I_mean_it() {
198198
let cv = malloc(16u as size_t);
199-
let p = unsafe { ptr(cv) };
199+
let p = ptr(cv);
200200

201201
set(cv, 0u, 32u8);
202202
set(cv, 1u, 33u8);

src/libstd/rope.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -1287,18 +1287,16 @@ mod tests {
12871287
node::Content(x) => {
12881288
let str = @mut ~"";
12891289
fn aux(str: @mut ~str, node: @node::Node) {
1290-
unsafe {
1291-
match (*node) {
1292-
node::Leaf(x) => {
1293-
*str += str::slice(
1294-
*x.content, x.byte_offset,
1295-
x.byte_offset + x.byte_len).to_owned();
1296-
}
1297-
node::Concat(ref x) => {
1298-
aux(str, x.left);
1299-
aux(str, x.right);
1300-
}
1301-
}
1290+
match (*node) {
1291+
node::Leaf(x) => {
1292+
*str += str::slice(
1293+
*x.content, x.byte_offset,
1294+
x.byte_offset + x.byte_len).to_owned();
1295+
}
1296+
node::Concat(ref x) => {
1297+
aux(str, x.left);
1298+
aux(str, x.right);
1299+
}
13021300
}
13031301
}
13041302
aux(str, x);

src/libstd/uv_iotask.rs

+53-61
Original file line numberDiff line numberDiff line change
@@ -224,36 +224,32 @@ struct AhData {
224224

225225
#[cfg(test)]
226226
fn impl_uv_iotask_async(iotask: &IoTask) {
227-
unsafe {
228-
let async_handle = ll::async_t();
229-
let ah_ptr = ptr::addr_of(&async_handle);
230-
let (exit_po, exit_ch) = stream::<()>();
231-
let ah_data = AhData {
232-
iotask: iotask.clone(),
233-
exit_ch: SharedChan::new(exit_ch)
234-
};
235-
let ah_data_ptr: *AhData = unsafe {
236-
ptr::to_unsafe_ptr(&ah_data)
237-
};
238-
debug!("about to interact");
239-
do interact(iotask) |loop_ptr| {
240-
unsafe {
241-
debug!("interacting");
242-
ll::async_init(loop_ptr, ah_ptr, async_handle_cb);
243-
ll::set_data_for_uv_handle(
244-
ah_ptr, ah_data_ptr as *libc::c_void);
245-
ll::async_send(ah_ptr);
246-
}
247-
};
248-
debug!("waiting for async close");
249-
exit_po.recv();
250-
}
227+
let async_handle = ll::async_t();
228+
let ah_ptr = ptr::addr_of(&async_handle);
229+
let (exit_po, exit_ch) = stream::<()>();
230+
let ah_data = AhData {
231+
iotask: iotask.clone(),
232+
exit_ch: SharedChan::new(exit_ch)
233+
};
234+
let ah_data_ptr: *AhData = ptr::to_unsafe_ptr(&ah_data);
235+
debug!("about to interact");
236+
do interact(iotask) |loop_ptr| {
237+
unsafe {
238+
debug!("interacting");
239+
ll::async_init(loop_ptr, ah_ptr, async_handle_cb);
240+
ll::set_data_for_uv_handle(
241+
ah_ptr, ah_data_ptr as *libc::c_void);
242+
ll::async_send(ah_ptr);
243+
}
244+
};
245+
debug!("waiting for async close");
246+
exit_po.recv();
251247
}
252248

253249
// this fn documents the bear minimum neccesary to roll your own
254250
// high_level_loop
255251
#[cfg(test)]
256-
unsafe fn spawn_test_loop(exit_ch: ~Chan<()>) -> IoTask {
252+
fn spawn_test_loop(exit_ch: ~Chan<()>) -> IoTask {
257253
let (iotask_port, iotask_ch) = stream::<IoTask>();
258254
do task::spawn_sched(task::ManualThreads(1u)) {
259255
debug!("about to run a test loop");
@@ -265,9 +261,7 @@ unsafe fn spawn_test_loop(exit_ch: ~Chan<()>) -> IoTask {
265261

266262
#[cfg(test)]
267263
extern fn lifetime_handle_close(handle: *libc::c_void) {
268-
unsafe {
269-
debug!("lifetime_handle_close ptr %?", handle);
270-
}
264+
debug!("lifetime_handle_close ptr %?", handle);
271265
}
272266

273267
#[cfg(test)]
@@ -279,38 +273,36 @@ extern fn lifetime_async_callback(handle: *libc::c_void,
279273

280274
#[test]
281275
fn test_uv_iotask_async() {
282-
unsafe {
283-
let (exit_po, exit_ch) = stream::<()>();
284-
let iotask = &spawn_test_loop(~exit_ch);
285-
286-
debug!("spawned iotask");
287-
288-
// using this handle to manage the lifetime of the
289-
// high_level_loop, as it will exit the first time one of
290-
// the impl_uv_hl_async() is cleaned up with no one ref'd
291-
// handles on the loop (Which can happen under
292-
// race-condition type situations.. this ensures that the
293-
// loop lives until, at least, all of the
294-
// impl_uv_hl_async() runs have been called, at least.
295-
let (work_exit_po, work_exit_ch) = stream::<()>();
296-
let work_exit_ch = SharedChan::new(work_exit_ch);
297-
for iter::repeat(7u) {
298-
let iotask_clone = iotask.clone();
299-
let work_exit_ch_clone = work_exit_ch.clone();
300-
do task::spawn_sched(task::ManualThreads(1u)) {
301-
debug!("async");
302-
impl_uv_iotask_async(&iotask_clone);
303-
debug!("done async");
304-
work_exit_ch_clone.send(());
305-
};
276+
let (exit_po, exit_ch) = stream::<()>();
277+
let iotask = &spawn_test_loop(~exit_ch);
278+
279+
debug!("spawned iotask");
280+
281+
// using this handle to manage the lifetime of the
282+
// high_level_loop, as it will exit the first time one of
283+
// the impl_uv_hl_async() is cleaned up with no one ref'd
284+
// handles on the loop (Which can happen under
285+
// race-condition type situations.. this ensures that the
286+
// loop lives until, at least, all of the
287+
// impl_uv_hl_async() runs have been called, at least.
288+
let (work_exit_po, work_exit_ch) = stream::<()>();
289+
let work_exit_ch = SharedChan::new(work_exit_ch);
290+
for iter::repeat(7u) {
291+
let iotask_clone = iotask.clone();
292+
let work_exit_ch_clone = work_exit_ch.clone();
293+
do task::spawn_sched(task::ManualThreads(1u)) {
294+
debug!("async");
295+
impl_uv_iotask_async(&iotask_clone);
296+
debug!("done async");
297+
work_exit_ch_clone.send(());
306298
};
307-
for iter::repeat(7u) {
308-
debug!("waiting");
309-
work_exit_po.recv();
310-
};
311-
debug!(~"sending teardown_loop msg..");
312-
exit(iotask);
313-
exit_po.recv();
314-
debug!(~"after recv on exit_po.. exiting..");
315-
}
299+
};
300+
for iter::repeat(7u) {
301+
debug!("waiting");
302+
work_exit_po.recv();
303+
};
304+
debug!(~"sending teardown_loop msg..");
305+
exit(iotask);
306+
exit_po.recv();
307+
debug!(~"after recv on exit_po.. exiting..");
316308
}

src/libstd/uv_ll.rs

+50-56
Original file line numberDiff line numberDiff line change
@@ -1422,10 +1422,8 @@ mod test {
14221422
}
14231423
14241424
extern fn server_after_close_cb(handle: *libc::c_void) {
1425-
unsafe {
1426-
debug!("SERVER server stream closed, should exit. h: %?",
1427-
handle);
1428-
}
1425+
debug!("SERVER server stream closed, should exit. h: %?",
1426+
handle);
14291427
}
14301428
14311429
extern fn client_stream_after_close_cb(handle: *libc::c_void) {
@@ -1709,48 +1707,46 @@ mod test {
17091707
// this is the impl for a test that is (maybe) ran on a
17101708
// per-platform/arch basis below
17111709
pub fn impl_uv_tcp_server_and_request() {
1712-
unsafe {
1713-
let bind_ip = ~"0.0.0.0";
1714-
let request_ip = ~"127.0.0.1";
1715-
let port = 8886;
1716-
let kill_server_msg = ~"does a dog have buddha nature?";
1717-
let server_resp_msg = ~"mu!";
1718-
let (client_port, client_chan) = stream::<~str>();
1719-
let client_chan = SharedChan::new(client_chan);
1720-
let (server_port, server_chan) = stream::<~str>();
1721-
let server_chan = SharedChan::new(server_chan);
1722-
1723-
let (continue_port, continue_chan) = stream::<bool>();
1724-
let continue_chan = SharedChan::new(continue_chan);
1725-
1726-
let kill_server_msg_copy = copy kill_server_msg;
1727-
let server_resp_msg_copy = copy server_resp_msg;
1728-
do task::spawn_sched(task::ManualThreads(1)) {
1729-
impl_uv_tcp_server(bind_ip, port,
1730-
copy kill_server_msg_copy,
1731-
copy server_resp_msg_copy,
1732-
server_chan.clone(),
1733-
continue_chan.clone());
1734-
};
1735-
1736-
// block until the server up is.. possibly a race?
1737-
debug!(~"before receiving on server continue_port");
1738-
continue_port.recv();
1739-
debug!(~"received on continue port, set up tcp client");
1740-
1741-
let kill_server_msg_copy = copy kill_server_msg;
1742-
do task::spawn_sched(task::ManualThreads(1u)) {
1743-
impl_uv_tcp_request(request_ip, port,
1744-
kill_server_msg_copy,
1745-
client_chan.clone());
1746-
};
1747-
1748-
let msg_from_client = server_port.recv();
1749-
let msg_from_server = client_port.recv();
1750-
1751-
assert!(str::contains(msg_from_client, kill_server_msg));
1752-
assert!(str::contains(msg_from_server, server_resp_msg));
1753-
}
1710+
let bind_ip = ~"0.0.0.0";
1711+
let request_ip = ~"127.0.0.1";
1712+
let port = 8886;
1713+
let kill_server_msg = ~"does a dog have buddha nature?";
1714+
let server_resp_msg = ~"mu!";
1715+
let (client_port, client_chan) = stream::<~str>();
1716+
let client_chan = SharedChan::new(client_chan);
1717+
let (server_port, server_chan) = stream::<~str>();
1718+
let server_chan = SharedChan::new(server_chan);
1719+
1720+
let (continue_port, continue_chan) = stream::<bool>();
1721+
let continue_chan = SharedChan::new(continue_chan);
1722+
1723+
let kill_server_msg_copy = copy kill_server_msg;
1724+
let server_resp_msg_copy = copy server_resp_msg;
1725+
do task::spawn_sched(task::ManualThreads(1)) {
1726+
impl_uv_tcp_server(bind_ip, port,
1727+
copy kill_server_msg_copy,
1728+
copy server_resp_msg_copy,
1729+
server_chan.clone(),
1730+
continue_chan.clone());
1731+
};
1732+
1733+
// block until the server up is.. possibly a race?
1734+
debug!(~"before receiving on server continue_port");
1735+
continue_port.recv();
1736+
debug!(~"received on continue port, set up tcp client");
1737+
1738+
let kill_server_msg_copy = copy kill_server_msg;
1739+
do task::spawn_sched(task::ManualThreads(1u)) {
1740+
impl_uv_tcp_request(request_ip, port,
1741+
kill_server_msg_copy,
1742+
client_chan.clone());
1743+
};
1744+
1745+
let msg_from_client = server_port.recv();
1746+
let msg_from_server = client_port.recv();
1747+
1748+
assert!(str::contains(msg_from_client, kill_server_msg));
1749+
assert!(str::contains(msg_from_server, server_resp_msg));
17541750
}
17551751
17561752
// FIXME don't run on fbsd or linux 32 bit(#2064)
@@ -1784,17 +1780,15 @@ mod test {
17841780
17851781
fn struct_size_check_common<TStruct>(t_name: ~str,
17861782
foreign_size: libc::c_uint) {
1787-
unsafe {
1788-
let rust_size = sys::size_of::<TStruct>();
1789-
let sizes_match = foreign_size as uint == rust_size;
1790-
if !sizes_match {
1791-
let output = fmt!(
1792-
"STRUCT_SIZE FAILURE: %s -- actual: %u expected: %u",
1793-
t_name, rust_size, foreign_size as uint);
1794-
debug!(output);
1795-
}
1796-
assert!(sizes_match);
1783+
let rust_size = sys::size_of::<TStruct>();
1784+
let sizes_match = foreign_size as uint == rust_size;
1785+
if !sizes_match {
1786+
let output = fmt!(
1787+
"STRUCT_SIZE FAILURE: %s -- actual: %u expected: %u",
1788+
t_name, rust_size, foreign_size as uint);
1789+
debug!(output);
17971790
}
1791+
assert!(sizes_match);
17981792
}
17991793
18001794
// struct size tests

0 commit comments

Comments
 (0)