interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / checker2 / test_c2_lrat.c

140 lines · 6567 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* test_c2_lrat: checker2's DIMACS reader and LRAT checker on hand-written cases (omega#446).
18 * Every refusal is checked by its exact rule name, so removing any GUARD line in c2_lrat.c fails
19 * a case here (tests/checker2/mutants.sh). Also: 20000 single-byte corruptions of a valid proof
20 * never crash and never make a satisfiable formula "unsatisfiable". */
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include "c2_lrat.h"
26
27static int fails, cases;
28
29/* (1 2)(1 -2)(-1 2)(-1 -2): unsatisfiable */
30static const char *UNSAT4 = "c four clauses over two variables\np cnf 2 4\n1 2 0\n1 -2 0\n-1 2 0\n-1 -2 0\n";
31static const char *GOOD = "5 1 0 1 2 0\n6 0 5 3 4 0\n";
32
33static void lrat(const char *name, const char *cnf_text, const char *proof, const char *want_rule)
34{
35 cases++;
36 C2Cnf cnf;
37 const char *why = "";
38 if (c2_cnf_parse(cnf_text, strlen(cnf_text), &cnf, &why)) {
39 printf("FAIL %s: cnf refused (%s)\n", name, why);
40 fails++;
41 return;
42 }
43 C2LratResult r;
44 int rc = c2_lrat_check(&cnf, proof, strlen(proof), &r);
45 int want_ok = strcmp(want_rule, "LRAT-OK") == 0;
46 if ((rc == 0) != want_ok || r.ok != want_ok || strcmp(r.rule, want_rule) != 0) {
47 printf("FAIL %s: rc=%d ok=%d rule=%s line=%ld, want %s\n", name, rc, r.ok, r.rule, r.line, want_rule);
48 fails++;
49 }
50 c2_cnf_free(&cnf);
51}
52
53static void cnf(const char *name, const char *text, const char *want)
54{
55 cases++;
56 C2Cnf c;
57 const char *why = "CNF-OK";
58 int rc = c2_cnf_parse(text, strlen(text), &c, &why);
59 if (rc == 0) c2_cnf_free(&c);
60 if ((rc == 0) != (strcmp(want, "CNF-OK") == 0) || strcmp(why, want) != 0) {
61 printf("FAIL %s: rc=%d why=%s, want %s\n", name, rc, why, want);
62 fails++;
63 }
64}
65
66int main(void)
67{
68 /* accepted */
69 lrat("valid", UNSAT4, GOOD, "LRAT-OK");
70 lrat("valid-with-deletions", UNSAT4, "5 1 0 1 2 0\n5 d 1 2 0\n6 0 5 3 4 0\n", "LRAT-OK");
71 lrat("valid-comments", UNSAT4, "c proof\n5 1 0 1 2 0\n\nc next\n6 0 5 3 4 0\n", "LRAT-OK");
72 lrat("valid-trailing-hints", UNSAT4, "5 1 0 1 2 3 0\n6 0 5 3 4 0\n", "LRAT-OK");
73 lrat("valid-tautology-step", UNSAT4, "5 1 -1 0 0\n6 1 0 1 2 0\n7 0 6 3 4 0\n", "LRAT-OK");
74 lrat("valid-empty-input-clause", "p cnf 1 2\n1 0\n0\n", "3 0 2 0\n", "LRAT-OK");
75 /* refused, one per rule */
76 lrat("hint-satisfied", UNSAT4, "5 1 0 3 0\n", "LRAT-HINT-NOT-UNIT");
77 lrat("hint-two-free", UNSAT4, "5 0 1 0\n", "LRAT-HINT-NOT-UNIT");
78 lrat("hint-unknown", UNSAT4, "5 1 0 1 9 0\n", "LRAT-HINT-UNKNOWN");
79 lrat("hint-deleted", UNSAT4, "4 d 2 0\n5 1 0 1 2 0\n6 0 5 3 4 0\n", "LRAT-HINT-UNKNOWN");
80 lrat("rat-hint", UNSAT4, "5 1 0 -1 2 0\n", "LRAT-RAT-UNSUPPORTED");
81 lrat("no-conflict", UNSAT4, "5 1 0 1 0\n", "LRAT-NO-CONFLICT");
82 lrat("no-hints", UNSAT4, "5 0 0\n", "LRAT-NO-CONFLICT");
83 lrat("no-empty-clause", UNSAT4, "5 1 0 1 2 0\n", "LRAT-NO-EMPTY-CLAUSE");
84 lrat("empty-proof", UNSAT4, "", "LRAT-NO-EMPTY-CLAUSE");
85 lrat("id-repeat", UNSAT4, "5 1 0 1 2 0\n5 0 5 3 4 0\n", "LRAT-ID-ORDER");
86 lrat("id-input-range", UNSAT4, "4 1 0 1 2 0\n", "LRAT-ID-ORDER");
87 /* ids are capped at (input clauses + proof lines): a huge id must not size the clause table */
88 lrat("id-huge", UNSAT4, "5 1 0 1 2 0\n99999999 0 5 3 4 0\n", "LRAT-ID-RANGE");
89 lrat("id-huge-first", UNSAT4, "100000000 1 0 1 2 0\n", "LRAT-ID-RANGE");
90 lrat("id-at-cap", UNSAT4, "5 1 0 1 2 0\n7 0 5 3 4 0\n", "LRAT-OK");
91 lrat("id-past-cap", UNSAT4, "5 1 0 1 2 0\n8 0 5 3 4 0\n", "LRAT-ID-RANGE");
92 lrat("literal-range", UNSAT4, "5 7 0 1 2 0\n", "LRAT-LITERAL");
93 lrat("literal-range-neg", UNSAT4, "5 -3 0 1 2 0\n", "LRAT-LITERAL");
94 lrat("truncated", UNSAT4, "5 1 0 1 2", "LRAT-SYNTAX");
95 lrat("garbage", UNSAT4, "5 x 0\n", "LRAT-SYNTAX");
96 lrat("id-zero", UNSAT4, "0 1 0 1 2 0\n", "LRAT-SYNTAX");
97 lrat("delete-unknown", UNSAT4, "4 d 9 0\n5 1 0 1 2 0\n6 0 5 3 4 0\n", "LRAT-DELETE-UNKNOWN");
98 lrat("delete-twice", UNSAT4, "4 d 1 1 0\n", "LRAT-DELETE-UNKNOWN");
99 lrat("satisfiable-formula", "p cnf 2 2\n1 2 0\n-1 -2 0\n", "3 0 1 2 0\n", "LRAT-HINT-NOT-UNIT");
100 lrat("conclusion-on-wrong-formula", "p cnf 2 3\n1 2 0\n1 -2 0\n-1 2 0\n", "4 1 0 1 2 0\n5 0 4 3 0\n",
101 "LRAT-NO-CONFLICT");
102
103 /* DIMACS reader */
104 cnf("cnf-ok", UNSAT4, "CNF-OK");
105 cnf("cnf-no-header", "1 2 0\n", "CNF-HEADER");
106 cnf("cnf-bad-header", "p cnf x 2\n", "CNF-HEADER");
107 cnf("cnf-count-high", "p cnf 2 3\n1 2 0\n", "CNF-COUNT");
108 cnf("cnf-count-low", "p cnf 2 1\n1 2 0\n-1 0\n", "CNF-COUNT");
109 cnf("cnf-unterminated", "p cnf 2 1\n1 2\n", "CNF-COUNT");
110 cnf("cnf-literal-range", "p cnf 2 1\n1 3 0\n", "CNF-LITERAL");
111 cnf("cnf-garbage", "p cnf 2 1\n1 y 0\n", "CNF-LITERAL");
112
113 /* robustness: byte corruptions of a valid proof against a satisfiable twin never verify, and
114 * against the unsatisfiable formula never crash */
115 const char *sat2 = "p cnf 2 4\n1 2 0\n1 -2 0\n-1 2 0\n2 0\n";
116 C2Cnf u, s;
117 const char *why;
118 if (c2_cnf_parse(UNSAT4, strlen(UNSAT4), &u, &why) || c2_cnf_parse(sat2, strlen(sat2), &s, &why)) return 2;
119 char buf[64];
120 size_t n = strlen(GOOD);
121 unsigned long x = 12345;
122 long sat_accepts = 0, runs = 0;
123 for (int it = 0; it < 20000; it++) {
124 memcpy(buf, GOOD, n + 1);
125 x = x * 6364136223846793005UL + 1442695040888963407UL;
126 size_t at = (x >> 33) % n;
127 buf[at] = " 0123456789-d\nc"[(x >> 20) % 15];
128 C2LratResult r;
129 c2_lrat_check(&u, buf, n, &r);
130 if (c2_lrat_check(&s, buf, n, &r) == 0) sat_accepts++;
131 runs++;
132 }
133 cases++;
134 if (sat_accepts) { printf("FAIL corruption: %ld proofs verified a satisfiable formula\n", sat_accepts); fails++; }
135 c2_cnf_free(&u);
136 c2_cnf_free(&s);
137
138 printf("test_c2_lrat: %d cases, %ld corruptions, %d failures\n", cases, runs, fails);
139 return fails ? 1 : 0;
140}