Skip to content

Commit

Permalink
Where relevant, replace equality checks in assert! with assert_eq!
Browse files Browse the repository at this point in the history
`assert_eq!` gives more debug info when the test fails by default than
`assert!`. This should help make debugging easier.
  • Loading branch information
AdminXVII committed Sep 17, 2019
1 parent 1662466 commit e8d237e
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 77 deletions.
14 changes: 7 additions & 7 deletions src/sys/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,11 @@ fn test_struct_kevent() {
FilterFlag::NOTE_CHILD | FilterFlag::NOTE_EXIT,
0x1337,
udata);
assert!(expected.ident == actual.ident());
assert!(expected.filter == actual.filter() as type_of_event_filter);
assert!(expected.flags == actual.flags().bits());
assert!(expected.fflags == actual.fflags().bits());
assert!(expected.data == actual.data() as type_of_data);
assert!(expected.udata == actual.udata() as type_of_udata);
assert!(mem::size_of::<libc::kevent>() == mem::size_of::<KEvent>());
assert_eq!(expected.ident, actual.ident());
assert_eq!(expected.filter, actual.filter() as type_of_event_filter);
assert_eq!(expected.flags, actual.flags().bits());
assert_eq!(expected.fflags, actual.fflags().bits());
assert_eq!(expected.data, actual.data() as type_of_data);
assert_eq!(expected.udata, actual.udata() as type_of_udata);
assert_eq!(mem::size_of::<libc::kevent>(), mem::size_of::<KEvent>());
}
4 changes: 2 additions & 2 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,12 +1245,12 @@ pub unsafe fn sockaddr_storage_to_addr(

match c_int::from(addr.ss_family) {
libc::AF_INET => {
assert!(len as usize == mem::size_of::<sockaddr_in>());
assert_eq!(len as usize, mem::size_of::<sockaddr_in>());
let ret = *(addr as *const _ as *const sockaddr_in);
Ok(SockAddr::Inet(InetAddr::V4(ret)))
}
libc::AF_INET6 => {
assert!(len as usize == mem::size_of::<sockaddr_in6>());
assert_eq!(len as usize, mem::size_of::<sockaddr_in6>());
Ok(SockAddr::Inet(InetAddr::V6(*(addr as *const _ as *const sockaddr_in6))))
}
libc::AF_UNIX => {
Expand Down
12 changes: 6 additions & 6 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ unsafe impl<T> Get<T> for GetStruct<T> {
}

unsafe fn unwrap(self) -> T {
assert!(self.len as usize == mem::size_of::<T>(), "invalid getsockopt implementation");
assert_eq!(self.len as usize, mem::size_of::<T>(), "invalid getsockopt implementation");
self.val
}
}
Expand Down Expand Up @@ -458,7 +458,7 @@ unsafe impl Get<bool> for GetBool {
}

unsafe fn unwrap(self) -> bool {
assert!(self.len as usize == mem::size_of::<c_int>(), "invalid getsockopt implementation");
assert_eq!(self.len as usize, mem::size_of::<c_int>(), "invalid getsockopt implementation");
self.val != 0
}
}
Expand Down Expand Up @@ -505,7 +505,7 @@ unsafe impl Get<u8> for GetU8 {
}

unsafe fn unwrap(self) -> u8 {
assert!(self.len as usize == mem::size_of::<u8>(), "invalid getsockopt implementation");
assert_eq!(self.len as usize, mem::size_of::<u8>(), "invalid getsockopt implementation");
self.val as u8
}
}
Expand Down Expand Up @@ -552,7 +552,7 @@ unsafe impl Get<usize> for GetUsize {
}

