File: tuple_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (364 lines) | stat: -rw-r--r-- 8,471 bytes parent folder | download
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
require "spec"
require "spec/helpers/iterate"

private class TupleSpecObj
  getter x : Int32

  def initialize(@x)
  end

  def clone
    TupleSpecObj.new(@x)
  end

  def_equals @x
end

describe "Tuple" do
  it "does size" do
    {1, 2, 1, 2}.size.should eq(4)
  end

  it "checks empty?" do
    Tuple.new.empty?.should be_true
    {1}.empty?.should be_false
  end

  describe "#[] with non-literal index" do
    it "gets tuple element" do
      a = {1, 2.5}
      i = 0
      a[i].should eq(1)
      i = 1
      a[i].should eq(2.5)
      i = -1
      a[i].should eq(2.5)
      i = -2
      a[i].should eq(1)
      typeof(a[i]).should eq(Int32 | Float64)
    end

    it "raises index out of bounds" do
      a = {1, 2.5}
      i = 2
      expect_raises(IndexError) { a[i] }
      i = -3
      expect_raises(IndexError) { a[i] }
    end
  end

  describe "#[]? with non-literal index" do
    it "gets tuple element or nil" do
      a = {1, 2.5}
      i = 0
      a[i]?.should eq(1)
      i = -1
      a[i]?.should eq(2.5)
      i = 2
      a[i]?.should be_nil
      i = -3
      a[i]?.should be_nil
      typeof(a[i]?).should eq(Int32 | Float64 | Nil)
    end
  end

  describe ".[] with non-literal index" do
    it "gets tuple metaclass element" do
      a = Tuple(Int32, Float64)
      i = 0
      a[i].should eq(Int32)
      i = 1
      a[i].should eq(Float64)
      i = -1
      a[i].should eq(Float64)
      i = -2
      a[i].should eq(Int32)
    end

    it "raises index out of bounds" do
      a = Tuple(Int32, Float64)
      i = 2
      expect_raises(IndexError) { a[i] }
      i = -3
      expect_raises(IndexError) { a[i] }
    end
  end

  describe ".[]? with non-literal index" do
    it "gets tuple metaclass element or nil" do
      a = Tuple(Int32, Float64)
      i = 0
      a[i]?.should eq(Int32)
      i = -1
      a[i]?.should eq(Float64)
      i = 2
      a[i]?.should be_nil
      i = -3
      a[i]?.should be_nil
      typeof(a[i]?).should eq(Union(Int32.class, Float64.class, Nil))
    end
  end

  it "does at" do
    a = {1, 2}
    a.at(1).should eq(2)
    a.at(-1).should eq(2)

    expect_raises(IndexError) { a.at(2) }
    expect_raises(IndexError) { a.at(-3) }

    a.at(2) { 3 }.should eq(3)
    a.at(-3) { 3 }.should eq(3)
  end

  describe "values_at" do
    it "returns the given indexes" do
      {"a", "b", "c", "d"}.values_at(1, 0, 2).should eq({"b", "a", "c"})
    end

    it "raises when passed an invalid index" do
      expect_raises IndexError do
        {"a"}.values_at(10)
      end
    end

    it "works with mixed types" do
      {1, "a", 1.0, false}.values_at(0, 1, 2, 3).should eq({1, "a", 1.0, false})
    end
  end

  it "does ==" do
    a = {1, 2}
    b = {3, 4}
    c = {1, 2, 3}
    d = {1}
    e = {1, 2}
    a.should eq(a)
    a.should eq(e)
    a.should_not eq(b)
    a.should_not eq(c)
    a.should_not eq(d)
  end

  it "does == with different types but same size" do
    {1, 2}.should eq({1.0, 2.0})
  end

  it "does == with another type" do
    {1, 2}.should_not eq(1)
  end

  it "does compare" do
    a = {1, 2}
    b = {3, 4}
    c = {1, 6}
    d = {3, 5}
    e = {0, 8}
    [a, b, c, d, e].sort.should eq([e, a, c, b, d])
    [a, b, c, d, e].min.should eq(e)
  end

  it "does compare with different sizes" do
    a = {2}
    b = {1, 2, 3}
    c = {1, 2}
    d = {1, 1}
    e = {1, 1, 3}
    [a, b, c, d, e].sort.should eq([d, e, c, b, a])
    [a, b, c, d, e].min.should eq(d)
  end

  it "does to_s" do
    {1, 2, 3}.to_s.should eq("{1, 2, 3}")
  end

  it "does each" do
    a = 0
    {1, 2, 3}.each do |i|
      a += i
    end.should be_nil
    a.should eq(6)
  end

  it "does dup" do
    r1, r2 = TupleSpecObj.new(10), TupleSpecObj.new(20)
    t = {r1, r2}
    u = t.dup
    u.size.should eq(2)
    u[0].should be(r1)
    u[1].should be(r2)
  end

  it "does clone" do
    r1, r2 = TupleSpecObj.new(10), TupleSpecObj.new(20)
    t = {r1, r2}
    u = t.clone
    u.size.should eq(2)
    u[0].x.should eq(r1.x)
    u[0].should_not be(r1)
    u[1].x.should eq(r2.x)
    u[1].should_not be(r2)
  end

  it "does Tuple.new, without type vars" do
    Tuple.new(1, 2, 3).should eq({1, 2, 3})
    Tuple.new([1, 2, 3]).should eq({[1, 2, 3]})
    Tuple.new(TupleSpecObj.new(10)).should eq({TupleSpecObj.new(10)})
  end

  it "does Tuple.new, with type vars" do
    Tuple(Int32, String).new(1, "a").should eq({1, "a"})
    Tuple(TupleSpecObj).new(TupleSpecObj.new(10)).should eq({TupleSpecObj.new(10)})
    typeof(Tuple.new).new.should eq(Tuple.new)

    t = Tuple(Int32 | String, Int32 | String).new(1, "a")
    t.should eq({1, "a"})
    t.class.should_not eq(Tuple(Int32, String))
  end

  it "does Tuple.from" do
    t = Tuple(Int32, Float64).from([1_i32, 2.0_f64])
    t.should eq({1_i32, 2.0_f64})
    t.class.should eq(Tuple(Int32, Float64))

    expect_raises ArgumentError do
      Tuple(Int32).from([1, 2])
    end

    expect_raises(TypeCastError, /[Cc]ast from String to Int32 failed/) do
      Tuple(Int32, String).from(["foo", 1])
    end
  end

  it "does Tuple#from" do
    t = {Int32, Float64}.from([1_i32, 2.0_f64])
    t.should eq({1_i32, 2.0_f64})
    t.class.should eq(Tuple(Int32, Float64))

    expect_raises ArgumentError do
      {Int32}.from([1, 2])
    end

    expect_raises(TypeCastError, /[Cc]ast from String to Int32 failed/) do
      {Int32, String}.from(["foo", 1])
    end
  end

  it "clones empty tuple" do
    Tuple.new.clone.should eq(Tuple.new)
  end

  it_iterates "#each", [1, 2, 3], {1, 2, 3}.each

  it "does map" do
    tuple = {1, 2.5, "a"}
    tuple2 = tuple.map &.to_s
    tuple2.is_a?(Tuple).should be_true
    tuple2.should eq({"1", "2.5", "a"})
  end

  it "does map_with_index" do
    tuple = {1, 1, 2, 2}
    tuple2 = tuple.map_with_index { |e, i| e + i }
    tuple2.should eq({1, 2, 4, 5})
  end

  it "does map_with_index, with offset" do
    tuple = {1, 1, 2, 2}
    tuple2 = tuple.map_with_index(10) { |e, i| e + i }
    tuple2.should eq({11, 12, 14, 15})
  end

  it "does reverse" do
    {1, 2.5, "a", 'c'}.reverse.should eq({'c', "a", 2.5, 1})
  end

  it_iterates "#reverse_each", [3, 2, 1], {1, 2, 3}.reverse_each

  it "gets first element" do
    tuple = {1, 2.5}
    tuple.first.should eq(1)
    typeof(tuple.first).should eq(Int32)
  end

  it "gets first? element" do
    tuple = {1, 2.5}
    tuple.first?.should eq(1)

    Tuple.new.first?.should be_nil
  end

  it "gets last element" do
    tuple = {1, 2.5, "a"}
    tuple.last.should eq("a")
    typeof(tuple.last).should eq(String)
  end

  it "gets last? element" do
    tuple = {1, 2.5, "a"}
    tuple.last?.should eq("a")

    Tuple.new.last?.should be_nil
  end

  it "does comparison" do
    tuple1 = {"a", "a", "c"}
    tuple2 = {"a", "b", "c"}
    (tuple1 <=> tuple2).should eq(-1)
    (tuple2 <=> tuple1).should eq(1)
  end

  it "does <=> for equality" do
    tuple1 = {0, 1}
    tuple2 = {0.0, 1}
    (tuple1 <=> tuple2).should eq(0)
  end

  it "does <=> with the same beginning and different size" do
    tuple1 = {1, 2, 3}
    tuple2 = {1, 2}
    (tuple1 <=> tuple2).should eq(1)
  end

  it "does types" do
    tuple = {1, 'a', "hello"}
    tuple.class.types.to_s.should eq("{Int32, Char, String}")
  end

  it "does ===" do
    ({1, 2} === {1, 2}).should be_true
    ({1, 2} === {1, 3}).should be_false
    ({1, 2, 3} === {1, 2}).should be_false
    ({/o+/, "bar"} === {"fox", "bar"}).should be_true
    ({1, 2} === nil).should be_false
  end

  it "does to_a" do
    ary = {1, 'a', true}.to_a
    ary.should eq([1, 'a', true])
    ary.size.should eq(3)

    ary = Tuple.new.to_a
    ary.size.should eq(0)
  end

  # Tuple#to_static_array don't compile on aarch64-darwin and
  # aarch64-linux-musl due to a codegen error caused by LLVM < 13.0.0.
  # See https://github.com/crystal-lang/crystal/issues/11358 for details.
  {% unless compare_versions(Crystal::LLVM_VERSION, "13.0.0") < 0 && flag?(:aarch64) && (flag?(:musl) || flag?(:darwin) || flag?(:android)) %}
    it "#to_static_array" do
      ary = {1, 'a', true}.to_static_array
      ary.should be_a(StaticArray(Int32 | Char | Bool, 3))
      ary.should eq(StaticArray[1, 'a', true])
      ary.size.should eq(3)

      ary = Tuple.new.to_static_array
      ary.should be_a(StaticArray(NoReturn, 0))
      ary.size.should eq(0)

      ary = Tuple(String | Int32).new(1).to_static_array
      ary.should be_a(StaticArray(String | Int32, 1))
      ary.should eq StaticArray[1.as(String | Int32)]
    end
  {% end %}
end