|
1 | 1 | import check50
|
2 | 2 | import check50.c
|
3 |
| -import re |
| 3 | + |
4 | 4 |
|
5 | 5 | @check50.check()
|
6 | 6 | def exists():
|
7 | 7 | """cash.c exists"""
|
8 | 8 | check50.exists("cash.c")
|
9 | 9 |
|
10 |
| - # Include additional unit testing file |
11 |
| - check50.include("testing.c") |
12 | 10 |
|
13 | 11 | @check50.check(exists)
|
14 | 12 | def compiles():
|
15 | 13 | """cash.c compiles"""
|
16 |
| - # Check if student code compiles |
17 | 14 | check50.c.compile("cash.c", lcs50=True)
|
18 |
| - |
19 |
| - # Rename main function to "distro_main" |
20 |
| - cash = re.sub("int\s+main", "int distro_main", open("cash.c").read()) |
21 | 15 |
|
22 |
| - # Read testing file |
23 |
| - testing = open("testing.c").read() |
24 | 16 |
|
25 |
| - # Combine student code and testing file |
26 |
| - with open("cash_test.c", "w") as f: |
27 |
| - f.write(cash) |
28 |
| - f.write("\n") |
29 |
| - f.write(testing) |
| 17 | +@check50.check(compiles) |
| 18 | +def test041(): |
| 19 | + """input of 41 yields output of 4""" |
| 20 | + check50.run("./cash").stdin("41").stdout(coins(4), "4\n").exit(0) |
30 | 21 |
|
31 |
| - check50.c.compile("cash_test.c", lcs50=True) |
32 | 22 |
|
33 | 23 | @check50.check(compiles)
|
34 |
| -def cents(): |
35 |
| - """get_cents returns integer number of cents""" |
36 |
| - check50.run("./cash_test 0").stdin("100", prompt = True).stdout("100").exit() |
| 24 | +def test001(): |
| 25 | + """input of 1 yields output of 1""" |
| 26 | + check50.run("./cash").stdin("1").stdout(coins(1), "1\n").exit(0) |
37 | 27 |
|
38 |
| -@check50.check(compiles) |
39 |
| -def negative(): |
40 |
| - """get_cents rejects negative input""" |
41 |
| - check50.run("./cash_test 0").stdin("-10", prompt = True).reject() |
42 | 28 |
|
43 | 29 | @check50.check(compiles)
|
44 |
| -def test_reject_foo(): |
45 |
| - """get_cents rejects a non-numeric input of "foo" """ |
46 |
| - check50.run("./cash_test 0").stdin("foo", prompt = True).reject() |
| 30 | +def test015(): |
| 31 | + """input of 15 yields output of 2""" |
| 32 | + check50.run("./cash").stdin("15").stdout(coins(2), "2\n").exit(0) |
47 | 33 |
|
48 |
| -@check50.check(compiles) |
49 |
| -def quarters0(): |
50 |
| - """calculate_quarters returns 2 when input is 50""" |
51 |
| - check50.run("./cash_test 1").stdout("2") |
52 | 34 |
|
53 | 35 | @check50.check(compiles)
|
54 |
| -def quarters1(): |
55 |
| - """calculate_quarters returns 1 when input is 42""" |
56 |
| - check50.run("./cash_test 2").stdout("1") |
| 36 | +def test160(): |
| 37 | + """input of 160 yields output of 7""" |
| 38 | + check50.run("./cash").stdin("160").stdout(coins(7), "7\n").exit(0) |
57 | 39 |
|
58 |
| -@check50.check(compiles) |
59 |
| -def dimes0(): |
60 |
| - """calculate_dimes returns 1 when input is 10""" |
61 |
| - check50.run("./cash_test 3").stdout("1") |
62 | 40 |
|
63 | 41 | @check50.check(compiles)
|
64 |
| -def dimes1(): |
65 |
| - """calculate_dimes returns 1 when input is 15""" |
66 |
| - check50.run("./cash_test 4").stdout("1") |
| 42 | +def test230(): |
| 43 | + """input of 2300 yields output of 92""" |
| 44 | + check50.run("./cash").stdin("2300").stdout(coins(92), "92\n").exit(0) |
67 | 45 |
|
68 |
| -@check50.check(compiles) |
69 |
| -def dimes2(): |
70 |
| - """calculate_dimes returns 7 when input is 73""" |
71 |
| - check50.run("./cash_test 8").stdout("7") |
72 |
| - |
73 |
| -@check50.check(compiles) |
74 |
| -def nickels0(): |
75 |
| - """calculate_nickels returns 1 when input is 5""" |
76 |
| - check50.run("./cash_test 5").stdout("1") |
77 | 46 |
|
78 | 47 | @check50.check(compiles)
|
79 |
| -def nickels1(): |
80 |
| - """calculate_nickels returns 5 when input is 28""" |
81 |
| - check50.run("./cash_test 6").stdout("5") |
| 48 | +def test_reject_negative(): |
| 49 | + """rejects a negative input like -1""" |
| 50 | + check50.run("./cash").stdin("-1").reject() |
82 | 51 |
|
83 |
| -@check50.check(compiles) |
84 |
| -def pennies(): |
85 |
| - """calculate_pennies returns 4 when input is 4""" |
86 |
| - check50.run("./cash_test 7").stdout("4") |
87 |
| - |
88 | 52 |
|
89 | 53 | @check50.check(compiles)
|
90 |
| -def test41(): |
91 |
| - """input of 41 cents yields output of 4 coins""" |
92 |
| - output = check50.run("./cash").stdin("41", prompt = True).stdout("4\n").exit() |
| 54 | +def test_reject_foo(): |
| 55 | + """rejects a non-numeric input of "foo" """ |
| 56 | + check50.run("./cash").stdin("foo").reject() |
| 57 | + |
93 | 58 |
|
94 | 59 | @check50.check(compiles)
|
95 |
| -def test160(): |
96 |
| - """input of 160 cents yields output of 7 coins""" |
97 |
| - check50.run("./cash").stdin("160", prompt = True).stdout("7\n").exit() |
| 60 | +def test_reject_empty(): |
| 61 | + """rejects a non-numeric input of "" """ |
| 62 | + check50.run("./cash").stdin("").reject() |
| 63 | + |
98 | 64 |
|
99 | 65 | def coins(num):
|
100 | 66 | # regex that matches `num` not surrounded by any other numbers (so coins(2) won't match e.g. 123)
|
|
0 commit comments