2003-09-04 12:18:59 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestStruct < Test::Unit::TestCase
|
|
|
|
def test_struct
|
|
|
|
struct_test = Struct.new("Test", :foo, :bar)
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(Struct::Test, struct_test)
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
test = struct_test.new(1, 2)
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(1, test.foo)
|
|
|
|
assert_equal(2, test.bar)
|
|
|
|
assert_equal(1, test[0])
|
|
|
|
assert_equal(2, test[1])
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
a, b = test.to_a
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(1, a)
|
|
|
|
assert_equal(2, b)
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
test[0] = 22
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(22, test.foo)
|
|
|
|
|
2003-09-04 12:18:59 -04:00
|
|
|
test.bar = 47
|
2003-09-05 11:15:43 -04:00
|
|
|
assert_equal(47, test.bar)
|
|
|
|
end
|
2005-06-01 11:03:09 -04:00
|
|
|
|
|
|
|
# [ruby-dev:26247] more than 10 struct members causes segmentation fault
|
|
|
|
def test_morethan10members
|
|
|
|
list = %w( a b c d e f g h i j k l m n o p )
|
|
|
|
until list.empty?
|
|
|
|
c = Struct.new(* list.map {|ch| ch.intern }).new
|
|
|
|
list.each do |ch|
|
|
|
|
c.__send__(ch)
|
|
|
|
end
|
|
|
|
list.pop
|
|
|
|
end
|
|
|
|
end
|
2006-02-05 10:10:01 -05:00
|
|
|
|
|
|
|
def test_small_structs
|
|
|
|
names = [:a, :b, :c, :d]
|
|
|
|
1.upto(4) {|n|
|
|
|
|
fields = names[0, n]
|
|
|
|
klass = Struct.new(*fields)
|
|
|
|
o = klass.new(*(0...n).to_a)
|
|
|
|
fields.each_with_index {|name, i|
|
|
|
|
assert_equal(i, o[name])
|
|
|
|
}
|
|
|
|
o = klass.new(*(0...n).to_a.reverse)
|
|
|
|
fields.each_with_index {|name, i|
|
|
|
|
assert_equal(n-i-1, o[name])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2008-01-23 08:49:06 -05:00
|
|
|
|
|
|
|
def test_inherit
|
|
|
|
klass = Struct.new(:a)
|
|
|
|
klass2 = Class.new(klass)
|
|
|
|
o = klass2.new(1)
|
|
|
|
assert_equal(1, o.a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_members
|
|
|
|
klass = Struct.new(:a)
|
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal([:a], klass.members)
|
|
|
|
assert_equal([:a], o.members)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ref
|
|
|
|
klass = Struct.new(:a)
|
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal(1, o[:a])
|
|
|
|
assert_raise(NameError) { o[:b] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_modify
|
|
|
|
klass = Struct.new(:a)
|
|
|
|
o = klass.new(1)
|
|
|
|
assert_raise(SecurityError) do
|
|
|
|
Thread.new do
|
|
|
|
$SAFE = 4
|
|
|
|
o.a = 2
|
|
|
|
end.value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_set
|
|
|
|
klass = Struct.new(:a)
|
|
|
|
o = klass.new(1)
|
|
|
|
o[:a] = 2
|
|
|
|
assert_equal(2, o[:a])
|
|
|
|
assert_raise(NameError) { o[:b] = 3 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_struct_new
|
|
|
|
Struct.instance_eval { const_set(:Foo, nil) }
|
|
|
|
assert_raise(NameError) { Struct.new("foo") }
|
|
|
|
assert_nothing_raised { Struct.new("Foo") }
|
|
|
|
Struct.instance_eval { remove_const(:Foo) }
|
|
|
|
assert_nothing_raised { Struct.new(:a) { } }
|
|
|
|
assert_raise(RuntimeError) { Struct.new(:a) { raise } }
|
|
|
|
|
|
|
|
assert_equal([:utime, :stime, :cutime, :cstime], Process.times.members)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_initialize
|
|
|
|
klass = Struct.new(:a)
|
|
|
|
assert_raise(ArgumentError) { klass.new(1, 2) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each
|
|
|
|
klass = Struct.new(:a, :b)
|
|
|
|
o = klass.new(1, 2)
|
|
|
|
assert_equal([1, 2], o.each.to_a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_pair
|
|
|
|
klass = Struct.new(:a, :b)
|
|
|
|
o = klass.new(1, 2)
|
|
|
|
assert_equal([[:a, 1], [:b, 2]], o.each_pair.to_a)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inspect
|
|
|
|
klass = Struct.new(:a)
|
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal("#<struct a=1>", o.inspect)
|
|
|
|
o.a = o
|
|
|
|
assert(o.inspect =~ /^#<struct a=#<struct #<.*?>:...>>$/)
|
|
|
|
|
|
|
|
Struct.new("Foo", :a)
|
|
|
|
o = Struct::Foo.new(1)
|
|
|
|
assert_equal("#<struct Struct::Foo a=1>", o.inspect)
|
|
|
|
Struct.instance_eval { remove_const(:Foo) }
|
|
|
|
|
|
|
|
klass = Struct.new(:a, :b)
|
|
|
|
o = klass.new(1, 2)
|
|
|
|
assert_equal("#<struct a=1, b=2>", o.inspect)
|
|
|
|
|
|
|
|
klass = Struct.new(:@a)
|
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal("#<struct :@a=1>", o.inspect)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_init_copy
|
|
|
|
klass = Struct.new(:a)
|
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal(o, o.dup)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_aref
|
|
|
|
klass = Struct.new(:a)
|
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal(1, o[0])
|
|
|
|
assert_raise(IndexError) { o[-2] }
|
|
|
|
assert_raise(IndexError) { o[1] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_aset
|
|
|
|
klass = Struct.new(:a)
|
|
|
|
o = klass.new(1)
|
|
|
|
o[0] = 2
|
|
|
|
assert_equal(2, o[:a])
|
|
|
|
assert_raise(IndexError) { o[-2] = 3 }
|
|
|
|
assert_raise(IndexError) { o[1] = 3 }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_values_at
|
|
|
|
klass = Struct.new(:a, :b, :c, :d, :e, :f)
|
|
|
|
o = klass.new(1, 2, 3, 4, 5, 6)
|
|
|
|
assert_equal([2, 4, 6], o.values_at(1, 3, 5))
|
|
|
|
assert_equal([2, 3, 4, 3, 4, 5], o.values_at(1..3, 2...5))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_select
|
|
|
|
klass = Struct.new(:a, :b, :c, :d, :e, :f)
|
|
|
|
o = klass.new(1, 2, 3, 4, 5, 6)
|
|
|
|
assert_equal([1, 3, 5], o.select {|v| v % 2 != 0 })
|
|
|
|
assert_raise(ArgumentError) { o.select(1) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_equal
|
|
|
|
klass1 = Struct.new(:a)
|
|
|
|
klass2 = Struct.new(:a, :b)
|
|
|
|
o1 = klass1.new(1)
|
|
|
|
o2 = klass1.new(1)
|
|
|
|
o3 = klass2.new(1)
|
|
|
|
assert(o1.==(o2))
|
|
|
|
assert(o1 != o3)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_hash
|
|
|
|
klass = Struct.new(:a)
|
|
|
|
o = klass.new(1)
|
|
|
|
assert(o.hash.is_a?(Fixnum))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eql
|
|
|
|
klass1 = Struct.new(:a)
|
|
|
|
klass2 = Struct.new(:a, :b)
|
|
|
|
o1 = klass1.new(1)
|
|
|
|
o2 = klass1.new(1)
|
|
|
|
o3 = klass2.new(1)
|
|
|
|
assert(o1.eql?(o2))
|
|
|
|
assert(!(o1.eql?(o3)))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_size
|
|
|
|
klass = Struct.new(:a)
|
|
|
|
o = klass.new(1)
|
|
|
|
assert_equal(1, o.size)
|
|
|
|
end
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|