z3 0.0.20180624 → 0.0.20180629

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c5b58b092ac3d83d29341b7286e74068497c6f3e59232b8b0e23373a5e2e4eb
4
- data.tar.gz: 5dfef87be452768052ece356eb0cd52f0725556769937d0030b1403a9422946e
3
+ metadata.gz: 9b62645ba88a89cc0c81a5f2088f4b919953d778d99f52af3798cd6b9f130e61
4
+ data.tar.gz: 97269d402d845d9192c558c271c2448cdc420d3c93d6bf2f3e2afb0238b039cc
5
5
  SHA512:
6
- metadata.gz: 209cd2c1ced0b55bfad330f725b296075b14651af5d7faee5e236e920a9c48d8e65db30a7b31aabc0b4744aca2389aae8ea5db78815e9fbde5c8f9e379fba091
7
- data.tar.gz: 43d4e8ec32e9a3eb433ea6a73b9e713ccd75182be3702be42ee2f60592a1c61a5a56ba0c0aca3711477fa7d6d5dfe85e21b5922701b38bdbef2882ce57569d71
6
+ metadata.gz: 2bc073a01310b704b5e64d5d6bd26f31b1b061e38f9d9edb3a0fed0e2e2db2989fbac863c35c7213d5f81ba0a74ec2d95c16c440ad8d0550d5ce842d1b843c64
7
+ data.tar.gz: 390f2516225de2bf7887ed42597587e53f3ec0c3ebe89d85c2aa43c54717e4dee3cb485c5d62199c94467866c776491de4ea758fec6f1b80f32689b577da7cb7
@@ -64,6 +64,16 @@ module Z3
64
64
  sort.new(LowLevel.mk_rotate_right(num, self))
65
65
  end
66
66
 
67
+ def extract(hi, lo)
68
+ raise Z3::Exception, "Trying to extract bits out of range" unless sort.size > hi and hi >= lo and lo >= 0
69
+ BitvecSort.new(hi - lo + 1).new(LowLevel.mk_extract(hi, lo, self))
70
+ end
71
+
72
+ def concat(other)
73
+ raise Z3::Exception, "Can only concatenate another Bitvec" unless other.is_a?(BitvecExpr)
74
+ BitvecSort.new(sort.size + other.sort.size).new(LowLevel.mk_concat(self, other))
75
+ end
76
+
67
77
  def zero_ext(size)
68
78
  BitvecSort.new(sort.size + size).new(LowLevel.mk_zero_ext(size, self))
69
79
  end
data/lib/z3/printer.rb CHANGED
@@ -53,7 +53,35 @@ module Z3
53
53
  name = decl.name
54
54
  args = a.arguments.map{|x| format_ast(x)}
55
55
  return PrintedExpr.new(name, false) if args.size == 0
56
- # All operators
56
+
57
+ # Special case common Bitvec operators
58
+ case name
59
+ when "rotate_left", "rotate_right", "zero_extend", "sign_extend"
60
+ if args.size == 1
61
+ n = Z3::LowLevel.get_decl_int_parameter(a.func_decl, 0)
62
+ return PrintedExpr.new("#{name}(#{args[0]}, #{n})", true)
63
+ end
64
+ when "bvxor", "bvand", "bvor", "bvadd", "bvsub"
65
+ if args.size == 2
66
+ pretty_name = {"bvxor" => "^", "bvand" => "&", "bvor" => "|", "bvadd" => "+", "bvsub" => "-"}[name]
67
+ return PrintedExpr.new("#{args[0].enforce_parentheses} #{pretty_name} #{args[1].enforce_parentheses}", true)
68
+ end
69
+ when "bvnot"
70
+ if args.size == 1
71
+ return PrintedExpr.new("~#{args[0].enforce_parentheses}")
72
+ end
73
+ when "bvneg"
74
+ if args.size == 1
75
+ return PrintedExpr.new("-#{args[0].enforce_parentheses}")
76
+ end
77
+ when "extract"
78
+ if args.size == 1
79
+ u = Z3::LowLevel.get_decl_int_parameter(a.func_decl, 0)
80
+ v = Z3::LowLevel.get_decl_int_parameter(a.func_decl, 1)
81
+ return PrintedExpr.new("#{name}(#{args[0]}, #{u}, #{v})", true)
82
+ end
83
+ end
84
+
57
85
  if name !~ /[a-z0-9]/
58
86
  if args.size == 2
59
87
  return PrintedExpr.new("#{args[0].enforce_parentheses} #{name} #{args[1].enforce_parentheses}", true)
@@ -1,6 +1,7 @@
1
1
  module Z3
2
2
  class BitvecSort < Sort
3
3
  def initialize(n)
4
+ raise Z3::Exception, "Bitvec width must be positive" unless n >= 1
4
5
  super LowLevel.mk_bv_sort(n)
5
6
  end
6
7
 
data/lib/z3/sort/sort.rb CHANGED
@@ -57,12 +57,16 @@ module Z3
57
57
  end
58
58
 
59
59
  def var(name)
60
- new(
61
- LowLevel.mk_const(
62
- LowLevel.mk_string_symbol(name),
63
- self,
60
+ if name.is_a?(Enumerable)
61
+ name.map{|v| var(v)}
62
+ else
63
+ new(
64
+ LowLevel.mk_const(
65
+ LowLevel.mk_string_symbol(name),
66
+ self,
67
+ )
64
68
  )
65
- )
69
+ end
66
70
  end
67
71
 
68
72
  # We pretend to be a class, sort of
@@ -4,6 +4,7 @@ module Z3
4
4
  let(:b) { Z3.Bitvec("b", 8) }
5
5
  let(:c) { Z3.Bitvec("c", 8) }
6
6
  let(:d) { Z3.Bitvec("d", 12) }
7
+ let(:e) { Z3.Bitvec("e", 4) }
7
8
  let(:x) { Z3.Bool("x") }
8
9
 
9
10
  it "==" do
@@ -168,28 +169,28 @@ module Z3
168
169
  expect([a == 127, b == 1, x == a.signed_div_no_overflow?(b)]).to have_solution(x => true)
169
170
  end
170
171
 
171
- ## This API is broken, z3 returns unevaluated bvsmul_noovfl(10, 10) instead of actual answer
172
-
173
- # it "signed_mul_no_overflow?" do
174
- # expect([a == 10, b == 10, x == a.signed_mul_no_overflow?(b)]).to have_solution(x => true)
175
- # expect([a == 20, b == 10, x == a.signed_mul_no_overflow?(b)]).to have_solution(x => false)
176
- # expect([a == 20, b == 20, x == a.signed_mul_no_overflow?(b)]).to have_solution(x => false)
177
- # expect([a == -10, b == -10, x == a.signed_mul_no_overflow?(b)]).to have_solution(x => true)
178
- # end
179
- #
180
- # it "unsigned_mul_no_overflow?" do
181
- # expect([a == 10, b == 10, x == a.unsigned_mul_no_overflow?(b)]).to have_solution(x => true)
182
- # expect([a == 20, b == 10, x == a.unsigned_mul_no_overflow?(b)]).to have_solution(x => true)
183
- # expect([a == 20, b == 20, x == a.unsigned_mul_no_overflow?(b)]).to have_solution(x => false)
184
- # expect([a == -10, b == -10, x == a.unsigned_mul_no_overflow?(b)]).to have_solution(x => false)
185
- # end
186
- #
172
+ it "signed_mul_no_overflow?" do
173
+ expect([a == 10, b == 10, x == a.signed_mul_no_overflow?(b)]).to have_solution(x => true)
174
+ expect([a == 20, b == 10, x == a.signed_mul_no_overflow?(b)]).to have_solution(x => false)
175
+ expect([a == 20, b == 20, x == a.signed_mul_no_overflow?(b)]).to have_solution(x => false)
176
+ expect([a == 10, b == -10, x == a.signed_mul_no_overflow?(b)]).to have_solution(x => false)
177
+ expect([a == -10, b == 10, x == a.signed_mul_no_overflow?(b)]).to have_solution(x => false)
178
+ expect([a == -10, b == -10, x == a.signed_mul_no_overflow?(b)]).to have_solution(x => false)
179
+ end
180
+
181
+ it "unsigned_mul_no_overflow?" do
182
+ expect([a == 10, b == 10, x == a.unsigned_mul_no_overflow?(b)]).to have_solution(x => true)
183
+ expect([a == 20, b == 10, x == a.unsigned_mul_no_overflow?(b)]).to have_solution(x => true)
184
+ expect([a == 20, b == 20, x == a.unsigned_mul_no_overflow?(b)]).to have_solution(x => false)
185
+ expect([a == -10, b == -10, x == a.unsigned_mul_no_overflow?(b)]).to have_solution(x => false)
186
+ end
187
+
187
188
  # # Inherently signed, unsigned can't underflow
188
- # it "signed_mul_no_underflow?" do
189
- # expect([a == -10, b == -10, x == a.signed_mul_no_underflow?(b)]).to have_solution(x => true)
190
- # expect([a == -20, b == -20, x == a.signed_mul_no_underflow?(b)]).to have_solution(x => true)
191
- # expect([a == -20, b == 20, x == a.signed_mul_no_underflow?(b)]).to have_solution(x => false)
192
- # end
189
+ it "signed_mul_no_underflow?" do
190
+ expect([a == -10, b == -10, x == a.signed_mul_no_underflow?(b)]).to have_solution(x => true)
191
+ expect([a == -20, b == -20, x == a.signed_mul_no_underflow?(b)]).to have_solution(x => true)
192
+ expect([a == -20, b == 20, x == a.signed_mul_no_underflow?(b)]).to have_solution(x => false)
193
+ end
193
194
 
194
195
  it "zero_ext / sign_ext" do
195
196
  expect([a == 100, d == a.zero_ext(4)]).to have_solution(d => 100)
@@ -204,5 +205,18 @@ module Z3
204
205
  expect([a == 0b0101_0110, b == a.rotate_right(1)]).to have_solution(b => 0b0_0101_011)
205
206
  expect([a == 0b0101_0110, b == a.rotate_right(4)]).to have_solution(b => 0b0110_0101)
206
207
  end
208
+
209
+ it "extract" do
210
+ expect([a == 0b0101_0110, e == a.extract(3, 0)]).to have_solution(e => 0b0110)
211
+ expect([a == 0b0101_0110, e == a.extract(7, 4)]).to have_solution(e => 0b0101)
212
+ expect{ a.extract(8, 4) }.to raise_error(Z3::Exception)
213
+ expect{ a.extract(2, 3) }.to raise_error(Z3::Exception)
214
+ expect{ a.extract(2, -1) }.to raise_error(Z3::Exception)
215
+ end
216
+
217
+ it "concat" do
218
+ expect([a == 0b0101_0110, e == 0b1101, d == a.concat(e)]).to have_solution(d => 0b0101_0110_1101)
219
+ expect([a == 0b0101_0110, e == 0b1101, d == e.concat(a)]).to have_solution(d => 0b1101_0101_0110)
220
+ end
207
221
  end
208
222
  end
@@ -32,5 +32,9 @@ module Z3
32
32
  expect(Z3.Bitvec("a", 8).inspect).to eq("Bitvec(8)<a>")
33
33
  expect(Z3.Bitvec("a", 32).inspect).to eq("Bitvec(32)<a>")
34
34
  end
35
+
36
+ it "number of bits must be positive" do
37
+ expect{ Z3.Bitvec("a", 0) }.to raise_error(Z3::Exception)
38
+ end
35
39
  end
36
40
  end
@@ -0,0 +1,18 @@
1
+ module Z3
2
+ describe "interface" do
3
+ it "single variables" do
4
+ expect(Z3.Bool("a")).to be_same_as(Z3::BoolSort.new.var("a"))
5
+ expect(Z3.Int("b")).to be_same_as(Z3::IntSort.new.var("b"))
6
+ expect(Z3.Real("c")).to be_same_as(Z3::RealSort.new.var("c"))
7
+ expect(Z3.Bitvec("d", 32)).to be_same_as(Z3::BitvecSort.new(32).var("d"))
8
+ end
9
+
10
+ it "multiple variables" do
11
+ expect(Z3.Int(%W[x y z])).to be_same_as([
12
+ Z3::IntSort.new.var("x"),
13
+ Z3::IntSort.new.var("y"),
14
+ Z3::IntSort.new.var("z"),
15
+ ])
16
+ end
17
+ end
18
+ end
data/spec/printer_spec.rb CHANGED
@@ -49,5 +49,35 @@ module Z3
49
49
  expect((-a) + (-b)).to stringify("(-a) + (-b)")
50
50
  end
51
51
  end
52
+
53
+ describe "bitvector operations" do
54
+ let(:a) { Z3.Bitvec("a", 32) }
55
+ let(:b) { Z3.Bitvec("b", 32) }
56
+
57
+ it "unary operators" do
58
+ expect(~a).to stringify("~a")
59
+ expect(-a).to stringify("-a")
60
+ end
61
+
62
+ it "binary operators" do
63
+ expect(a + b).to stringify("a + b")
64
+ expect(a - b).to stringify("a - b")
65
+ expect(a & b).to stringify("a & b")
66
+ expect(a ^ b).to stringify("a ^ b")
67
+ expect(a | b).to stringify("a | b")
68
+ end
69
+
70
+ it "special operators" do
71
+ expect(a.rotate_left(3)).to stringify("rotate_left(a, 3)")
72
+ expect(a.rotate_right(4)).to stringify("rotate_right(a, 4)")
73
+ expect(a.unsigned_lshift(5)).to stringify("bvshl(a, 5)")
74
+ expect(a.signed_rshift(6)).to stringify("bvashr(a, 6)")
75
+ expect(a.unsigned_lshift(7)).to stringify("bvshl(a, 7)")
76
+ expect(a.extract(20, 5)).to stringify("extract(a, 20, 5)")
77
+ expect(a.zero_ext(4)).to stringify("zero_extend(a, 4)")
78
+ expect(a.sign_ext(4)).to stringify("sign_extend(a, 4)")
79
+ expect(a.concat(b)).to stringify("concat(a, b)")
80
+ end
81
+ end
52
82
  end
53
83
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: z3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.20180624
4
+ version: 0.0.20180629
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Wegrzanowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-24 00:00:00.000000000 Z
11
+ date: 2018-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -232,6 +232,7 @@ files:
232
232
  - spec/integration/sudoku_spec.rb
233
233
  - spec/integration/verbal_arithmetic_spec.rb
234
234
  - spec/integration/zebra_puzzle_spec.rb
235
+ - spec/interface_spec.rb
235
236
  - spec/model_spec.rb
236
237
  - spec/optimize_spec.rb
237
238
  - spec/printer_spec.rb