Added 2 tests to list.rs, improve semantic and using Default trait instant of function new

This commit is contained in:
Quentin Legot 2023-03-21 22:40:49 +01:00
parent d3b2d0bac6
commit b9c329219a
3 changed files with 54 additions and 20 deletions

View File

@ -26,7 +26,7 @@ impl Semaphore {
/// - *counter* initial value of counter
/// - *thread_manager* Thread manager which managing threads
pub fn new(counter: i32) -> Semaphore{
Semaphore { counter, waiting_queue: List::new()}
Semaphore { counter, waiting_queue: List::default() }
}
/// Decrement the value, and wait if it becomes < 0. Checking the
@ -98,7 +98,7 @@ impl Lock {
/// ### Parameters
/// - **thread_manager** Thread manager which managing threads
pub fn new() -> Lock {
Lock { owner: None, waiting_queue: List::new(), free: true }
Lock { owner: None, waiting_queue: List::default(), free: true }
}
/// Wait until the lock become free. Checking the
@ -196,7 +196,7 @@ impl Condition {
/// ### Parameters
/// - *thread_manager* Thread manager which managing threads
pub fn new() -> Condition {
Condition{ waiting_queue: List::new()}
Condition{ waiting_queue: List::default()}
}
/// Block the calling thread (put it in the wait queue).

View File

@ -28,8 +28,8 @@ impl ThreadManager {
Self {
g_current_thread: Option::None,
g_thread_to_be_destroyed: Option::None,
g_alive: List::new(),
ready_list: List::new(),
g_alive: List::default(),
ready_list: List::default(),
}
}

View File

@ -31,16 +31,11 @@ pub struct IterMut<'a, T> {
impl<T: PartialEq> List<T> {
/// Create an empty list
pub fn new() -> Self {
List { head: ptr::null_mut(), tail: ptr::null_mut() }
}
/// Push an item at the end of the list
pub fn push(&mut self, elem: T) {
unsafe {
let new_tail = Box::into_raw(Box::new(Node {
elem: elem,
elem,
next: ptr::null_mut(),
}));
@ -97,10 +92,12 @@ impl<T: PartialEq> List<T> {
/// Worst case complexity of this function is O(n)
pub fn contains(&self, elem: &T) -> bool {
let mut iter = self.iter();
let element = iter.next();
let mut element = iter.next();
while element.is_some() {
if element.unwrap() == elem {
return true;
} else {
element = iter.next();
}
}
false
@ -159,9 +156,16 @@ impl<T: PartialEq> List<T> {
}
}
impl<T: PartialEq> Default for List<T> {
/// Create an empty list
fn default() -> Self {
Self { head: ptr::null_mut(), tail: ptr::null_mut() }
}
}
impl<T: PartialEq> Drop for List<T> {
fn drop(&mut self) {
while let Some(_) = self.pop() {} // removing every item from list (necessary as we using unsafe function)
while self.pop().is_some() {} // removing every item from list (necessary as we using unsafe function)
}
}
@ -207,7 +211,7 @@ mod test {
#[test]
fn basics() {
let mut list = List::new();
let mut list = List::default();
// Check empty list behaves right
assert_eq!(list.pop(), None);
@ -236,7 +240,7 @@ mod test {
#[test]
fn peek() {
let mut list = List::new();
let mut list = List::default();
assert_eq!(list.peek(), None);
assert_eq!(list.peek_mut(), None);
list.push(1);
@ -249,7 +253,7 @@ mod test {
#[test]
fn into_iter() {
let mut list = List::new();
let mut list = List::default();
list.push(1);
list.push(2);
list.push(3);
@ -263,7 +267,7 @@ mod test {
#[test]
fn iter() {
let mut list = List::new();
let mut list = List::default();
list.push(1);
list.push(2);
list.push(3);
@ -276,8 +280,10 @@ mod test {
#[test]
fn iter_mut() {
let mut list = List::new();
list.push(1); list.push(2); list.push(3);
let mut list = List::default();
list.push(1);
list.push(2);
list.push(3);
let mut iter = list.iter_mut();
assert_eq!(iter.next(), Some(&mut 1));
@ -285,9 +291,37 @@ mod test {
assert_eq!(iter.next(), Some(&mut 3));
}
#[test]
fn contains_test() {
let mut list = List::default();
assert_eq!(list.peek(), None);
list.push(1);
list.push(2);
list.push(3);
assert_eq!(list.contains(&1), true);
assert_eq!(list.contains(&4), false);
}
#[test]
fn remove_test() {
let mut list = List::default();
assert_eq!(list.peek(), None);
list.push(1);
list.push(2);
list.push(3);
assert_eq!(list.contains(&2), true);
list.remove(&2);
assert_eq!(list.contains(&2), false);
assert_eq!(list.pop(), Option::Some(1));
assert_eq!(list.pop(), Option::Some(3));
assert_eq!(list.pop(), Option::None);
}
#[test]
fn miri_test() {
let mut list = List::new();
let mut list = List::default();
list.push(1);
list.push(2);