unsafe fn unwrap(self) -> usize {
assert!(self.len as usize == mem::size_of::<c_int>(), "invalid getsockopt implementation");
assert_eq!(self.len as usize, mem::size_of::<c_int>(), "invalid getsockopt implementation");
self.val as usize
}
}
Expand Down Expand Up @@ -644,7 +644,7 @@ mod test {

let (a, b) = socketpair(AddressFamily::Unix, SockType::Stream, None, SockFlag::empty()).unwrap();
let a_type = getsockopt(a, super::SockType).unwrap();
assert!(a_type == SockType::Stream);
assert_eq!(a_type, SockType::Stream);
close(a).unwrap();
close(b).unwrap();
}
Expand All @@ -656,7 +656,7 @@ mod test {

let s = socket(AddressFamily::Inet, SockType::Datagram, SockFlag::empty(), None).unwrap();
let s_type = getsockopt(s, super::SockType).unwrap();
assert!(s_type == SockType::Datagram);
assert_eq!(s_type, SockType::Datagram);
close(s).unwrap();
}

Expand Down
14 changes: 7 additions & 7 deletions src/sys/termios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
//! # let mut t = unsafe { Termios::default_uninit() };
//! # cfsetspeed(&mut t, BaudRate::B9600);
//! let speed = cfgetispeed(&t);
//! assert!(speed == cfgetospeed(&t));
//! assert_eq!(speed, cfgetospeed(&t));
//! cfsetispeed(&mut t, speed);
//! # }
//! ```
Expand All @@ -99,8 +99,8 @@
//! # fn main() {
//! # let mut t = unsafe { Termios::default_uninit() };
//! # cfsetspeed(&mut t, BaudRate::B9600);
//! assert!(cfgetispeed(&t) == BaudRate::B9600);
//! assert!(cfgetospeed(&t) == BaudRate::B9600);
//! assert_eq!(cfgetispeed(&t), BaudRate::B9600);
//! assert_eq!(cfgetospeed(&t), BaudRate::B9600);
//! # }
//! ```
//!
Expand All @@ -118,8 +118,8 @@
//! # fn main() {
//! # let mut t = unsafe { Termios::default_uninit() };
//! # cfsetspeed(&mut t, 9600u32);
//! assert!(cfgetispeed(&t) == 9600u32);
//! assert!(cfgetospeed(&t) == 9600u32);
//! assert_eq!(cfgetispeed(&t), 9600u32);
//! assert_eq!(cfgetospeed(&t), 9600u32);
//! # }
//! ```
//!
Expand All @@ -137,8 +137,8 @@
//! # fn main() {
//! # let mut t = unsafe { Termios::default_uninit() };
//! # cfsetspeed(&mut t, 9600u32);
//! assert!(cfgetispeed(&t) == BaudRate::B9600.into());
//! assert!(u32::from(BaudRate::B9600) == 9600u32);
//! assert_eq!(cfgetispeed(&t), BaudRate::B9600.into());
//! assert_eq!(u32::from(BaudRate::B9600), 9600u32);
//! # }
//! ```
//!
Expand Down
82 changes: 41 additions & 41 deletions test/sys/test_aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ fn test_aio_suspend() {
}
}

assert!(wcb.aio_return().unwrap() as usize == WBUF.len());
assert!(rcb.aio_return().unwrap() as usize == rlen);
assert_eq!(wcb.aio_return().unwrap() as usize, WBUF.len());
assert_eq!(rcb.aio_return().unwrap() as usize, rlen);
}

// Test a simple aio operation with no completion notification. We must poll
Expand All @@ -196,11 +196,11 @@ fn test_read() {
aiocb.read().unwrap();

let err = poll_aio(&mut aiocb);
assert!(err == Ok(()));
assert!(aiocb.aio_return().unwrap() as usize == EXPECT.len());
assert_eq!(err, Ok(()));
assert_eq!(aiocb.aio_return().unwrap() as usize, EXPECT.len());
}

assert!(EXPECT == rbuf.deref().deref());
assert_eq!(EXPECT, rbuf.deref().deref());
}

/// `AioCb::read` should not modify the `AioCb` object if `libc::aio_read`
Expand Down Expand Up @@ -242,11 +242,11 @@ fn test_read_into_mut_slice() {
aiocb.read().unwrap();

let err = poll_aio(&mut aiocb);
assert!(err == Ok(()));
assert!(aiocb.aio_return().unwrap() as usize == EXPECT.len());
assert_eq!(err, Ok(()));
assert_eq!(aiocb.aio_return().unwrap() as usize, EXPECT.len());
}

