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()