11// Author: Diffblue Ltd.
22
3- #include < testing-utils/use_catch.h>
3+ // / \file
4+ // / Unit tests for smt2_convt
5+
6+ #include < util/bitvector_types.h>
7+ #include < util/namespace.h>
8+ #include < util/std_expr.h>
9+ #include < util/symbol_table.h>
410
511#include < solvers/smt2/smt2_conv.h>
12+ #include < testing-utils/use_catch.h>
613
714TEST_CASE (
815 " smt2_convt::convert_identifier character escaping." ,
@@ -17,3 +24,93 @@ TEST_CASE(
1724 CHECK (smt2_convt::convert_identifier (" |" ) == " |&124;|" );
1825 CHECK (smt2_convt::convert_identifier (" &" ) == " &" );
1926}
27+
28+ // / Helper: extract the "(assert ...)" line from SMT2 output of set_to
29+ static std::string get_assert (const exprt &red_expr)
30+ {
31+ symbol_tablet symbol_table;
32+ namespacet ns (symbol_table);
33+ std::ostringstream out;
34+ smt2_convt conv (ns, " test" , " " , " QF_BV" , smt2_convt::solvert::GENERIC , out);
35+ conv.set_to (red_expr, true );
36+ std::string result = out.str ();
37+ auto pos = result.find (" (assert " );
38+ REQUIRE (pos != std::string::npos);
39+ // strip trailing newline
40+ auto end = result.find_last_not_of (' \n ' );
41+ return result.substr (pos, end - pos + 1 );
42+ }
43+
44+ TEST_CASE (" smt2_convt reduction operators" , " [core][solvers][smt2]" )
45+ {
46+ unsignedbv_typet u2 (2 );
47+ symbol_exprt sym (" x" , u2);
48+
49+ SECTION (" reduction_and" )
50+ {
51+ REQUIRE (
52+ get_assert (unary_predicate_exprt{ID_reduction_and, sym}) ==
53+ " (assert (= x (_ bv3 2)))" );
54+ }
55+
56+ SECTION (" reduction_nand" )
57+ {
58+ REQUIRE (
59+ get_assert (unary_predicate_exprt{ID_reduction_nand, sym}) ==
60+ " (assert (not (= x (_ bv3 2))))" );
61+ }
62+
63+ SECTION (" reduction_or" )
64+ {
65+ REQUIRE (
66+ get_assert (unary_predicate_exprt{ID_reduction_or, sym}) ==
67+ " (assert (not (= x (_ bv0 2))))" );
68+ }
69+
70+ SECTION (" reduction_nor" )
71+ {
72+ REQUIRE (
73+ get_assert (unary_predicate_exprt{ID_reduction_nor, sym}) ==
74+ " (assert (not (not (= x (_ bv0 2)))))" );
75+ }
76+
77+ SECTION (" reduction_xor" )
78+ {
79+ REQUIRE (
80+ get_assert (unary_predicate_exprt{ID_reduction_xor, sym}) ==
81+ " (assert (let ((?rop x)) "
82+ " (= (bvxor ((_ extract 0 0) ?rop) ((_ extract 1 1) ?rop)) #b1)))" );
83+ }
84+
85+ SECTION (" reduction_xnor" )
86+ {
87+ REQUIRE (
88+ get_assert (unary_predicate_exprt{ID_reduction_xnor, sym}) ==
89+ " (assert (not (let ((?rop x)) "
90+ " (= (bvxor ((_ extract 0 0) ?rop) ((_ extract 1 1) ?rop)) #b1))))" );
91+ }
92+
93+ SECTION (" reduction_xor 1-bit" )
94+ {
95+ symbol_exprt sym1 (" y" , unsignedbv_typet (1 ));
96+ REQUIRE (
97+ get_assert (unary_predicate_exprt{ID_reduction_xor, sym1}) ==
98+ " (assert (= y #b1))" );
99+ }
100+
101+ SECTION (" reduction_and 1-bit" )
102+ {
103+ symbol_exprt sym1 (" y" , unsignedbv_typet (1 ));
104+ REQUIRE (
105+ get_assert (unary_predicate_exprt{ID_reduction_and, sym1}) ==
106+ " (assert (= y (_ bv1 1)))" );
107+ }
108+
109+ SECTION (" reduction_or 1-bit" )
110+ {
111+ symbol_exprt sym1 (" y" , unsignedbv_typet (1 ));
112+ REQUIRE (
113+ get_assert (unary_predicate_exprt{ID_reduction_or, sym1}) ==
114+ " (assert (not (= y (_ bv0 1))))" );
115+ }
116+ }
0 commit comments