aboutsummaryrefslogtreecommitdiff
path: root/rust/simple-linked-list/tests
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2020-12-26 17:48:38 +0000
committerFederico Igne <git@federicoigne.com>2021-11-03 18:55:08 +0000
commit02481656966b0a8dfc95cf3c22bcc049660ff7d4 (patch)
tree8e39798fcaf27931d91c2088423fd4e97adcfc2d /rust/simple-linked-list/tests
parent4e2052c4d792540c2f742b2c2a081b11117ed41d (diff)
downloadexercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.tar.gz
exercism-02481656966b0a8dfc95cf3c22bcc049660ff7d4.zip
Move Rust exercises in a subdirectory
Diffstat (limited to 'rust/simple-linked-list/tests')
-rw-r--r--rust/simple-linked-list/tests/simple-linked-list.rs118
1 files changed, 118 insertions, 0 deletions
diff --git a/rust/simple-linked-list/tests/simple-linked-list.rs b/rust/simple-linked-list/tests/simple-linked-list.rs
new file mode 100644
index 0000000..c89f8b5
--- /dev/null
+++ b/rust/simple-linked-list/tests/simple-linked-list.rs
@@ -0,0 +1,118 @@
1use simple_linked_list::SimpleLinkedList;
2
3#[test]
4fn test_new_list_is_empty() {
5 let list: SimpleLinkedList<u32> = SimpleLinkedList::new();
6 assert_eq!(list.len(), 0, "list's length must be 0");
7}
8
9#[test]
10fn test_push_increments_length() {
11 let mut list: SimpleLinkedList<u32> = SimpleLinkedList::new();
12 list.push(1);
13 assert_eq!(list.len(), 1, "list's length must be 1");
14 list.push(2);
15 assert_eq!(list.len(), 2, "list's length must be 2");
16}
17
18#[test]
19fn test_pop_decrements_length() {
20 let mut list: SimpleLinkedList<u32> = SimpleLinkedList::new();
21 list.push(1);
22 list.push(2);
23 list.pop();
24 assert_eq!(list.len(), 1, "list's length must be 1");
25 list.pop();
26 assert_eq!(list.len(), 0, "list's length must be 0");
27}
28
29#[test]
30fn test_is_empty() {
31 let mut list: SimpleLinkedList<u32> = SimpleLinkedList::new();
32 assert!(list.is_empty(), "List wasn't empty on creation");
33 for inserts in 0..100 {
34 for i in 0..inserts {
35 list.push(i);
36 assert!(
37 !list.is_empty(),
38 "List was empty after having inserted {}/{} elements",
39 i,
40 inserts
41 );
42 }
43 for i in 0..inserts {
44 assert!(
45 !list.is_empty(),
46 "List was empty before removing {}/{} elements",
47 i,
48 inserts
49 );
50 list.pop();
51 }
52 assert!(
53 list.is_empty(),
54 "List wasn't empty after having removed {} elements",
55 inserts
56 );
57 }
58}
59
60#[test]
61fn test_pop_returns_head_element_and_removes_it() {
62 let mut list: SimpleLinkedList<u32> = SimpleLinkedList::new();
63 list.push(1);
64 list.push(2);
65 assert_eq!(list.pop(), Some(2), "Element must be 2");
66 assert_eq!(list.pop(), Some(1), "Element must be 1");
67 assert_eq!(list.pop(), None, "No element should be contained in list");
68}
69
70#[test]
71fn test_peek_returns_reference_to_head_element_but_does_not_remove_it() {
72 let mut list: SimpleLinkedList<u32> = SimpleLinkedList::new();
73 assert_eq!(list.peek(), None, "No element should be contained in list");
74 list.push(2);
75 assert_eq!(list.peek(), Some(&2), "Element must be 2");
76 assert_eq!(list.peek(), Some(&2), "Element must be still 2");
77 list.push(3);
78 assert_eq!(list.peek(), Some(&3), "Head element is now 3");
79 assert_eq!(list.pop(), Some(3), "Element must be 3");
80 assert_eq!(list.peek(), Some(&2), "Head element is now 2");
81 assert_eq!(list.pop(), Some(2), "Element must be 2");
82 assert_eq!(list.peek(), None, "No element should be contained in list");
83}
84
85#[test]
86fn test_from_slice() {
87 let mut array = vec!["1", "2", "3", "4"];
88 let mut list: SimpleLinkedList<_> = array.drain(..).collect();
89 assert_eq!(list.pop(), Some("4"));
90 assert_eq!(list.pop(), Some("3"));
91 assert_eq!(list.pop(), Some("2"));
92 assert_eq!(list.pop(), Some("1"));
93}
94
95#[test]
96fn test_reverse() {
97 let mut list: SimpleLinkedList<u32> = SimpleLinkedList::new();
98 list.push(1);
99 list.push(2);
100 list.push(3);
101 let mut rev_list = list.rev();
102 assert_eq!(rev_list.pop(), Some(1));
103 assert_eq!(rev_list.pop(), Some(2));
104 assert_eq!(rev_list.pop(), Some(3));
105 assert_eq!(rev_list.pop(), None);
106}
107
108#[test]
109fn test_into_vector() {
110 let mut v = Vec::new();
111 let mut s = SimpleLinkedList::new();
112 for i in 1..4 {
113 v.push(i);
114 s.push(i);
115 }
116 let s_as_vec: Vec<i32> = s.into();
117 assert_eq!(v, s_as_vec);
118}