Testing Code:
#Humyra Taifoor
#Testing Encryption Functions
#November 19 2018
import itertools #imports itertools and allows the comparison of 2 lists
import time #imports the time module
def testing_1():
"""(str)-> str
Runs the code with the assigned test cases and compares the expected outputs and the actual outputs.
If the expcted output and the actual output match, then it returns pass, if not, then it returns false.
>>>testing_1('hi')
Pass
>>>testing_1('123community')
Pass
"""
print("Using the first method of encryption, our test cases are 'hi', '123community','dollar for your sadness','cOm+5l![At3D_w./', '2*2^4+58'|'', ' '")
print("and the expected outputs are 'ihoy', 'ytinummoc321oy', 'ssendas ruoy rof rallodoy','/?<.w_D3tA[!l5+mOcoy',''|'85+4^2*2oy', ' oy'")
test_cases = ['hi', '123community','dollar for your sadness','cOm+5l![At3D_w./', "2*2^4+58'|'", ' ']
output_from_code = []
expected_output = ['ihoy', 'ytinummoc321oy', 'ssendas ruoy rof rallodoy','/?<.w_D3tA[!l5+mOcoy',"'|'85+4^2*2oy", ' oy']
#encryption code
for x in test_cases: #iterates through every item in test_cases
y = list(x[::-1]) #reverses every word
y.append('oy') #adds an oy to the end of the last word of the reversed phrase
z = ''.join(y) #turns the list back into a string
output_from_code.append(z) #appends the output from the encryption code to output_from_code so that it can be compared later
#comparing outputs
for f,b in itertools.zip_longest(output_from_code,expected_output):#runs through every item in output_from_code and expected_output and runs until the longest iterable ends
if f == b: #compares the output from the code and the expected output
print("Pass") #if the output from the code and the expected output are the same, it prints pass
else:
print("False") #if the output from the code and the expected output are not the same, it prints false
testing_1()
print(" ")
def testing_2():
"""(str)-> str
Runs the code with the assigned test cases and compares the expected outputs and the actual outputs.
If the expcted output and the actual output match, then it returns pass, if not, then it returns false.
>>>testing_2('ihoy')
Pass
>>>testing_2('ytinummoc321oy')
Pass
"""
print("Using the first method of decryption, our test cases are 'ihoy', 'ytinummoc321oy', 'ssendas ruoy rof rallodoy','/?<.w_D3tA[!l5+mOcoy',''|'85+4^2*2oy', ' oy'")
print("and the expected outputs are 'hi', '123community','dollar for your sadness','cOm+5l![At3D_w./', '2*2^4+58'|'', ' '")
test_cases = ['ihoy', 'ytinummoc321oy', 'ssendas ruoy rof rallodoy','/?<.w_D3tA[!l5+mOcoy',"'|'85+4^2*2oy", ' oy']
output_from_code = []
expected_output = ['hi', '123community','dollar for your sadness','cOm+5l![At3D_w./', "2*2^4+58'|'", ' ']
#decryption code
for x in test_cases: #iterates through every item in test_cases
y = list(x[::-1]) #turns the phrase into a list and reverses the letters
y.remove('y') #removes the 'y' that was orginally added
y.remove('o') #removes the 'o' that was orginally added
z = ''.join(y) #turns the list back into a string
output_from_code.append(z) #appends the output from the encryption code to output_from_code so that it can be compared later
#comparing outputs
for f,b in itertools.zip_longest(output_from_code,expected_output):#runs through every item in output_from_code and expected_output and runs until the longest iterable ends
if f == b: #compares the output from the code and the exected output
print("Pass") #if the output from the code and the expected output are the same, it prints pass
else:
print("False") #if the output from the code and the expected output are not the same, it prints false
time.sleep(0.5)
testing_2()
print(" ")
def testing_3():
"""(str)-> str
Runs the code with the assigned test cases and the assigned key and compares the expected outputs and the actual outputs.
If the expcted output and the actual output match, then it returns pass, if not, then it returns false.
>>>testing_3('hi')
Pass
>>>testing_3('|')
False
"""
print("Using the second method of encryption, in which the key is '5', our test cases are 'hi', '123community',")
print("'dollar for your sadness','cOm+5l![At3D_w./', '2*2^4+58'|'', ' ','|'','|'")
print("And the expected outputs are 'mn', '678htrrzsny~',")
print("'ssendas ruoy rof rallodoy','/?<.w_D3tA[!l5+mOcoy',''|'85+4^2*2oy', ' oy','|'','|'")
test_cases = ['hi', '123community','dollar for your sadness','cOm+5l![At3D_w./', "2*2^4+58'|'", ' ',"|'",'|']
key = '5'
output_from_code = []
expected_output = ['mn', '678htrrzsny~','itqqfw%ktw%~tzw%xfisjxx','hTr0:q&`Fy8Id|3AD4','7/7c90:=,�,', '%%%','�"','�']
#encryption code
for x in test_cases: #iterates through every item in test_cases
output = [] #this allows the appending of numbers to these variables later
finaloutput = []
finalword = []
for character in x: #a for loop for every character in the variable x
number = ord(character) #turns each letter into a number (ASCII number)
output.append(number) #appends each number to the variable output
for character in output: #a for loop for every character in output
character = int(character) + int(key)#adds the key to the number that represents the letter from the inpuuted message and adds it to every number in output
finaloutput.append(character) #appends each new number to the variable finaloutput
x = finaloutput
for i in x: #a for loop for every i in the variable x (the variable x is the list of new numbers)
finalword.append(chr(i)) #iteratures through the list and turns every ASCII number back into a letter and appends them to the variable finalword
y = finalword
newword = ''.join(y) #turns the list of the letters into a string
output_from_code.append(newword) #appends the output from the encryption code to output_from_code so that it can be compared later
#comparing outputs
for f,b in itertools.zip_longest(output_from_code,expected_output):
if f == b: #compares the output from the code and the exected output
print("Pass") #if the output from the code and the expected output are the same, it prints pass
else:
print("False") #if the output from the code and the expected output are not the same, it prints false
time.sleep(0.5)
testing_3()
print(" ")
def testing_4():
"""(str)-> str
Runs the code with the assigned test cases and the assigned key and compares the expected outputs and the actual outputs.
If the expcted output and the actual output match, then it returns pass, if not, then it returns false.
>>>testing_4('mn')
Pass
>>>testing_4('�')
False
"""
print("Using the second method of decryption, in which the key is '5', our test cases are 'ihoy', 'ytinummoc321oy',")
print("'ssendas ruoy rof rallodoy','/?<.w_D3tA[!l5+mOcoy',''|'85+4^2*2oy', ' oy','|'','|'")
print("And the expected outputs are 'hi', '123community','dollar for your sadness','cOm+5l![At3D_w./', '2*2^4+58'|'', ' ','|'','|'")
test_cases = ['mn', '678htrrzsny~','itqqfw%ktw%~tzw%xfisjxx','hTr0:q&`Fy8Id|3AD4','7/7c90:=,�,', '%%%','�"','�']
key = '5'
output_from_code = []
expected_output = ['hi', '123community','dollar for your sadness','cOm+5l![At3D_w./', "2*2^4+58'|'", ' ',"|'",'|']
#decryption code
for x in test_cases:
output = [] #this allows the appending of numbers to these variables later
finaloutput = []
finalword = []
for character in x: #a for loop for every character in the variable x
number = ord(character) #turns each letter into a number
output.append(number) #appends each number to the variable output
for character in output: #a for loop for every character in output
character = int(character) - int(key)#subtracts the key to the number that represents the letter from the encrypted message
finaloutput.append(character) #appends each number to the variable finaloutput
x = finaloutput
for i in x: #a for loop for every i in the variable x
finalword.append(chr(i)) #turns the numbers back into letters and appends them to the variable finalword
y = finalword #assigns the final word to variable y
newword = ''.join(y) #turns the list of the letters into a string
output_from_code.append(newword) #appends the output from the encryption code to output_from_code so that it can be compared later
#comparing outputs
for f,b in itertools.zip_longest(output_from_code,expected_output):
if f == b: #compares the output from the code and the exected output
print("Pass") #if the output from the code and the expected output are the same, it prints pass
else:
print("False") #if the output from the code and the expected output are not the same, it prints false
time.sleep(0.5)
testing_4()
print(" ")
print("To see what the actual outputs are for the failed tests, open up the encryption.py file from assignment 3")