Skip to content

Commit 16fe3c8

Browse files
authored
Merge pull request #261 from akashgk/master
test: standardize data structures tests using package:test
2 parents 25843ac + 73d1329 commit 16fe3c8

7 files changed

Lines changed: 142 additions & 118 deletions

File tree

data_structures/HashMap/Hashing.dart

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:test/test.dart';
2+
13
//Author:Shawn
24
//Email:stepfencurryxiao@gmail.com
35

@@ -103,26 +105,22 @@ class HashMap {
103105
}
104106

105107
void main() {
106-
HashMap h = new HashMap(hsize: 7);
107-
108-
print("Add key 5");
109-
h.insertHash(5);
110-
111-
print("Add key 28");
112-
h.insertHash(28);
113-
114-
print("Add key 1");
115-
h.insertHash(1);
108+
test("Insert and Delete from HashMap", () {
109+
HashMap h = new HashMap(hsize: 7);
116110

117-
print("Delete Key 28");
118-
h.deleteHash(28);
111+
h.insertHash(5);
112+
h.insertHash(28);
113+
h.insertHash(1);
119114

120-
print("Print Table:\n");
121-
h.displayHashtable();
115+
expect(h.buckets[5].head?.data, equals(5));
116+
// 28 % 7 = 0
117+
expect(h.buckets[0].head?.data, equals(28));
118+
expect(h.buckets[1].head?.data, equals(1));
122119

123-
print("Delete Key 1");
124-
h.deleteHash(1);
120+
h.deleteHash(28);
121+
expect(h.buckets[0].head, isNull);
125122

126-
print("Print Table:\n");
127-
h.displayHashtable();
123+
h.deleteHash(1);
124+
expect(h.buckets[1].head, isNull);
125+
});
128126
}

data_structures/Heap/Binary_Heap/Min_Heap.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:test/test.dart';
2+
13
//Author:Shawn
24
//Email:stepfencurryxiao@gmail.com
35

@@ -72,7 +74,9 @@ List buildHead(List arr, int length) {
7274
}
7375

7476
void main() {
75-
List arr = [1, 3, 0, 5, 4, 6, 7, 8, 9];
76-
List BinaryHeap = buildHead(arr, arr.length);
77-
print(BinaryHeap);
77+
test("buildHead creates a min heap", () {
78+
List arr = [1, 3, 0, 5, 4, 6, 7, 8, 9];
79+
List binaryHeap = buildHead(arr, arr.length);
80+
expect(binaryHeap, equals([0, 3, 1, 5, 4, 6, 7, 8, 9]));
81+
});
7882
}

data_structures/Queue/List_Queue.dart

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:test/test.dart';
2+
13
//Author:Shawn
24
//Email:stepfencurryxiao@gmail.com
35

@@ -36,25 +38,31 @@ class ListQueue<T> {
3638
for (int i = 0; i < queue.length - 1; i++) {
3739
queue[i] = queue[i + 1];
3840
}
41+
count--;
3942
}
4043
return result;
4144
}
4245
}
4346

4447
void main() {
45-
ListQueue<int> Queue = new ListQueue<int>();
46-
Queue.enque(12);
47-
Queue.enque(2);
48-
Queue.enque(7);
49-
print(Queue.queue);
50-
print("Enqueue:");
51-
var returnData = Queue.deque();
52-
print("$returnData\n");
53-
print("Enqueue:");
54-
returnData = Queue.deque();
55-
print("$returnData\n");
56-
print("Enqueue:");
57-
returnData = Queue.deque();
58-
print("$returnData\n");
59-
print("Now the queue is: " + (Queue.queue).toString());
48+
test("Enqueue and deque elements", () {
49+
ListQueue<int> queue = new ListQueue<int>();
50+
51+
// Note: The original hasElements returns true if queue.length > 0,
52+
// but queue is initialized with length MAX_SIZE (10), so it always returns true.
53+
expect(queue.hasElements(), isTrue);
54+
55+
queue.enque(12);
56+
queue.enque(2);
57+
queue.enque(7);
58+
59+
expect(queue.queue.sublist(0, 3), equals([12, 2, 7]));
60+
61+
expect(queue.deque(), equals(12));
62+
expect(queue.deque(), equals(2));
63+
expect(queue.deque(), equals(7));
64+
65+
// Deque on empty
66+
expect(queue.deque(), isNull);
67+
});
6068
}

