Skip to content

Commit

Permalink
refactor: move tests to the test dir (#2257)
Browse files Browse the repository at this point in the history
* refactor: move tests to the test dir

* try fixing it

* try fixing it

* fix Redox and FreeBSD

* document it
  • Loading branch information
SteveLauC authored Dec 11, 2023
1 parent 26b070a commit c0d8ad4
Show file tree
Hide file tree
Showing 30 changed files with 1,089 additions and 1,114 deletions.
7 changes: 7 additions & 0 deletions CONVENTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,10 @@ cases, it will be the version of the next release. And a user-friendly note
should be added. Normally, there should be a new interface that will replace
the old one, so a note should be something like: "`<New Interface>` should be
used instead".

## Where to put a test

If you want to add a test for a feature that is in `xxx.rs`, then the test should
be put in the corresponding `test_xxx.rs` file unless you cannot do this, e.g.,
the test involves private stuff and thus cannot be added outside of Nix, then
it is allowed to leave the test in `xxx.rs`.
53 changes: 0 additions & 53 deletions src/sys/aio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,56 +1191,3 @@ pub fn lio_listio(
})
.map(drop)
}

#[cfg(test)]
mod t {
use super::*;

/// aio_suspend relies on casting Rust Aio* struct pointers to libc::aiocb
/// pointers. This test ensures that such casts are valid.
#[test]
fn casting() {
let sev = SigevNotify::SigevNone;
let aiof = AioFsync::new(666, AioFsyncMode::O_SYNC, 0, sev);
assert_eq!(
aiof.as_ref() as *const libc::aiocb,
&aiof as *const AioFsync as *const libc::aiocb
);

let mut rbuf = [];
let aior = AioRead::new(666, 0, &mut rbuf, 0, sev);
assert_eq!(
aior.as_ref() as *const libc::aiocb,
&aior as *const AioRead as *const libc::aiocb
);

let wbuf = [];
let aiow = AioWrite::new(666, 0, &wbuf, 0, sev);
assert_eq!(
aiow.as_ref() as *const libc::aiocb,
&aiow as *const AioWrite as *const libc::aiocb
);
}

#[cfg(target_os = "freebsd")]
#[test]
fn casting_vectored() {
let sev = SigevNotify::SigevNone;

let mut rbuf = [];
let mut rbufs = [IoSliceMut::new(&mut rbuf)];
let aiorv = AioReadv::new(666, 0, &mut rbufs[..], 0, sev);
assert_eq!(
aiorv.as_ref() as *const libc::aiocb,
&aiorv as *const AioReadv as *const libc::aiocb
);

let wbuf = [];
let wbufs = [IoSlice::new(&wbuf)];
let aiowv = AioWritev::new(666, 0, &wbufs, 0, sev);
assert_eq!(
aiowv.as_ref() as *const libc::aiocb,
&aiowv as *const AioWritev as *const libc::aiocb
);
}
}
39 changes: 0 additions & 39 deletions src/sys/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,42 +441,3 @@ pub fn ev_set(
ev.kevent.data = 0;
ev.kevent.udata = udata as type_of_udata;
}

#[test]
fn test_struct_kevent() {
use std::mem;

let udata: intptr_t = 12345;

let actual = KEvent::new(
0xdead_beef,
EventFilter::EVFILT_READ,
EventFlag::EV_ONESHOT | EventFlag::EV_ADD,
FilterFlag::NOTE_CHILD | FilterFlag::NOTE_EXIT,
0x1337,
udata,
);
assert_eq!(0xdead_beef, actual.ident());
let filter = actual.kevent.filter;
assert_eq!(libc::EVFILT_READ, filter);
assert_eq!(libc::EV_ONESHOT | libc::EV_ADD, actual.flags().bits());
assert_eq!(libc::NOTE_CHILD | libc::NOTE_EXIT, actual.fflags().bits());
assert_eq!(0x1337, actual.data());
assert_eq!(udata as type_of_udata, actual.udata() as type_of_udata);
assert_eq!(mem::size_of::<libc::kevent>(), mem::size_of::<KEvent>());
}

