-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgrader_tests.py
46 lines (36 loc) · 1.36 KB
/
grader_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import unittest
import random
import string
from graderutils.graderunittest import points
# ASCII letters with significant probability of spaces
char_distribution = 10*' ' + string.ascii_letters
def random_string(n):
return ''.join(random.choice(char_distribution) for _ in range(n))
def noisy_copy(s, copy_prob):
"""Create a copy of s by copying each character with a given probability, else draw random character"""
return ''.join(c if random.random() < copy_prob else random.choice(char_distribution) for c in s)
class Test(unittest.TestCase):
def setUp(self):
self.user_data = {}
def set_marked_and_assert_equal(self, a, b):
marked_b = [(b_char, a_char == b_char) for a_char, b_char in zip(a, b)]
self.user_data = {"string_a": a, "marked_b": marked_b}
if a != b:
self.fail("Strings were not equal")
@points(1)
def test1_strings_equal(self):
# Expected
a = "The sand was yellow"
# Compared
b = "The song was mellow"
self.set_marked_and_assert_equal(a, b)
@points(1)
def test2_strings_equal(self):
a = "The sand was yellow"
b = a
self.set_marked_and_assert_equal(a, b)
@points(1)
def test3_random_strings_equal(self):
a = random_string(200)
b = noisy_copy(a, 0.8)
self.set_marked_and_assert_equal(a, b)