external 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/History +5 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +168 -0
  4. data/lib/ext_arc.rb +108 -0
  5. data/lib/ext_arr.rb +727 -0
  6. data/lib/ext_ind.rb +1120 -0
  7. data/lib/external/base.rb +85 -0
  8. data/lib/external/chunkable.rb +105 -0
  9. data/lib/external/enumerable.rb +137 -0
  10. data/lib/external/io.rb +398 -0
  11. data/lib/external.rb +3 -0
  12. data/test/benchmarks/benchmarks_20070918.txt +45 -0
  13. data/test/benchmarks/benchmarks_20070921.txt +91 -0
  14. data/test/benchmarks/benchmarks_20071006.txt +147 -0
  15. data/test/benchmarks/test_copy_file.rb +80 -0
  16. data/test/benchmarks/test_pos_speed.rb +47 -0
  17. data/test/benchmarks/test_read_time.rb +55 -0
  18. data/test/cached_ext_ind_test.rb +219 -0
  19. data/test/check/benchmark_check.rb +441 -0
  20. data/test/check/namespace_conflicts_check.rb +23 -0
  21. data/test/check/pack_check.rb +90 -0
  22. data/test/ext_arc_test.rb +286 -0
  23. data/test/ext_arr/alt_sep.txt +3 -0
  24. data/test/ext_arr/cr_lf_input.txt +3 -0
  25. data/test/ext_arr/input.index +0 -0
  26. data/test/ext_arr/input.txt +1 -0
  27. data/test/ext_arr/inputb.index +0 -0
  28. data/test/ext_arr/inputb.txt +1 -0
  29. data/test/ext_arr/lf_input.txt +3 -0
  30. data/test/ext_arr/lines.txt +19 -0
  31. data/test/ext_arr/without_index.txt +1 -0
  32. data/test/ext_arr_test.rb +534 -0
  33. data/test/ext_ind_test.rb +1472 -0
  34. data/test/external/base_test.rb +74 -0
  35. data/test/external/chunkable_test.rb +182 -0
  36. data/test/external/index/input.index +0 -0
  37. data/test/external/index/inputb.index +0 -0
  38. data/test/external/io_test.rb +414 -0
  39. data/test/external_test_helper.rb +31 -0
  40. data/test/external_test_suite.rb +4 -0
  41. data/test/test_array.rb +1192 -0
  42. metadata +104 -0
@@ -0,0 +1,1472 @@
1
+ require File.join(File.dirname(__FILE__), 'external_test_helper.rb')
2
+ require 'ext_ind'
3
+ require 'fileutils'
4
+
5
+ require 'ext_arr'
6
+
7
+ class ExtIndTest < Test::Unit::TestCase
8
+ include Benchmark
9
+ include External
10
+ include TestArray
11
+
12
+ attr_reader :index, :tempfile
13
+
14
+ def setup
15
+ # cls represents an array
16
+ @cls = ExtInd
17
+
18
+ @tempfile = Tempfile.new("indextest")
19
+ @tempfile << array.pack(format)
20
+ @tempfile.pos = 0
21
+
22
+ @index = ExtInd.new(@tempfile)
23
+ end
24
+
25
+ def array
26
+ [1,2,3,4,5]
27
+ end
28
+
29
+ def framed_array
30
+ [[1],[2],[3],[4],[5]]
31
+ end
32
+
33
+ def format
34
+ "I*"
35
+ end
36
+
37
+ def teardown
38
+ @tempfile.close unless @tempfile.closed?
39
+ end
40
+
41
+ #
42
+ # readme doc test
43
+ #
44
+
45
+ def test_readme_doc_for_ext_ind
46
+ ea = ExtArr.new
47
+ assert_equal ExtInd, ea.index.class
48
+ index = ea.index
49
+ assert_equal 'I*', index.format
50
+ assert_equal 2, index.frame
51
+ index << [1,2]
52
+ index << [3,4]
53
+ assert_equal [[1,2],[3,4]], index.to_a
54
+
55
+ Tempfile.open('test_readme_doc_for_ext_ind') do |file|
56
+ file << [1,2,3].pack("IQS")
57
+ file << [4,5,6].pack("IQS")
58
+ file << [7,8,9].pack("IQS")
59
+ file.flush
60
+
61
+ index = ExtInd.new(file, :format => "IQS")
62
+ assert_equal [4,5,6], index[1]
63
+ assert_equal [[1,2,3],[4,5,6],[7,8,9]], index.to_a
64
+ end
65
+ end
66
+
67
+ #
68
+ # setup tests
69
+ #
70
+
71
+ def test_setup
72
+ assert_equal ExtInd, @cls
73
+
74
+ assert_equal 0, index.pos
75
+ assert_equal 5, index.length
76
+ assert_equal 4, index.frame_size
77
+ assert_equal 8 * 2**20, index.buffer_size
78
+ assert_equal [0], index.nil_value
79
+ assert !index.cached?
80
+ assert_nil index.cache
81
+ assert_equal({:format => "I", :buffer_size => 8 * 2**20, :nil_value => [0], :cached => false}, index.options)
82
+
83
+ tempfile.pos = 0
84
+ assert_equal array.pack(format), tempfile.read
85
+ assert_equal tempfile.path, index.io.path
86
+ end
87
+
88
+ #
89
+ # class read tests
90
+ #
91
+
92
+ def test_read_returns_the_index_file_in_frame
93
+ assert_equal framed_array, ExtInd.read(tempfile.path)
94
+
95
+ tempfile.pos = tempfile.length
96
+ tempfile << [6].pack("I")
97
+ tempfile.flush
98
+ tempfile.pos = 0
99
+ assert_equal [1,2,3,4,5,6].pack("I*"), tempfile.read
100
+
101
+ assert_equal [[1,2],[3,4],[5,6]], ExtInd.read(tempfile.path, :format => 'II')
102
+ end
103
+
104
+ #
105
+ # class directive size tests
106
+ #
107
+
108
+ def test_directive_size_returns_the_number_of_bytes_to_pack_a_directive
109
+ # @ | Moves to absolute position
110
+ # not implemented
111
+ assert_nil ExtInd.directive_size('@')
112
+ # A | ASCII string (space padded, count is width)
113
+ assert_equal 1, ["a"].pack("A").length
114
+ assert_equal 1, ExtInd.directive_size('A')
115
+ # a | ASCII string (null padded, count is width)
116
+ assert_equal 1, ["a"].pack("a").length
117
+ assert_equal 1, ExtInd.directive_size('a')
118
+ # B | Bit string (descending bit order)
119
+ assert_equal 1, ['a'].pack("B").length
120
+ assert_equal 1, ExtInd.directive_size('B')
121
+ # b | Bit string (ascending bit order)
122
+ assert_equal 1, ['a'].pack("b").length
123
+ assert_equal 1, ExtInd.directive_size('b')
124
+ # C | Unsigned char
125
+ assert_equal 1, [1].pack("C").length
126
+ assert_equal 1, ExtInd.directive_size('C')
127
+ # c | Char
128
+ assert_equal 1, [1].pack("c").length
129
+ assert_equal 1, ExtInd.directive_size('c')
130
+ # D, d | Double-precision float, native format
131
+ assert_equal 8, [1].pack("D").length
132
+ assert_equal 8, ExtInd.directive_size('D')
133
+ assert_equal 8, [1].pack("d").length
134
+ assert_equal 8, ExtInd.directive_size('d')
135
+ # E | Double-precision float, little-endian byte order
136
+ assert_equal 8, [1].pack("E").length
137
+ assert_equal 8, ExtInd.directive_size('E')
138
+ # e | Single-precision float, little-endian byte order
139
+ assert_equal 4, [1].pack("e").length
140
+ assert_equal 4, ExtInd.directive_size('e')
141
+ # F, f | Single-precision float, native format
142
+ assert_equal 4, [1].pack("F").length
143
+ assert_equal 4, ExtInd.directive_size('F')
144
+ assert_equal 4, [1].pack("f").length
145
+ assert_equal 4, ExtInd.directive_size('f')
146
+ # G | Double-precision float, network (big-endian) byte order
147
+ assert_equal 8, [1].pack("G").length
148
+ assert_equal 8, ExtInd.directive_size('G')
149
+ # g | Single-precision float, network (big-endian) byte order
150
+ assert_equal 4, [1].pack("g").length
151
+ assert_equal 4, ExtInd.directive_size('g')
152
+ # H | Hex string (high nibble first)
153
+ assert_equal 1, ['a'].pack("H").length
154
+ assert_equal 1, ExtInd.directive_size('H')
155
+ # h | Hex string (low nibble first)
156
+ assert_equal 1, ['a'].pack("h").length
157
+ assert_equal 1, ExtInd.directive_size('h')
158
+ # I | Unsigned integer
159
+ assert_equal 4, [1].pack("I").length
160
+ assert_equal 4, ExtInd.directive_size('I')
161
+ # i | Integer
162
+ assert_equal 4, [1].pack("i").length
163
+ assert_equal 4, ExtInd.directive_size('i')
164
+ # L | Unsigned long
165
+ assert_equal 4, [1].pack("L").length
166
+ assert_equal 4, ExtInd.directive_size('L')
167
+ # l | Long
168
+ assert_equal 4, [1].pack("l").length
169
+ assert_equal 4, ExtInd.directive_size('l')
170
+ # M | Quoted printable, MIME encoding (see RFC2045)
171
+ assert_equal 3, ['a'].pack("M").length
172
+ assert_equal 3, ExtInd.directive_size('M')
173
+ # m | Base64 encoded string
174
+ assert_equal 5, ['a'].pack("m").length
175
+ assert_equal 5, ExtInd.directive_size('m')
176
+ # N | Long, network (big-endian) byte order
177
+ assert_equal 4, [1].pack("N").length
178
+ assert_equal 4, ExtInd.directive_size('N')
179
+ # n | Short, network (big-endian) byte-order
180
+ assert_equal 2, [1].pack("n").length
181
+ assert_equal 2, ExtInd.directive_size('n')
182
+ # P | Pointer to a structure (fixed-length string)
183
+ assert_equal 4, ['a'].pack("P").length
184
+ assert_equal 4, ExtInd.directive_size('P')
185
+ # p | Pointer to a null-terminated string
186
+ assert_equal 4, ['a'].pack("p").length
187
+ assert_equal 4, ExtInd.directive_size('p')
188
+ # Q, q | 64-bit number
189
+ assert_equal 8, [1].pack("Q").length
190
+ assert_equal 8, ExtInd.directive_size('Q')
191
+ assert_equal 8, [1].pack("q").length
192
+ assert_equal 8, ExtInd.directive_size('q')
193
+ # S | Unsigned short
194
+ assert_equal 2, [1].pack("S").length
195
+ assert_equal 2, ExtInd.directive_size('S')
196
+ # s | Short
197
+ assert_equal 2, [1].pack("s").length
198
+ assert_equal 2, ExtInd.directive_size('s')
199
+ # U | UTF-8
200
+ assert_equal 1, [1].pack("U").length
201
+ assert_equal 1, ExtInd.directive_size('U')
202
+ # u | UU-encoded string
203
+ assert_equal 6, ['a'].pack("u").length
204
+ assert_equal 6, ExtInd.directive_size('u')
205
+ # V | Long, little-endian byte order
206
+ assert_equal 4, [1].pack("V").length
207
+ assert_equal 4, ExtInd.directive_size('V')
208
+ # v | Short, little-endian byte order
209
+ assert_equal 2, [1].pack("v").length
210
+ assert_equal 2, ExtInd.directive_size('v')
211
+ # w | BER-compressed integer\fnm
212
+ # not implemented
213
+ assert_equal 1, [1].pack("w").length
214
+ assert_equal 1, ExtInd.directive_size('w')
215
+ # X | Back up a byte
216
+ # not implemented
217
+ assert_nil ExtInd.directive_size('X')
218
+ # x | Null byte
219
+ assert_equal 1, [nil].pack("x").length
220
+ assert_equal 1, ExtInd.directive_size('x')
221
+ # Z | Same as ``a'', except that null is added with *
222
+ assert_equal 1, ['a'].pack("Z").length
223
+ assert_equal 1, ExtInd.directive_size('Z')
224
+ end
225
+
226
+ #
227
+ # initialize tests
228
+ #
229
+
230
+ def test_index_initialized_to_single_int_format_by_default
231
+ index = @cls.new
232
+
233
+ assert_equal 'I*', index.format
234
+ assert_equal 1, index.frame
235
+ assert_equal 4, index.frame_size
236
+ assert_equal [0], index.nil_value
237
+ assert !index.cached?
238
+ assert_nil index.cache
239
+ end
240
+
241
+ def test_initialize_calculates_frame_and_frame_size_from_format
242
+ {
243
+ 'I' => [1, 4],
244
+ 'II' => [2, 8],
245
+ 'IQS' => [3, 14]
246
+ }.each_pair do |format, expected|
247
+ index = @cls.new(nil, :format => format)
248
+
249
+ assert_equal expected, [index.frame, index.frame_size]
250
+ assert_equal Array.new(expected.first, 0), index.nil_value
251
+ end
252
+ end
253
+
254
+ def test_format_with_unknown_directive_raises_error
255
+ assert_raise(ArgumentError) { @cls.new(nil, :format => 'x') }
256
+ end
257
+
258
+ def test_default_nil_value_is_calculated_according_to_frame
259
+ index = @cls.new(nil, :format => "I")
260
+ assert_equal [0], index.nil_value
261
+
262
+ index = @cls.new(nil, :format => "II")
263
+ assert_equal [0, 0], index.nil_value
264
+ end
265
+
266
+ def test_nil_value_can_be_provided_in_options
267
+ index = @cls.new(nil, :nil_value => [8])
268
+ assert_equal [8], index.nil_value
269
+
270
+ index = @cls.new(nil, :format => "II", :nil_value => [8,9])
271
+ assert_equal [8,9], index.nil_value
272
+ end
273
+
274
+ def test_nil_value_option_raises_error_if_out_of_frame
275
+ assert_raise(ArgumentError) { @cls.new(nil, :nil_value => [8, 9]) }
276
+ assert_raise(ArgumentError) { @cls.new(nil, :format => 'II', :nil_value => [0]) }
277
+ end
278
+
279
+ #
280
+ # buffer_size test
281
+ #
282
+
283
+ def test_index_and_io_chunk_and_gap_size_are_calculated_relative_to_buffer_size
284
+ index = @cls.new(nil, :buffer_size => 40000)
285
+ assert_equal 40000, index.buffer_size
286
+ assert_equal 4, index.frame_size
287
+ assert_equal 10000, index.default_blksize
288
+ assert_equal 40000, index.io.default_blksize
289
+
290
+ index.buffer_size = 80000
291
+ assert_equal 20000, index.default_blksize
292
+ assert_equal 80000, index.io.default_blksize
293
+ end
294
+
295
+ def test_buffer_size_is_io_default_blksize
296
+ index = @cls.new
297
+
298
+ index.io.default_blksize = 1000
299
+ assert_equal 1000, index.io.default_blksize
300
+ assert_equal 1000, index.buffer_size
301
+ end
302
+
303
+ #
304
+ # default_blksize test
305
+ #
306
+
307
+ def test_default_blksize_sets_io_default_blksize
308
+ index = @cls.new
309
+ assert_equal 4, index.frame_size
310
+
311
+ index.default_blksize = 10000
312
+ assert_equal 10000, index.default_blksize
313
+ assert_equal 40000, index.io.default_blksize
314
+ end
315
+
316
+ #
317
+ # nil_value tests
318
+ #
319
+
320
+ def test_nil_value_documentation
321
+ i = @cls.new
322
+ assert_equal [0], i.nil_value
323
+ assert_equal "\000\000\000\000", i.nil_value(false)
324
+ end
325
+
326
+ #
327
+ # length test
328
+ #
329
+
330
+ def test_length_returns_io_length_divided_by_frame_size
331
+ assert_equal 20, tempfile.length
332
+ assert_equal 5, index.length
333
+
334
+ tempfile.length = 4
335
+ assert_equal 1, index.length
336
+
337
+ tempfile.length = 0
338
+ assert_equal 0, index.length
339
+ end
340
+
341
+ #
342
+ # pos test
343
+ #
344
+
345
+ def test_pos_returns_io_pos_divided_by_frame_size
346
+ assert_equal 0, index.io.pos
347
+ assert_equal 0, index.pos
348
+
349
+ index.io.pos = 4
350
+ assert_equal 1, index.pos
351
+
352
+ index.io.pos = 20
353
+ assert_equal 5, index.pos
354
+ end
355
+
356
+ #
357
+ # pos= test
358
+ #
359
+
360
+ def test_pos_set_documentation
361
+ i = @cls[[1],[2],[3]]
362
+ assert_equal 3, i.length
363
+ i.pos = 2
364
+ assert_equal 2, i.pos
365
+ i.pos = -1
366
+ assert_equal 2, i.pos
367
+ end
368
+
369
+ def test_pos_set_sets_io_pos_to_index_value_of_input_times_frame_size
370
+ index.pos = 1
371
+ assert_equal 1, index.pos
372
+ assert_equal 4, index.io.pos
373
+
374
+ index.pos = -1
375
+ assert_equal 4, index.pos
376
+ assert_equal 16, index.io.pos
377
+ end
378
+
379
+ def test_positions_can_be_set_beyond_the_index_length
380
+ index.pos = 10
381
+ assert_equal 10, index.pos
382
+ end
383
+
384
+ def test_pos_set_raises_error_if_out_of_bounds
385
+ assert_raise(ArgumentError) { index.pos = -6 }
386
+ end
387
+
388
+ #
389
+ # readbytes test
390
+ #
391
+
392
+ def test_readbytes_documentation
393
+ i = @cls[[1],[2],[3]]
394
+ assert_equal [1,2,3], i.readbytes.unpack("I*")
395
+ assert_equal [1], i.readbytes(1,0).unpack("I*")
396
+ assert_equal [2,3], i.readbytes(10,1).unpack("I*")
397
+ i.pos = 3
398
+ assert_equal "", i.readbytes
399
+ assert_equal nil, i.readbytes(1)
400
+ end
401
+
402
+ def test_readbytes_returns_bytestring_for_n_and_pos
403
+ assert_equal array.pack(format), index.readbytes(5,0)
404
+ assert_equal array.pack(format), index.readbytes(5,-5)
405
+ assert_equal [2,3].pack(format), index.readbytes(2,1)
406
+
407
+ assert_equal array.pack(format), index.readbytes(10,0)
408
+ assert_equal array.pack(format), index.readbytes(10,-5)
409
+
410
+ index.pos = 0
411
+ assert_equal array.pack(format), index.readbytes
412
+
413
+ index.pos = 3
414
+ assert_equal [4,5].pack(format), index.readbytes
415
+
416
+ index.pos = 3
417
+ assert_equal [4].pack(format), index.readbytes(1)
418
+ end
419
+
420
+ def test_readbytes_returns_nil_if_n_is_specified_and_no_entries_can_be_read
421
+ assert_nil index.readbytes(1,5)
422
+ end
423
+
424
+ def test_readbytes_returns_empty_string_if_n_is_nil_and_no_entries_can_be_read
425
+ assert_equal "", index.readbytes(nil, 5)
426
+ end
427
+
428
+ def test_readbytes_raises_error_if_position_is_out_of_bounds
429
+ assert_raise(ArgumentError) { index.readbytes(1,-6) }
430
+ end
431
+
432
+ def test_readbytes_behavior_is_like_io_behavior
433
+ tempfile.pos = 20
434
+ assert_equal "", tempfile.read(nil)
435
+ assert_nil tempfile.read(1)
436
+ end
437
+
438
+ #
439
+ # unpack tests
440
+ #
441
+
442
+ def test_unpack_documentation
443
+ assert_equal "I*", index.format
444
+ assert_equal [1], index.unpack( [1].pack('I*') )
445
+ assert_equal [[1], [2], [3]], index.unpack( [1,2,3].pack('I*') )
446
+ assert_equal [], index.unpack("")
447
+ end
448
+
449
+ def test_unpack_unpacks_string_into_frames_using_format
450
+ assert_equal [[1],[2],[3],[4],[5]], index.unpack(array.pack(format))
451
+ assert_equal [1], index.unpack([1].pack(format))
452
+ assert_equal [], index.unpack("")
453
+ end
454
+
455
+ #
456
+ # read tests
457
+ #
458
+
459
+ def test_read_documentation
460
+ i = @cls[[1],[2],[3]]
461
+ assert_equal 0, i.pos
462
+ assert_equal [[1],[2],[3]], i.read
463
+ assert_equal [1], i.read(1,0)
464
+ assert_equal [[2],[3]], i.read(10,1)
465
+
466
+ i.pos = 3
467
+ assert_equal [], i.read
468
+ assert_equal nil, i.read(1)
469
+ end
470
+
471
+ def test_read_returns_unpacked_array_for_n_and_pos
472
+ assert_equal framed_array, index.read(5,0)
473
+ assert_equal framed_array, index.read(5,-5)
474
+ assert_equal [[2],[3]], index.read(2,1)
475
+
476
+ assert_equal framed_array, index.read(10,0)
477
+ assert_equal framed_array, index.read(10,-5)
478
+
479
+ index.pos = 0
480
+ assert_equal framed_array, index.read
481
+
482
+ index.pos = 3
483
+ assert_equal [[4],[5]], index.read
484
+
485
+ index.pos = 3
486
+ assert_equal [4], index.read(1)
487
+ end
488
+
489
+ def test_read_returns_nil_if_n_is_specified_and_no_entries_can_be_read
490
+ assert_nil index.read(1,5)
491
+ end
492
+
493
+ def test_read_returns_empty_array_if_n_is_nil_and_no_entries_can_be_read
494
+ assert_equal [], index.read(nil, 5)
495
+ end
496
+
497
+ def test_read_raises_error_if_position_is_out_of_bounds
498
+ assert_raise(ArgumentError) { index.read(1,-6) }
499
+ end
500
+
501
+ #
502
+ # test unframed_write
503
+ #
504
+
505
+ def test_documentation
506
+ i = @cls[]
507
+ i.unframed_write([2,3], 1)
508
+ i.pos = 0
509
+ i.unframed_write([1])
510
+ assert_equal [[1],[2],[3]], i.read(3, 0)
511
+ end
512
+
513
+ def test_unframed_write_unframed_writes_packed_array_to_io_at_pos_and_adjusts_io_length
514
+ index = @cls.new
515
+ index.unframed_write([1,2,3])
516
+ assert_equal 12, index.io.length
517
+ assert_equal 12, index.io.pos
518
+
519
+ index.io.pos = 0
520
+ assert_equal [1,2,3].pack("I*"), index.io.read
521
+
522
+ index.unframed_write([-2], 1)
523
+ assert_equal 12, index.io.length
524
+ assert_equal 8, index.io.pos
525
+
526
+ index.io.pos = 0
527
+ assert_equal [1,-2,3].pack("I*"), index.io.read
528
+ end
529
+
530
+ def test_unframed_write_pads_with_nil_value_if_position_is_past_length
531
+ index = @cls.new nil, :nil_value => [8]
532
+ assert_equal 0, index.length
533
+
534
+ index.unframed_write([1,2,3], 2)
535
+
536
+ index.io.pos = 0
537
+ assert_equal [8,8,1,2,3], index.io.read.unpack("I*")
538
+ end
539
+
540
+ def test_unframed_write_unframed_writes_nothing_with_empty_array
541
+ assert_equal 20, index.io.length
542
+
543
+ index.unframed_write([])
544
+ index.pos = 0
545
+ assert_equal 20, index.io.length
546
+
547
+ index.unframed_write([], 0)
548
+ index.pos = 0
549
+ assert_equal 20, index.io.length
550
+ end
551
+
552
+ def test_unframed_write_raises_error_if_array_is_not_in_frame
553
+ index = @cls.new(nil, :format => "II")
554
+ assert_raise(ArgumentError) { index.unframed_write([1]) }
555
+ assert_raise(ArgumentError) { index.unframed_write([1,2,3]) }
556
+ end
557
+
558
+ #
559
+ # mixed formats test
560
+ #
561
+
562
+ def test_read_handles_mixed_formats
563
+ index = @cls.new tempfile, :format => "IQS"
564
+ tempfile.pos = 0
565
+ a = [1,2,3].pack("IQS")
566
+ b = [4,5,6].pack("IQS")
567
+ c = [7,8,9].pack("IQS")
568
+ tempfile << a + b + c
569
+
570
+ index.pos=0
571
+ assert_equal [[1,2,3],[4,5,6],[7,8,9]], index.read
572
+
573
+ index.pos=1
574
+ assert_equal [4,5,6], index.read(1)
575
+ end
576
+
577
+ def test_unframed_write_handles_mixed_formats
578
+ index = @cls.new tempfile, :format => "IQS"
579
+ a = [1,2,3].pack("IQS")
580
+ b = [4,5,6].pack("IQS")
581
+ c = [7,8,9].pack("IQS")
582
+ d = [-4,-5,-6].pack("IQS")
583
+
584
+ index.unframed_write([1,2,3,4,5,6,7,8,9])
585
+ tempfile.pos=0
586
+ assert_equal a+b+c, tempfile.read
587
+
588
+ index.pos=1
589
+ index.unframed_write([-4,-5,-6])
590
+ tempfile.pos=0
591
+ assert_equal a+d+c, tempfile.read
592
+ end
593
+
594
+ #
595
+ # numeric format range tests
596
+ #
597
+
598
+ unless defined?(SHRT_MIN)
599
+ SHRT_MIN = -32768
600
+ SHRT_MAX = 32767
601
+
602
+ USHRT_MIN = 0
603
+ USHRT_MAX = 65535
604
+
605
+ LONG_MIN = -2147483648
606
+ LONG_MAX = 2147483647
607
+
608
+ ULONG_MIN = 0
609
+ ULONG_MAX = 4294967295
610
+
611
+ LLONG_MIN = -9223372036854775808
612
+ LLONG_MAX = 9223372036854775807
613
+
614
+ ULLONG_MIN = 0
615
+ ULLONG_MAX = 18446744073709551615
616
+ end
617
+
618
+ def test_read_and_unframed_write_handles_full_numeric_range_for_numeric_formats
619
+ # S handles an unsigned short
620
+ i = @cls.new tempfile, :format => 'S'
621
+
622
+ i.unframed_write([USHRT_MIN], 0)
623
+ assert_equal [USHRT_MIN], i.read(1,0)
624
+ i.unframed_write([USHRT_MAX], 0)
625
+ assert_equal [USHRT_MAX], i.read(1,0)
626
+
627
+ i.unframed_write([USHRT_MIN-1], 0)
628
+ assert_equal [USHRT_MAX], i.read(1,0)
629
+
630
+ # s handles an signed short
631
+ i = @cls.new tempfile, :format => 's'
632
+
633
+ i.unframed_write([SHRT_MIN], 0)
634
+ assert_equal [SHRT_MIN], i.read(1,0)
635
+ i.unframed_write([SHRT_MAX], 0)
636
+ assert_equal [SHRT_MAX], i.read(1,0)
637
+
638
+ i.unframed_write([SHRT_MIN], 0)
639
+ assert_equal [SHRT_MIN], i.read(1,0)
640
+ i.unframed_write([SHRT_MIN-1], 0)
641
+ assert_equal [SHRT_MAX], i.read(1,0)
642
+
643
+ # I,L handle an unsigned long
644
+ ['I', 'L'].each do |format|
645
+ i = @cls.new tempfile, :format => format
646
+
647
+ i.unframed_write([ULONG_MIN], 0)
648
+ assert_equal [ULONG_MIN], i.read(1,0)
649
+ i.unframed_write([ULONG_MAX], 0)
650
+ assert_equal [ULONG_MAX], i.read(1,0)
651
+
652
+ i.unframed_write([ULONG_MIN-1], 0)
653
+ assert_equal [ULONG_MAX], i.read(1,0)
654
+ end
655
+
656
+ # i,l handle an signed long
657
+ ['i', 'l'].each do |format|
658
+ i = @cls.new tempfile, :format => format
659
+
660
+ i.unframed_write([LONG_MIN], 0)
661
+ assert_equal [LONG_MIN], i.read(1,0)
662
+ i.unframed_write([LONG_MAX], 0)
663
+ assert_equal [LONG_MAX], i.read(1,0)
664
+
665
+ i.unframed_write([LONG_MIN], 0)
666
+ assert_equal [LONG_MIN], i.read(1,0)
667
+ i.unframed_write([LONG_MIN-1], 0)
668
+ assert_equal [LONG_MAX], i.read(1,0)
669
+ end
670
+
671
+ # Q handles an unsigned long long
672
+ i = @cls.new tempfile, :format => 'Q'
673
+
674
+ i.unframed_write([ULLONG_MIN], 0)
675
+ assert_equal [ULLONG_MIN], i.read(1,0)
676
+ i.unframed_write([ULLONG_MAX], 0)
677
+ assert_equal [ULLONG_MAX], i.read(1,0)
678
+
679
+ i.unframed_write([ULLONG_MIN-1], 0)
680
+ assert_equal [ULLONG_MAX], i.read(1,0)
681
+
682
+ # q handles an signed long long
683
+ i = @cls.new tempfile, :format => 'q'
684
+
685
+ i.unframed_write([LLONG_MIN], 0)
686
+ assert_equal [LLONG_MIN], i.read(1,0)
687
+ i.unframed_write([LLONG_MAX], 0)
688
+ assert_equal [LLONG_MAX], i.read(1,0)
689
+
690
+ i.unframed_write([LLONG_MIN], 0)
691
+ assert_equal [LLONG_MIN], i.read(1,0)
692
+ i.unframed_write([LLONG_MIN-1], 0)
693
+ assert_equal [LLONG_MAX], i.read(1,0)
694
+ end
695
+
696
+ def test_read_and_unframed_write_cycle_numerics_beyond_natural_range
697
+ # S handles an unsigned short
698
+ i = @cls.new tempfile, :format => 'S'
699
+
700
+ i.unframed_write([-USHRT_MAX], 0)
701
+ assert_equal [1], i.read(1,0)
702
+ i.unframed_write([USHRT_MIN-1], 0)
703
+ assert_equal [USHRT_MAX], i.read(1,0)
704
+
705
+ # s handles an signed short
706
+ i = @cls.new tempfile, :format => 's'
707
+
708
+ i.unframed_write([SHRT_MIN], 0)
709
+ assert_equal [SHRT_MIN], i.read(1,0)
710
+ i.unframed_write([SHRT_MIN-1], 0)
711
+ assert_equal [SHRT_MAX], i.read(1,0)
712
+
713
+ # I,L handle an unsigned long
714
+ ['I', 'L'].each do |format|
715
+ i = @cls.new tempfile, :format => format
716
+
717
+ i.unframed_write([-ULONG_MAX], 0)
718
+ assert_equal [1], i.read(1,0)
719
+ i.unframed_write([ULONG_MIN-1], 0)
720
+ assert_equal [ULONG_MAX], i.read(1,0)
721
+ end
722
+
723
+ # i,l handle an signed long
724
+ ['i', 'l'].each do |format|
725
+ i = @cls.new tempfile, :format => format
726
+
727
+ i.unframed_write([LONG_MIN], 0)
728
+ assert_equal [LONG_MIN], i.read(1,0)
729
+ i.unframed_write([LONG_MIN-1], 0)
730
+ assert_equal [LONG_MAX], i.read(1,0)
731
+ end
732
+
733
+ # Q handles an unsigned long long
734
+ i = @cls.new tempfile, :format => 'Q'
735
+
736
+ i.unframed_write([-ULLONG_MAX], 0)
737
+ assert_equal [1], i.read(1,0)
738
+ i.unframed_write([ULLONG_MIN-1], 0)
739
+ assert_equal [ULLONG_MAX], i.read(1,0)
740
+
741
+ # q handles an signed long long
742
+ i = @cls.new tempfile, :format => 'q'
743
+
744
+ i.unframed_write([LLONG_MIN], 0)
745
+ assert_equal [LLONG_MIN], i.read(1,0)
746
+ i.unframed_write([LLONG_MIN-1], 0)
747
+ assert_equal [LLONG_MAX], i.read(1,0)
748
+ end
749
+
750
+ def test_numerics_cycle_up_to_the_unsigned_max_in_either_sign
751
+ # S,s,I,i,L,l all can cycle up to the size of an ULONG
752
+ ['S','s','I','i','L','l'].each do |format|
753
+ i = @cls.new tempfile, :format => format
754
+
755
+ assert_raise(RangeError) { i.unframed_write([-(ULONG_MAX+1)]) }
756
+ assert_raise(RangeError) { i.unframed_write([(ULONG_MAX+1)]) }
757
+ end
758
+
759
+ # Q,q can cycle up to the size of an ULLONG
760
+ ['Q', 'q'].each do |format|
761
+ i = @cls.new tempfile, :format => format
762
+
763
+ assert_raise(RangeError) { i.unframed_write([-(ULLONG_MAX+1)]) }
764
+ assert_raise(RangeError) { i.unframed_write([(ULLONG_MAX+1)]) }
765
+ end
766
+ end
767
+
768
+ #############################
769
+ # Array method documentation
770
+ #############################
771
+
772
+ def test_AREF_doc
773
+ io = StringIO.new [1,2,3,4,5].pack("I*")
774
+ i = ExtInd.new(io, :format => 'I')
775
+ assert_equal [3], i[2]
776
+ assert_equal nil, i[6]
777
+ assert_equal [[2],[3]], i[1,2]
778
+ assert_equal [[2],[3],[4]], i[1..3]
779
+ assert_equal [[5]], i[4..7]
780
+ assert_equal nil, i[6..10]
781
+ assert_equal [[3],[4],[5]], i[-3,3]
782
+ assert_equal nil, i[5]
783
+ assert_equal [], i[5,1]
784
+ assert_equal [], i[5..10]
785
+ end
786
+
787
+ def test_ASET_doc
788
+ io = StringIO.new ""
789
+ i = ExtInd.new(io, :format => 'I')
790
+ assert_equal [0], i.nil_value
791
+
792
+ i[4] = [4]
793
+ assert_equal [[0], [0], [0], [0], [4]], i.to_a
794
+
795
+ i[0, 3] = [ [1], [2], [3] ]
796
+ assert_equal [[1], [2], [3], [0], [4]], i.to_a
797
+
798
+ i[1..2] = [ [5], [6] ]
799
+ assert_equal [[1], [5], [6], [0], [4]], i.to_a
800
+
801
+ i[0, 2] = [[7]]
802
+ assert_equal [[7], [6], [0], [4]], i.to_a
803
+
804
+ i[0..2] = [[8]]
805
+ assert_equal [[8], [4]], i.to_a
806
+
807
+ i[-1] = [9]
808
+ assert_equal [[8], [9]], i.to_a
809
+
810
+ i[1..-1] = nil
811
+ assert_equal [[8]], i.to_a
812
+ end
813
+
814
+ #############################
815
+ # Modified Array methods tests
816
+ #############################
817
+
818
+ def test_01_square_brackets
819
+ # Changes: results returned in frame
820
+
821
+ a = @cls[ 5, 4, 3, 2, 1 ]
822
+ assert_instance_of(@cls, a)
823
+ assert_equal(5, a.length)
824
+ #5.times { |i| assert_equal(5-i, a[i]) }
825
+ 5.times { |i| assert_equal([5-i], a[i]) }
826
+ assert_nil(a[6])
827
+ end
828
+
829
+ def test_PLUS # '+'
830
+ # Changes: strings not allowed in ExtInd
831
+ # replace 'cat' with 4, 'dog' with 5
832
+
833
+ assert_equal(@cls[], @cls[] + @cls[])
834
+ assert_equal(@cls[1], @cls[1] + @cls[])
835
+ assert_equal(@cls[1], @cls[] + @cls[1])
836
+ assert_equal(@cls[1, 1], @cls[1] + @cls[1])
837
+ #assert_equal(@cls['cat', 'dog', 1, 2, 3], %w(cat dog) + (1..3).to_a)
838
+ assert_equal(@cls[4, 5, 1, 2, 3], @cls[4,5] + @cls[*(1..3).to_a])
839
+
840
+ # Additional:
841
+ # check addition of Array to ExtInd (can't add ExtInd to Array)
842
+ assert_equal(@cls[4, 5, 1, 2, 3], @cls[4,5] + [[1],[2],[3]])
843
+ assert_raise(TypeError) { [4,5] + @cls[*(1..3).to_a] }
844
+
845
+ # check result is distinct from factors
846
+ a = @cls[1]
847
+ b = @cls[2]
848
+ c = a + b
849
+ assert_equal [[1],[2]], c.to_a
850
+
851
+ a.concat [[1]]
852
+ b.concat [[2]]
853
+ c.concat [[3]]
854
+ assert_equal [[1],[1]], a.to_a
855
+ assert_equal [[2],[2]], b.to_a
856
+ assert_equal [[1],[2],[3]], c.to_a
857
+ end
858
+
859
+ def test_LSHIFT # '<<'
860
+ # Changes: inputs must be in frame and can't take
861
+ # strings. And ExtInd can't accept itself as an entry
862
+
863
+ a = @cls[]
864
+ #a << 1
865
+ a << [1]
866
+ assert_equal(@cls[1], a)
867
+ #a << 2 << 3
868
+ a << [2] << [3]
869
+ assert_equal(@cls[1, 2, 3], a)
870
+ #a << nil << 'cat'
871
+ #assert_equal(@cls[1, 2, 3, nil, 'cat'], a)
872
+ #a << a
873
+ #assert_equal(@cls[1, 2, 3, nil, 'cat', a], a)
874
+ end
875
+
876
+ def test_CMP # '<=>'
877
+ # Changes: strings not allowed in ExtInd
878
+ # replace 'cat' with 4, 'dog' with 5
879
+ assert_equal(-1, 4 <=> 5)
880
+
881
+ assert_equal(0, @cls[] <=> @cls[])
882
+ assert_equal(0, @cls[1] <=> @cls[1])
883
+ #assert_equal(0, @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3, 'cat'])
884
+ assert_equal(0, @cls[1, 2, 3] <=> @cls[1, 2, 3])
885
+ assert_equal(-1, @cls[] <=> @cls[1])
886
+ assert_equal(1, @cls[1] <=> @cls[])
887
+ #assert_equal(-1, @cls[1, 2, 3] <=> @cls[1, 2, 3, 'cat'])
888
+ assert_equal(-1, @cls[1, 2, 3] <=> @cls[1, 2, 3, 4])
889
+ #assert_equal(1, @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3])
890
+ assert_equal(1, @cls[1, 2, 3, 4] <=> @cls[1, 2, 3])
891
+ #assert_equal(-1, @cls[1, 2, 3, 'cat'] <=> @cls[1, 2, 3, 'dog'])
892
+ assert_equal(-1, @cls[1, 2, 3, 4] <=> @cls[1, 2, 3, 5])
893
+ #assert_equal(1, @cls[1, 2, 3, 'dog'] <=> @cls[1, 2, 3, 'cat'])
894
+ assert_equal(1, @cls[1, 2, 3, 5] <=> @cls[1, 2, 3, 4])
895
+ end
896
+
897
+ def test_AREF # '[]'
898
+ # Changes: results returned in frame
899
+
900
+ a = @cls[*(1..100).to_a]
901
+
902
+ #assert_equal(1, a[0])
903
+ assert_equal([1], a[0])
904
+ #assert_equal(100, a[99])
905
+ assert_equal([100], a[99])
906
+ assert_nil(a[100])
907
+ #assert_equal(100, a[-1])
908
+ assert_equal([100], a[-1])
909
+ #assert_equal(99, a[-2])
910
+ assert_equal([99], a[-2])
911
+ #assert_equal(1, a[-100])
912
+ assert_equal([1], a[-100])
913
+ assert_nil(a[-101])
914
+ assert_nil(a[-101,0])
915
+ assert_nil(a[-101,1])
916
+ assert_nil(a[-101,-1])
917
+ assert_nil(a[10,-1])
918
+
919
+ # assert_equal(@cls[1], a[0,1])
920
+ assert_equal([[1]], a[0,1])
921
+ #assert_equal(@cls[100], a[99,1])
922
+ assert_equal([[100]], a[99,1])
923
+ #assert_equal(@cls[], a[100,1])
924
+ assert_equal([], a[100,1])
925
+ #assert_equal(@cls[100], a[99,100])
926
+ assert_equal([[100]], a[99,100])
927
+ #assert_equal(@cls[100], a[-1,1])
928
+ assert_equal([[100]], a[-1,1])
929
+ #assert_equal(@cls[99], a[-2,1])
930
+ assert_equal([[99]], a[-2,1])
931
+ #assert_equal(@cls[], a[-100,0])
932
+ assert_equal([], a[-100,0])
933
+ #assert_equal(@cls[1], a[-100,1])
934
+ assert_equal([[1]], a[-100,1])
935
+
936
+ assert_equal(@cls[10, 11, 12], a[9, 3])
937
+ assert_equal(@cls[10, 11, 12], a[-91, 3])
938
+
939
+ # assert_equal(@cls[1], a[0..0])
940
+ assert_equal([[1]], a[0..0])
941
+ # assert_equal(@cls[100], a[99..99])
942
+ assert_equal([[100]], a[99..99])
943
+ # assert_equal(@cls[], a[100..100])
944
+ assert_equal([], a[100..100])
945
+ # assert_equal(@cls[100], a[99..200])
946
+ assert_equal([[100]], a[99..200])
947
+ # assert_equal(@cls[100], a[-1..-1])
948
+ assert_equal([[100]], a[-1..-1])
949
+ # assert_equal(@cls[99], a[-2..-2])
950
+ assert_equal([[99]], a[-2..-2])
951
+
952
+ assert_equal(@cls[10, 11, 12], a[9..11])
953
+ assert_equal(@cls[10, 11, 12], a[-91..-89])
954
+
955
+ assert_nil(a[10, -3])
956
+ # Ruby 1.8 feature change:
957
+ # Array#[size..x] returns [] instead of nil.
958
+ #assert_nil(a[10..7])
959
+ assert_equal [], a[10..7]
960
+
961
+ assert_raise(TypeError) {a['cat']}
962
+ end
963
+
964
+ def test_ASET # '[]='
965
+ # Changes: values and results specified in frame
966
+ # added mirror tests to ensure testing using array
967
+ # and index inputs
968
+
969
+ # -- pair --
970
+ a = @cls[*(0..99).to_a]
971
+ #assert_equal(0, a[0] = 0)
972
+ assert_equal([0], a[0] = [0])
973
+ assert_equal(@cls[0] + @cls[*(1..99).to_a], a)
974
+
975
+ a = @cls[*(0..99).to_a]
976
+ b = @cls[0]
977
+ assert_equal(b, a[0] = b)
978
+ assert_equal(@cls[0] + @cls[*(1..99).to_a], a)
979
+
980
+ # -- pair --
981
+ a = @cls[*(0..99).to_a]
982
+ #assert_equal(0, a[10,10] = 0)
983
+ assert_equal([[0]], a[10,10] = [[0]])
984
+ assert_equal(@cls[*(0..9).to_a] + @cls[0] + @cls[*(20..99).to_a], a)
985
+
986
+ a = @cls[*(0..99).to_a]
987
+ b = @cls[0]
988
+ assert_equal(b, a[10,10] = b)
989
+ assert_equal(@cls[*(0..9).to_a] + @cls[0] + @cls[*(20..99).to_a], a)
990
+
991
+ # -- pair --
992
+ a = @cls[*(0..99).to_a]
993
+ #assert_equal(0, a[-1] = 0)
994
+ assert_equal([0], a[-1] = [0])
995
+ assert_equal(@cls[*(0..98).to_a] + @cls[0], a)
996
+
997
+ a = @cls[*(0..99).to_a]
998
+ b = @cls[0]
999
+ assert_equal(b, a[-1] = b)
1000
+ assert_equal(@cls[*(0..98).to_a] + @cls[0], a)
1001
+
1002
+ # -- pair --
1003
+ a = @cls[*(0..99).to_a]
1004
+ #assert_equal(0, a[-10, 10] = 0)
1005
+ assert_equal([[0]], a[-10, 10] = [[0]])
1006
+ assert_equal(@cls[*(0..89).to_a] + @cls[0], a)
1007
+
1008
+ a = @cls[*(0..99).to_a]
1009
+ b = @cls[0]
1010
+ assert_equal(b, a[-10, 10] = b)
1011
+ assert_equal(@cls[*(0..89).to_a] + @cls[0], a)
1012
+
1013
+ # -- pair --
1014
+ a = @cls[*(0..99).to_a]
1015
+ #assert_equal(0, a[0,1000] = 0)
1016
+ assert_equal([[0]], a[0,1000] = [[0]])
1017
+ assert_equal(@cls[0] , a)
1018
+
1019
+ a = @cls[*(0..99).to_a]
1020
+ b = @cls[0]
1021
+ assert_equal(b, a[0,1000] = b)
1022
+ assert_equal(@cls[0] , a)
1023
+
1024
+ # -- pair --
1025
+ a = @cls[*(0..99).to_a]
1026
+ #assert_equal(0, a[10..19] = 0)
1027
+ assert_equal([[0]], a[10..19] = [[0]])
1028
+ assert_equal(@cls[*(0..9).to_a] + @cls[0] + @cls[*(20..99).to_a], a)
1029
+
1030
+ a = @cls[*(0..99).to_a]
1031
+ b = @cls[0]
1032
+ assert_equal(b, a[10..19] = b)
1033
+ assert_equal(@cls[*(0..9).to_a] + @cls[0] + @cls[*(20..99).to_a], a)
1034
+
1035
+ # -- pair --
1036
+ # Changes: cannot take strings,
1037
+ # replace a,b,c with 1001, 1002, 10003
1038
+ #b = @cls[*%w( a b c )]
1039
+ b = @cls[1001, 1002, 10003]
1040
+ c = [[1001],[1002],[10003]]
1041
+ a = @cls[*(0..99).to_a]
1042
+ assert_equal(b, a[0,1] = b)
1043
+ assert_equal(b + @cls[*(1..99).to_a], a)
1044
+
1045
+ a = @cls[*(0..99).to_a]
1046
+ assert_equal(c, a[0,1] = c)
1047
+ assert_equal(b + @cls[*(1..99).to_a], a)
1048
+
1049
+ # -- pair --
1050
+ a = @cls[*(0..99).to_a]
1051
+ assert_equal(b, a[10,10] = b)
1052
+ assert_equal(@cls[*(0..9).to_a] + b + @cls[*(20..99).to_a], a)
1053
+
1054
+ a = @cls[*(0..99).to_a]
1055
+ assert_equal(c, a[10,10] = c)
1056
+ assert_equal(@cls[*(0..9).to_a] + c + @cls[*(20..99).to_a], a)
1057
+
1058
+ # -- pair --
1059
+ a = @cls[*(0..99).to_a]
1060
+ assert_equal(b, a[-1, 1] = b)
1061
+ assert_equal(@cls[*(0..98).to_a] + b, a)
1062
+
1063
+ a = @cls[*(0..99).to_a]
1064
+ assert_equal(c, a[-1, 1] = c)
1065
+ assert_equal(@cls[*(0..98).to_a] + c, a)
1066
+
1067
+ # -- pair --
1068
+ a = @cls[*(0..99).to_a]
1069
+ assert_equal(b, a[-10, 10] = b)
1070
+ assert_equal(@cls[*(0..89).to_a] + b, a)
1071
+
1072
+ a = @cls[*(0..99).to_a]
1073
+ assert_equal(c, a[-10, 10] = c)
1074
+ assert_equal(@cls[*(0..89).to_a] + c, a)
1075
+
1076
+ # -- pair --
1077
+ a = @cls[*(0..99).to_a]
1078
+ assert_equal(b, a[0,1000] = b)
1079
+ assert_equal(b , a)
1080
+
1081
+ a = @cls[*(0..99).to_a]
1082
+ assert_equal(c, a[0,1000] = c)
1083
+ assert_equal(c , a.to_a)
1084
+
1085
+ # -- pair --
1086
+ a = @cls[*(0..99).to_a]
1087
+ assert_equal(b, a[10..19] = b)
1088
+ assert_equal(@cls[*(0..9).to_a] + b + @cls[*(20..99).to_a], a)
1089
+
1090
+ a = @cls[*(0..99).to_a]
1091
+ assert_equal(c, a[10..19] = c)
1092
+ assert_equal(@cls[*(0..9).to_a] + c + @cls[*(20..99).to_a], a)
1093
+
1094
+ # Ruby 1.8 feature change:
1095
+ # assigning nil does not remove elements.
1096
+ =begin
1097
+ a = @cls[*(0..99).to_a]
1098
+ assert_equal(nil, a[0,1] = nil)
1099
+ assert_equal(@cls[*(1..99).to_a], a)
1100
+
1101
+ a = @cls[*(0..99).to_a]
1102
+ assert_equal(nil, a[10,10] = nil)
1103
+ assert_equal(@cls[*(0..9).to_a] + @cls[*(20..99).to_a], a)
1104
+
1105
+ a = @cls[*(0..99).to_a]
1106
+ assert_equal(nil, a[-1, 1] = nil)
1107
+ assert_equal(@cls[*(0..98).to_a], a)
1108
+
1109
+ a = @cls[*(0..99).to_a]
1110
+ assert_equal(nil, a[-10, 10] = nil)
1111
+ assert_equal(@cls[*(0..89).to_a], a)
1112
+
1113
+ a = @cls[*(0..99).to_a]
1114
+ assert_equal(nil, a[0,1000] = nil)
1115
+ assert_equal(@cls[] , a)
1116
+
1117
+ a = @cls[*(0..99).to_a]
1118
+ assert_equal(nil, a[10..19] = nil)
1119
+ assert_equal(@cls[*(0..9).to_a] + @cls[*(20..99).to_a], a)
1120
+ =end
1121
+
1122
+ # Changes: should have @cls in definition
1123
+
1124
+ a = @cls[1, 2, 3]
1125
+ a[1, 0] = a
1126
+ #assert_equal([1, 1, 2, 3, 2, 3], a)
1127
+ assert_equal(@cls[1, 1, 2, 3, 2, 3], a)
1128
+
1129
+ a = @cls[1, 2, 3]
1130
+ a[-1, 0] = a
1131
+ #assert_equal([1, 2, 1, 2, 3, 3], a)
1132
+ assert_equal(@cls[1, 2, 1, 2, 3, 3], a)
1133
+
1134
+ # Additional:
1135
+
1136
+ # -- test self insertions --
1137
+ a = @cls[1, 2, 3]
1138
+ a[1, 3] = a
1139
+ assert_equal(@cls[1,1,2,3], a)
1140
+
1141
+ a = @cls[1, 2, 3]
1142
+ a[3, 3] = a
1143
+ assert_equal(@cls[1,2,3,1,2,3], a)
1144
+
1145
+ a = @cls[1, 2, 3]
1146
+ a[4, 3] = a
1147
+ assert_equal(@cls[1,2,3,0,1,2,3], a)
1148
+
1149
+ # -- test insertions where padding is necessary --
1150
+ # -- pair --
1151
+ a = @cls[1,2,3, {:nil_value => [8]}]
1152
+ b = @cls[1001, 1002, 10003, {:nil_value => [8]}]
1153
+ assert_equal(b, a[4, 3] = b)
1154
+ assert_equal(@cls[1,2,3,8,1001,1002,10003, {:nil_value => [8]}], a.to_a)
1155
+
1156
+ a = @cls[1,2,3, {:nil_value => [8]}]
1157
+ c = [[1001], [1002], [10003]]
1158
+ assert_equal(c, a[4, 3] = c)
1159
+ assert_equal(@cls[1,2,3,8,1001,1002,10003, {:nil_value => [8]}], a)
1160
+
1161
+ # -- pair --
1162
+ a = @cls[1,2,3, {:nil_value => [8]}]
1163
+ b = @cls[1001, 1002, 10003, {:nil_value => [8]}]
1164
+ assert_equal(b, a[4, 1] = b)
1165
+ assert_equal(@cls[1,2,3,8,1001,1002,10003, {:nil_value => [8]}], a)
1166
+
1167
+ a = @cls[1,2,3, {:nil_value => [8]}]
1168
+ c = [[1001], [1002], [10003]]
1169
+ assert_equal(c, a[4, 1] = c)
1170
+ assert_equal(@cls[1,2,3,8,1001,1002,10003, {:nil_value => [8]}], a)
1171
+
1172
+ # -- test insertions with nils --
1173
+ # -- pair --
1174
+ a = @cls[1,2,3]
1175
+ b = @cls[1001, nil, nil, 10003]
1176
+ assert_equal(b, a[1, 3] = b)
1177
+ assert_equal(@cls[1,1001,0,0,10003], a)
1178
+
1179
+ a = @cls[1,2,3]
1180
+ c = [[1001], nil, nil, [10003]]
1181
+ assert_equal(c, a[1, 3] = c)
1182
+ assert_equal(@cls[1,1001,0,0,10003], a)
1183
+
1184
+ # -- insert beyond end of index, with inconsistent range --
1185
+ # first check the array behavior, then assert the same with ExtInd
1186
+ a = (0..5).to_a
1187
+ assert_equal([11,12,13], a[11..12] = [11,12,13])
1188
+ assert_equal((0..5).to_a + [nil,nil,nil,nil,nil] + (11..13).to_a, a)
1189
+
1190
+ # -- pair --
1191
+ a = @cls[*(0..5).to_a]
1192
+ b = @cls[11,12,13]
1193
+ assert_equal(b, a[11..12] = b)
1194
+ assert_equal(@cls[*(0..5).to_a] + @cls[0,0,0,0,0] + b, a)
1195
+
1196
+ a = @cls[*(0..5).to_a]
1197
+ c = [[11],[12],[13]]
1198
+ assert_equal(c, a[11..12] = c)
1199
+ assert_equal(@cls[*(0..5).to_a] + @cls[0,0,0,0,0] + c, a)
1200
+ end
1201
+
1202
+ def test_at
1203
+ # Chagnes: values must be in frame
1204
+
1205
+ a = @cls[*(0..99).to_a]
1206
+ # assert_equal(0, a.at(0))
1207
+ assert_equal([0], a.at(0))
1208
+ # assert_equal(10, a.at(10))
1209
+ assert_equal([10], a.at(10))
1210
+ # assert_equal(99, a.at(99))
1211
+ assert_equal([99], a.at(99))
1212
+ assert_equal(nil, a.at(100))
1213
+ # assert_equal(99, a.at(-1))
1214
+ assert_equal([99], a.at(-1))
1215
+ # assert_equal(0, a.at(-100))
1216
+ assert_equal([0], a.at(-100))
1217
+ assert_equal(nil, a.at(-101))
1218
+
1219
+ assert_raise(TypeError) { a.at('cat') }
1220
+ end
1221
+
1222
+ def test_concat
1223
+ # Changes: ExtInd does not support Array/ExtInd nesting
1224
+
1225
+ assert_equal(@cls[1, 2, 3, 4], @cls[1, 2].concat(@cls[3, 4]))
1226
+ assert_equal(@cls[1, 2, 3, 4], @cls[].concat(@cls[1, 2, 3, 4]))
1227
+ assert_equal(@cls[1, 2, 3, 4], @cls[1, 2, 3, 4].concat(@cls[]))
1228
+ assert_equal(@cls[], @cls[].concat(@cls[]))
1229
+ #assert_equal(@cls[@cls[1, 2], @cls[3, 4]], @cls[@cls[1, 2]].concat(@cls[@cls[3, 4]]))
1230
+
1231
+ # Changes: should have @cls in definition
1232
+
1233
+ a = @cls[1, 2, 3]
1234
+ a.concat(a)
1235
+ #assert_equal([1, 2, 3, 1, 2, 3], a)
1236
+ assert_equal(@cls[1, 2, 3, 1, 2, 3], a)
1237
+ end
1238
+
1239
+ def test_each
1240
+ # Changes: cannot take strings,
1241
+ # replace (ant bat cat dog) with [1,2,3,4]
1242
+
1243
+ #a = @cls[*%w( ant bat cat dog )]
1244
+ a = @cls[*(1..4).to_a]
1245
+ i = 0
1246
+ a.each { |e|
1247
+ assert_equal(a[i], e)
1248
+ i += 1
1249
+ }
1250
+ assert_equal(4, i)
1251
+
1252
+ a = @cls[]
1253
+ i = 0
1254
+ a.each { |e|
1255
+ assert_equal(a[i], e)
1256
+ i += 1
1257
+ }
1258
+ assert_equal(0, i)
1259
+
1260
+ assert_equal(a, a.each {})
1261
+ end
1262
+
1263
+ def test_each_index
1264
+ # Changes: cannot take strings,
1265
+ # replace (ant bat cat dog) with [1,2,3,4]
1266
+
1267
+ #a = @cls[*%w( ant bat cat dog )]
1268
+ a = @cls[*(1..4).to_a]
1269
+ i = 0
1270
+ a.each_index { |ind|
1271
+ assert_equal(i, ind)
1272
+ i += 1
1273
+ }
1274
+ assert_equal(4, i)
1275
+
1276
+ a = @cls[]
1277
+ i = 0
1278
+ a.each_index { |ind|
1279
+ assert_equal(i, ind)
1280
+ i += 1
1281
+ }
1282
+ assert_equal(0, i)
1283
+
1284
+ assert_equal(a, a.each_index {})
1285
+ end
1286
+
1287
+ def test_eql?
1288
+ assert(@cls[].eql?(@cls[]))
1289
+ assert(@cls[1].eql?(@cls[1]))
1290
+ assert(@cls[1, 1, 2, 2].eql?(@cls[1, 1, 2, 2]))
1291
+
1292
+ # Changes: all values are treated according to the format
1293
+ # so these floats are converted to ints and the ExtInds
1294
+ # are equal
1295
+ #assert(!@cls[1.0, 1.0, 2.0, 2.0].eql?(@cls[1, 1, 2, 2]))
1296
+ assert(@cls[1.0, 1.0, 2.0, 2.0].eql?(@cls[1, 1, 2, 2]))
1297
+ end
1298
+
1299
+ def test_first
1300
+ # Changes: must be in frame
1301
+ #assert_equal(3, @cls[3, 4, 5].first)
1302
+ assert_equal([3], @cls[3, 4, 5].first)
1303
+ assert_equal(nil, @cls[].first)
1304
+ end
1305
+
1306
+ def test_last
1307
+ # Changes: must be in frame
1308
+
1309
+ assert_equal(nil, @cls[].last)
1310
+ # assert_equal(1, @cls[1].last)
1311
+ assert_equal([1], @cls[1].last)
1312
+ # assert_equal(99, @cls[*(3..99).to_a].last)
1313
+ assert_equal([99], @cls[*(3..99).to_a].last)
1314
+ end
1315
+
1316
+ def test_to_a
1317
+ a = @cls[ 1, 2, 3 ]
1318
+ # Changes: can't do this comparison by object id
1319
+ #a_id = a.__id__
1320
+ #assert_equal(a, a.to_a)
1321
+ assert_equal [[1],[2],[3]], a.to_a
1322
+ #assert_equal(a_id, a.to_a.__id__)
1323
+ end
1324
+
1325
+ #############################
1326
+ # Additional Array methods tests
1327
+ #############################
1328
+
1329
+ #
1330
+ # ASET tests
1331
+ #
1332
+
1333
+ def test_ASET_raises_error_if_input_is_not_in_frame
1334
+ a = @cls.new
1335
+ assert_raise(ArgumentError) { a[0] = 1 }
1336
+ assert_raise(ArgumentError) { a[0,1] = [1] }
1337
+ assert_raise(ArgumentError) { a[0,1] = [[1,2]] }
1338
+ end
1339
+
1340
+ def test_ASET_raises_error_if_input_is_index_with_different_attributes
1341
+ a = @cls.new(nil, :format => "I")
1342
+ b = @cls.new(nil, :format => "II")
1343
+ assert_raise(ArgumentError) { a[0,1] = b }
1344
+ end
1345
+
1346
+ #######################
1347
+ # Benchmark tests
1348
+ #######################
1349
+
1350
+ #
1351
+ # benchmarks
1352
+ #
1353
+
1354
+ def index_bm_test(mode, length=20, array=nil, &block)
1355
+ benchmark_test(length) do |x|
1356
+ ['I', 'IIIIIIIIII'].each do |format|
1357
+ unless array
1358
+ array = []
1359
+ 1.upto(10000) {|i| array << i }
1360
+ end
1361
+
1362
+ Tempfile.open('benchmark') do |file|
1363
+ file << array.pack('I*')
1364
+
1365
+ begin
1366
+ index = @cls.new(file, :format => format)
1367
+ yield(x, format, index)
1368
+ ensure
1369
+ index.close if index
1370
+ end
1371
+ end
1372
+ end
1373
+
1374
+ yield(x, "array reference", array)
1375
+ end
1376
+
1377
+ array
1378
+ end
1379
+
1380
+ def test_element_reference_speed_for_index
1381
+ n = 100
1382
+ index_bm_test('r') do |x, type, index|
1383
+ puts type
1384
+
1385
+ x.report("#{n}kx [index]") { (n*1000).times { index[1000] } }
1386
+ x.report("#{n}kx [range]") { (n*1000).times { index[1000..1000] } }
1387
+ x.report("#{n}kx [s,1]") { (n*1000).times { index[1000, 1] } }
1388
+ x.report("#{n}kx [s,10]") { (n*1000).times { index[1000, 10] } }
1389
+ x.report("#{n}kx [s,100]") { (n*1000).times { index[1000, 100] } }
1390
+
1391
+ puts
1392
+ end
1393
+ end
1394
+
1395
+ def test_element_assignment_speed_for_index
1396
+ index_bm_test('r+') do |x, type, index|
1397
+ puts type
1398
+
1399
+ n = 10
1400
+ obj = Array.new(index.respond_to?(:frame) ? index.frame : 1, 0)
1401
+
1402
+ x.report("#{n}kx [index]=") do
1403
+ (n*1000).times { index[1000] = obj}
1404
+ end
1405
+ x.report("#{n}kx [range]=") do
1406
+ (n*1000).times { index[1000..1000] = [obj] }
1407
+ end
1408
+ x.report("#{n}kx [s,1]=") do
1409
+ (n*1000).times { index[1000,1] = [obj] }
1410
+ end
1411
+
1412
+ puts
1413
+ end
1414
+ end
1415
+ end
1416
+
1417
+
1418
+
1419
+ class Hold
1420
+ def btest_each_speed
1421
+ indexbm_test('r') do |x, type, index|
1422
+ x.report("10x #{type} - cs100k") { 10.times { index.each {|e|} } }
1423
+ #x.report("10x #{type} - cs100") { 10.times { index.each(0,index.length, 100) {|e|} } }
1424
+ end
1425
+ end
1426
+
1427
+
1428
+ def btest_push_speed
1429
+ indexbm_test('r+') do |x, type, index|
1430
+ obj = Array.new(index.frame, 0)
1431
+ x.report("10kx #{type} index=") do
1432
+ 10000.times { index << obj}
1433
+ end
1434
+ end
1435
+ end
1436
+
1437
+ def btest_sort_speed
1438
+ benchmark_test(20) do |x|
1439
+ n = 1
1440
+ unsorted, sorted = sort_arrays(n*10**6)
1441
+
1442
+ x.report("#{n}M array sort") do
1443
+ unsorted.sort
1444
+ end
1445
+
1446
+ index = setup_index({:default_blksize => 100000}, unsorted)
1447
+ x.report("#{n}M index sort") do
1448
+ index.sort.close
1449
+ end
1450
+ index.close
1451
+
1452
+ index = setup_index({:frame => 2, :default_blksize => 100000}, unsorted)
1453
+ x.report("#{n}M frame with block") do
1454
+ index.sort {|a, b| b <=> a }.close
1455
+ end
1456
+ index.close
1457
+
1458
+ sindex = setup_sindex({:default_blksize => 100000}, unsorted)
1459
+ x.report("#{n}M sindex sort") do
1460
+ sindex.sort.close
1461
+ end
1462
+ sindex.close
1463
+
1464
+ # cannot finish... not sure why
1465
+ # sindex = setup_sindex({:frame => 2, :default_blksize => 100000}, unsorted)
1466
+ # x.report("#{n}M frame with block") do
1467
+ # sindex.sort {|a, b| b <=> a }.close
1468
+ # end
1469
+ # sindex.close
1470
+ end
1471
+ end
1472
+ end