assert!(rbuf == EXPECT);
assert_eq!(rbuf, EXPECT);
}

// Tests from_ptr
Expand All @@ -272,11 +272,11 @@ fn test_read_into_pointer() {
aiocb.read().unwrap();

let err = poll_aio(&mut aiocb);
assert!(err == Ok(()));
assert!(aiocb.aio_return().unwrap() as usize == EXPECT.len());
assert_eq!(err, Ok(()));
assert_eq!(aiocb.aio_return().unwrap() as usize, EXPECT.len());
}

assert!(rbuf == EXPECT);
assert_eq!(rbuf, EXPECT);
}

// Test reading into an immutable buffer. It should fail
Expand Down Expand Up @@ -318,13 +318,13 @@ fn test_write() {
aiocb.write().unwrap();

let err = poll_aio(&mut aiocb);
assert!(err == Ok(()));
assert!(aiocb.aio_return().unwrap() as usize == wbuf.len());
assert_eq!(err, Ok(()));
assert_eq!(aiocb.aio_return().unwrap() as usize, wbuf.len());

f.seek(SeekFrom::Start(0)).unwrap();
let len = f.read_to_end(&mut rbuf).unwrap();
assert!(len == EXPECT.len());
assert!(rbuf == EXPECT);
assert_eq!(len, EXPECT.len());
assert_eq!(rbuf, EXPECT);
}

// Tests `AioCb::from_boxed_slice` with `Bytes`
Expand All @@ -348,13 +348,13 @@ fn test_write_bytes() {
aiocb.write().unwrap();

let err = poll_aio(&mut aiocb);
assert!(err == Ok(()));
assert!(aiocb.aio_return().unwrap() as usize == expected_len);
assert_eq!(err, Ok(()));
assert_eq!(aiocb.aio_return().unwrap() as usize, expected_len);

f.seek(SeekFrom::Start(0)).unwrap();
let len = f.read_to_end(&mut rbuf).unwrap();
assert!(len == EXPECT.len());
assert!(rbuf == EXPECT);
assert_eq!(len, EXPECT.len());
assert_eq!(rbuf, EXPECT);
}

// Tests `AioCb::from_boxed_mut_slice` with `BytesMut`
Expand Down Expand Up @@ -406,13 +406,13 @@ fn test_write_from_pointer() {
aiocb.write().unwrap();

let err = poll_aio(&mut aiocb);
assert!(err == Ok(()));
assert!(aiocb.aio_return().unwrap() as usize == wbuf.len());
assert_eq!(err, Ok(()));
assert_eq!(aiocb.aio_return().unwrap() as usize, wbuf.len());

f.seek(SeekFrom::Start(0)).unwrap();
let len = f.read_to_end(&mut rbuf).unwrap();
assert!(len == EXPECT.len());
assert!(rbuf == EXPECT);
assert_eq!(len, EXPECT.len());
assert_eq!(rbuf, EXPECT);
}

/// `AioCb::write` should not modify the `AioCb` object if `libc::aio_write`
Expand Down Expand Up @@ -473,11 +473,11 @@ fn test_write_sigev_signal() {
thread::sleep(time::Duration::from_millis(10));
}

assert!(aiocb.aio_return().unwrap() as usize == WBUF.len());
assert_eq!(aiocb.aio_return().unwrap() as usize, WBUF.len());
f.seek(SeekFrom::Start(0)).unwrap();
let len = f.read_to_end(&mut rbuf).unwrap();
assert!(len == EXPECT.len());
assert!(rbuf == EXPECT);
assert_eq!(len, EXPECT.len());
assert_eq!(rbuf, EXPECT);
}

