summaryrefslogtreecommitdiffhomepage
path: root/test/t/struct.rb
blob: 5cf6929b8fa8704dfeb2d2fe996a2c1df01d4041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
##
# Struct ISO Test

if Object.const_defined?(:Struct)
  assert('Struct', '15.2.18') do
    Struct.class == Class
  end

  assert('Struct superclass', '15.2.18.2') do
    Struct.superclass == Object
  end

  assert('Struct.new', '15.2.18.3.1') do
    c = Struct.new(:m1, :m2)
    c.superclass == Struct and
      c.members == [:m1,:m2]
  end

  assert('Struct#==', '15.2.18.4.1') do
    c = Struct.new(:m1, :m2)
    cc1 = c.new(1,2)
    cc2 = c.new(1,2)
    cc1 == cc2
  end

  assert('Struct#[]', '15.2.18.4.2') do
    c = Struct.new(:m1, :m2)
    cc = c.new(1,2)
    cc[:m1] == 1 and cc["m2"] == 2
  end

  assert('Struct#[]=', '15.2.18.4.3') do
    c = Struct.new(:m1, :m2)
    cc = c.new(1,2)
    cc[:m1] = 3
    cc[:m1] == 3
  end

  assert('Struct#each', '15.2.18.4.4') do
    c = Struct.new(:m1, :m2)
    cc = c.new(1,2)
    a = []
    cc.each{|x|
      a << x
    }
    a[0] == 1 and a[1] == 2
  end

  assert('Struct#each_pair', '15.2.18.4.5') do
    c = Struct.new(:m1, :m2)
    cc = c.new(1,2)
    a = []
    cc.each_pair{|k,v|
      a << [k,v]
    }
    a[0] == [:m1, 1] and a[1] == [:m2, 2]
  end

  assert('Struct#members', '15.2.18.4.6') do
    c = Struct.new(:m1, :m2)
    cc = c.new(1,2)
    cc.members == [:m1,:m2]
  end

  assert('Struct#select', '15.2.18.4.7') do
    c = Struct.new(:m1, :m2)
    cc = c.new(1,2)
    cc.select{|v| v % 2 == 0} == [2]
  end
end