File: GrammarTest.java

package info (click to toggle)
cvc5 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 87,260 kB
  • sloc: cpp: 383,850; java: 12,207; python: 12,090; sh: 5,679; ansic: 4,729; lisp: 763; perl: 208; makefile: 38
file content (235 lines) | stat: -rw-r--r-- 7,457 bytes parent folder | download
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/******************************************************************************
 * Top contributors (to current version):
 *   Aina Niemetz, Mudathir Mohamed, Andrew Reynolds
 *
 * This file is part of the cvc5 project.
 *
 * Copyright (c) 2009-2025 by the authors listed in the file AUTHORS
 * in the top-level source directory and their institutional affiliations.
 * All rights reserved.  See the file COPYING in the top-level source
 * directory for licensing information.
 * ****************************************************************************
 *
 * Black box testing of the guards of the Java API functions.
 */

package tests;
import static io.github.cvc5.Kind.*;
import static org.junit.jupiter.api.Assertions.*;

import io.github.cvc5.*;
import java.util.Arrays;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class GrammarTest
{
  private TermManager d_tm;
  private Solver d_solver;

  @BeforeEach
  void setUp()
  {
    d_tm = new TermManager();
    d_solver = new Solver(d_tm);
  }

  @AfterEach
  void tearDown()
  {
    Context.deletePointers();
  }

  @Test
  void testToString()
  {
    d_solver.setOption("sygus", "true");
    Sort bool = d_tm.getBooleanSort();
    Term start = d_tm.mkVar(bool);
    Grammar g = d_solver.mkGrammar(new Term[] {}, new Term[] {start});
    assertFalse(g.isNull());
    g.addRule(start, d_tm.mkBoolean(false));
    g.toString();
  }

  @Test
  void addRule()
  {
    d_solver.setOption("sygus", "true");
    Sort bool = d_tm.getBooleanSort();
    Sort integer = d_tm.getIntegerSort();

    Term nullTerm = new Term();
    Term start = d_tm.mkVar(bool);
    Term nts = d_tm.mkVar(bool);

    Grammar g = d_solver.mkGrammar(new Term[] {}, new Term[] {start});

    assertDoesNotThrow(() -> g.addRule(start, d_tm.mkBoolean(false)));

    assertThrows(CVC5ApiException.class, () -> g.addRule(nullTerm, d_tm.mkBoolean(false)));
    assertThrows(CVC5ApiException.class, () -> g.addRule(start, nullTerm));
    assertThrows(CVC5ApiException.class, () -> g.addRule(nts, d_tm.mkBoolean(false)));
    assertThrows(CVC5ApiException.class, () -> g.addRule(start, d_tm.mkInteger(0)));

    d_solver.synthFun("f", new Term[] {}, bool, g);

    assertThrows(CVC5ApiException.class, () -> g.addRule(start, d_tm.mkBoolean(false)));
  }

  @Test
  void addRules()
  {
    d_solver.setOption("sygus", "true");
    Sort bool = d_tm.getBooleanSort();
    Sort integer = d_tm.getIntegerSort();

    Term nullTerm = new Term();
    Term start = d_tm.mkVar(bool);
    Term nts = d_tm.mkVar(bool);

    Grammar g = d_solver.mkGrammar(new Term[] {}, new Term[] {start});

    assertDoesNotThrow(() -> g.addRules(start, new Term[] {d_tm.mkBoolean(false)}));

    assertThrows(
        CVC5ApiException.class, () -> g.addRules(nullTerm, new Term[] {d_tm.mkBoolean(false)}));
    assertThrows(CVC5ApiException.class, () -> g.addRules(start, new Term[] {nullTerm}));
    assertThrows(CVC5ApiException.class, () -> g.addRules(nts, new Term[] {d_tm.mkBoolean(false)}));
    assertThrows(CVC5ApiException.class, () -> g.addRules(start, new Term[] {d_tm.mkInteger(0)}));

    d_solver.synthFun("f", new Term[] {}, bool, g);

    assertThrows(
        CVC5ApiException.class, () -> g.addRules(start, new Term[] {d_tm.mkBoolean(false)}));
  }

  @Test
  void addAnyConstant()
  {
    d_solver.setOption("sygus", "true");
    Sort bool = d_tm.getBooleanSort();

    Term nullTerm = new Term();
    Term start = d_tm.mkVar(bool);
    Term nts = d_tm.mkVar(bool);

    Grammar g = d_solver.mkGrammar(new Term[] {}, new Term[] {start});

    assertDoesNotThrow(() -> g.addAnyConstant(start));
    assertDoesNotThrow(() -> g.addAnyConstant(start));

    assertThrows(CVC5ApiException.class, () -> g.addAnyConstant(nullTerm));
    assertThrows(CVC5ApiException.class, () -> g.addAnyConstant(nts));

    d_solver.synthFun("f", new Term[] {}, bool, g);

    assertThrows(CVC5ApiException.class, () -> g.addAnyConstant(start));
  }

  @Test
  void addAnyVariable()
  {
    d_solver.setOption("sygus", "true");
    Sort bool = d_tm.getBooleanSort();

    Term nullTerm = new Term();
    Term x = d_tm.mkVar(bool);
    Term start = d_tm.mkVar(bool);
    Term nts = d_tm.mkVar(bool);

    Grammar g1 = d_solver.mkGrammar(new Term[] {x}, new Term[] {start});
    Grammar g2 = d_solver.mkGrammar(new Term[] {}, new Term[] {start});

    assertDoesNotThrow(() -> g1.addAnyVariable(start));
    assertDoesNotThrow(() -> g1.addAnyVariable(start));
    assertDoesNotThrow(() -> g2.addAnyVariable(start));

    assertThrows(CVC5ApiException.class, () -> g1.addAnyVariable(nullTerm));
    assertThrows(CVC5ApiException.class, () -> g1.addAnyVariable(nts));

    d_solver.synthFun("f", new Term[] {}, bool, g1);

    assertThrows(CVC5ApiException.class, () -> g1.addAnyVariable(start));
  }

  @Test
  void equalHash()
  {
    d_solver.setOption("sygus", "true");

    Sort bool = d_tm.getBooleanSort();
    Term x = d_tm.mkVar(bool, "x");
    Term start1 = d_tm.mkVar(bool, "start");
    Term start2 = d_tm.mkVar(bool, "start");
    Grammar g1, g2;

    {
      g1 = d_solver.mkGrammar(new Term[] {}, new Term[] {start1});
      g2 = d_solver.mkGrammar(new Term[] {}, new Term[] {start1});
      assertEquals(g1.hashCode(), g1.hashCode());
      assertEquals(g1.hashCode(), g2.hashCode());
      assertTrue(g1.equals(g1));
      assertFalse(g1.equals(g2));
    }

    {
      g1 = d_solver.mkGrammar(new Term[] {}, new Term[] {start1});
      g2 = d_solver.mkGrammar(new Term[] {x}, new Term[] {start1});
      assertNotEquals(g1.hashCode(), g2.hashCode());
      assertTrue(g1.equals(g1));
      assertFalse(g1.equals(g2));
    }

    {
      g1 = d_solver.mkGrammar(new Term[] {x}, new Term[] {start1});
      g2 = d_solver.mkGrammar(new Term[] {x}, new Term[] {start2});
      assertNotEquals(g1.hashCode(), g2.hashCode());
      assertTrue(g1.equals(g1));
      assertFalse(g1.equals(g2));
    }

    {
      g1 = d_solver.mkGrammar(new Term[] {x}, new Term[] {start1});
      g2 = d_solver.mkGrammar(new Term[] {x}, new Term[] {start1});
      g2.addAnyVariable(start1);
      assertNotEquals(g1.hashCode(), g2.hashCode());
      assertTrue(g1.equals(g1));
      assertFalse(g1.equals(g2));
    }

    {
      g1 = d_solver.mkGrammar(new Term[] {x}, new Term[] {start1});
      g2 = d_solver.mkGrammar(new Term[] {x}, new Term[] {start1});
      Term[] rules = new Term[] {d_tm.mkFalse()};
      g1.addRules(start1, rules);
      g2.addRules(start1, rules);
      assertEquals(g1.hashCode(), g2.hashCode());
      assertTrue(g1.equals(g1));
      assertFalse(g1.equals(g2));
    }

    {
      g1 = d_solver.mkGrammar(new Term[] {x}, new Term[] {start1});
      g2 = d_solver.mkGrammar(new Term[] {x}, new Term[] {start1});
      Term[] rules2 = new Term[] {d_tm.mkFalse()};
      g2.addRules(start1, rules2);
      assertNotEquals(g1.hashCode(), g2.hashCode());
      assertTrue(g1.equals(g1));
      assertFalse(g1.equals(g2));
    }

    {
      g1 = d_solver.mkGrammar(new Term[] {x}, new Term[] {start1});
      g2 = d_solver.mkGrammar(new Term[] {x}, new Term[] {start1});
      Term[] rules1 = new Term[] {d_tm.mkTrue()};
      Term[] rules2 = new Term[] {d_tm.mkFalse()};
      g1.addRules(start1, rules1);
      g2.addRules(start1, rules2);
      assertNotEquals(g1.hashCode(), g2.hashCode());
      assertTrue(g1.equals(g1));
      assertFalse(g1.equals(g2));
    }
  }
}