Skip to content

Commit

Permalink
LLVM::Value, LLVM::Type, LLVM::Module: Added missing 'eql?' method
Browse files Browse the repository at this point in the history
  • Loading branch information
ishikawa committed Feb 18, 2011
1 parent 4e30b47 commit 5d19d02
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 9 deletions.
4 changes: 4 additions & 0 deletions lib/llvm/core/module.rb
Expand Up @@ -23,6 +23,10 @@ def ==(other)
end
end

def eql?(other)
other.instance_of?(self.class) && self == other
end

def self.create(name)
new(C.LLVMModuleCreateWithName(name))
end
Expand Down
4 changes: 4 additions & 0 deletions lib/llvm/core/type.rb
Expand Up @@ -20,6 +20,10 @@ def ==(type)
end
end

def eql?(other)
other.instance_of?(self.class) && self == other
end

def kind
C.LLVMGetTypeKind(self)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/llvm/core/value.rb
Expand Up @@ -23,6 +23,10 @@ def ==(other)
end
end

def eql?(other)
other.instance_of?(self.class) && self == other
end

def self.type
raise NotImplementedError, "#{self.name}.type() is abstract."
end
Expand Down
80 changes: 71 additions & 9 deletions test/equality_test.rb
Expand Up @@ -7,23 +7,85 @@ def setup
LLVM.init_x86
end

class MyModule < LLVM::Module; end
class MyInt < LLVM::Int32; end
class MyType < LLVM::Type; end
class MyFunction < LLVM::Function; end

def assert_equalities(options)
map = {
:equal => method(:assert_equal),
:not_equal => method(:assert_not_equal),
:same => method(:assert_same),
:not_same => method(:assert_not_same),
:eql => lambda {|n, m, name| assert n.eql?(m), name },
:not_eql => lambda {|n, m, name| assert !n.eql?(m), name },
}

map.each do |name, callable|
options[name].combination(2).each do |n, m|
callable.call(n, m, name.to_s)
end
end

end

def test_int_value
int1 = LLVM::Int32.from_i(1)
int2 = LLVM::Int32.from_ptr(int1.to_ptr)
int3 = LLVM::Int32.from_i(2)
int4 = MyInt.from_ptr(int1.to_ptr)

assert_equalities :equal => [int1, int2, int4],
:not_equal => [int1, int3],
:same => [int1, int1],
:not_same => [int1, int2, int3, int4],
:eql => [int1, int2],
:not_eql => [int1, int3, int4]
end

def test_module
mod = LLVM::Module.create('test')
assert_equal mod, mod
assert_equal mod, LLVM::Module.from_ptr(mod.to_ptr)
mod1 = LLVM::Module.create('test')
mod2 = LLVM::Module.from_ptr(mod1.to_ptr)
mod3 = LLVM::Module.create('dummy')
mod4 = MyModule.from_ptr(mod1.to_ptr)

assert_equalities :equal => [mod1, mod2, mod4],
:not_equal => [mod1, mod3],
:same => [mod1, mod1],
:not_same => [mod1, mod2, mod3, mod4],
:eql => [mod1, mod2],
:not_eql => [mod1, mod3, mod4]
end

def test_type
type = LLVM::Float.type
assert_equal type, type
assert_equal type, LLVM::Float.type
type1 = LLVM::Float.type
type2 = LLVM::Type.from_ptr(type1.to_ptr)
type3 = LLVM::Double.type
type4 = MyType.from_ptr(type1.to_ptr)

assert_equalities :equal => [type1, type2, type4],
:not_equal => [type1, type3],
:same => [type1, type1],
:not_same => [type1, type2, type3, type4],
:eql => [type1, type2],
:not_eql => [type1, type3, type4]
end

def test_function
mod = LLVM::Module.create('test')
fn = mod.functions.add('test', LLVM.Void)
assert_equal fn, fn
assert_equal fn, mod.functions.named('test')

fn1 = mod.functions.add('test1', LLVM.Void)
fn2 = LLVM::Function.from_ptr(fn1.to_ptr)
fn3 = mod.functions.add('test2', LLVM.Void)
fn4 = MyFunction.from_ptr(fn1.to_ptr)

assert_equalities :equal => [fn1, fn2, fn4],
:not_equal => [fn1, fn3],
:same => [fn1, fn1],
:not_same => [fn1, fn2, fn3, fn4],
:eql => [fn1, fn2],
:not_eql => [fn1, fn3, fn4]
end

end
Expand Down

0 comments on commit 5d19d02

Please sign in to comment.