Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Coding_V5/test_algs.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
133 lines (99 sloc)
3.37 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Test Cases for the Program | |
YOU ARE NOT EXPECTED TO MODIFY THIS FILE!!! | |
""" | |
import unittest | |
import basicAlgs | |
class IntergrationTest(unittest.TestCase): | |
""" | |
Test the main program runs correctly | |
""" | |
def test_search_one(self): | |
""" | |
Test searching algorithm with sorted string | |
""" | |
values = [1,2,3,4,5,6] | |
out = basicAlgs.searcher(values, 3) | |
self.assertEqual(out, 2) | |
def test_search_two(self): | |
""" | |
Test searching algorithm with even numbers | |
""" | |
values = [1,2,4,8,16,32] | |
out = basicAlgs.searcher(values, 8) | |
self.assertEqual(out, 3) | |
def test_search_three(self): | |
""" | |
Test search with unsorted string | |
""" | |
values = [7,6,5,4,3,2,1] | |
out = basicAlgs.searcher(values, 3) | |
self.assertEqual(out, 2) | |
def text_search_text(self): | |
""" | |
Test our conversion function | |
""" | |
textVals = "1,2,3,4,5" | |
textNum = 3 | |
values, num = basicAlgs.convertValues(textVals, textNum) | |
self.assertEqual(values, [1,2,3,4,5]) | |
self.assertEqual(num, 3) | |
class TestAlgorithms(unittest.TestCase): | |
""" | |
Test the base algorithms work correctly | |
""" | |
def test_Bubble(self): | |
""" | |
Test our bubble sort function | |
""" | |
theArray = [5,7,4,6,2,9,1] | |
out = basicAlgs.bubblesort(theArray) | |
self.assertEqual(out, [1,2,4,5,6,7,9], "Items not sorted correctly") | |
def test_BubbleOrder(self): | |
""" | |
Does our bubble sort work if | |
everything is in order | |
""" | |
theArray = [1,2,3,4,5,6,7] | |
out = basicAlgs.bubblesort(theArray) | |
self.assertEqual(out, [1,2,3,4,5,6,7], "Array not sorted correctly") | |
def test_Binsearch(self): | |
""" | |
Test binary search | |
""" | |
theArray = [1,2,3,4,5,6] | |
out = basicAlgs.binarySearch(theArray, 4) | |
self.assertEqual(out, 3, "Value not found in Array") | |
def test_Binsearch_Extremes(self): | |
""" | |
Test binary search can find items at | |
the extremes of the array | |
""" | |
theArray = [1,2,3,4,5,6,7,8] | |
out = basicAlgs.binarySearch(theArray, 1) | |
self.assertEqual(out, 0, "Extreme value not found") | |
out = basicAlgs.binarySearch(theArray, 8) | |
self.assertEqual(out, 7, "Extreme value not found") | |
def test_Binsearch_NoVal(self): | |
""" | |
Test binary search when the value is not in the array | |
""" | |
theArray = [1,2,3,5,6,7] | |
out = basicAlgs.binarySearch(theArray, 4) | |
self.assertFalse(out, "Array finds value when none exists") | |
def test_binSerarch_Odd(self): | |
""" | |
Test the binary search when we have an Odd number of items | |
""" | |
theArray = [1,2,3,4,5,6,7] | |
for idx, value in enumerate(theArray): | |
out = basicAlgs.binarySearch(theArray, value) | |
self.assertEqual(out, idx, "Test Fails with odd sized array") | |
def test_binSerarch_Even(self): | |
""" | |
Test the binary search when we have an even number of items | |
""" | |
theArray = [1,2,3,4,5,6,7,8] | |
for idx, value in enumerate(theArray): | |
out = basicAlgs.binarySearch(theArray, value) | |
self.assertEqual(out, idx, "Test Fails with Even sized array") |