|
| 1 | +import os |
| 2 | +import inspect |
| 3 | + |
| 4 | + |
| 5 | +files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("shifted")] |
| 6 | +for f in files: |
| 7 | + exec("import " + f[:-3] + " as " + f[:-3]) |
| 8 | + |
| 9 | + |
| 10 | +def test_names(): |
| 11 | + for f in files: |
| 12 | + assert "shifted" in dir(eval(f[:-3])), ( |
| 13 | + "shifted is not defined in " + f[:-3] |
| 14 | + ) |
| 15 | + |
| 16 | + |
| 17 | +def test_callables(): |
| 18 | + for f in files: |
| 19 | + assert callable(eval(f[:-3]).shifted), ( |
| 20 | + "shifted is not callable in " + f[:-3] |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +def test_loc(): |
| 25 | + for f in files: |
| 26 | + f = os.path.join(os.path.dirname(__file__), f) |
| 27 | + with open(f, "r") as file: |
| 28 | + loc = len(file.readlines()) |
| 29 | + assert loc < 10, "Too many lines in " + f[:-3] |
| 30 | + |
| 31 | + |
| 32 | +def test_perfect_samples(): |
| 33 | + perfect_samples = [ |
| 34 | + [1, 2, 3], |
| 35 | + [1, 2, 3, 4, 5], |
| 36 | + [1, 2, 3, 4, 5, 6, 7, 8, 9], |
| 37 | + ] |
| 38 | + for f in files: |
| 39 | + for sample in perfect_samples: |
| 40 | + assert eval(f[:-3]).shifted(sample) == 0, ( |
| 41 | + "The return value should be 0 for perfect samples in " + f[:-3] |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def test_low_shift(): |
| 46 | + low_shift = [ |
| 47 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10], |
| 48 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10], |
| 49 | + ] |
| 50 | + for f in files: |
| 51 | + for sample in low_shift: |
| 52 | + assert 0 < eval(f[:-3]).shifted(sample) < 20, ( |
| 53 | + "The return value should be between 0 and 10 for low shift in " + f[:-3] |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def test_moderate_shift(): |
| 58 | + moderate_shift = [ |
| 59 | + [1, 2, 5, 9, 30], |
| 60 | + [1, 1, 1, 5, 9, 18], |
| 61 | + ] |
| 62 | + for f in files: |
| 63 | + for sample in moderate_shift: |
| 64 | + assert 20 < eval(f[:-3]).shifted(sample) < 50, ( |
| 65 | + "The return value should be between 10 and 20 for moderate shift in " + f[:-3] |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def test_high_shift(): |
| 70 | + high_shift = [ |
| 71 | + [1, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 100, 100, 100], |
| 72 | + [2, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 90, 90, 100, 100], |
| 73 | + [3, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 90, 90, 90, 90, 90], |
| 74 | + [4, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 70, 70, 70, 80, 80, 80], |
| 75 | + ] |
| 76 | + for f in files: |
| 77 | + for sample in high_shift: |
| 78 | + assert 50 < eval(f[:-3]).shifted(sample) < 80, ( |
| 79 | + "The return value should be between 20 and 30 for high shift in " + f[:-3] |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def test_very_high_shift(): |
| 84 | + very_high_shift = [ |
| 85 | + [1, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 100, 100, 100, 100, 100], |
| 86 | + [2, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 100, 100, 100, 100, 100], |
| 87 | + [3, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 100, 100, 100, 100, 100, 100, 100], |
| 88 | + [4, 2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 9, 100, 100, 100, 100, 100, 100, 100], |
| 89 | + ] |
| 90 | + for f in files: |
| 91 | + for sample in very_high_shift: |
| 92 | + assert 80 < eval(f[:-3]).shifted(sample), ( |
| 93 | + "The return value should be between 30 and 40 for very high shift in " + f[:-3] |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +def test_with_negative_numbers(): |
| 98 | + for f in files: |
| 99 | + assert 0 < eval(f[:-3]).shifted( |
| 100 | + [-1, -2, -2, -3, -4, -5, -5, -5, -6, -7, -8, -9, -100, -100, -100, -100] |
| 101 | + ) > 0, ( |
| 102 | + "The return value should be positive even if the numbers are negative in " + f[:-3] |
| 103 | + ) |
0 commit comments