diff --git a/cadence/tests/scripts/uint64-linked-list/contains.cdc b/cadence/tests/scripts/uint64-linked-list/contains.cdc new file mode 100644 index 00000000..83d72679 --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/contains.cdc @@ -0,0 +1,13 @@ +import "UInt64LinkedList" + +/// Returns true if contains is accurate for present and absent ids. +access(all) fun main(): Bool { + let list <- UInt64LinkedList.createList() + list.insertAtHead(id: 10) + list.insertAtHead(id: 20) + let ok = list.contains(id: 10) + && list.contains(id: 20) + && !list.contains(id: 99) + destroy list + return ok +} diff --git a/cadence/tests/scripts/uint64-linked-list/empty_list_state.cdc b/cadence/tests/scripts/uint64-linked-list/empty_list_state.cdc new file mode 100644 index 00000000..d12745ac --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/empty_list_state.cdc @@ -0,0 +1,11 @@ +import "UInt64LinkedList" + +/// Returns true if a freshly created list has nil head, nil tail, and contains nothing. +access(all) fun main(): Bool { + let list <- UInt64LinkedList.createList() + let ok = list.head == nil + && list.tail == nil + && !list.contains(id: 1) + destroy list + return ok +} diff --git a/cadence/tests/scripts/uint64-linked-list/insert_duplicate_panics.cdc b/cadence/tests/scripts/uint64-linked-list/insert_duplicate_panics.cdc new file mode 100644 index 00000000..c5ead736 --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/insert_duplicate_panics.cdc @@ -0,0 +1,9 @@ +import "UInt64LinkedList" + +/// Expected to fail — inserting a duplicate id violates the pre-condition. +access(all) fun main() { + let list <- UInt64LinkedList.createList() + list.insertAtHead(id: 1) + list.insertAtHead(id: 1) + destroy list +} diff --git a/cadence/tests/scripts/uint64-linked-list/insert_multiple_order.cdc b/cadence/tests/scripts/uint64-linked-list/insert_multiple_order.cdc new file mode 100644 index 00000000..a41ca3dd --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/insert_multiple_order.cdc @@ -0,0 +1,13 @@ +import "UInt64LinkedList" + +/// Returns true if head is the most recently inserted element and tail is the oldest. +access(all) fun main(): Bool { + let list <- UInt64LinkedList.createList() + list.insertAtHead(id: 1) + list.insertAtHead(id: 2) + list.insertAtHead(id: 3) + // head = 3 (most recent), tail = 1 (oldest) + let ok = list.head == 3 && list.tail == 1 + destroy list + return ok +} diff --git a/cadence/tests/scripts/uint64-linked-list/insert_single.cdc b/cadence/tests/scripts/uint64-linked-list/insert_single.cdc new file mode 100644 index 00000000..e300d4e8 --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/insert_single.cdc @@ -0,0 +1,12 @@ +import "UInt64LinkedList" + +/// Returns true if a single inserted element becomes both head and tail. +access(all) fun main(): Bool { + let list <- UInt64LinkedList.createList() + list.insertAtHead(id: 42) + let ok = list.contains(id: 42) + && list.head == 42 + && list.tail == 42 + destroy list + return ok +} diff --git a/cadence/tests/scripts/uint64-linked-list/remove_absent.cdc b/cadence/tests/scripts/uint64-linked-list/remove_absent.cdc new file mode 100644 index 00000000..46e4eb7e --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/remove_absent.cdc @@ -0,0 +1,10 @@ +import "UInt64LinkedList" + +/// Returns true if removing a non-existent id returns false without panicking. +access(all) fun main(): Bool { + let list <- UInt64LinkedList.createList() + list.insertAtHead(id: 5) + let removed = list.remove(id: 999) + destroy list + return !removed +} diff --git a/cadence/tests/scripts/uint64-linked-list/remove_from_empty.cdc b/cadence/tests/scripts/uint64-linked-list/remove_from_empty.cdc new file mode 100644 index 00000000..d6c71a9f --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/remove_from_empty.cdc @@ -0,0 +1,9 @@ +import "UInt64LinkedList" + +/// Returns true if removing from an empty list returns false without panicking. +access(all) fun main(): Bool { + let list <- UInt64LinkedList.createList() + let removed = list.remove(id: 42) + destroy list + return !removed +} diff --git a/cadence/tests/scripts/uint64-linked-list/remove_head.cdc b/cadence/tests/scripts/uint64-linked-list/remove_head.cdc new file mode 100644 index 00000000..1292cb50 --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/remove_head.cdc @@ -0,0 +1,15 @@ +import "UInt64LinkedList" + +/// Returns true if removing the head promotes the next element to head. +access(all) fun main(): Bool { + let list <- UInt64LinkedList.createList() + list.insertAtHead(id: 1) + list.insertAtHead(id: 2) + list.insertAtHead(id: 3) + // list: 3 <-> 2 <-> 1 (head=3, tail=1) + let removed = list.remove(id: 3) + // list: 2 <-> 1 + let ok = removed && list.head == 2 && list.tail == 1 + destroy list + return ok +} diff --git a/cadence/tests/scripts/uint64-linked-list/remove_middle.cdc b/cadence/tests/scripts/uint64-linked-list/remove_middle.cdc new file mode 100644 index 00000000..cb80b9d9 --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/remove_middle.cdc @@ -0,0 +1,20 @@ +import "UInt64LinkedList" + +/// Returns true if removing a middle element re-links its neighbors correctly. +access(all) fun main(): Bool { + let list <- UInt64LinkedList.createList() + list.insertAtHead(id: 1) + list.insertAtHead(id: 2) + list.insertAtHead(id: 3) + // list: 3 <-> 2 <-> 1 + let removed = list.remove(id: 2) + // list: 3 <-> 1 + let ok = removed + && list.head == 3 + && list.tail == 1 + && !list.contains(id: 2) + && list.contains(id: 3) + && list.contains(id: 1) + destroy list + return ok +} diff --git a/cadence/tests/scripts/uint64-linked-list/remove_single.cdc b/cadence/tests/scripts/uint64-linked-list/remove_single.cdc new file mode 100644 index 00000000..31bd1c3f --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/remove_single.cdc @@ -0,0 +1,14 @@ +import "UInt64LinkedList" + +/// Returns true if removing the only element leaves an empty list. +access(all) fun main(): Bool { + let list <- UInt64LinkedList.createList() + list.insertAtHead(id: 7) + let removed = list.remove(id: 7) + let ok = removed + && list.head == nil + && list.tail == nil + && !list.contains(id: 7) + destroy list + return ok +} diff --git a/cadence/tests/scripts/uint64-linked-list/remove_tail.cdc b/cadence/tests/scripts/uint64-linked-list/remove_tail.cdc new file mode 100644 index 00000000..415d2500 --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/remove_tail.cdc @@ -0,0 +1,15 @@ +import "UInt64LinkedList" + +/// Returns true if removing the tail promotes the previous element to tail. +access(all) fun main(): Bool { + let list <- UInt64LinkedList.createList() + list.insertAtHead(id: 1) + list.insertAtHead(id: 2) + list.insertAtHead(id: 3) + // list: 3 <-> 2 <-> 1 (head=3, tail=1) + let removed = list.remove(id: 1) + // list: 3 <-> 2 + let ok = removed && list.head == 3 && list.tail == 2 + destroy list + return ok +} diff --git a/cadence/tests/scripts/uint64-linked-list/tail_walk_empty.cdc b/cadence/tests/scripts/uint64-linked-list/tail_walk_empty.cdc new file mode 100644 index 00000000..51e69ea1 --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/tail_walk_empty.cdc @@ -0,0 +1,9 @@ +import "UInt64LinkedList" + +/// Returns the length of tailWalk on an empty list — should be 0. +access(all) fun main(): Int { + let list <- UInt64LinkedList.createList() + let walked = list.tailWalk(limit: 5) + destroy list + return walked.length +} diff --git a/cadence/tests/scripts/uint64-linked-list/tail_walk_limit.cdc b/cadence/tests/scripts/uint64-linked-list/tail_walk_limit.cdc new file mode 100644 index 00000000..7719efc4 --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/tail_walk_limit.cdc @@ -0,0 +1,12 @@ +import "UInt64LinkedList" + +/// Returns tailWalk result capped to limit=2. +access(all) fun main(): [UInt64] { + let list <- UInt64LinkedList.createList() + list.insertAtHead(id: 1) + list.insertAtHead(id: 2) + list.insertAtHead(id: 3) + let walked = list.tailWalk(limit: 2) + destroy list + return walked +} diff --git a/cadence/tests/scripts/uint64-linked-list/tail_walk_order.cdc b/cadence/tests/scripts/uint64-linked-list/tail_walk_order.cdc new file mode 100644 index 00000000..b7e0abbe --- /dev/null +++ b/cadence/tests/scripts/uint64-linked-list/tail_walk_order.cdc @@ -0,0 +1,13 @@ +import "UInt64LinkedList" + +/// Returns ids from tailWalk — should be oldest-first (tail toward head). +access(all) fun main(): [UInt64] { + let list <- UInt64LinkedList.createList() + list.insertAtHead(id: 1) + list.insertAtHead(id: 2) + list.insertAtHead(id: 3) + // head=3, tail=1 → tailWalk yields [1, 2, 3] + let walked = list.tailWalk(limit: 10) + destroy list + return walked +} diff --git a/cadence/tests/uint64_linked_list_test.cdc b/cadence/tests/uint64_linked_list_test.cdc new file mode 100644 index 00000000..87848a20 --- /dev/null +++ b/cadence/tests/uint64_linked_list_test.cdc @@ -0,0 +1,102 @@ +import Test + +access(all) fun setup() { + let err = Test.deployContract( + name: "UInt64LinkedList", + path: "../contracts/UInt64LinkedList.cdc", + arguments: [] + ) + Test.expect(err, Test.beNil()) +} + +access(all) +fun executeScript(_ path: String, _ args: [AnyStruct]): Test.ScriptResult { + return Test.executeScript(Test.readFile(path), args) +} + +// ─── Tests ─────────────────────────────────────────────────────────────────── + +access(all) fun test_EmptyListState() { + let res = executeScript("./scripts/uint64-linked-list/empty_list_state.cdc", []) + Test.expect(res, Test.beSucceeded()) + Test.assertEqual(true, res.returnValue as! Bool) +} + +access(all) fun test_InsertSingle() { + let res = executeScript("./scripts/uint64-linked-list/insert_single.cdc", []) + Test.expect(res, Test.beSucceeded()) + Test.assertEqual(true, res.returnValue as! Bool) +} + +access(all) fun test_InsertMultiple_HeadAndTailOrder() { + let res = executeScript("./scripts/uint64-linked-list/insert_multiple_order.cdc", []) + Test.expect(res, Test.beSucceeded()) + Test.assertEqual(true, res.returnValue as! Bool) +} + +access(all) fun test_Contains() { + let res = executeScript("./scripts/uint64-linked-list/contains.cdc", []) + Test.expect(res, Test.beSucceeded()) + Test.assertEqual(true, res.returnValue as! Bool) +} + +access(all) fun test_RemoveSingle() { + let res = executeScript("./scripts/uint64-linked-list/remove_single.cdc", []) + Test.expect(res, Test.beSucceeded()) + Test.assertEqual(true, res.returnValue as! Bool) +} + +access(all) fun test_RemoveHead() { + let res = executeScript("./scripts/uint64-linked-list/remove_head.cdc", []) + Test.expect(res, Test.beSucceeded()) + Test.assertEqual(true, res.returnValue as! Bool) +} + +access(all) fun test_RemoveTail() { + let res = executeScript("./scripts/uint64-linked-list/remove_tail.cdc", []) + Test.expect(res, Test.beSucceeded()) + Test.assertEqual(true, res.returnValue as! Bool) +} + +access(all) fun test_RemoveMiddle() { + let res = executeScript("./scripts/uint64-linked-list/remove_middle.cdc", []) + Test.expect(res, Test.beSucceeded()) + Test.assertEqual(true, res.returnValue as! Bool) +} + +access(all) fun test_RemoveAbsent() { + let res = executeScript("./scripts/uint64-linked-list/remove_absent.cdc", []) + Test.expect(res, Test.beSucceeded()) + Test.assertEqual(true, res.returnValue as! Bool) +} + +access(all) fun test_RemoveFromEmpty() { + let res = executeScript("./scripts/uint64-linked-list/remove_from_empty.cdc", []) + Test.expect(res, Test.beSucceeded()) + Test.assertEqual(true, res.returnValue as! Bool) +} + +access(all) fun test_TailWalk_Order() { + let res = executeScript("./scripts/uint64-linked-list/tail_walk_order.cdc", []) + Test.expect(res, Test.beSucceeded()) + let expected: [UInt64] = [1, 2, 3] + Test.assertEqual(expected, res.returnValue as! [UInt64]) +} + +access(all) fun test_TailWalk_Limit() { + let res = executeScript("./scripts/uint64-linked-list/tail_walk_limit.cdc", []) + Test.expect(res, Test.beSucceeded()) + let walked = res.returnValue as! [UInt64] + Test.assertEqual(2, walked.length) +} + +access(all) fun test_TailWalk_Empty() { + let res = executeScript("./scripts/uint64-linked-list/tail_walk_empty.cdc", []) + Test.expect(res, Test.beSucceeded()) + Test.assertEqual(0, res.returnValue as! Int) +} + +access(all) fun test_InsertDuplicate_Panics() { + let res = executeScript("./scripts/uint64-linked-list/insert_duplicate_panics.cdc", []) + Test.expect(res, Test.beFailed()) +}