// Test LioCb::listio with LIO_WAIT, so all AIO ops should be complete by the
Expand Down Expand Up @@ -516,15 +516,15 @@ fn test_liocb_listio_wait() {
let err = liocb.listio(LioMode::LIO_WAIT, SigevNotify::SigevNone);
err.expect("lio_listio");

assert!(liocb.aio_return(0).unwrap() as usize == WBUF.len());
assert!(liocb.aio_return(1).unwrap() as usize == rlen);
assert_eq!(liocb.aio_return(0).unwrap() as usize, WBUF.len());
assert_eq!(liocb.aio_return(1).unwrap() as usize, rlen);
}
assert!(rbuf.deref().deref() == b"3456");
assert_eq!(rbuf.deref().deref(), b"3456");

f.seek(SeekFrom::Start(0)).unwrap();
let len = f.read_to_end(&mut rbuf2).unwrap();
assert!(len == EXPECT.len());
assert!(rbuf2 == EXPECT);
assert_eq!(len, EXPECT.len());
assert_eq!(rbuf2, EXPECT);
}

// Test LioCb::listio with LIO_NOWAIT and no SigEvent, so we must use some other
Expand Down Expand Up @@ -565,15 +565,15 @@ fn test_liocb_listio_nowait() {

poll_aio(&mut liocb.aiocbs[0]).unwrap();
poll_aio(&mut liocb.aiocbs[1]).unwrap();
assert!(liocb.aiocbs[0].aio_return().unwrap() as usize == WBUF.len());
assert!(liocb.aiocbs[1].aio_return().unwrap() as usize == rlen);
assert_eq!(liocb.aiocbs[0].aio_return().unwrap() as usize, WBUF.len());
assert_eq!(liocb.aiocbs[1].aio_return().unwrap() as usize, rlen);
}
assert!(rbuf.deref().deref() == b"3456");
assert_eq!(rbuf.deref().deref(), b"3456");

f.seek(SeekFrom::Start(0)).unwrap();
let len = f.read_to_end(&mut rbuf2).unwrap();
assert!(len == EXPECT.len());
assert!(rbuf2 == EXPECT);
assert_eq!(len, EXPECT.len());
assert_eq!(rbuf2, EXPECT);
}

// Test LioCb::listio with LIO_NOWAIT and a SigEvent to indicate when all
Expand Down Expand Up @@ -624,15 +624,15 @@ fn test_liocb_listio_signal() {
thread::sleep(time::Duration::from_millis(10));
}

assert!(liocb.aiocbs[0].aio_return().unwrap() as usize == WBUF.len());
assert!(liocb.aiocbs[1].aio_return().unwrap() as usize == rlen);
assert_eq!(liocb.aiocbs[0].aio_return().unwrap() as usize, WBUF.len());
assert_eq!(liocb.aiocbs[1].aio_return().unwrap() as usize, rlen);
}
assert!(rbuf.deref().deref() == b"3456");
assert_eq!(rbuf.deref().deref(), b"3456");

f.seek(SeekFrom::Start(0)).unwrap();
let len = f.read_to_end(&mut rbuf2).unwrap();
assert!(len == EXPECT.len());
assert!(rbuf2 == EXPECT);
assert_eq!(len, EXPECT.len());
assert_eq!(rbuf2, EXPECT);
}

