8
8
9
9
package org .sosy_lab .java_smt .solvers .yices2 ;
10
10
11
+ import static com .google .common .base .Preconditions .checkArgument ;
11
12
import static com .google .common .truth .Truth .assertThat ;
12
13
import static org .junit .Assert .assertThrows ;
13
14
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .YICES_APP_TERM ;
16
17
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .YICES_BV_CONST ;
17
18
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .YICES_BV_SUM ;
18
19
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .YICES_EQ_TERM ;
20
+ import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .YICES_FORALL_TERM ;
19
21
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .YICES_NOT_TERM ;
20
22
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .YICES_OR_TERM ;
23
+ import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .YICES_POWER_PRODUCT ;
21
24
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_add ;
22
25
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_and ;
23
26
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_and2 ;
42
45
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_check_context ;
43
46
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_context_disable_option ;
44
47
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_eq ;
48
+ import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_exists ;
45
49
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_exit ;
46
50
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_false ;
51
+ import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_forall ;
47
52
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_free_config ;
48
53
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_free_context ;
49
54
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_function_type ;
55
+ import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_get_term_by_name ;
50
56
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_get_term_name ;
51
57
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_idiv ;
52
58
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_iff ;
53
59
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_init ;
54
60
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_int32 ;
55
61
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_int64 ;
56
62
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_int_type ;
63
+ import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_is_thread_safe ;
57
64
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_mul ;
58
- import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_named_variable ;
59
65
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_new_config ;
60
66
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_new_context ;
61
67
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_new_uninterpreted_term ;
68
+ import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_new_variable ;
62
69
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_not ;
63
70
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_or ;
64
71
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_or2 ;
82
89
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_term_num_children ;
83
90
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_term_to_string ;
84
91
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_true ;
92
+ import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_type_of_term ;
93
+ import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_type_to_string ;
85
94
import static org .sosy_lab .java_smt .solvers .yices2 .Yices2NativeApi .yices_zero_extend ;
86
95
87
96
import com .google .common .base .Joiner ;
88
- import com .google .common .base .Preconditions ;
89
97
import com .google .common .collect .Lists ;
90
98
import com .google .common .primitives .Ints ;
91
99
import java .math .BigInteger ;
92
- import java .util .ArrayList ;
93
100
import java .util .Arrays ;
94
- import java .util .List ;
95
101
import org .junit .After ;
96
102
import org .junit .AssumptionViolatedException ;
97
103
import org .junit .Before ;
@@ -196,6 +202,7 @@ public void arithAddUNSAT() {
196
202
assertThat (yices_check_context (env , 0 )).isEqualTo (UNSAT );
197
203
}
198
204
205
+ @ SuppressWarnings ("CheckReturnValue" )
199
206
@ Test
200
207
public void rationalError () {
201
208
assertThrows (IllegalArgumentException .class , () -> yices_rational32 (1 , 0 ));
@@ -204,10 +211,10 @@ public void rationalError() {
204
211
@ Test
205
212
public void negativeRationalError () {
206
213
// TODO negative unsigned integer causes no error. Need to ensure positive value before
207
- int rat = yices_rational32 (1 , -5 );
208
- System .out .println (rat ); // "use" variable
214
+ assertThat (yices_rational32 (1 , -5 )).isGreaterThan (0 );
209
215
}
210
216
217
+ @ SuppressWarnings ("CheckReturnValue" )
211
218
@ Test
212
219
public void wrongType () {
213
220
int one = yices_int32 (1 );
@@ -300,7 +307,7 @@ public void bvValueTest() {
300
307
int bv = yices_bvconst_int64 (4 , value );
301
308
if (yices_term_constructor (bv ) == YICES_BV_CONST ) {
302
309
int [] littleEndianBV = yices_bv_const_value (bv , yices_term_bitsize (bv ));
303
- Preconditions . checkArgument (littleEndianBV .length != 0 , "BV was empty" );
310
+ checkArgument (littleEndianBV .length != 0 , "BV was empty" );
304
311
String bigEndianBV = Joiner .on ("" ).join (Lists .reverse (Ints .asList (littleEndianBV )));
305
312
BigInteger big = new BigInteger (bigEndianBV , 2 );
306
313
assertThat (big ).isEqualTo (BigInteger .valueOf (value ));
@@ -473,80 +480,100 @@ public void sumComponents() {
473
480
int [] oneX = {three , x };
474
481
int sumOneX = yices_sum (2 , oneX );
475
482
for (int i = 0 ; i < yices_term_num_children (sumOneX ); i ++) {
476
- System . out . println (yices_term_to_string (sumOneX ));
477
- System . out . println (Arrays .toString (yices_sum_component (sumOneX , i )));
483
+ assertThat (yices_term_to_string (sumOneX )). isNotNull ( );
484
+ assertThat (Arrays .toString (yices_sum_component (sumOneX , i ))). isNotNull ( );
478
485
}
479
486
int [] twoX = {three , x , x };
480
487
int sumTwoX = yices_sum (3 , twoX );
481
488
for (int i = 0 ; i < yices_term_num_children (sumTwoX ); i ++) {
482
- System . out . println (yices_term_to_string (sumTwoX ));
483
- System . out . println (Arrays .toString (yices_sum_component (sumTwoX , i )));
489
+ assertThat (yices_term_to_string (sumTwoX )). isNotNull ( );
490
+ assertThat (Arrays .toString (yices_sum_component (sumTwoX , i ))). isNotNull ( );
484
491
}
485
492
int [] twoThrees = {three , x , three };
486
493
int sumTwoThrees = yices_sum (3 , twoThrees );
487
494
for (int i = 0 ; i < yices_term_num_children (sumTwoThrees ); i ++) {
488
- System . out . println (yices_term_to_string (sumTwoThrees ));
489
- System . out . println (Arrays .toString (yices_sum_component (sumTwoThrees , i )));
495
+ assertThat (yices_term_to_string (sumTwoThrees )). isNotNull ( );
496
+ assertThat (Arrays .toString (yices_sum_component (sumTwoThrees , i ))). isNotNull ( );
490
497
}
491
498
int xTimesRational = yices_mul (rat , x );
492
499
int [] ratSum = {three , xTimesRational };
493
500
int sumRatX = yices_sum (2 , ratSum );
494
501
for (int i = 0 ; i < yices_term_num_children (sumRatX ); i ++) {
495
- System . out . println (yices_term_to_string (sumRatX ));
496
- System . out . println (Arrays .toString (yices_sum_component (sumRatX , i )));
502
+ assertThat (yices_term_to_string (sumRatX )). isNotNull ( );
503
+ assertThat (Arrays .toString (yices_sum_component (sumRatX , i ))). isNotNull ( );
497
504
}
498
505
}
499
506
500
507
@ Test
501
508
public void bvSumComponents () {
502
- int bv1 = yices_parse_bvbin ("00101" );
509
+ String bv1StringValue = "00101" ;
510
+ int bv1 = yices_parse_bvbin (bv1StringValue );
503
511
int bv5type = yices_bv_type (5 );
504
512
int x = yices_named_variable (bv5type , "x" );
505
513
int negativeX = yices_bvmul (yices_bvconst_minus_one (5 ), x );
506
514
int add = yices_bvadd (bv1 , negativeX );
507
- for (int i = 0 ; i < yices_term_num_children (add ); i ++) {
508
- System .out .println (yices_term_to_string (add ));
509
- int [] component = yices_bvsum_component (add , i , yices_term_bitsize (add ));
510
- String value =
511
- Joiner .on ("" )
512
- .join (
513
- Lists .reverse (
514
- Ints .asList (Arrays .copyOfRange (component , 0 , component .length - 1 ))));
515
- int term = component [component .length - 1 ];
516
- System .out .println ("Value of coefficient: " + value );
517
- System .out .println ("Coefficient as BigInt: " + new BigInteger (value , 2 ));
518
- System .out .println ("Term id: " + term );
519
- }
520
- }
521
-
522
- @ Test
515
+ assertThat (yices_term_num_children (add )).isEqualTo (2 );
516
+ assertThat (yices_term_to_string (add )).isNotNull ();
517
+
518
+ int [] component1 = yices_bvsum_component (add , 0 , yices_term_bitsize (add ));
519
+ String value1 =
520
+ Joiner .on ("" )
521
+ .join (
522
+ Lists .reverse (
523
+ Ints .asList (Arrays .copyOfRange (component1 , 0 , component1 .length - 1 ))));
524
+ int term1 = component1 [component1 .length - 1 ];
525
+ // Value of coefficient
526
+ assertThat (value1 ).isEqualTo (bv1StringValue );
527
+ // Coefficient as BigInt
528
+ assertThat (new BigInteger (value1 , 2 )).isEqualTo (BigInteger .valueOf (5 ));
529
+ // Term id is NULL (-1) for i = 0
530
+ assertThat (term1 ).isEqualTo (-1 );
531
+
532
+ int [] component2 = yices_bvsum_component (add , 1 , yices_term_bitsize (add ));
533
+ String value2 =
534
+ Joiner .on ("" )
535
+ .join (
536
+ Lists .reverse (
537
+ Ints .asList (Arrays .copyOfRange (component2 , 0 , component2 .length - 1 ))));
538
+ int term2 = component2 [component2 .length - 1 ];
539
+ // Value of coefficient (-1 == 11111)
540
+ assertThat (value2 ).isEqualTo ("11111" );
541
+ // Coefficient as BigInt (31 because it has no sign bit, and -1 is max for bv)
542
+ assertThat (new BigInteger (value2 , 2 )).isEqualTo (BigInteger .valueOf (31 ));
543
+ // Term id is NULL (-1) for i = 0
544
+ assertThat (term2 ).isEqualTo (x );
545
+ }
546
+
547
+ @ SuppressWarnings ("CheckReturnValue" )
548
+ @ Test (expected = IllegalArgumentException .class )
523
549
public void bvExtensionStructureTest () {
550
+ int initialSize = 5 ;
524
551
int extendBy = 5 ;
525
- int x = yices_named_variable (yices_bv_type (5 ), "x" );
526
- List < Integer > terms = new ArrayList <>( );
527
- terms . add ( yices_sign_extend ( x , extendBy ) );
528
- terms . add ( yices_sign_extend ( x , extendBy ));
529
- terms . add ( yices_zero_extend ( x , extendBy ) );
530
- terms . add ( yices_zero_extend ( x , extendBy ) );
531
- for ( int t : terms ) {
532
- System . out . println ( "--------BEGIN-------" );
533
- System . out . println ( yices_term_to_string ( t ) );
534
- for ( int i = 0 ; i < yices_term_num_children ( t ); i ++) {
535
- System . out . println ( yices_term_to_string ( yices_term_child ( t , i )));
536
- }
537
- int bv = yices_proj_arg ( yices_term_child ( t , 0 ) );
538
- int bvSize = yices_term_bitsize ( bv ) ;
539
- int extendedBy = yices_term_num_children ( t ) - bvSize ;
540
- System . out . println ( "Extended by: " + extendedBy );
541
- if ( extendedBy != 0 ) {
542
- if ( yices_term_child ( t , bvSize ) == yices_false ()) {
543
- System . out . println ( "Zero-Extend" ) ;
544
- } else {
545
- System . out . println ( "Sign-extend" );
546
- }
547
- }
548
- System . out . println ( "--------END-------" );
549
- }
552
+ int x = yices_named_variable (yices_bv_type (initialSize ), "x" );
553
+ int signExtendedX = yices_sign_extend ( x , extendBy );
554
+ int zeroExtendedX = yices_zero_extend ( x , extendBy );
555
+
556
+ assertThat ( yices_term_to_string ( x )). isNotNull ( );
557
+ assertThat ( yices_term_num_children ( x )). isEqualTo ( 0 );
558
+ assertThat ( yices_term_num_children ( signExtendedX )). isEqualTo ( initialSize + extendBy );
559
+ assertThat ( yices_term_to_string ( signExtendedX )). isNotNull ( );
560
+ assertThat ( yices_term_num_children ( zeroExtendedX )). isEqualTo ( initialSize + extendBy );
561
+ assertThat ( yices_term_to_string ( zeroExtendedX )). isNotNull ();
562
+
563
+ int bvSignExt = yices_proj_arg ( yices_term_child ( signExtendedX , 0 ));
564
+ int bvSizeSignExt = yices_term_bitsize ( bvSignExt );
565
+ int extendedBySignExt = yices_term_num_children ( signExtendedX ) - bvSizeSignExt ;
566
+ assertThat ( extendedBySignExt ). isEqualTo ( extendBy ) ;
567
+
568
+ int bvZeroExt = yices_proj_arg ( yices_term_child ( zeroExtendedX , 0 ));
569
+ int bvSizeZeroExt = yices_term_bitsize ( bvZeroExt );
570
+ int extendedByZeroExt = yices_term_num_children ( zeroExtendedX ) - bvSizeZeroExt ;
571
+ assertThat ( extendedByZeroExt ). isEqualTo ( extendBy );
572
+
573
+ assertThat ( yices_term_child ( zeroExtendedX , bvSizeZeroExt )). isEqualTo ( yices_false ());
574
+ assertThat ( yices_term_child ( signExtendedX , bvSizeSignExt )). isNotEqualTo ( yices_false ());
575
+
576
+ yices_proj_arg ( yices_term_child ( x , 0 )); // throws
550
577
}
551
578
552
579
@ Test
@@ -572,10 +599,53 @@ public void bvMul() {
572
599
int type = yices_bv_type (5 );
573
600
int bv2 = yices_named_variable (type , "x" );
574
601
int mul = yices_bvmul (bv2 , bv2 );
575
- System .out .println (yices_term_constructor (mul ));
602
+ assertThat (yices_term_constructor (mul )).isEqualTo (YICES_POWER_PRODUCT );
603
+ // bv2 + bv2 == bv2²
576
604
int [] component = yices_product_component (mul , 0 );
577
- System .out .println (component [0 ]);
578
- System .out .println (component [1 ]);
579
- System .out .println (yices_term_constructor (yices_bvpower (component [0 ], component [1 ])));
605
+ assertThat (component [0 ]).isEqualTo (bv2 );
606
+ assertThat (component [1 ]).isEqualTo (2 );
607
+ assertThat (yices_term_constructor (yices_bvpower (component [0 ], component [1 ]))).isGreaterThan (0 );
608
+ }
609
+
610
+ @ Test
611
+ public void isThreadSafe () {
612
+ // TODO: this explains why our concurrency tests fail ;D FIX!
613
+ assertThat (yices_is_thread_safe ()).isEqualTo (0 );
614
+ }
615
+
616
+ @ Test
617
+ public void quantifierTest () {
618
+ int boundVar = yices_new_variable (yices_int_type ());
619
+ int eleven = yices_int32 (11 );
620
+ int body = yices_eq (eleven , boundVar );
621
+
622
+ int forall = yices_forall (1 , new int [] {boundVar }, body );
623
+ int exists = yices_exists (1 , new int [] {boundVar }, body );
624
+
625
+ assertThat (yices_term_constructor (forall )).isEqualTo (YICES_FORALL_TERM );
626
+ assertThat (yices_term_constructor (exists )).isEqualTo (YICES_NOT_TERM );
627
+ assertThat (yices_term_constructor (yices_term_child (exists , 0 ))).isEqualTo (YICES_FORALL_TERM );
628
+ }
629
+
630
+ /**
631
+ * Only to be used for tests in this class. Old implementation used for creating/retrieving named
632
+ * variables. Superseded by {@link Yices2FormulaCreator#createNamedVariable} for reasons outlined
633
+ * there.
634
+ */
635
+ private static int yices_named_variable (int type , String name ) {
636
+ int termFromName = yices_get_term_by_name (name );
637
+ if (termFromName != -1 ) {
638
+ int termFromNameType = yices_type_of_term (termFromName );
639
+ checkArgument (
640
+ type == termFromNameType ,
641
+ "Cannot override symbol '%s' with new symbol '%s' of type '%s'" ,
642
+ yices_type_to_string (termFromNameType ),
643
+ name ,
644
+ yices_type_to_string (type ));
645
+ return termFromName ;
646
+ }
647
+ int var = yices_new_uninterpreted_term (type );
648
+ yices_set_term_name (var , name );
649
+ return var ;
580
650
}
581
651
}
0 commit comments