File: test_vector.rb

package info (click to toggle)
sonic-pi 3.2.2~repack-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 71,872 kB
  • sloc: ruby: 30,548; cpp: 8,490; sh: 957; ansic: 461; erlang: 360; lisp: 141; makefile: 44
file content (376 lines) | stat: -rw-r--r-- 10,372 bytes parent folder | download | duplicates (4)
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
365
366
367
368
369
370
371
372
373
374
375
376
#--
# This file is part of Sonic Pi: http://sonic-pi.net
# Full project source: https://github.com/samaaron/sonic-pi
# License: https://github.com/samaaron/sonic-pi/blob/master/LICENSE.md
#
# Copyright 2013, 2014, 2015, 2016 by Sam Aaron (http://sam.aaron.name).
# All rights reserved.
#
# Permission is granted for use, copying, modification, and
# distribution of modified versions of this work as long as this
# notice is included.
#++

require_relative "./setup_test"
require_relative "../lib/sonicpi/lang/core"

module SonicPi
  class VectorTester < Minitest::Test
    include SonicPi::Lang::Core

    def test_index
      v = vector(:a, :b, :c)
      assert_equal(v[0], :a)
      assert_equal(v[-1], :c)
      assert_equal(v[-100], nil)
      assert_equal(v[100], nil)
    end

    def test_amper
      v1 = vector( 1, 1, 3, 5)
      v2 = vector( 3, 2, 1)
      a2 = [3, 2, 1]
      v3 = vector(1, 3)
      assert_equal(v1 & v2, v3)
      assert_equal(v1 & a2, v3)
    end

    def test_plus_listy
      v = [ 1, 2, 3 ].to_spv + [ 4, 5 ].to_spv
      assert_equal vector(1, 2, 3, 4, 5), v


      v2 = [ 1, 2, 3 ].to_spv + [ 4, 5 ]
      assert_equal vector(1, 2, 3, 4, 5), v2
    end

    def test_plus_other
      v = [ 1, 2, 3 ].to_spv + 10
      assert_equal(vector(11, 12, 13), v)

      v2 = [ 1, 2, 3 ].to_spv + 10.5
      assert_equal(vector(11.5, 12.5, 13.5), v2)
    end

    def test_minus_listy
      v = [ 1, 2, 3 ].to_spv - [ 4, 5, 1, 2 ].to_spv
      assert_equal vector(3), v

      v2 = [ 1, 2, 3 ].to_spv - [ 4, 5, 1, 2 ]
      assert_equal vector(3), v2
    end

    def test_minus_other
      v = [ 11, 12, 13 ].to_spv - 10
      assert_equal(vector(1, 2, 3), v)

      v2 = [ 11.5, 12.5, 13.5 ].to_spv - 10.5
      assert_equal(vector(1, 2, 3), v2)
    end

    def test_spaceship
      v1 = [ "a", "a", "c" ].to_spv
      v2 = [ "a", "b", "c" ].to_spv
      assert_equal(v1 <=> v2, -1)

      v1 = [ "a", "a", "c" ].to_spv
      v2 = [ "a", "b", "c" ]
      assert_equal(v1 <=> v2, -1)

      v3 = [ 1, 2, 3, 4, 5, 6 ].to_spv
      v4 = [ 1, 2 ].to_spv
      assert_equal(v3 <=> v4, 1)

      v3 = [ 1, 2, 3, 4, 5, 6 ].to_spv
      v4 = [ 1, 2 ]
      assert_equal(v3 <=> v4, 1)

      v5 = [ 1, 2 ].to_spv
      v6 = [ 1, :two ].to_spv
      assert_equal(v5 <=> v6, nil)

      v5 = [ 1, 2 ].to_spv
      v6 = [ 1, :two ]
      assert_equal(v5 <=> v6, nil)
    end

    def test_double_equals
      assert_not_equal([ "a", "c" ].to_spv, [ "a", "c", 7 ].to_spv)
      assert_not_equal([ "a", "c" ].to_spv, [ "a", "c", 7 ])
      assert_equal([ "a", "c", 7 ].to_spv,  [ "a", "c", 7].to_spv)
      assert_not_equal([ "a", "c", 7 ].to_spv,  [ "a", "d", "f"].to_spv)
    end

    def test_square_brackets
      a = [ "a", "b", "c", "d", "e" ].to_spv
      assert_equal(a[0], "a")
      assert_equal(a[1], "b")
      assert_equal(a[2], "c")

      assert_equal(a[6], nil)
      assert_equal(a[1, 2], [ "b", "c" ].to_spv)
      assert_equal(a[1..3], [ "b", "c", "d" ].to_spv)
      assert_equal(a[4..7], [ "e" ].to_spv)
      assert_equal(a[6..10], nil)
      assert_equal(a[-3, 3], [ "c", "d", "e" ].to_spv)

      assert_equal(a[5], nil)
      assert_equal(a[6, 1], nil)
      assert_equal(a[5, 1], [].to_spv)
      assert_equal(a[5..10], [].to_spv)
    end

    def test_all
      v = [1, 2, 3].to_spv
      assert(v.all? {|v| v > 0})
      assert_not(v.all? {|v| v < 0})
    end

    def test_any
      v = [1, 2, 3].to_spv
      assert(v.any? {|v| v > 0})
      assert_not(v.any? {|v| v > 3})
    end

    def test_compact
      a = [ "a", nil, "b", nil, "c", nil ].to_spv
      assert_equal(a.compact, [ "a", "b", "c" ].to_spv)
    end

    def test_each
      res = []
      v = [1, 2, 3].to_spv
      v.each do |el|
        res << el * 2
      end
      assert_equal([2, 4, 6], res)
    end

    def test_each_with_index
      res = []
      v = [1, 2, 3].to_spv
      v.each_with_index do |el, i|
        res << [el * 2, i]
      end
      assert_equal([[2, 0], [4, 1], [6, 2]], res)
    end

    def test_empty
      assert([].to_spv.empty?)
      assert_not([3].to_spv.empty?)
    end

    def test_eql
      v1 = [1, 2 ,3].to_spv
      v2 = [1, 2 ,3].to_spv
      v3 = [0, 2 ,3].to_spv
      assert(v1.eql?(v2))
      assert(v1.eql?(v1))
      assert_not(v1.eql?(v3))
    end

    def test_filter
      res = [1,2,3,4,5].to_spv.filter {|num| num.even? }
      assert_equal(res, [2, 4].to_spv)

      a = (%w[ a b c d e f ]).to_spv
      res2 = a.filter {|v| v =~ /[aeiou]/ }
      assert_equal(res2, ["a", "e"].to_spv)
    end

    def test_first
      assert_equal([1, 2, 3].to_spv.first, 1)
      assert_equal([1, 2, 3].to_spv.first(2), [1, 2].to_spv)
      assert_equal([1, 2, 3].to_spv.first(20), [1, 2, 3].to_spv)
    end

    def test_flatten
      s = [ 1, 2, 3 ].to_spv
      t = [ 4, 5, 6, [7, 8].to_spv ].to_spv
      a = [ s, t, 9, 10 ].to_spv
      res = a.flatten
      assert_equal(res, [1, 2, 3, 4, 5, 6, 7, 8, 9,10].to_spv)

      s2 = [ 1, 2, 3 ].to_spv
      t2 = [ 4, 5, 6, [7, 8] ].to_spv
      a2 = [ s2, t2, 9, 10 ].to_spv
      res2 = a2.flatten
      assert_equal(res2, [1, 2, 3, 4, 5, 6, 7, 8, 9,10].to_spv)

      a3 = [ 1, 2, [3, [4, 5].to_spv ].to_spv ].to_spv
      res3 = a3.flatten(1)
      assert_equal(res3, [1, 2, 3, [4, 5].to_spv].to_spv)

      s4 = [ 1, 2, 3 ].to_spv
      t4 = [ 4, 5, 6, [7, [8,8,8]] ].to_spv
      a4 = [ s4, t4, 9, 10 ].to_spv
      res4 = a4.flatten
      assert_equal(res4, [1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9,10].to_spv)

      s5 = [ 1, 2, 3 ].to_spv
      t5 = [ 4, 5, 6, [7, [8,8,[8, 8].to_spv]] ].to_spv
      a5 = [ s5, t5, 9, 10 ].to_spv
      res5 = a5.flatten(2)
      assert_equal(res5, [1, 2, 3, 4, 5, 6, 7, [8, 8, [8, 8].to_spv], 9,10].to_spv)
    end

    def test_flatten_within_standard_array
      s1 = [ 1, 2, 3 ].to_spv
      assert_equal(s1.to_a, [s1].flatten)
    end

    def test_flat_map
      res = [1, 2, 3, 4].to_spv.flat_map { |e| [e, -e].to_spv }
      assert_equal(res, [1, -1, 2, -2, 3, -3, 4, -4].to_spv)

      res2 = [1, 2, 3, 4].to_spv.flat_map { |e| [e, -e] }
      assert_equal(res2, [1, -1, 2, -2, 3, -3, 4, -4].to_spv)
#[[1, 2], [3, 4]].flat_map { |e| e + [100] } #=> [1, 2, 100, 3, 4, 100]
    end

    def test_index
      a = [ "a", "b", "c" ].to_spv
      assert_equal(a.index("b"), 1)
      assert_equal(a.index("z"), nil)
      assert_equal(a.index {|x| x == "b"}, 1)
    end

    def test_join
      assert_equal([ "a", "b", "c" ].to_spv.join, "abc")
      assert_equal([ "a", "b", "c" ].to_spv.join("-"), "a-b-c")
    end

    def test_last
      a = [ "w", "x", "y", "z" ].to_spv
      assert_equal(a.last, "z")
      assert_equal(a.last(2), ["y", "z"].to_spv)
    end

    def test_length
      vector = [3, 2, 1].to_spv
      assert_equal(3, vector.length)
    end

    def test_map
      res = []
      v = [1, 2, 3].to_spv
      v.map do |el|
        res << el * 2
      end
      assert_equal([2, 4, 6], res)
    end

    def test_max
      ary = (%w(albatross dog horse)).to_spv
      assert_equal(ary.max,  "horse")
      assert_equal(ary.max {|a, b| a.length <=> b.length}, "albatross")


      assert_equal(ary.max(2), ["horse", "dog"].to_spv)
      assert_equal(ary.max(2) {|a, b| a.length <=> b.length }, ["albatross", "horse"].to_spv)
    end

    def test_min
      ary = (%w(albatross dog horse)).to_spv
      assert_equal(ary.min,  "albatross")
      assert_equal(ary.min {|a, b| a.length <=> b.length}, "dog")


      assert_equal(ary.min(2), ["albatross", "dog"].to_spv)
      assert_equal(ary.min(2) {|a, b| a.length <=> b.length }, ["dog", "horse"].to_spv)
    end

    def test_reverse
      assert_equal([ "a", "b", "c" ].to_spv.reverse, ["c", "b", "a"].to_spv)
      assert_equal([ 1 ].to_spv.reverse, [1].to_spv)
    end

    def test_rotate
      a = [ "a", "b", "c", "d" ].to_spv
      assert_equal(a.rotate, ["b", "c", "d", "a"].to_spv)
      assert_equal(a,["a", "b", "c", "d"].to_spv)
      assert_equal(a.rotate(2), ["c", "d", "a", "b"].to_spv)
      assert_equal(a.rotate(-3),["b", "c", "d", "a"].to_spv)
    end

    def test_shuffle
      a = [ "a", "b", "c", "d", "e", "f", "g", "h", "i" ].to_spv * 20
      # hopefully this doesn't shuffle it to be the same ;-)
      assert_not_equal(a, a.shuffle)
      assert_equal(a.class, a.shuffle.class)
    end

    def test_sort
      v = [ "d", "a", "e", "c", "b" ].to_spv
      assert_equal(v.sort, ["a", "b", "c", "d", "e"].to_spv)
      assert_equal(v.sort {|a, b| b <=> a}, ["e", "d", "c", "b", "a"].to_spv)
    end

    def test_take
      a = [1, 2, 3, 4, 5, 0].to_spv
      assert_equal(a.take(3), [1, 2, 3].to_spv)
      assert_equal(a.take(-3), [0, 5, 4].to_spv)
      assert_equal([].to_spv.take(-3), [].to_spv)

      assert_equal([1, 2, 3].to_spv.take(7), [1, 2, 3, 1, 2, 3, 1].to_spv)
    end

    def test_drop
      a = [1, 2, 3, 4, 5, 0].to_spv
      assert_equal(a.drop(3), [4, 5, 0].to_spv)
      assert_equal(a.drop(30), [].to_spv)
    end

    def test_drop_last
      a = [1, 2, 3, 4, 5, 0].to_spv
      assert_equal(a.drop_last, [1, 2, 3, 4, 5].to_spv)
      assert_equal(a.drop_last(3), [1, 2, 3].to_spv)
    end

    def test_size
      assert_equal([1, 2, 3, 4].to_spv.size, 4)
    end

    def test_uniq
      a = [ "a", "a", "b", "b", "c" ].to_spv
      assert_equal(a.uniq, ["a", "b", "c"].to_spv)

      b = [["student","sam"].to_spv, ["student","george"].to_spv, ["teacher","matz"].to_spv].to_spv
      assert_equal(b.uniq {|s| s.first},  [["student", "sam"].to_spv, ["teacher", "matz"].to_spv].to_spv)
    end

    def test_value_at
      a = (%w{ a b c d e f }).to_spv
      assert_equal(["b", "d", "f"].to_spv, a.values_at(1, 3, 5))
      assert_equal(["b", "d", "f", nil].to_spv, a.values_at(1, 3, 5, 7))
      assert_equal(["f", "e", "e", nil].to_spv, a.values_at(-1, -2, -2, -7))
      assert_equal(["e", "f", nil, "d", "e", "f"].to_spv, a.values_at(4..6, 3...6))
    end

    def test_star
      v = vector(:a, :b, :c)
      v2 = vector(:a, :b, :c, :a, :b, :c)
      assert_equal(v * 2, v2)
    end



    def test_init
      v = vector(:a, :b, :c)
      assert v.sp_thread_safe?
    end

    def test_init_w_non_ts_vals
      v = [:a, :b, []].to_spv
      v2 = v.__sp_make_thread_safe

      assert !v.sp_thread_safe?

      assert v == v2

      assert v2.sp_thread_safe?
      assert v2[2].frozen?
    end

  end
end