summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-array-ext/test/array.rb
blob: 8a6f50fe4a3678076119a9bb5a00f2aa01dd0179 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
##
# Array(Ext) Test

assert("Array::try_convert") do
  Array.try_convert([1]) == [1] and
  Array.try_convert("1").nil?
end

assert("Array#assoc") do
  s1 = [ "colors", "red", "blue", "green" ]
  s2 = [ "letters", "a", "b", "c" ]
  s3 = "foo"
  a  = [ s1, s2, s3 ]

  a.assoc("letters") == [ "letters", "a", "b", "c" ] and
  a.assoc("foo").nil?
end

assert("Array#at") do
  a = [ "a", "b", "c", "d", "e" ]
  a.at(0)  == "a" and a.at(-1) == "e"
end

assert("Array#rassoc") do
  a = [ [ 1, "one"], [2, "two"], [3, "three"], ["ii", "two"] ]

  a.rassoc("two") == [2, "two"] and
  a.rassoc("four").nil?
end

assert("Array#uniq!") do
  a = [1, 2, 3, 1]
  a.uniq!
  a == [1, 2, 3]
end

assert("Array#uniq") do
  a = [1, 2, 3, 1]
  a.uniq == [1, 2, 3] && a == [1, 2, 3, 1]
end

assert("Array#-") do
  a = [1, 2, 3, 1]
  b = [1]
  c = 1
  e1 = nil

  begin
    a - c
  rescue => e1
  end

  (a - b) == [2, 3] and e1.class == TypeError and a == [1, 2, 3, 1]
end

assert("Array#|") do
  a = [1, 2, 3, 1]
  b = [1, 4]
  c = 1
  e1 = nil

  begin
    a | c
  rescue => e1
  end

  (a | b) == [1, 2, 3, 4] and e1.class == TypeError and a == [1, 2, 3, 1]
end

assert("Array#&") do
  a = [1, 2, 3, 1]
  b = [1, 4]
  c = 1
  e1 = nil

  begin
    a & c
  rescue => e1
  end

  (a & b) == [1] and e1.class == TypeError and a == [1, 2, 3, 1]
end

assert("Array#flatten") do
  [1, 2, "3", {4=>5}, :'6'] == [1, 2, "3", {4=>5}, :'6'].flatten and
  [1, 2, 3, 4, 5, 6] == [1, 2, [3, 4, 5], 6].flatten and
  [1, 2, 3, 4, 5, 6] == [1, 2, [3, [4, 5], 6]].flatten and
  [1, [2, [3, [4, [5, [6]]]]]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(0) and
  [1, 2, [3, [4, [5, [6]]]]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(1) and
  [1, 2, 3, [4, [5, [6]]]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(2) and
  [1, 2, 3, 4, [5, [6]]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(3) and
  [1, 2, 3, 4, 5, [6]] == [1, [2, [3, [4, [5, [6]]]]]].flatten(4) and
  [1, 2, 3, 4, 5, 6] == [1, [2, [3, [4, [5, [6]]]]]].flatten(5)
end

assert("Array#flatten!") do
  [1, 2, 3, 4, 5, 6] == [1, 2, [3, [4, 5], 6]].flatten!
end

assert("Array#compact") do
  a = [1, nil, "2", nil, :t, false, nil]
  a.compact == [1, "2", :t, false] && a == [1, nil, "2", nil, :t, false, nil]
end

assert("Array#compact!") do
  a = [1, nil, "2", nil, :t, false, nil]
  a.compact!
  a == [1, "2", :t, false]
end

assert("Array#[]") do
  a = [ "a", "b", "c", "d", "e" ]
  a[1.1]   == "b" and
  a[1,2]   == ["b", "c"] and
  a[1..-2] == ["b", "c", "d"]
end