This function contains errors. Your task will be to nd the errors using testing.
ID: 3776769 • Letter: T
Question
This function contains errors. Your task will be to nd the errors using testing. You’ll hand in an explanation of what the errors are, and how you xed them, but you will not hand in the corrected code.
1. Write white-box and black-box tests for the function merge(). You should do this without Python, either on paper, or in a simple document. Don’t worry about running your tests until you have thought about which tests you want. Make sure you have at least 3 black-box tests, and 3 white-box tests, as a minimum. More tests may be useful.
2. Implement your test cases in a document named a8q2_testing.py. This can be done using the techniques given in the textbook , or as demonstrated in the Lab 8.
3. Run your tests, and copy/paste the console output to a document named a8q2_output.txt. The console output you hand in should come from the program before you try to x it!
4. Try to deduce the problems with the merge() function from the output of your testing. Then x the errors in the function, so that your test driver does not indicate any faults.
5.In the document a8q2_output.txt, describe the error(s) you found, and how you xed it (them). You do not need to write a lot; a sentence or two for each error is all we want. Write your explanation as if you are explaining the error to a colleague of yours who wrote the function
Explanation / Answer
hey heres everything, try to check and arrange . I have also provided corrected code at the end.If anything need to be added or if any doubts post comments.
testcase:
Black box tests:
1. positive values in increasing order in two lists.
2. negative values in increasing order in two lists.
3. one list with negative values and other with positive values or vice-versa which are in increasing order.
White Box Tests:
1.From while it can be seen that if listA is of 1 element or listB is of 1 element, it wont arrange the result list correctly.
2.In the if else conditions present inside while loop show that in two cases i.e. whether listA element greater than listB or vice-versa it stores only listA element and not respective smaller element of either.
3.From the final if condition, it can be seen that if there are fewer elements in listA than listB, then it fails to arrange B elements to the result.
4.From while loop condition and the final if condition, it can be seen that if all elements in listA are smaller than listB then it will return listA.
a8q2_output.txt:
Output:
Black-box tests:
1.positive elements of listA and listB:
listA=[1,20000,300000,400000000]
listB=[22,400,500,600]
output:
[1,20000,20000,20000,20000,300000,400000000]
expected:
[1,22,400,500,600,20000,300000,400000000]
2.negative elements of listA and listB:
listA=[-1000,-900,-700,-200]
listB=[-500,-190,-20]
output:
[-1000,-900,-700,-200]
expected:
[-1000,-900,-700,-500,-200,-190,-20]
3.positive elements of one list and negative elements in other list
listA=[-100,-77,-43,-29]
listB=[4,10,50,100]
output:
[-100,-77,-43,-29]
expected:
[-100,-77,-43,-29,4,10,50,100]
White Box texts:
1.listA or listB is of one element
listA=[20]
listB=[10,30,40]
output:
[20]
expected:
[10,20,30,40]
2.Inside If/else conditions in while, only listA elements appended.
listA=[100,200,300]
listB=[1,2,3]
output:
[100,100,100,200,300]
expected:
[1,2,3,100,200,300]
3.listA elements fewer than listB elements
listA=[20,40,60]
listB=[10,25,40,70,80,90]
output:
[20,20,40,40,40,60]
expected:
[10,20,25,40,70,80,90]
4.if listA elements smaller than all listB
listA=10,20,30]
listB=[40,50,60]
output:
[10,20,30]
expected:
[10,20,30,40,50,60]
Errors in the code:
There are no compile errors but there are errors regarding the functionality of the program.
1.while loop in the code is implemented with wrong condition i.e. while indexA< len(listA) -1 and indexB < len(listB) -1 , this tells that if theres one element initially or while running the loop, it ignores one last element to compare, so it can be changed as:
while indexA <=len(listA)-1 and indexB <= len(listB)-1 :
2.Inside if/else conditions in the while loop,i.e. if listA[indexA]>listB[].. inside these both if/else conditions statement is same i.e. result.append(listA[indexA]) instead it has to be which ever element is smaller that must be appended:
if listA[indexA] < listB[indexB]:
result.append(listA[indexA])
indexA = indexA + 1
else:
result.append(listB[indexB])
indexB = indexB + 1
3.At the end, theres if statement which is for adding remaining elements into result list but theres no else condition for adding remaining elements of listB into result list, so added
else:
result.extend(listB[indexB:])
Corrected program:
def merge(listA, listB):
'''
Given 2 lists whose elements are in increasing order,
merge them into one list of elements in increasing order
:param listA: a list in increasing order
:param listB: a list in increasing order
:return: a list with all elements in increasing order
'''
result = []
indexA = 0
indexB = 0
while indexA <=len(listA)-1 and indexB <= len(listB)-1 :
# step through the lists one element at a time
# put the smaller element in the result
if listA[indexA] < listB[indexB]:
result.append(listA[indexA])
indexA = indexA + 1
else:
result.append(listB[indexB])
indexB = indexB + 1
# add the remaining elements
if indexA < len(listA):
result.extend(listA[indexA:])
else:
result.extend(listB[indexB:])
return result
listA=[5,40,50]
listB=[10]
res=merge(listA,listB);
print(res)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.