// Try to use LioCb::listio to read into an immutable buffer. It should fail
Expand Down
12 changes: 6 additions & 6 deletions test/test_mq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn test_mq_send_and_receive() {
let mut buf = [0u8; 32];
let mut prio = 0u32;
let len = mq_receive(mqd1, &mut buf, &mut prio).unwrap();
assert!(prio == 1);
assert_eq!(prio, 1);

mq_close(mqd1).unwrap();
mq_close(mqd0).unwrap();
Expand Down Expand Up @@ -116,10 +116,10 @@ fn test_mq_set_nonblocking() {
let mqd = r.unwrap();
mq_set_nonblock(mqd).unwrap();
let new_attr = mq_getattr(mqd);
assert!(new_attr.unwrap().flags() == MQ_OFlag::O_NONBLOCK.bits() as c_long);
assert_eq!(new_attr.unwrap().flags(), MQ_OFlag::O_NONBLOCK.bits() as c_long);
mq_remove_nonblock(mqd).unwrap();
let new_attr = mq_getattr(mqd);
assert!(new_attr.unwrap().flags() == 0);
assert_eq!(new_attr.unwrap().flags(), 0);
mq_close(mqd).unwrap();
}

Expand All @@ -141,12 +141,12 @@ fn test_mq_unlink() {
let mqd = r.unwrap();

let res_unlink = mq_unlink(mq_name_opened);
assert!(res_unlink == Ok(()) );
assert_eq!(res_unlink, Ok(()) );

let res_unlink_not_opened = mq_unlink(mq_name_not_opened);
assert!(res_unlink_not_opened == Err(Sys(ENOENT)) );
assert_eq!(res_unlink_not_opened, Err(Sys(ENOENT)) );

mq_close(mqd).unwrap();
let res_unlink_after_close = mq_unlink(mq_name_opened);
assert!(res_unlink_after_close == Err(Sys(ENOENT)) );
assert_eq!(res_unlink_after_close, Err(Sys(ENOENT)) );
}
4 changes: 2 additions & 2 deletions test/test_pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn test_ptsname_copy() {
// Get the name of the slave
let slave_name1 = unsafe { ptsname(&master_fd) }.unwrap();
let slave_name2 = unsafe { ptsname(&master_fd) }.unwrap();
assert!(slave_name1 == slave_name2);
assert_eq!(slave_name1, slave_name2);
// Also make sure that the string was actually copied and they point to different parts of
// memory.
assert!(slave_name1.as_ptr() != slave_name2.as_ptr());
Expand All @@ -71,7 +71,7 @@ fn test_ptsname_r_copy() {
// Get the name of the slave
let slave_name1 = ptsname_r(&master_fd).unwrap();
let slave_name2 = ptsname_r(&master_fd).unwrap();
assert!(slave_name1 == slave_name2);
assert_eq!(slave_name1, slave_name2);
assert!(slave_name1.as_ptr() != slave_name2.as_ptr());
}

Expand Down
8 changes: 4 additions & 4 deletions test/test_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ fn assert_stat_results(stat_result: Result<FileStat>) {
assert!(stats.st_dev > 0); // must be positive integer, exact number machine dependent
assert!(stats.st_ino > 0); // inode is positive integer, exact number machine dependent
assert!(stats.st_mode > 0); // must be positive integer
assert!(stats.st_nlink == 1); // there links created, must be 1
assert_eq!(stats.st_nlink, 1); // there links created, must be 1
assert!(valid_uid_gid(stats)); // must be positive integers
assert!(stats.st_size == 0); // size is 0 because we did not write anything to the file
assert_eq!(stats.st_size, 0); // size is 0 because we did not write anything to the file
assert!(stats.st_blksize > 0); // must be positive integer, exact number machine dependent
assert!(stats.st_blocks <= 16); // Up to 16 blocks can be allocated for a blank file
}
Expand All @@ -63,8 +63,8 @@ fn assert_lstat_results(stat_result: Result<FileStat>) {
// st_mode is c_uint (u32 on Android) while S_IFMT is mode_t
// (u16 on Android), and that will be a compile error.
// On other platforms they are the same (either both are u16 or u32).
assert!((stats.st_mode as usize) & (S_IFMT as usize) == S_IFLNK as usize); // should be a link
assert!(stats.st_nlink == 1); // there links created, must be 1
assert_eq!((stats.st_mode as usize) & (S_IFMT as usize), S_IFLNK as usize); // should be a link
assert_eq!(stats.st_nlink, 1); // there links created, must be 1
assert!(valid_uid_gid(stats)); // must be positive integers
assert!(stats.st_size > 0); // size is > 0 because it points to another file
assert!(stats.st_blksize > 0); // must be positive integer, exact number machine dependent
Expand Down
Loading

0 comments on commit e8d237e

Please sign in to comment.