Created
November 29, 2018 19:31
-
-
Save roxsula/710c243bd775323cb0795bef6e510357 to your computer and use it in GitHub Desktop.
This an example of how to use my previous post about a generator of test cases for linked lists
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
''' | |
min_max_sum_linked_list(): | |
“Walk a linked list (passed in as a parameter) and return a min, max, and average of the values at the end.” | |
Function returns min, max, and average only if linked list contains numbers with these conditions: int, long, float, positive, negative. | |
Average is returned in float to provide an accurate result | |
Test Suite: | |
Node class | |
LinkedList class with methods insert_tail(data) and monitoring() | |
genTestCases class with methods generateCustomTestCase(values), generateRandomTestCase(max_len, max_val), generateEqualValuesTestCase(max_len, val) | |
runTests(): | |
10 Directed Tests using generateCustomTestCase() | |
1 Random Test using generateRandomTestCase() | |
3 Negative Tests: None, Linked list with characters, Linked list with numbers and characters | |
DebugL1 will show the results of each TestCase | |
DebugL2 will show the linked list generated | |
''' | |
#*************** FUNCTION TO BE TESTED ***************# | |
def min_max_sum_linked_list(obj_list): | |
value = None | |
arr = [] | |
if obj_list == None: | |
print('ERROR: Linked list is empty. You should have at least 1 node') | |
return -1 | |
current = obj_list.head | |
#Walk in linked list | |
while current != None: | |
value = current.data | |
#Validate nodes are numbers: int, float, long | |
if type(value) == int or type(value) == float or type(value) == long: | |
arr.append(value) | |
else: | |
print('ERROR: Invalid Linked List. Only numbers accepted. Failed with data {}'.format(current.data)) | |
return -1 | |
current = current.next #Next Node | |
return [min(arr), max(arr), (float(sum(arr))/len(arr))] | |
#******************** TEST SUITE ********************# | |
class Node: | |
def __init__(self, data=None, next=None): | |
self.data = data | |
self.next = next | |
class LinkedList(Node): | |
def __init__(self, head=None): | |
self.head = head | |
def insert_tail(self, data): | |
if (self.head == None): | |
self.head = Node(data) | |
else: | |
current = self.head | |
while (current.next != None): | |
current = current.next | |
current.next = Node(data) | |
def monitoring(self): | |
node = self.head | |
while node != None: | |
print(node.data), | |
node = node.next | |
print('\n') | |
class genTestCases(LinkedList): | |
def __init__(self, debug = False): | |
self.test_list = LinkedList() | |
self.arr = [] | |
self.debug = debug | |
#User set specific values | |
def generateCustomTestCase(self, values): | |
self.arr = values | |
for i in values: | |
self.test_list.insert_tail(i) | |
if self.debug: | |
print('Custom Test') | |
print('Reference Array = {}'.format(self.arr)) | |
print('linked list') | |
self.test_list.monitoring() | |
#User set max list length desired and the max value of the nodes | |
def generateRandomTestCase(self, max_len, max_val): | |
length = random.randint(0, max_len) | |
for j in range(length): | |
rand_val = random.randint(0, max_len ) | |
self.test_list.insert_tail(rand_val) | |
self.arr.append(rand_val) | |
if self.debug: | |
print('Random Test') | |
print('Reference Array = {}'.format(self.arr)) | |
print('linked list') | |
self.test_list.monitoring() | |
#User set max list length and the value to repeat | |
def generateEqualValuesTestCase(self, max_len, val): | |
self.arr = max_len * [val] | |
for k in range(max_len): | |
self.test_list.insert_tail(val) | |
if self.debug: | |
print('Equal Values') | |
print('Reference Array = {}'.format(self.arr)) | |
print('linked list') | |
self.test_list.monitoring() | |
#************ MODEL REFERENCE FOR TESTING ************# | |
def refFunction(arr): | |
#Validate nodes are numbers: int, float, long | |
if (arr != None) and (all(type(item)==int or type(item)==long or type(item)==float for item in arr)): | |
return [min(arr), max(arr), (float(sum(arr))/len(arr))] | |
else: | |
return -1 | |
#****************** TEST EXECUTION *******************# | |
def runTests(debugL1=False, debugL2 = False, negTesting=False): | |
#Generates Test Cases Objects | |
TC = [genTestCases(debug=debugL2) for i in range(13)] | |
#Counters for pass/fail cases | |
testCasePassed = 0 | |
testCaseFailed = 0 | |
'''Sequential positive numbers''' | |
TC[0].generateCustomTestCase([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) | |
resultFunction = min_max_sum_linked_list(TC[0].test_list) #Linked List | |
expectedValues = refFunction(TC[0].arr) #Reference Array | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
if debugL1: | |
print("Function Results = {}".format(resultFunction)) | |
print("Expected Values = {}".format(expectedValues)) | |
'''Sequential negative numbers''' | |
TC[1].generateCustomTestCase([-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]) | |
resultFunction = min_max_sum_linked_list(TC[1].test_list) | |
expectedValues = refFunction(TC[1].arr) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
if debugL1: | |
print("Function Results = {}".format(resultFunction)) | |
print("Expected Values = {}".format(expectedValues)) | |
'''Sequential negative and positive numbers''' | |
TC[2].generateCustomTestCase([-1, -2, -3, -4, -5, 0, 1, 2, 3, 4, 5]) | |
resultFunction = min_max_sum_linked_list(TC[2].test_list) | |
expectedValues = refFunction(TC[2].arr) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
if debugL1: | |
print("Function Results = {}".format(resultFunction)) | |
print("Expected Values = {}".format(expectedValues)) | |
'''Combine positive and negative numbers''' | |
TC[3].generateCustomTestCase([0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10]) | |
resultFunction = min_max_sum_linked_list(TC[3].test_list) | |
expectedValues = refFunction(TC[3].arr) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
if debugL1: | |
print("Function Results = {}".format(resultFunction)) | |
print("Expected Values = {}".format(expectedValues)) | |
'''Repeat and consecutive numbers''' | |
TC[4].generateCustomTestCase([1, 1, -2, -2, 3, 3, -4, -4, 5, 5, 0]) | |
resultFunction = min_max_sum_linked_list(TC[4].test_list) | |
expectedValues = refFunction(TC[4].arr) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
if debugL1: | |
print("Function Results = {}".format(resultFunction)) | |
print("Expected Values = {}".format(expectedValues)) | |
'''Long numbers''' | |
TC[5].generateCustomTestCase([12038941902837401982340198273409187234, 22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222, 12038941902837401982340198273409187232]) | |
resultFunction = min_max_sum_linked_list(TC[5].test_list) | |
expectedValues = refFunction(TC[5].arr) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
if debugL1: | |
print("Function Results = {}".format(resultFunction)) | |
print("Expected Values = {}".format(expectedValues)) | |
'''Long with negative numbers''' | |
TC[6].generateCustomTestCase([-12038941902837401982340198273409187234, 22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222, 12038941902837401982340198273409187232]) | |
resultFunction = min_max_sum_linked_list(TC[6].test_list) | |
expectedValues = refFunction(TC[6].arr) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
if debugL1: | |
print("Function Results = {}".format(resultFunction)) | |
print("Expected Values = {}".format(expectedValues)) | |
'''Hexadecimal numbers''' | |
TC[7].generateCustomTestCase([0x2A, 0x2A, 0x2A, 0xBB, 0xBEBE]) | |
resultFunction = min_max_sum_linked_list(TC[7].test_list) | |
expectedValues = refFunction(TC[7].arr) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
if debugL1: | |
print("Function Results = {}".format(resultFunction)) | |
print("Expected Values = {}".format(expectedValues)) | |
'''Equal Hexadecimal numbers''' | |
TC[8].generateCustomTestCase([0x2A, 0x2A, 0x2A, 0x2A, 0x2A]) | |
resultFunction = min_max_sum_linked_list(TC[8].test_list) | |
expectedValues = refFunction(TC[8].arr) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
if debugL1: | |
print("Function Results = {}".format(resultFunction)) | |
print("Expected Values = {}".format(expectedValues)) | |
'''Random Test 0 <= length,values <= 100''' | |
TC[9].generateRandomTestCase(100,100) | |
resultFunction = min_max_sum_linked_list(TC[9].test_list) | |
expectedValues = refFunction(TC[9].arr) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
if debugL1: | |
print("Function Results = {}".format(resultFunction)) | |
print("Expected Values = {}".format(expectedValues)) | |
'''A linked list of 15 elements all with 22 as value''' | |
TC[10].generateEqualValuesTestCase(15,22) | |
resultFunction = min_max_sum_linked_list(TC[10].test_list) | |
expectedValues = refFunction(TC[10].arr) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
if debugL1: | |
print("Function Results = {}".format(resultFunction)) | |
print("Expected Values = {}".format(expectedValues)) | |
if negTesting: #Negative Testing | |
"""None Corner Case""" | |
resultFunction = min_max_sum_linked_list(None) | |
expectedValues = refFunction(None) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
"""No numbers Corner Case""" | |
TC[11].generateCustomTestCase(['A', 'b', '*']) | |
resultFunction = min_max_sum_linked_list(TC[11].test_list) | |
expectedValues = refFunction(TC[11].arr) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
"""Combine no numbers and numbers Corner Case""" | |
TC[12].generateCustomTestCase([1, 2, 'C', 'b', '*', 3]) | |
resultFunction = min_max_sum_linked_list(TC[12].test_list) | |
expectedValues = refFunction(TC[12].arr) | |
if resultFunction == -1: | |
testCaseFailed += 1 | |
elif resultFunction == expectedValues: | |
testCasePassed += 1 | |
else: | |
testCaseFailed += 1 | |
if testCaseFailed > 0: | |
if debugL1: | |
print('{} test cases passed and {} test cases failed'.format(testCasePassed,testCaseFailed)) | |
print('VALIDATION FAILED') | |
return -1 | |
else: | |
if debugL1: | |
print('{} test cases passed and {} test cases failed'.format(testCasePassed,testCaseFailed)) | |
print('VALIDATION PASSED') | |
return 0 | |
#******************** USE CASES ********************# | |
#Return 0: Passed Return -1: Failed | |
runTests() | |
#runTests(debugL1 = True) | |
#runTests(negTesting=True) | |
#runTests(debugL1 = True, debugL2 = True) | |
#runTests(debugL1 = True, negTesting=True) | |
#runTests(debugL1 = True, debugL2 = True, negTesting=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment