summaryrefslogtreecommitdiff
path: root/test/unit/expr/node_manager_black.h
blob: e059fa50951ed616ad73aa49e639f5f9e16b0313 (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*********************                                                        */
/** node_manager_black.h
 ** Original author: 
 ** Major contributors: none
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009, 2010  The Analysis of Computer Systems Group (ACSys)
 ** Courant Institute of Mathematical Sciences
 ** New York University
 ** See the file COPYING in the top-level source directory for licensing
 ** information.
 **
 ** White box testing of CVC4::NodeManager.
 **/

#include <cxxtest/TestSuite.h>

#include <string>
#include <vector>

#include "expr/node_manager.h"
#include "context/context.h"

#include "util/rational.h"
#include "util/integer.h"

using namespace CVC4;
using namespace CVC4::expr;
using namespace CVC4::kind;
using namespace CVC4::context;

class NodeManagerBlack : public CxxTest::TestSuite {

  Context* d_context;
  NodeManager* d_nodeManager;
  NodeManagerScope* d_scope;

public:

  void setUp() {
    d_context = new Context;
    d_nodeManager = new NodeManager(d_context);
    d_scope = new NodeManagerScope(d_nodeManager);
  }

  void tearDown() {
    delete d_scope;
    delete d_nodeManager;
    delete d_context;
  }

  void testMkNode1() {
    Node x = d_nodeManager->mkVar("x",d_nodeManager->booleanType());
    Node n = d_nodeManager->mkNode(NOT, x);
    TS_ASSERT_EQUALS( n.getNumChildren(), 1u );
    TS_ASSERT_EQUALS( n.getKind(), NOT);
    TS_ASSERT_EQUALS( n[0], x);
  }

  void testMkNode2() {
    Node x = d_nodeManager->mkVar("x",d_nodeManager->booleanType());
    Node y = d_nodeManager->mkVar("y",d_nodeManager->booleanType());
    Node n = d_nodeManager->mkNode(IMPLIES, x, y);
    TS_ASSERT_EQUALS( n.getNumChildren(), 2u );
    TS_ASSERT_EQUALS( n.getKind(), IMPLIES);
    TS_ASSERT_EQUALS( n[0], x);
    TS_ASSERT_EQUALS( n[1], y);
  }

  void testMkNode3() {
    Node x = d_nodeManager->mkVar("x",d_nodeManager->booleanType());
    Node y = d_nodeManager->mkVar("y",d_nodeManager->booleanType());
    Node z = d_nodeManager->mkVar("z",d_nodeManager->booleanType());
    Node n = d_nodeManager->mkNode(AND, x, y, z);
    TS_ASSERT_EQUALS( n.getNumChildren(), 3u );
    TS_ASSERT_EQUALS( n.getKind(), AND);
    TS_ASSERT_EQUALS( n[0], x);
    TS_ASSERT_EQUALS( n[1], y);
    TS_ASSERT_EQUALS( n[2], z);
  }

  void testMkNode4() {
    Node x1 = d_nodeManager->mkVar("x1",d_nodeManager->booleanType());
    Node x2 = d_nodeManager->mkVar("x2",d_nodeManager->booleanType());
    Node x3 = d_nodeManager->mkVar("x3",d_nodeManager->booleanType());
    Node x4 = d_nodeManager->mkVar("x4",d_nodeManager->booleanType());
    Node n = d_nodeManager->mkNode(AND, x1, x2, x3, x4);
    TS_ASSERT_EQUALS( n.getNumChildren(), 4u );
    TS_ASSERT_EQUALS( n.getKind(), AND );
    TS_ASSERT_EQUALS( n[0], x1 );
    TS_ASSERT_EQUALS( n[1], x2 );
    TS_ASSERT_EQUALS( n[2], x3 );
    TS_ASSERT_EQUALS( n[3], x4 );
  }

  void testMkNode5() {
    Node x1 = d_nodeManager->mkVar("x1",d_nodeManager->booleanType());
    Node x2 = d_nodeManager->mkVar("x2",d_nodeManager->booleanType());
    Node x3 = d_nodeManager->mkVar("x3",d_nodeManager->booleanType());
    Node x4 = d_nodeManager->mkVar("x4",d_nodeManager->booleanType());
    Node x5 = d_nodeManager->mkVar("x5",d_nodeManager->booleanType());
    Node n = d_nodeManager->mkNode(AND, x1, x2, x3, x4, x5);
    TS_ASSERT_EQUALS( n.getNumChildren(), 5u );
    TS_ASSERT_EQUALS( n.getKind(), AND);
    TS_ASSERT_EQUALS( n[0], x1);
    TS_ASSERT_EQUALS( n[1], x2);
    TS_ASSERT_EQUALS( n[2], x3);
    TS_ASSERT_EQUALS( n[3], x4);
    TS_ASSERT_EQUALS( n[4], x5);
  }

  void testMkNodeVecOfNode() {
    Node x1 = d_nodeManager->mkVar("x1",d_nodeManager->booleanType());
    Node x2 = d_nodeManager->mkVar("x2",d_nodeManager->booleanType());
    Node x3 = d_nodeManager->mkVar("x3",d_nodeManager->booleanType());
    std::vector<Node> args;
    args.push_back(x1);
    args.push_back(x2);
    args.push_back(x3);
    Node n = d_nodeManager->mkNode(AND, args);
    TS_ASSERT_EQUALS( n.getNumChildren(), args.size() );
    TS_ASSERT_EQUALS( n.getKind(), AND);
    for( unsigned i = 0; i < args.size(); ++i ) {
      TS_ASSERT_EQUALS( n[i], args[i]);
    }
  }

  void testMkNodeVecOfTNode() {
    Node x1 = d_nodeManager->mkVar("x1",d_nodeManager->booleanType());
    Node x2 = d_nodeManager->mkVar("x2",d_nodeManager->booleanType());
    Node x3 = d_nodeManager->mkVar("x3",d_nodeManager->booleanType());
    std::vector<TNode> args;
    args.push_back(x1);
    args.push_back(x2);
    args.push_back(x3);
    Node n = d_nodeManager->mkNode(AND, args);
    TS_ASSERT_EQUALS( n.getNumChildren(), args.size() );
    TS_ASSERT_EQUALS( n.getKind(), AND);
    for( unsigned i = 0; i < args.size(); ++i ) {
      TS_ASSERT_EQUALS( n[i], args[i]);
    }
  }

  void testMkVar1() {
    Type* booleanType = d_nodeManager->booleanType();
    Node x = d_nodeManager->mkVar(booleanType);
    TS_ASSERT_EQUALS(x.getKind(),VARIABLE);
    TS_ASSERT_EQUALS(x.getNumChildren(),0u);
    TS_ASSERT_EQUALS(x.getType(),booleanType);
  }

  void testMkVar2() {
    Type* booleanType = d_nodeManager->booleanType();
    Node x = d_nodeManager->mkVar("x", booleanType);
    TS_ASSERT_EQUALS(x.getKind(),VARIABLE);
    TS_ASSERT_EQUALS(x.getNumChildren(),0u);
    TS_ASSERT_EQUALS(x.getAttribute(VarNameAttr()),"x");
    TS_ASSERT_EQUALS(x.getType(),booleanType);
  }

  void testMkConstBool() {
    Node tt = d_nodeManager->mkConst(true);
    TS_ASSERT_EQUALS(tt.getConst<bool>(),true);
    Node ff = d_nodeManager->mkConst(false);
    TS_ASSERT_EQUALS(ff.getConst<bool>(),false);
  }


  void testMkConstInt() {
    Integer i = "3";
    Node n = d_nodeManager->mkConst(i);
    TS_ASSERT_EQUALS(n.getConst<Integer>(),i);
  }

  void testMkConstRat() {
    Rational r = "3/2";
    Node n = d_nodeManager->mkConst(r);
    TS_ASSERT_EQUALS(n.getConst<Rational>(),r);
  }

  void testHasOperator() {
    TS_ASSERT( NodeManager::hasOperator(AND) );
    TS_ASSERT( NodeManager::hasOperator(OR) );
    TS_ASSERT( NodeManager::hasOperator(NOT) );
    TS_ASSERT( NodeManager::hasOperator(APPLY_UF) );
    TS_ASSERT( !NodeManager::hasOperator(VARIABLE) );
  }

  void testBooleanType() {
    Type* booleanType1 = d_nodeManager->booleanType();
    Type* booleanType2 = d_nodeManager->booleanType();
    Type* otherType = d_nodeManager->mkSort("T");
    TS_ASSERT( booleanType1->isBoolean() );
    TS_ASSERT_EQUALS(booleanType1, booleanType2);
    TS_ASSERT( booleanType1 != otherType );
  }

  void testKindType() {
    Type* kindType1 = d_nodeManager->kindType();
    Type* kindType2 = d_nodeManager->kindType();
    Type* otherType = d_nodeManager->mkSort("T");
    TS_ASSERT( kindType1->isKind() );
    TS_ASSERT_EQUALS(kindType1, kindType2);
    TS_ASSERT( kindType1 != otherType );
    // TODO: Is there a way to get the type of otherType (it should == kindType)
  }

  /* TODO: All of the type comparisons in all of the following tests are
   * strictly for pointer equality. This is too weak for disequalities and
   * too strong for equalities! Also, it means we can't really check for
   * structural equality between two identical calls to mkFunctionType (see
   * the commented out assertions at the bottom of each test). Unfortunately,
   * with the current type implementation it's the best we can do. */

  void testMkFunctionType2() {
    Type *booleanType = d_nodeManager->booleanType();
    Type *t = d_nodeManager->mkFunctionType(booleanType,booleanType);
    TS_ASSERT( t != booleanType );
    TS_ASSERT( t->isFunction() );

    FunctionType* ft = (FunctionType*)t;
    TS_ASSERT_EQUALS(ft->getArgTypes().size(), 1u);
    TS_ASSERT_EQUALS(ft->getArgTypes()[0], booleanType);
    TS_ASSERT_EQUALS(ft->getRangeType(), booleanType);

/*    Type *t2 = d_nodeManager->mkFunctionType(booleanType,booleanType);
    TS_ASSERT_EQUALS( t, t2 );*/
  }

  void testMkFunctionTypeVecType() {
    Type *a = d_nodeManager->mkSort("a");
    Type *b = d_nodeManager->mkSort("b");
    Type *c = d_nodeManager->mkSort("c");

    std::vector<Type*> argTypes;
    argTypes.push_back(a);
    argTypes.push_back(b);

    Type *t = d_nodeManager->mkFunctionType(argTypes,c);

    TS_ASSERT( t != a );
    TS_ASSERT( t != b );
    TS_ASSERT( t != c );
    TS_ASSERT( t->isFunction() );

    FunctionType* ft = (FunctionType*)t;
    TS_ASSERT_EQUALS(ft->getArgTypes().size(), argTypes.size());
    TS_ASSERT_EQUALS(ft->getArgTypes()[0], a);
    TS_ASSERT_EQUALS(ft->getArgTypes()[1], b);
    TS_ASSERT_EQUALS(ft->getRangeType(), c);

/*    Type *t2 = d_nodeManager->mkFunctionType(argTypes,c);
    TS_ASSERT_EQUALS( t, t2 );*/
  }

  void testMkFunctionTypeVec() {
    Type *a = d_nodeManager->mkSort("a");
    Type *b = d_nodeManager->mkSort("b");
    Type *c = d_nodeManager->mkSort("c");

    std::vector<Type*> types;
    types.push_back(a);
    types.push_back(b);
    types.push_back(c);

    Type *t = d_nodeManager->mkFunctionType(types);

    TS_ASSERT( t != a );
    TS_ASSERT( t != b );
    TS_ASSERT( t != c );
    TS_ASSERT( t->isFunction() );

    FunctionType* ft = (FunctionType*)t;
    TS_ASSERT_EQUALS(ft->getArgTypes().size(), types.size()-1);
    TS_ASSERT_EQUALS(ft->getArgTypes()[0], a);
    TS_ASSERT_EQUALS(ft->getArgTypes()[1], b);
    TS_ASSERT_EQUALS(ft->getRangeType(), c);

/*    Type *t2 = d_nodeManager->mkFunctionType(types);
    TS_ASSERT_EQUALS( t, t2 );*/
  }

  void testMkPredicateType() {
    Type *a = d_nodeManager->mkSort("a");
    Type *b = d_nodeManager->mkSort("b");
    Type *c = d_nodeManager->mkSort("c");
    Type *booleanType = d_nodeManager->booleanType();

    std::vector<Type*> argTypes;
    argTypes.push_back(a);
    argTypes.push_back(b);
    argTypes.push_back(c);

    Type *t = d_nodeManager->mkPredicateType(argTypes);

    TS_ASSERT( t != a );
    TS_ASSERT( t != b );
    TS_ASSERT( t != c );
    TS_ASSERT( t != booleanType );
    TS_ASSERT( t->isFunction() );

    FunctionType* ft = (FunctionType*)t;
    TS_ASSERT_EQUALS(ft->getArgTypes().size(), argTypes.size());
    TS_ASSERT_EQUALS(ft->getArgTypes()[0], a);
    TS_ASSERT_EQUALS(ft->getArgTypes()[1], b);
    TS_ASSERT_EQUALS(ft->getArgTypes()[2], c);
    TS_ASSERT_EQUALS(ft->getRangeType(), booleanType);

//    Type *t2 = d_nodeManager->mkPredicateType(argTypes);
//    TS_ASSERT_EQUALS( t, t2 );
  }
};
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback