Skip to content

Commit 9113afa

Browse files
committed
Some specs and Z3::Sort#to_s etc.
1 parent 072f299 commit 9113afa

File tree

6 files changed

+139
-2
lines changed

6 files changed

+139
-2
lines changed

.rspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--require spec_helper

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The rest of `Z3` is high level API, but the interface is extremely unstable at t
1010

1111
You can use most Ruby operators to construct ASTs, but use `~ | &` instead of `! || &&` for boolean operators.
1212

13+
As for API internals, attributes starting with `_` are FFI internals you shouldn't touch, other attributes are generally legitimate Ruby objects.
14+
1315
### Requirements
1416

1517
To use it, you'll need to install `z3`. On OSX that would be:

lib/z3/core.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Z3::Core
1818
attach_function :Z3_mk_bool_sort, [ctx_pointer], sort_pointer
1919
attach_function :Z3_mk_int_sort, [ctx_pointer], sort_pointer
2020
attach_function :Z3_mk_real_sort, [ctx_pointer], sort_pointer
21+
attach_function :Z3_sort_to_string, [ctx_pointer, sort_pointer], :string
2122
# Solver API
2223
attach_function :Z3_mk_solver, [ctx_pointer], solver_pointer
2324
attach_function :Z3_solver_push, [ctx_pointer, solver_pointer], :void

lib/z3/sort.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
class Z3::Sort
2-
attr_reader :_sort
2+
attr_reader :_sort, :ctx
33
# Do not use .new directly
4-
def initialize(_sort, ctx:Z3::Context.main)
4+
def initialize(_sort, ctx: Z3::Context.main)
55
@ctx = ctx
66
@_sort = _sort
77
end
88

9+
def ==(other)
10+
other.is_a?(Z3::Sort) and @_sort == other._sort
11+
end
12+
13+
def to_s
14+
Z3::Core.Z3_sort_to_string(@ctx._context, @_sort)
15+
end
16+
17+
def inspect
18+
"Z3::Sort<#{to_s}>"
19+
end
20+
921
class << self
1022
def bool(ctx: Z3::Context.main)
1123
Z3::Sort.new(Z3::Core::Z3_mk_bool_sort(ctx._context), ctx: ctx)

spec/sort_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
describe Z3::Sort do
2+
it "create Bool sort" do
3+
expect(Z3::Sort.bool.to_s).to eq("Bool")
4+
expect(Z3::Sort.bool.inspect).to eq("Z3::Sort<Bool>")
5+
end
6+
7+
it "create Int sort" do
8+
expect(Z3::Sort.int.to_s).to eq("Int")
9+
expect(Z3::Sort.int.inspect).to eq("Z3::Sort<Int>")
10+
end
11+
12+
it "create Real sort" do
13+
expect(Z3::Sort.real.to_s).to eq("Real")
14+
expect(Z3::Sort.real.inspect).to eq("Z3::Sort<Real>")
15+
end
16+
17+
it "supports ==" do
18+
expect(Z3::Sort.bool).to eq(Z3::Sort.bool)
19+
expect(Z3::Sort.int).to eq(Z3::Sort.int)
20+
expect(Z3::Sort.bool).to_not eq(Z3::Sort.int)
21+
end
22+
end

spec/spec_helper.rb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
require_relative "../lib/z3"
2+
3+
# This file was generated by the `rspec --init` command. Conventionally, all
4+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5+
# The generated `.rspec` file contains `--require spec_helper` which will cause
6+
# this file to always be loaded, without a need to explicitly require it in any
7+
# files.
8+
#
9+
# Given that it is always loaded, you are encouraged to keep this file as
10+
# light-weight as possible. Requiring heavyweight dependencies from this file
11+
# will add to the boot time of your test suite on EVERY test run, even for an
12+
# individual file that may not need all of that loaded. Instead, consider making
13+
# a separate helper file that requires the additional dependencies and performs
14+
# the additional setup, and require it from the spec files that actually need
15+
# it.
16+
#
17+
# The `.rspec` file also contains a few flags that are not defaults but that
18+
# users commonly want.
19+
#
20+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
21+
RSpec.configure do |config|
22+
# rspec-expectations config goes here. You can use an alternate
23+
# assertion/expectation library such as wrong or the stdlib/minitest
24+
# assertions if you prefer.
25+
config.expect_with :rspec do |expectations|
26+
# This option will default to `true` in RSpec 4. It makes the `description`
27+
# and `failure_message` of custom matchers include text for helper methods
28+
# defined using `chain`, e.g.:
29+
# be_bigger_than(2).and_smaller_than(4).description
30+
# # => "be bigger than 2 and smaller than 4"
31+
# ...rather than:
32+
# # => "be bigger than 2"
33+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
34+
end
35+
36+
# rspec-mocks config goes here. You can use an alternate test double
37+
# library (such as bogus or mocha) by changing the `mock_with` option here.
38+
config.mock_with :rspec do |mocks|
39+
# Prevents you from mocking or stubbing a method that does not exist on
40+
# a real object. This is generally recommended, and will default to
41+
# `true` in RSpec 4.
42+
mocks.verify_partial_doubles = true
43+
end
44+
45+
# The settings below are suggested to provide a good initial experience
46+
# with RSpec, but feel free to customize to your heart's content.
47+
=begin
48+
# These two settings work together to allow you to limit a spec run
49+
# to individual examples or groups you care about by tagging them with
50+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
51+
# get run.
52+
config.filter_run :focus
53+
config.run_all_when_everything_filtered = true
54+
55+
# Allows RSpec to persist some state between runs in order to support
56+
# the `--only-failures` and `--next-failure` CLI options. We recommend
57+
# you configure your source control system to ignore this file.
58+
config.example_status_persistence_file_path = "spec/examples.txt"
59+
60+
# Limits the available syntax to the non-monkey patched syntax that is
61+
# recommended. For more details, see:
62+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
63+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
64+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
65+
config.disable_monkey_patching!
66+
67+
# This setting enables warnings. It's recommended, but in some cases may
68+
# be too noisy due to issues in dependencies.
69+
config.warnings = true
70+
71+
# Many RSpec users commonly either run the entire suite or an individual
72+
# file, and it's useful to allow more verbose output when running an
73+
# individual spec file.
74+
if config.files_to_run.one?
75+
# Use the documentation formatter for detailed output,
76+
# unless a formatter has already been configured
77+
# (e.g. via a command-line flag).
78+
config.default_formatter = 'doc'
79+
end
80+
81+
# Print the 10 slowest examples and example groups at the
82+
# end of the spec run, to help surface which specs are running
83+
# particularly slow.
84+
config.profile_examples = 10
85+
86+
# Run specs in random order to surface order dependencies. If you find an
87+
# order dependency and want to debug it, you can fix the order by providing
88+
# the seed, which is printed after each run.
89+
# --seed 1234
90+
config.order = :random
91+
92+
# Seed global randomization in this process using the `--seed` CLI option.
93+
# Setting this allows you to use `--seed` to deterministically reproduce
94+
# test failures related to randomization by passing the same `--seed` value
95+
# as the one that triggered the failure.
96+
Kernel.srand config.seed
97+
=end
98+
end

0 commit comments

Comments
 (0)