File size: 2,537 Bytes
28adce7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import unittest

from t2.submission import check_c

SPEC = ('/*@ Import Coq Require Import PVbench.Toy.rocq.spec_lib */\n'
        'void helper(int n) { return; }\n'
        'int entry(int n)\n/*@ Require n > 0 Ensure __return == n */\n{ return n; }\n')


def codes(submitted, headers=None, answer_headers=()):
    problems, _ = check_c(SPEC, submitted, headers=headers or {}, answer_headers=answer_headers)
    return [p['code'] for p in problems]


class AnswerHeaderRuleTests(unittest.TestCase):
    def test_header_prototype_with_named_spec_is_an_auxiliary_declaration(self):
        header = '/*@ Extern Coq (p : Z -> Prop) */\nvoid helper(int n)\n/*@ low_level_spec Require p(n) Ensure emp */;\n'
        self.assertEqual(codes('#include "defs.h"\n' + SPEC, {'defs.h': header}, ('defs.h',)), [])

    def test_unnamed_prototype_contract_may_supply_or_repeat_but_not_replace(self):
        supply = 'void helper(int n)\n/*@ Require n >= 0 Ensure emp */;\n'
        self.assertEqual(codes('#include "defs.h"\n' + SPEC, {'defs.h': supply}, ('defs.h',)), [])
        repeat = 'int entry(int n)\n/*@ Require n > 0 Ensure __return == n */;\n'
        self.assertEqual(codes('#include "defs.h"\n' + SPEC, {'defs.h': repeat}, ('defs.h',)), [])
        replace = 'int entry(int n)\n/*@ Require n >= 0 Ensure __return == n */;\n'
        self.assertIn('CODE_CHANGED', codes('#include "defs.h"\n' + SPEC, {'defs.h': replace}, ('defs.h',)))
        self.assertEqual(codes(supply + SPEC), [])   # the same prototype written inline

    def test_guard_defines_are_allowed_only_for_names_no_fixed_source_mentions(self):
        header = '#ifndef DEFS_H\n#define DEFS_H\n#ifndef HELPER_IMPLEMENTED\nvoid helper(int n)\n/*@ low_level_spec Require emp Ensure emp */;\n#endif\n#endif\n'
        ok = '#define HELPER_IMPLEMENTED\n#include "defs.h"\n' + SPEC
        self.assertEqual(codes(ok, {'defs.h': header}, ('defs.h',)), [])
        self.assertIn('CODE_CHANGED', codes('#define entry\n' + SPEC))
        self.assertIn('CODE_CHANGED', codes('#define FLAG 1\n' + SPEC))
        fixed = {'fixed.h': '#ifndef FEATURE\nint entry(int n);\n#endif\n'}
        self.assertIn('CODE_CHANGED', codes('#define FEATURE\n' + SPEC, fixed))

    def test_backend_includes_inside_an_answer_header_are_not_token_changes(self):
        header = '#include "verification_stdlib.h"\n/*@ Extern Coq (p : Z -> Prop) */\n'
        self.assertEqual(codes('#include "defs.h"\n' + SPEC, {'defs.h': header}, ('defs.h',)), [])


if __name__ == '__main__':
    unittest.main()