diff --git a/data_structures/HashMap/Hashing.dart b/data_structures/HashMap/Hashing.dart index 3a6ad40..59c5b6d 100644 --- a/data_structures/HashMap/Hashing.dart +++ b/data_structures/HashMap/Hashing.dart @@ -1,3 +1,5 @@ +import 'package:test/test.dart'; + //Author:Shawn //Email:stepfencurryxiao@gmail.com @@ -103,26 +105,22 @@ class HashMap { } void main() { - HashMap h = new HashMap(hsize: 7); - - print("Add key 5"); - h.insertHash(5); - - print("Add key 28"); - h.insertHash(28); - - print("Add key 1"); - h.insertHash(1); + test("Insert and Delete from HashMap", () { + HashMap h = new HashMap(hsize: 7); - print("Delete Key 28"); - h.deleteHash(28); + h.insertHash(5); + h.insertHash(28); + h.insertHash(1); - print("Print Table:\n"); - h.displayHashtable(); + expect(h.buckets[5].head?.data, equals(5)); + // 28 % 7 = 0 + expect(h.buckets[0].head?.data, equals(28)); + expect(h.buckets[1].head?.data, equals(1)); - print("Delete Key 1"); - h.deleteHash(1); + h.deleteHash(28); + expect(h.buckets[0].head, isNull); - print("Print Table:\n"); - h.displayHashtable(); + h.deleteHash(1); + expect(h.buckets[1].head, isNull); + }); } diff --git a/data_structures/Heap/Binary_Heap/Min_Heap.dart b/data_structures/Heap/Binary_Heap/Min_Heap.dart index ca6962a..3b56f6c 100644 --- a/data_structures/Heap/Binary_Heap/Min_Heap.dart +++ b/data_structures/Heap/Binary_Heap/Min_Heap.dart @@ -1,3 +1,5 @@ +import 'package:test/test.dart'; + //Author:Shawn //Email:stepfencurryxiao@gmail.com @@ -72,7 +74,9 @@ List buildHead(List arr, int length) { } void main() { - List arr = [1, 3, 0, 5, 4, 6, 7, 8, 9]; - List BinaryHeap = buildHead(arr, arr.length); - print(BinaryHeap); + test("buildHead creates a min heap", () { + List arr = [1, 3, 0, 5, 4, 6, 7, 8, 9]; + List binaryHeap = buildHead(arr, arr.length); + expect(binaryHeap, equals([0, 3, 1, 5, 4, 6, 7, 8, 9])); + }); } diff --git a/data_structures/Queue/List_Queue.dart b/data_structures/Queue/List_Queue.dart index ca7d8d5..6015375 100644 --- a/data_structures/Queue/List_Queue.dart +++ b/data_structures/Queue/List_Queue.dart @@ -1,3 +1,5 @@ +import 'package:test/test.dart'; + //Author:Shawn //Email:stepfencurryxiao@gmail.com @@ -36,25 +38,31 @@ class ListQueue { for (int i = 0; i < queue.length - 1; i++) { queue[i] = queue[i + 1]; } + count--; } return result; } } void main() { - ListQueue Queue = new ListQueue(); - Queue.enque(12); - Queue.enque(2); - Queue.enque(7); - print(Queue.queue); - print("Enqueue:"); - var returnData = Queue.deque(); - print("$returnData\n"); - print("Enqueue:"); - returnData = Queue.deque(); - print("$returnData\n"); - print("Enqueue:"); - returnData = Queue.deque(); - print("$returnData\n"); - print("Now the queue is: " + (Queue.queue).toString()); + test("Enqueue and deque elements", () { + ListQueue queue = new ListQueue(); + + // Note: The original hasElements returns true if queue.length > 0, + // but queue is initialized with length MAX_SIZE (10), so it always returns true. + expect(queue.hasElements(), isTrue); + + queue.enque(12); + queue.enque(2); + queue.enque(7); + + expect(queue.queue.sublist(0, 3), equals([12, 2, 7])); + + expect(queue.deque(), equals(12)); + expect(queue.deque(), equals(2)); + expect(queue.deque(), equals(7)); + + // Deque on empty + expect(queue.deque(), isNull); + }); } diff --git a/data_structures/Queue/Priority_Queue.dart b/data_structures/Queue/Priority_Queue.dart index 94ceffe..30a8978 100644 --- a/data_structures/Queue/Priority_Queue.dart +++ b/data_structures/Queue/Priority_Queue.dart @@ -1,3 +1,5 @@ +import 'package:test/test.dart'; + class PriorityQueue { List> _dataStore = >[]; @@ -62,14 +64,22 @@ class QueueItem { } void main() { - PriorityQueue queue = new PriorityQueue(); - queue.enqueue(1, 2); - queue.enqueue(2, 1); - queue.enqueue(3, 3); - queue.enqueue(4, 2); - - print(queue.dequeue()); - print(queue.dequeue()); - print(queue.dequeue()); - print(queue.dequeue()); + test("Enqueue and dequeue based on priority", () { + PriorityQueue queue = new PriorityQueue(); + queue.enqueue(1, 2); + queue.enqueue(2, 1); + queue.enqueue(3, 3); + queue.enqueue(4, 2); + + expect(queue.size, equals(4)); + expect(queue.front, equals(2)); // priority 1 + + expect(queue.dequeue(), equals(2)); + expect(queue.dequeue(), equals(1)); // priority 2, enqueued first + expect(queue.dequeue(), equals(4)); // priority 2 + expect(queue.dequeue(), equals(3)); // priority 3 + + expect(queue.isEmpty, isTrue); + expect(queue.dequeue(), isNull); + }); } diff --git a/data_structures/Stack/Array_Stack.dart b/data_structures/Stack/Array_Stack.dart index 09d218e..2d7136c 100644 --- a/data_structures/Stack/Array_Stack.dart +++ b/data_structures/Stack/Array_Stack.dart @@ -1,5 +1,4 @@ -import 'package:test/expect.dart'; -import 'package:test/scaffolding.dart'; +import 'package:test/test.dart'; class ArrayStack { /// [stack] @@ -45,50 +44,37 @@ class ArrayStack { } void main() { - ArrayStack arrayStack = new ArrayStack(6); - - arrayStack.push('1'); - arrayStack.push("2"); - arrayStack.push('3'); - arrayStack.push("4"); - arrayStack.push('5'); - arrayStack.push("6"); - - test('test case 1', () { - expect(arrayStack.stack, ['1', '2', '3', '4', '5', '6']); - }); - - test('test case 2: pop stack', () { - expect('6', arrayStack.pop()); - }); - - test('test case 3: pop stack', () { - expect('5', arrayStack.pop()); - }); - - test('test case 4: pop stack', () { - expect('4', arrayStack.pop()); + test('test case 1: push and get stack', () { + ArrayStack arrayStack = new ArrayStack(6); + arrayStack.push('1'); + arrayStack.push("2"); + arrayStack.push('3'); + arrayStack.push("4"); + arrayStack.push('5'); + arrayStack.push("6"); + expect(arrayStack.stack, equals(['1', '2', '3', '4', '5', '6'])); }); - test('test case 5: pop stack', () { - expect('3', arrayStack.pop()); + test('test case 2: pop stack items', () { + ArrayStack arrayStack = new ArrayStack(6); + arrayStack.push('1'); + arrayStack.push("2"); + arrayStack.push('3'); + arrayStack.push("4"); + arrayStack.push('5'); + arrayStack.push("6"); + + expect(arrayStack.pop(), equals('6')); + expect(arrayStack.pop(), equals('5')); + expect(arrayStack.pop(), equals('4')); + expect(arrayStack.pop(), equals('3')); + expect(arrayStack.pop(), equals('2')); + expect(arrayStack.pop(), equals('1')); + expect(arrayStack.pop(), isNull); }); - test('test case 6: pop stack', () { - expect('2', arrayStack.pop()); - }); - - test('test case 7: pop stack', () { - expect('1', arrayStack.pop()); - }); - - test('test case 8: pop stack', () { - expect(null, arrayStack.pop()); - }); - - ArrayStack arrayStack2 = new ArrayStack(3); - - test('test case 9', () { - expect(arrayStack2.stack, [null, null, null]); + test('test case 9: empty stack', () { + ArrayStack arrayStack2 = new ArrayStack(3); + expect(arrayStack2.stack, equals([null, null, null])); }); } diff --git a/data_structures/Stack/Linked_List_Stack.dart b/data_structures/Stack/Linked_List_Stack.dart index 1ce5844..69ac400 100644 --- a/data_structures/Stack/Linked_List_Stack.dart +++ b/data_structures/Stack/Linked_List_Stack.dart @@ -1,3 +1,5 @@ +import 'package:test/test.dart'; + //Author: Shawn //Email: stepfencurryxiao@gmail.com @@ -57,20 +59,27 @@ class LinkedListStack { } } -int main() { - LinkedListStack Stack = new LinkedListStack(); - var returnData; - print("Push 2 5 9 7 to the stack\n"); - Stack.push("2"); - Stack.push("5"); - Stack.push("9"); - Stack.push("7"); - print("Successful push!\n"); - returnData = Stack.pop(); - print("Pop a data: $returnData\n"); - returnData = Stack.pop(); - print("Pop a data: $returnData\n"); - returnData = Stack.pop(); - print("Pop a data: $returnData\n"); - return 0; +void main() { + test("Push and Pop elements from LinkedListStack", () { + LinkedListStack stack = new LinkedListStack(); + + expect(stack.isEmpty(), isTrue); + expect(stack.getSize(), equals(0)); + + stack.push("2"); + stack.push("5"); + stack.push("9"); + stack.push("7"); + + expect(stack.isEmpty(), isFalse); + expect(stack.getSize(), equals(4)); + + expect(stack.pop(), equals("7")); + expect(stack.pop(), equals("9")); + expect(stack.pop(), equals("5")); + expect(stack.pop(), equals("2")); + + expect(stack.isEmpty(), isTrue); + expect(stack.pop(), isNull); + }); } diff --git a/data_structures/binary_tree/basic_binary_tree.dart b/data_structures/binary_tree/basic_binary_tree.dart index e89398b..7eb5540 100644 --- a/data_structures/binary_tree/basic_binary_tree.dart +++ b/data_structures/binary_tree/basic_binary_tree.dart @@ -1,3 +1,5 @@ +import 'package:test/test.dart'; + //Author:Shawn //Email:stepfencurryxiao@gmail.com @@ -71,18 +73,25 @@ bool is_full_binary_tree(var tree) { //Main function for testing void main() { - var tree = Node(1); - tree.left = Node(2); - tree.right = Node(3); - tree.left.left = Node(4); - tree.left.right = Node(5); - tree.left.right.left = Node(6); - tree.right.left = Node(7); - tree.right.left.left = Node(8); - tree.right.left.left.right = Node(9); + test("Tree operations", () { + var tree = Node(1); + tree.left = Node(2); + tree.right = Node(3); + tree.left.left = Node(4); + tree.left.right = Node(5); + tree.left.right.left = Node(6); + tree.right.left = Node(7); + tree.right.left.left = Node(8); + tree.right.left.left.right = Node(9); + + expect(is_full_binary_tree(tree), isFalse); + expect(depth_of_tree(tree), equals(5.0)); + }); - print(is_full_binary_tree(tree)); - print(depth_of_tree(tree)); - print("Tree is:\n"); - display(tree); + test("is_full_binary_tree on full tree", () { + var fullTree = Node(1); + fullTree.left = Node(2); + fullTree.right = Node(3); + expect(is_full_binary_tree(fullTree), isTrue); + }); }