~dricottone/riddler-230106

ref: 0216807bec87546055061031b3ebb14870072800 riddler-230106/classic/test.py -rwxr-xr-x 2.3 KiB
0216807bDominic Ricottone Solution to classic riddler 1 year, 10 months ago
                                                                                
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3

from .main import *

def _test_schedule(generated_schedule, literal_schedule):
    """Helper function for `test_schedules`."""
    diff = set(generated_schedule.keys()) - set(literal_schedule.keys())
    try:
        assert len(diff) == 0
    except:
        print(f"Generated schedule contains an impossible quantity of assemblers: {diff}")

    for n in sorted(literal_schedule.keys()):
        try:
            assert generated_schedule[n][0] == literal_schedule[n][0]
            assert generated_schedule[n][1] == literal_schedule[n][1]
        except:
            print(f"Generated schedule is wrong for {n} assemblers")
            print(f"Is:        {generated_schedule[n]}")
            print(f"Should be: {literal_schedule[n]}")

def test_schedules():
    # check that only one strategy exists for schedules with too few hours
    for i in range(HOURS_TO_PRODUCE_ASSEMBLER):
        res = assembler_schedule(i)
        _test_schedule(res, { 1: (i, 0) })

    # check that complex strategies can be formed
    res = assembler_schedule(HOURS_TO_PRODUCE_ASSEMBLER)
    _test_schedule(res, {
        1: (HOURS_TO_PRODUCE_ASSEMBLER, 0),
        2: (0, 0),
    })

    # check handling of remaining hours without fighter production potential
    res = assembler_schedule(HOURS_TO_PRODUCE_ASSEMBLER + 1)
    _test_schedule(res, {
        1: (HOURS_TO_PRODUCE_ASSEMBLER + 1, 0),
        2: (1, 0),
    })

    # check handling of remaining hours with fighter production potential
    res = assembler_schedule(HOURS_TO_PRODUCE_ASSEMBLER * 2)
    _test_schedule(res, {
        1: (HOURS_TO_PRODUCE_ASSEMBLER * 2, 0),
        2: (HOURS_TO_PRODUCE_ASSEMBLER, 0),
        3: (0, FIGHTERS_PRODUCED_IN_ONE_UNIT),
        4: (0, 0),
    })

    # check that non-trivial ranges are bounded correctly
    res = assembler_schedule(HOURS_TO_PRODUCE_ASSEMBLER * 3)
    _test_schedule(res, {
        1: (HOURS_TO_PRODUCE_ASSEMBLER * 3, 0),
        2: (HOURS_TO_PRODUCE_ASSEMBLER * 2, 0),
        3: (HOURS_TO_PRODUCE_ASSEMBLER, FIGHTERS_PRODUCED_IN_ONE_UNIT),
        4: (HOURS_TO_PRODUCE_ASSEMBLER, 0),
        5: (0, FIGHTERS_PRODUCED_IN_ONE_UNIT*3),
        6: (0, FIGHTERS_PRODUCED_IN_ONE_UNIT*2),
        7: (0, FIGHTERS_PRODUCED_IN_ONE_UNIT),
        8: (0, 0),
    })

def main():
    print("Running test suite...")
    test_schedules()

if __name__ == "__main__":
    main()