#[test]
fn test_kevent_filter() {
let udata: intptr_t = 12345;

let actual = KEvent::new(
0xdead_beef,
EventFilter::EVFILT_READ,
EventFlag::EV_ONESHOT | EventFlag::EV_ADD,
FilterFlag::NOTE_CHILD | FilterFlag::NOTE_EXIT,
0x1337,
udata,
);
assert_eq!(EventFilter::EVFILT_READ, actual.filter().unwrap());
}
25 changes: 0 additions & 25 deletions src/sys/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,28 +396,3 @@ pub fn getrusage(who: UsageWho) -> Result<Usage> {
Errno::result(res).map(|_| Usage(rusage.assume_init()))
}
}

#[cfg(test)]
mod test {
use super::{getrusage, UsageWho};

#[test]
pub fn test_self_cpu_time() {
// Make sure some CPU time is used.
let mut numbers: Vec<i32> = (1..1_000_000).collect();
numbers.iter_mut().for_each(|item| *item *= 2);

// FIXME: this is here to help ensure the compiler does not optimize the whole
// thing away. Replace the assert with test::black_box once stabilized.
assert_eq!(numbers[100..200].iter().sum::<i32>(), 30_100);

let usage = getrusage(UsageWho::RUSAGE_SELF)
.expect("Failed to call getrusage for SELF");
let rusage = usage.as_ref();

let user = usage.user_time();
assert!(user.tv_sec() > 0 || user.tv_usec() > 0);
assert_eq!(user.tv_sec(), rusage.ru_utime.tv_sec);
assert_eq!(user.tv_usec(), rusage.ru_utime.tv_usec);
}
}
224 changes: 0 additions & 224 deletions src/sys/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,227 +317,3 @@ where
Errno::result(res)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::sys::time::{TimeVal, TimeValLike};
use crate::unistd::{pipe, write};
use std::os::unix::io::{AsFd, RawFd};

#[test]
fn fdset_insert() {
let mut fd_set = FdSet::new();

for i in 0..FD_SETSIZE {
let borrowed_i = unsafe { BorrowedFd::borrow_raw(i as RawFd) };
assert!(!fd_set.contains(borrowed_i));
}

let fd_seven = unsafe { BorrowedFd::borrow_raw(7) };
fd_set.insert(fd_seven);

assert!(fd_set.contains(fd_seven));
}

#[test]
fn fdset_remove() {
let mut fd_set = FdSet::new();

for i in 0..FD_SETSIZE {
let borrowed_i = unsafe { BorrowedFd::borrow_raw(i as RawFd) };
assert!(!fd_set.contains(borrowed_i));
}

let fd_seven = unsafe { BorrowedFd::borrow_raw(7) };
fd_set.insert(fd_seven);
fd_set.remove(fd_seven);

for i in 0..FD_SETSIZE {
let borrowed_i = unsafe { BorrowedFd::borrow_raw(i as RawFd) };
assert!(!fd_set.contains(borrowed_i));
}
}

#[test]
#[allow(non_snake_case)]
fn fdset_clear() {
let mut fd_set = FdSet::new();
let fd_one = unsafe { BorrowedFd::borrow_raw(1) };
let fd_FD_SETSIZE_divided_by_two =
unsafe { BorrowedFd::borrow_raw((FD_SETSIZE / 2) as RawFd) };
let fd_FD_SETSIZE_minus_one =
unsafe { BorrowedFd::borrow_raw((FD_SETSIZE - 1) as RawFd) };
fd_set.insert(fd_one);
fd_set.insert(fd_FD_SETSIZE_divided_by_two);
fd_set.insert(fd_FD_SETSIZE_minus_one);

fd_set.clear();

for i in 0..FD_SETSIZE {
let borrowed_i = unsafe { BorrowedFd::borrow_raw(i as RawFd) };
assert!(!fd_set.contains(borrowed_i));
}
}

