summaryrefslogtreecommitdiff
path: root/monkey/test.py
blob: bca55d85c3ce0f2fffdf3a26c7490ca7c4fd5bf7 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# CodeQ: an online programming tutor.
# Copyright (C) 2015 UL FRI
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import pickle
import sys

from termcolor import colored

from db.models import CodeqUser, Problem, Solution
from .graph import graphviz
from . import fix, fix_hints
from prolog.util import annotate, compose, stringify
import server.problems
from .util import indent

# Get problem id from commandline.
if len(sys.argv) < 2:
    print('usage: ' + sys.argv[0] + ' <pid>')
    sys.exit(1)

problem = Problem.get(id=sys.argv[1])

solved_problems = [p for p in CodeqUser.solved_problems(1, problem.language)
                           if p != (problem.group, problem.identifier)]
other_solutions = server.problems.solutions_for_problems(problem.language, solved_problems)
problem_module = server.problems.load_problem(problem.language, problem.group, problem.identifier, 'common')
name = server.problems.load_problem(problem.language, problem.group, problem.identifier, 'en').name

# Testing function.
def test(code):
    correct, hints = problem_module.test(code, solved_problems)
    return correct

traces = [s.trace for s in Solution.filter(problem_id=problem.id)]

# Load hint database stored in edits.pickle.
edits, submissions, queries, names = pickle.load(open('edits.pickle', 'rb'))
edits, submissions, queries, names = edits[problem.id], submissions[problem.id], queries[problem.id], names[problem.id]

# Load current status (programs for which a hint was found).
try:
    done = pickle.load(open('status-'+str(problem.id)+'.pickle', 'rb'))
except:
    done = []

def print_hint(code, solution, steps, fix_time, n_tested):
    if solution:
        print(colored('Hint found! Tested {} programs in {:.1f} s.'.format(n_tested, fix_time), 'green'))
        print(colored(' Edits', 'blue'))
        for step_type, pos, a, b in steps:
            print('  {}: {} {} → {}'.format(pos, step_type, stringify(a), stringify(b)))
        print(colored(' Hints', 'blue'))
        for fix_type, start, end, msg in fix_hints(code, steps):
            print('  {}-{}: {} (fix type: {})'.format(start, end, msg, fix_type))
        print(colored(' Final version', 'blue'))
        print(indent(compose(annotate(solution)), 2))
    else:
        print(colored('Hint not found! Tested {} programs in {:.1f} s.'.format(n_tested, fix_time), 'red'))

# Run interactive loop.
if len(sys.argv) == 2:
    while True:
        # Read the program from stdin.
        print('Enter program, end with empty line:')
        code = ''
        try:
            while True:
                line = input()
                if not line:
                    break
                code += line + '\n'
        except EOFError:
            break

        # Try finding a fix.
        print(colored('Analyzing program…', 'yellow'))
        solution, steps, fix_time, n_tested = fix(name, code, edits, test, debug=True)
        print_hint(code, solution, steps, fix_time, n_tested)

# Test fix() on incorrect student submissions.
elif sys.argv[2] == 'test':
    timeout = int(sys.argv[3]) if len(sys.argv) == 4 else 10

    # Find incorrect submissions.
    incorrect_all = []
    for submission, count in sorted(submissions.items()):
        if not test(submission):
            # This incorrect submission appeared in [count] traces.
            incorrect_all += [submission]*count
    incorrect = set(incorrect_all)

    print('Fixing {}/{} programs (timeout={})…'.format(
        len([p for p in incorrect if p not in done]), len(incorrect), timeout))

    for i, program in enumerate(sorted(incorrect)):
        if program in done:
            continue
        print(colored('Analyzing program {0}/{1}…'.format(i+1, len(incorrect)), 'yellow'))
        print(indent(compose(annotate(program)), 2))

        solution, steps, fix_time, n_tested = fix(name, program, edits, test, timeout=timeout)
        if solution:
            done.append(program)
        print_hint(program, solution, steps, fix_time, n_tested)
        print()

        pickle.dump(done, open('status-'+str(problem.id)+'.pickle', 'wb'))

    print('Found hints for ' + str(len(done)) + ' of ' + str(len(incorrect)) + ' incorrect programs')

# Print info for this problem.
elif sys.argv[2] == 'info':
    # With no additional arguments, print some stats.
    if len(sys.argv) == 3:
        print('Problem {} ({}): {} edits and {} unique submissions in {} traces'.format(
            problem.id, colored(name, 'yellow'),
            colored(str(len(edits)), 'yellow'),
            colored(str(len(submissions)), 'yellow'),
            colored(str(len(traces)), 'yellow')))

    # Print all observed edits and their costs.
    elif sys.argv[3] == 'edits':
        for (before, after), cost in sorted(edits.items(), key=lambda x: x[1]):
            print(' {:.4f}\t{} → {}'.format(cost, stringify(before) if before else 'ε',
                                                  stringify(after) if after else 'ε'))
    # Print all observed edits and their costs.
    elif sys.argv[3] == 'names':
        for name, count in sorted(names.items(), key=lambda x: x[1]):
            print(' {:.4f}\t{}'.format(count,  name))

    # Print all student queries and their counts.
    elif sys.argv[3] == 'queries':
        for query, count in queries.most_common():
            print('  ' + str(count) + '\t' + query)

    # Print all student submissions and their counts.
    elif sys.argv[3] == 'submissions':
        for submission, count in submissions.most_common():
            print('{}\t{}'.format(count, submission))