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
|
2003-09-04 12:18:59 -04:00
|
|
|
end
|