Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Tests Coverage - First Batch #229

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions data_structures/binary_tree/basic_binary_tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Node {
}

/*In order traversal of the tree*/
void display(var tree) {
void display(Node tree) {
if (tree == null) {
return;
}
Expand All @@ -39,7 +39,7 @@ void display(var tree) {
*This is the recursive function to find the depth of
* binary tree.
*/
double depth_of_tree(var tree) {
double depth_of_tree(Node tree) {
if (tree == null) {
return 0;
} else {
Expand All @@ -55,7 +55,7 @@ double depth_of_tree(var tree) {
}

/*This function returns that is it full binary tree or not*/
bool is_full_binary_tree(var tree) {
bool is_full_binary_tree(Node tree) {
if (tree == null) {
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ repository: https://github.com/TheAlgorithms/Dart
dependencies:
test: ^1.15.4
coverage: ^1.6.0
welltested_annotation: ^1.0.1

dev_dependencies:
stack: ^0.2.1
build_runner:
mockito:

environment:
sdk: ">=2.10.0 <3.0.0"
1 change: 1 addition & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Populate other dart test files here...
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'package:test/test.dart';
import '../../../../../data_structures/Heap/Binary_Heap/Max_heap.dart';

void main() {
group('MaxHeap buildHeap tests', () {
test('Test case 1: buildHeap with a valid array', () {
List<int> array = [48, 12, 24, 7, 8, -5, 24, 391, 24, 56, 2, 6, 8, 41];
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap(array);

expect(maxHeap.heap,
equals([391, 56, 41, 24, 48, 8, 24, 7, 12, 24, 2, 6, -5, 8]));
});

test(
'Test case 2: buildHeap with a valid array containing negative numbers',
() {
List<int> array = [
-7,
2,
3,
8,
-10,
4,
-6,
-10,
-2,
-7,
10,
5,
2,
9,
-9,
-5,
3,
8
];
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap(array);

expect(
maxHeap.heap,
equals([
10,
9,
8,
8,
5,
4,
3,
-2,
-7,
-7,
3,
-10,
2,
-6,
-9,
-5,
-10,
2
]));
});

test('Test case 3: buildHeap with an empty array', () {
List<int> array = [];
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap(array);

expect(maxHeap.heap, equals([]));
});

test('Test case 4: buildHeap with a single element array', () {
List<int> array = [5];
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap(array);

expect(maxHeap.heap, equals([5]));
});

test('Test case 5: buildHeap with a sorted array', () {
List<int> array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap(array);

expect(maxHeap.heap, equals([9, 5, 8, 4, 2, 6, 7, 1, 3]));
});

test('Test case 6: buildHeap with a reverse sorted array', () {
List<int> array = [9, 8, 7, 6, 5, 4, 3, 2, 1];
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap(array);

expect(maxHeap.heap, equals([9, 8, 7, 6, 5, 4, 3, 2, 1]));
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:test/test.dart';
import '../../../../../data_structures/Heap/Binary_Heap/Max_heap.dart';

void main() {
group('MaxHeap insert tests', () {
test('Test case 1: Inserting into an empty heap', () {
MaxHeap maxHeap = new MaxHeap();
maxHeap.insert(5);
expect(maxHeap.peek(), equals(5));
});

test('Test case 2: Inserting into a non-empty heap', () {
MaxHeap maxHeap = new MaxHeap();
List<int> array = [48, 12, 24, 7, 8, -5, 24, 391, 24, 56, 2, 6, 8, 41];
maxHeap.buildHeap(array);
maxHeap.insert(100);
expect(maxHeap.peek(), equals(391));
});

test('Test case 3: Inserting a value smaller than the current max', () {
MaxHeap maxHeap = new MaxHeap();
List<int> array = [48, 12, 24, 7, 8, -5, 24, 391, 24, 56, 2, 6, 8, 41];
maxHeap.buildHeap(array);
maxHeap.insert(50);
expect(maxHeap.peek(), equals(391));
});

test('Test case 4: Inserting a value larger than the current max', () {
MaxHeap maxHeap = new MaxHeap();
List<int> array = [48, 12, 24, 7, 8, -5, 24, 391, 24, 56, 2, 6, 8, 41];
maxHeap.buildHeap(array);
maxHeap.insert(500);
expect(maxHeap.peek(), equals(500));
});

test('Test case 5: Inserting a value equal to the current max', () {
MaxHeap maxHeap = new MaxHeap();
List<int> array = [48, 12, 24, 7, 8, -5, 24, 391, 24, 56, 2, 6, 8, 41];
maxHeap.buildHeap(array);
maxHeap.insert(391);
expect(maxHeap.peek(), equals(391));
});

test('Test case 6: Inserting a negative value', () {
MaxHeap maxHeap = new MaxHeap();
List<int> array = [48, 12, 24, 7, 8, -5, 24, 391, 24, 56, 2, 6, 8, 41];
maxHeap.buildHeap(array);
maxHeap.insert(-10);
expect(maxHeap.peek(), equals(391));
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:test/test.dart';
import '../../../../../data_structures/Heap/Binary_Heap/Max_heap.dart';

void main() {
group('MaxHeap isEmpty tests', () {
test('Test case 1: Non-empty heap', () {
List<int> array = [48, 12, 24, 7, 8, -5, 24, 391, 24, 56, 2, 6, 8, 41];
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap(array);

expect(maxHeap.isEmpty(), isFalse);
});

test('Test case 2: Empty heap', () {
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap([]);
expect(maxHeap.isEmpty(), isTrue);
});

test('Test case 3: Heap with one element', () {
List<int> array = [5];
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap(array);

expect(maxHeap.isEmpty(), isFalse);
});

test('Test case 4: Heap with negative elements', () {
List<int> array = [-7, -10, -6, -10, -2, -7, -9, -5];
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap(array);

expect(maxHeap.isEmpty(), isFalse);
});

test('Test case 5: Heap with all zero elements', () {
List<int> array = [0, 0, 0, 0, 0];
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap(array);

expect(maxHeap.isEmpty(), isFalse);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'package:test/test.dart';
import '../../../../../data_structures/Heap/Binary_Heap/Max_heap.dart';

void main() {
group('MaxHeap Tests', () {
test('Test case 1: Non-empty heap', () {
List<int> array = [48, 12, 24, 7, 8, -5, 24, 391, 24, 56, 2, 6, 8, 41];
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap(array);

expect(maxHeap.peek(), equals(391));
maxHeap.remove();
expect(maxHeap.peek(), equals(56));

maxHeap.insert(-100);
expect(maxHeap.peek(), equals(56));

maxHeap.insert(1000);
expect(maxHeap.peek(), equals(1000));

maxHeap.remove();
expect(maxHeap.peek(), equals(56));

expect(maxHeap.isEmpty(), isFalse);
});

test('Test case 2: Non-empty heap with negative numbers', () {
List<int> array = [
-7,
2,
3,
8,
-10,
4,
-6,
-10,
-2,
-7,
10,
5,
2,
9,
-9,
-5,
3,
8
];
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap(array);

expect(maxHeap.peek(), equals(10));
maxHeap.remove();
expect(maxHeap.peek(), equals(9));

maxHeap.insert(890);
expect(maxHeap.peek(), equals(890));

maxHeap.remove();
expect(maxHeap.peek(), equals(9));

expect(maxHeap.isEmpty(), isFalse);

maxHeap.insert(1);
expect(maxHeap.peek(), equals(9));
});

test('Test case 3: Empty heap', () {
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap([]);
expect(maxHeap.peek(), isNull);
expect(maxHeap.isEmpty(), isTrue);
});

test('Test case 4: Single element heap', () {
MaxHeap maxHeap = new MaxHeap();
maxHeap.buildHeap([]);
maxHeap.insert(5);
expect(maxHeap.peek(), equals(5));
expect(maxHeap.isEmpty(), isFalse);
});
});
}
Loading
Loading