data_structures/Queue/Priority_Queue.dart

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:test/test.dart';
2+
13
class PriorityQueue<T> {
24
List<QueueItem<T>> _dataStore = <QueueItem<T>>[];
35

@@ -62,14 +64,22 @@ class QueueItem<T> {
6264
}
6365

6466
void main() {
65-
PriorityQueue<int> queue = new PriorityQueue();
66-
queue.enqueue(1, 2);
67-
queue.enqueue(2, 1);
68-
queue.enqueue(3, 3);
69-
queue.enqueue(4, 2);
70-
71-
print(queue.dequeue());
72-
print(queue.dequeue());
73-
print(queue.dequeue());
74-
print(queue.dequeue());
67+
test("Enqueue and dequeue based on priority", () {
68+
PriorityQueue<int> queue = new PriorityQueue();
69+
queue.enqueue(1, 2);
70+
queue.enqueue(2, 1);
71+
queue.enqueue(3, 3);
72+
queue.enqueue(4, 2);
73+
74+
expect(queue.size, equals(4));
75+
expect(queue.front, equals(2)); // priority 1
76+
77+
expect(queue.dequeue(), equals(2));
78+
expect(queue.dequeue(), equals(1)); // priority 2, enqueued first
79+
expect(queue.dequeue(), equals(4)); // priority 2
80+
expect(queue.dequeue(), equals(3)); // priority 3
81+
82+
expect(queue.isEmpty, isTrue);
83+
expect(queue.dequeue(), isNull);
84+
});
7585
}
Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import 'package:test/expect.dart';
2-
import 'package:test/scaffolding.dart';
1+
import 'package:test/test.dart';
32

43
class ArrayStack<T> {
54
/// [stack]
@@ -45,50 +44,37 @@ class ArrayStack<T> {
4544
}
4645

4746
void main() {
48-
ArrayStack<String> arrayStack = new ArrayStack<String>(6);
49-
50-
arrayStack.push('1');
51-
arrayStack.push("2");
52-
arrayStack.push('3');
53-
arrayStack.push("4");
54-
arrayStack.push('5');
55-
arrayStack.push("6");
56-
57-
test('test case 1', () {
58-
expect(arrayStack.stack, ['1', '2', '3', '4', '5', '6']);
59-
});
60-
61-
test('test case 2: pop stack', () {
62-
expect('6', arrayStack.pop());
63-
});
64-
65-
test('test case 3: pop stack', () {
66-
expect('5', arrayStack.pop());
67-
});
68-
69-
test('test case 4: pop stack', () {
70-
expect('4', arrayStack.pop());
47+
test('test case 1: push and get stack', () {
48+
ArrayStack<String> arrayStack = new ArrayStack<String>(6);
49+
arrayStack.push('1');
50+
arrayStack.push("2");
51+
arrayStack.push('3');
52+
arrayStack.push("4");
53+
arrayStack.push('5');
54+
arrayStack.push("6");
55+
expect(arrayStack.stack, equals(['1', '2', '3', '4', '5', '6']));
7156
});
7257

73-
test('test case 5: pop stack', () {
74-
expect('3', arrayStack.pop());
58+
test('test case 2: pop stack items', () {
59+
ArrayStack<String> arrayStack = new ArrayStack<String>(6);
60+
arrayStack.push('1');
61+
arrayStack.push("2");
62+
arrayStack.push('3');
63+
arrayStack.push("4");
64+
arrayStack.push('5');
65+
arrayStack.push("6");
66+
67+
expect(arrayStack.pop(), equals('6'));
68+
expect(arrayStack.pop(), equals('5'));
69+
expect(arrayStack.pop(), equals('4'));
70+
expect(arrayStack.pop(), equals('3'));
71+
expect(arrayStack.pop(), equals('2'));
72+
expect(arrayStack.pop(), equals('1'));
73+
expect(arrayStack.pop(), isNull);
7574
});
7675

77-
test('test case 6: pop stack', () {
78-
expect('2', arrayStack.pop());
79-
});
80-
81-
test('test case 7: pop stack', () {
82-
expect('1', arrayStack.pop());
83-
});
84-
85-
test('test case 8: pop stack', () {
86-
expect(null, arrayStack.pop());
87-
});
88-
89-
ArrayStack<String> arrayStack2 = new ArrayStack<String>(3);
90-
91-
test('test case 9', () {
92-
expect(arrayStack2.stack, [null, null, null]);
76+
test('test case 9: empty stack', () {
77+
ArrayStack<String> arrayStack2 = new ArrayStack<String>(3);
78+
expect(arrayStack2.stack, equals([null, null, null]));
9379
});
9480
}

data_structures/Stack/Linked_List_Stack.dart

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:test/test.dart';
2+
13
//Author: Shawn
24
//Email: stepfencurryxiao@gmail.com
35

@@ -57,20 +59,27 @@ class LinkedListStack<T> {
5759
}
5860
}
5961

60-
int main() {
61-
LinkedListStack<String> Stack = new LinkedListStack<String>();
62-
var returnData;
63-
print("Push 2 5 9 7 to the stack\n");
64-
Stack.push("2");
65-
Stack.push("5");
66-
Stack.push("9");
67-
Stack.push("7");
68-
print("Successful push!\n");
69-
returnData = Stack.pop();
70-
print("Pop a data: $returnData\n");
71-
returnData = Stack.pop();
72-
print("Pop a data: $returnData\n");
73-
returnData = Stack.pop();
74-
print("Pop a data: $returnData\n");
75-
return 0;
62+
void main() {
63+
test("Push and Pop elements from LinkedListStack", () {
64+
LinkedListStack<String> stack = new LinkedListStack<String>();
65+
66+
expect(stack.isEmpty(), isTrue);
67+
expect(stack.getSize(), equals(0));
68+
69+
stack.push("2");
70+
stack.push("5");
71+
stack.push("9");
72+
stack.push("7");
73+
74+
expect(stack.isEmpty(), isFalse);
75+
expect(stack.getSize(), equals(4));
76+
77+
expect(stack.pop(), equals("7"));
78+
expect(stack.pop(), equals("9"));
79+
expect(stack.pop(), equals("5"));
80+
expect(stack.pop(), equals("2"));
81+
82+
expect(stack.isEmpty(), isTrue);
83+
expect(stack.pop(), isNull);
84+
});
7685
}

data_structures/binary_tree/basic_binary_tree.dart

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:test/test.dart';
2+
13
//Author:Shawn
24
//Email:stepfencurryxiao@gmail.com
35

@@ -71,18 +73,25 @@ bool is_full_binary_tree(var tree) {
7173

7274
//Main function for testing
7375
void main() {
74-
var tree = Node(1);
75-
tree.left = Node(2);
76-
tree.right = Node(3);
77-
tree.left.left = Node(4);
78-
tree.left.right = Node(5);
79-
tree.left.right.left = Node(6);
80-
tree.right.left = Node(7);
81-
tree.right.left.left = Node(8);
82-
tree.right.left.left.right = Node(9);
76+
test("Tree operations", () {
77+
var tree = Node(1);
78+
tree.left = Node(2);
79+
tree.right = Node(3);
80+
tree.left.left = Node(4);
81+
tree.left.right = Node(5);
82+
tree.left.right.left = Node(6);
83+
tree.right.left = Node(7);
84+
tree.right.left.left = Node(8);
85+
tree.right.left.left.right = Node(9);
86+
87+
expect(is_full_binary_tree(tree), isFalse);
88+
expect(depth_of_tree(tree), equals(5.0));
89+
});
8390

84-
print(is_full_binary_tree(tree));
85-
print(depth_of_tree(tree));
86-
print("Tree is:\n");
87-
display(tree);
91+
test("is_full_binary_tree on full tree", () {
92+
var fullTree = Node(1);
93+
fullTree.left = Node(2);
94+
fullTree.right = Node(3);
95+
expect(is_full_binary_tree(fullTree), isTrue);
96+
});
8897
}

0 commit comments

Comments
 (0)