| 1 |
/****************************************************************************** |
| 2 |
* Top contributors (to current version): |
| 3 |
* Daniel Larraz |
| 4 |
* |
| 5 |
* This file is part of the cvc5 project. |
| 6 |
* |
| 7 |
* Copyright (c) 2009-2025 by the authors listed in the file AUTHORS |
| 8 |
* in the top-level source directory and their institutional affiliations. |
| 9 |
* All rights reserved. See the file COPYING in the top-level source |
| 10 |
* directory for licensing information. |
| 11 |
* **************************************************************************** |
| 12 |
* |
| 13 |
* "Ouroborous" test: does cvc5 read its own output? |
| 14 |
* |
| 15 |
* The "Ouroborous" test, named after the serpent that swallows its |
| 16 |
* own tail, ensures that cvc5 can parse some input, output it again |
| 17 |
* (in any of its languages) and then parse it again. The result of |
| 18 |
* the first parse must be equal to the result of the second parse; |
| 19 |
* both strings and expressions are compared for equality. |
| 20 |
* |
| 21 |
* To add a new test, simply add a call to runTestString() under |
| 22 |
* runTest(), below. If you don't specify an input language, |
| 23 |
* LANG_SMTLIB_V2 is used. If your example depends on symbolic constants, |
| 24 |
* you'll need to declare them in the "declarations" global, just |
| 25 |
* below, in SMT-LIBv2 form (but they're good for all languages). |
| 26 |
*/ |
| 27 |
|
| 28 |
import io.github.cvc5.*; |
| 29 |
import io.github.cvc5.modes.*; |
| 30 |
import java.io.*; |
| 31 |
|
| 32 |
public class Ouroborous |
| 33 |
{ |
| 34 |
public static void main(String[] args) |
| 35 |
{ |
| 36 |
try |
| 37 |
{ |
| 38 |
System.exit(runTest()); |
| 39 |
} |
| 40 |
catch (CVC5ApiException e) |
| 41 |
{ |
| 42 |
System.err.println(e.getMessage()); |
| 43 |
} |
| 44 |
catch (Exception e) |
| 45 |
{ |
| 46 |
System.err.println("non-cvc5 exception thrown"); |
| 47 |
e.printStackTrace(); |
| 48 |
} |
| 49 |
System.exit(1); |
| 50 |
} |
| 51 |
|
| 52 |
private static String parse(String instr, String inputLanguage, String outputLanguage) |
| 53 |
throws CVC5ApiException |
| 54 |
{ |
| 55 |
assert inputLanguage.equals("smt2"); |
| 56 |
assert outputLanguage.equals("smt2"); |
| 57 |
|
| 58 |
String declarations = "(set-logic ALL)\n" |
| 59 |
+ "(declare-sort U 0)\n" |
| 60 |
+ "(declare-fun f (U) U)\n" |
| 61 |
+ "(declare-fun x () U)\n" |
| 62 |
+ "(declare-fun y () U)\n" |
| 63 |
+ "(assert (= (f x) x))\n" |
| 64 |
+ "(declare-fun a () (Array U (Array U U)))\n"; |
| 65 |
|
| 66 |
TermManager tm = new TermManager(); |
| 67 |
Solver solver = new Solver(tm); |
| 68 |
|
| 69 |
solver.setOption("input-language", inputLanguage); |
| 70 |
solver.setOption("output-language", outputLanguage); |
| 71 |
SymbolManager symman = new SymbolManager(tm); |
| 72 |
InputParser parser = new InputParser(solver, symman); |
| 73 |
|
| 74 |
parser.setStringInput(InputLanguage.SMT_LIB_2_6, declarations, "internal-buffer"); |
| 75 |
|
| 76 |
Command c; |
| 77 |
while (true) |
| 78 |
{ |
| 79 |
c = parser.nextCommand(); |
| 80 |
if (c == null || c.isNull()) |
| 81 |
{ |
| 82 |
break; |
| 83 |
} |
| 84 |
c.invoke(solver, symman); |
| 85 |
} |
| 86 |
assert parser.done(); |
| 87 |
|
| 88 |
parser.setStringInput(InputLanguage.SMT_LIB_2_6, instr, "internal-buffer"); |
| 89 |
|
| 90 |
Term e = parser.nextTerm(); |
| 91 |
String s = e.toString(); |
| 92 |
assert parser.nextTerm() == null; // should be no more terms |
| 93 |
return s; |
| 94 |
} |
| 95 |
|
| 96 |
private static String translate(String instr, String inputLanguage, String outputLanguage) |
| 97 |
throws CVC5ApiException |
| 98 |
{ |
| 99 |
assert inputLanguage.equals("smt2"); |
| 100 |
assert outputLanguage.equals("smt2"); |
| 101 |
|
| 102 |
System.out.println("=============================================="); |
| 103 |
System.out.println( |
| 104 |
"translating from " + inputLanguage + " to " + outputLanguage + " this string:"); |
| 105 |
System.out.println(instr); |
| 106 |
|
| 107 |
String outstr = parse(instr, inputLanguage, outputLanguage); |
| 108 |
|
| 109 |
System.out.println("got this:"); |
| 110 |
System.out.println(outstr); |
| 111 |
System.out.println("reparsing as " + outputLanguage); |
| 112 |
|
| 113 |
String poutstr = parse(outstr, outputLanguage, outputLanguage); |
| 114 |
assert outstr.equals(poutstr); |
| 115 |
|
| 116 |
System.out.println("got same expressions " + outstr + " and " + poutstr); |
| 117 |
System.out.println("=============================================="); |
| 118 |
|
| 119 |
return outstr; |
| 120 |
} |
| 121 |
|
| 122 |
private static void runTestString(String instr, String instrLanguage) throws CVC5ApiException |
| 123 |
{ |
| 124 |
System.out.println(); |
| 125 |
System.out.println("starting with: " + instr); |
| 126 |
System.out.println(" in language " + instrLanguage); |
| 127 |
|
| 128 |
String smt2str = translate(instr, instrLanguage, "smt2"); |
| 129 |
System.out.println("in SMT2 : " + smt2str); |
| 130 |
|
| 131 |
String outstr = translate(smt2str, "smt2", "smt2"); |
| 132 |
System.out.println("to SMT2 : " + outstr); |
| 133 |
System.out.println(); |
| 134 |
|
| 135 |
assert outstr.equals(smt2str); |
| 136 |
} |
| 137 |
|
| 138 |
private static int runTest() throws CVC5ApiException |
| 139 |
{ |
| 140 |
runTestString("(= (f (f y)) x)", "smt2"); |
| 141 |
runTestString("(= ((_ extract 2 1) (bvnot (bvadd #b000 #b011))) #b10)", "smt2"); |
| 142 |
runTestString("((_ extract 2 0) (bvnot (bvadd (bvmul #b001 #b011) #b011)))", "smt2"); |
| 143 |
return 0; |
| 144 |
} |
| 145 |
} |