#[test]
fn fdset_highest() {
let mut set = FdSet::new();
assert_eq!(
set.highest().map(|borrowed_fd| borrowed_fd.as_raw_fd()),
None
);
let fd_zero = unsafe { BorrowedFd::borrow_raw(0) };
let fd_ninety = unsafe { BorrowedFd::borrow_raw(90) };
set.insert(fd_zero);
assert_eq!(
set.highest().map(|borrowed_fd| borrowed_fd.as_raw_fd()),
Some(0)
);
set.insert(fd_ninety);
assert_eq!(
set.highest().map(|borrowed_fd| borrowed_fd.as_raw_fd()),
Some(90)
);
set.remove(fd_zero);
assert_eq!(
set.highest().map(|borrowed_fd| borrowed_fd.as_raw_fd()),
Some(90)
);
set.remove(fd_ninety);
assert_eq!(
set.highest().map(|borrowed_fd| borrowed_fd.as_raw_fd()),
None
);

let fd_four = unsafe { BorrowedFd::borrow_raw(4) };
let fd_five = unsafe { BorrowedFd::borrow_raw(5) };
let fd_seven = unsafe { BorrowedFd::borrow_raw(7) };
set.insert(fd_four);
set.insert(fd_five);
set.insert(fd_seven);
assert_eq!(
set.highest().map(|borrowed_fd| borrowed_fd.as_raw_fd()),
Some(7)
);
}

#[test]
fn fdset_fds() {
let mut set = FdSet::new();
let fd_zero = unsafe { BorrowedFd::borrow_raw(0) };
let fd_ninety = unsafe { BorrowedFd::borrow_raw(90) };
assert_eq!(
set.fds(None)
.map(|borrowed_fd| borrowed_fd.as_raw_fd())
.collect::<Vec<_>>(),
vec![]
);
set.insert(fd_zero);
assert_eq!(
set.fds(None)
.map(|borrowed_fd| borrowed_fd.as_raw_fd())
.collect::<Vec<_>>(),
vec![0]
);
set.insert(fd_ninety);
assert_eq!(
set.fds(None)
.map(|borrowed_fd| borrowed_fd.as_raw_fd())
.collect::<Vec<_>>(),
vec![0, 90]
);

// highest limit
assert_eq!(
set.fds(Some(89))
.map(|borrowed_fd| borrowed_fd.as_raw_fd())
.collect::<Vec<_>>(),
vec![0]
);
assert_eq!(
set.fds(Some(90))
.map(|borrowed_fd| borrowed_fd.as_raw_fd())
.collect::<Vec<_>>(),
vec![0, 90]
);
}

#[test]
fn test_select() {
let (r1, w1) = pipe().unwrap();
let (r2, _w2) = pipe().unwrap();

write(&w1, b"hi!").unwrap();
let mut fd_set = FdSet::new();
fd_set.insert(r1.as_fd());
fd_set.insert(r2.as_fd());

let mut timeout = TimeVal::seconds(10);
assert_eq!(
1,
select(None, &mut fd_set, None, None, &mut timeout).unwrap()
);
assert!(fd_set.contains(r1.as_fd()));
assert!(!fd_set.contains(r2.as_fd()));
}

#[test]
fn test_select_nfds() {
let (r1, w1) = pipe().unwrap();
let (r2, _w2) = pipe().unwrap();

write(&w1, b"hi!").unwrap();
let mut fd_set = FdSet::new();
fd_set.insert(r1.as_fd());
fd_set.insert(r2.as_fd());

let mut timeout = TimeVal::seconds(10);
{
assert_eq!(
1,
select(
Some(
fd_set
.highest()
.map(|borrowed_fd| borrowed_fd.as_raw_fd())
.unwrap()
+ 1
),
&mut fd_set,
None,
None,
&mut timeout
)
.unwrap()
);
}
assert!(fd_set.contains(r1.as_fd()));
assert!(!fd_set.contains(r2.as_fd()));
}

#[test]
fn test_select_nfds2() {
let (r1, w1) = pipe().unwrap();
write(&w1, b"hi!").unwrap();
let (r2, _w2) = pipe().unwrap();
let mut fd_set = FdSet::new();
fd_set.insert(r1.as_fd());
fd_set.insert(r2.as_fd());

let mut timeout = TimeVal::seconds(10);
assert_eq!(
1,
select(
std::cmp::max(r1.as_raw_fd(), r2.as_raw_fd()) + 1,
&mut fd_set,
None,
None,
&mut timeout
)
.unwrap()
);
assert!(fd_set.contains(r1.as_fd()));
assert!(!fd_set.contains(r2.as_fd()));
}
}
Loading

0 comments on commit c0d8ad4

Please sign in to comment.