Skip to content

Commit 4c5e4e7

Browse files
committed
Add unit tests for UInt64LinkedList contract
1 parent e880797 commit 4c5e4e7

15 files changed

Lines changed: 277 additions & 0 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import "UInt64LinkedList"
2+
3+
/// Returns true if contains is accurate for present and absent ids.
4+
access(all) fun main(): Bool {
5+
let list <- UInt64LinkedList.createList()
6+
list.insertAtHead(id: 10)
7+
list.insertAtHead(id: 20)
8+
let ok = list.contains(id: 10)
9+
&& list.contains(id: 20)
10+
&& !list.contains(id: 99)
11+
destroy list
12+
return ok
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import "UInt64LinkedList"
2+
3+
/// Returns true if a freshly created list has nil head, nil tail, and contains nothing.
4+
access(all) fun main(): Bool {
5+
let list <- UInt64LinkedList.createList()
6+
let ok = list.head == nil
7+
&& list.tail == nil
8+
&& !list.contains(id: 1)
9+
destroy list
10+
return ok
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import "UInt64LinkedList"
2+
3+
/// Expected to fail — inserting a duplicate id violates the pre-condition.
4+
access(all) fun main() {
5+
let list <- UInt64LinkedList.createList()
6+
list.insertAtHead(id: 1)
7+
list.insertAtHead(id: 1)
8+
destroy list
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import "UInt64LinkedList"
2+
3+
/// Returns true if head is the most recently inserted element and tail is the oldest.
4+
access(all) fun main(): Bool {
5+
let list <- UInt64LinkedList.createList()
6+
list.insertAtHead(id: 1)
7+
list.insertAtHead(id: 2)
8+
list.insertAtHead(id: 3)
9+
// head = 3 (most recent), tail = 1 (oldest)
10+
let ok = list.head == 3 && list.tail == 1
11+
destroy list
12+
return ok
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import "UInt64LinkedList"
2+
3+
/// Returns true if a single inserted element becomes both head and tail.
4+
access(all) fun main(): Bool {
5+
let list <- UInt64LinkedList.createList()
6+
list.insertAtHead(id: 42)
7+
let ok = list.contains(id: 42)
8+
&& list.head == 42
9+
&& list.tail == 42
10+
destroy list
11+
return ok
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import "UInt64LinkedList"
2+
3+
/// Returns true if removing a non-existent id returns false without panicking.
4+
access(all) fun main(): Bool {
5+
let list <- UInt64LinkedList.createList()
6+
list.insertAtHead(id: 5)
7+
let removed = list.remove(id: 999)
8+
destroy list
9+
return !removed
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import "UInt64LinkedList"
2+
3+
/// Returns true if removing from an empty list returns false without panicking.
4+
access(all) fun main(): Bool {
5+
let list <- UInt64LinkedList.createList()
6+
let removed = list.remove(id: 42)
7+
destroy list
8+
return !removed
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import "UInt64LinkedList"
2+
3+
/// Returns true if removing the head promotes the next element to head.
4+
access(all) fun main(): Bool {
5+
let list <- UInt64LinkedList.createList()
6+
list.insertAtHead(id: 1)
7+
list.insertAtHead(id: 2)
8+
list.insertAtHead(id: 3)
9+
// list: 3 <-> 2 <-> 1 (head=3, tail=1)
10+
let removed = list.remove(id: 3)
11+
// list: 2 <-> 1
12+
let ok = removed && list.head == 2 && list.tail == 1
13+
destroy list
14+
return ok
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import "UInt64LinkedList"
2+
3+
/// Returns true if removing a middle element re-links its neighbors correctly.
4+
access(all) fun main(): Bool {
5+
let list <- UInt64LinkedList.createList()
6+
list.insertAtHead(id: 1)
7+
list.insertAtHead(id: 2)
8+
list.insertAtHead(id: 3)
9+
// list: 3 <-> 2 <-> 1
10+
let removed = list.remove(id: 2)
11+
// list: 3 <-> 1
12+
let ok = removed
13+
&& list.head == 3
14+
&& list.tail == 1
15+
&& !list.contains(id: 2)
16+
&& list.contains(id: 3)
17+
&& list.contains(id: 1)
18+
destroy list
19+
return ok
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import "UInt64LinkedList"
2+
3+
/// Returns true if removing the only element leaves an empty list.
4+
access(all) fun main(): Bool {
5+
let list <- UInt64LinkedList.createList()
6+
list.insertAtHead(id: 7)
7+
let removed = list.remove(id: 7)
8+
let ok = removed
9+
&& list.head == nil
10+
&& list.tail == nil
11+
&& !list.contains(id: 7)
12+
destroy list
13+
return ok
14+
}

0 commit comments

Comments
 (0)