-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add tests without modifying code #10740
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
Changes from 4 commits
c414518
41fffa1
ff894a8
6aa37f2
16d377e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -4,7 +4,17 @@ | |||
class Onepad: | ||||
@staticmethod | ||||
def encrypt(text: str) -> tuple[list[int], list[int]]: | ||||
"""Function to encrypt text using pseudo-random numbers""" | ||||
""" | ||||
Function to encrypt text using pseudo-random numbers | ||||
>>> Onepad().encrypt("") | ||||
([], []) | ||||
>>> random.seed(1) | ||||
>>> Onepad().encrypt(" ") | ||||
([6969], [69]) | ||||
>>> random.seed(1) | ||||
>>> Onepad().encrypt("Hello") | ||||
([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61]) | ||||
""" | ||||
plain = [ord(i) for i in text] | ||||
key = [] | ||||
cipher = [] | ||||
|
@@ -17,7 +27,21 @@ def encrypt(text: str) -> tuple[list[int], list[int]]: | |||
|
||||
@staticmethod | ||||
def decrypt(cipher: list[int], key: list[int]) -> str: | ||||
"""Function to decrypt text using pseudo-random numbers.""" | ||||
""" | ||||
Function to decrypt text using pseudo-random numbers. | ||||
>>> Onepad().decrypt([], []) | ||||
'' | ||||
>>> Onepad().decrypt([35], []) | ||||
'' | ||||
>>> Onepad().decrypt([], [35]) | ||||
Traceback (most recent call last): | ||||
... | ||||
IndexError: list index out of range | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
>>> random.seed(1) | ||||
>>> Onepad().decrypt([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61]) | ||||
'Hello' | ||||
""" | ||||
plain = [] | ||||
for i in range(len(key)): | ||||
p = int((cipher[i] - (key[i]) ** 2) / key[i]) | ||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,6 +4,23 @@ | |||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def res(x, y): | ||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||
Reduces large number to a more manageable number | ||||||||||||||||||||||||||||||||||||||||
>>> res(5, 7) | ||||||||||||||||||||||||||||||||||||||||
4.892790030352132 | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
>>> res(0, 5) | ||||||||||||||||||||||||||||||||||||||||
0 | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
>>> res(3, 0) | ||||||||||||||||||||||||||||||||||||||||
1 | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
>>> res(-1, 5) | ||||||||||||||||||||||||||||||||||||||||
Traceback (most recent call last): | ||||||||||||||||||||||||||||||||||||||||
... | ||||||||||||||||||||||||||||||||||||||||
ValueError: math domain error | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better if the tests do not require extra scrolling when someone is reading the code. If they want to read all the tests, they can take the time to do so but often readers will want to look at the function signature and then jump straight to the code. Keeping the tests in tight block will allow them them to study the function signature and the code without too much visual distraction.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||
if 0 not in (x, y): | ||||||||||||||||||||||||||||||||||||||||
# We use the relation x^y = y*log10(x), where 10 is the base. | ||||||||||||||||||||||||||||||||||||||||
return y * math.log10(x) | ||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
def get_word_pattern(word: str) -> str: | ||
""" | ||
Returns numerical pattern of character appearances in given word | ||
>>> get_word_pattern() | ||
Traceback (most recent call last): | ||
... | ||
TypeError: get_word_pattern() missing 1 required positional argument: 'word' | ||
>>> get_word_pattern("") | ||
'' | ||
>>> get_word_pattern(" ") | ||
'0' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please put the tests that work correctly first and then afterward the ones that raise exceptions. Also, please add: |
||
>>> get_word_pattern("pattern") | ||
'0.1.2.2.3.4.5' | ||
>>> get_word_pattern("word pattern") | ||
|
Uh oh!
There was an error while loading. Please reload this page.