xqsr3 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +27 -0
  3. data/README.md +26 -0
  4. data/lib/xqsr3/command_line_utilities/map_option_string.rb +137 -0
  5. data/lib/xqsr3/containers/frequency_map.rb +473 -0
  6. data/lib/xqsr3/containers/multi_map.rb +383 -0
  7. data/lib/xqsr3/diagnostics/exception_utilities.rb +216 -0
  8. data/lib/xqsr3/doc_.rb +116 -0
  9. data/lib/xqsr3/extensions/enumerable.rb +4 -0
  10. data/lib/xqsr3/extensions/enumerable/collect_with_index.rb +71 -0
  11. data/lib/xqsr3/extensions/enumerable/unique.rb +102 -0
  12. data/lib/xqsr3/extensions/io.rb +3 -0
  13. data/lib/xqsr3/extensions/io/writelines.rb +66 -0
  14. data/lib/xqsr3/extensions/kernel.rb +3 -0
  15. data/lib/xqsr3/extensions/kernel/raise_with_options.rb +68 -0
  16. data/lib/xqsr3/extensions/string.rb +5 -0
  17. data/lib/xqsr3/extensions/string/ends_with.rb +8 -0
  18. data/lib/xqsr3/extensions/string/map_option_string.rb +8 -0
  19. data/lib/xqsr3/extensions/string/starts_with.rb +8 -0
  20. data/lib/xqsr3/extensions/string/to_symbol.rb +8 -0
  21. data/lib/xqsr3/extensions/test/unit.rb +5 -0
  22. data/lib/xqsr3/extensions/test/unit/assert_eql.rb +18 -0
  23. data/lib/xqsr3/extensions/test/unit/assert_not.rb +18 -0
  24. data/lib/xqsr3/extensions/test/unit/assert_not_eql.rb +18 -0
  25. data/lib/xqsr3/io/writelines.rb +192 -0
  26. data/lib/xqsr3/quality/parameter_checking.rb +343 -0
  27. data/lib/xqsr3/string_utilities/ends_with.rb +134 -0
  28. data/lib/xqsr3/string_utilities/starts_with.rb +127 -0
  29. data/lib/xqsr3/string_utilities/to_symbol.rb +145 -0
  30. data/lib/xqsr3/version.rb +68 -0
  31. data/test/unit/command_line_utilities/tc_map_option_string.rb +39 -0
  32. data/test/unit/command_line_utilities/ts_all.rb +13 -0
  33. data/test/unit/containers/tc_frequency_map.rb +738 -0
  34. data/test/unit/containers/tc_multi_map.rb +640 -0
  35. data/test/unit/containers/ts_all.rb +13 -0
  36. data/test/unit/diagnostics/tc_exception_utilities.rb +221 -0
  37. data/test/unit/diagnostics/ts_all.rb +13 -0
  38. data/test/unit/extensions/enumerable/tc_collect_with_index.rb +36 -0
  39. data/test/unit/extensions/enumerable/tc_unique.rb +47 -0
  40. data/test/unit/extensions/enumerable/ts_all.rb +13 -0
  41. data/test/unit/extensions/io/tc_writelines.rb +110 -0
  42. data/test/unit/extensions/io/ts_all.rb +13 -0
  43. data/test/unit/extensions/kernel/tc_raise_with_options.rb +239 -0
  44. data/test/unit/extensions/kernel/ts_all.rb +13 -0
  45. data/test/unit/extensions/string/tc_ends_with.rb +70 -0
  46. data/test/unit/extensions/string/tc_map_option_string.rb +37 -0
  47. data/test/unit/extensions/string/tc_starts_with.rb +70 -0
  48. data/test/unit/extensions/string/tc_to_symbol.rb +51 -0
  49. data/test/unit/extensions/string/ts_all.rb +13 -0
  50. data/test/unit/extensions/ts_all.rb +13 -0
  51. data/test/unit/io/tc_writelines.rb +200 -0
  52. data/test/unit/io/ts_all.rb +13 -0
  53. data/test/unit/quality/tc_parameter_checking.rb +181 -0
  54. data/test/unit/quality/ts_all.rb +13 -0
  55. data/test/unit/tc_version.rb +37 -0
  56. data/test/unit/ts_all.rb +13 -0
  57. metadata +101 -0
@@ -0,0 +1,640 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), '../../../lib')
4
+
5
+ require 'xqsr3/containers/multi_map'
6
+
7
+ require 'xqsr3/extensions/test/unit'
8
+ require 'test/unit'
9
+
10
+ include ::Xqsr3::Containers
11
+
12
+ class Test_Xqsr3_Containers_MultiMap < Test::Unit::TestCase
13
+
14
+ def test_class_operator_subscript_1
15
+
16
+ mm = MultiMap[]
17
+
18
+ assert mm.empty?
19
+ assert_equal 0, mm.size
20
+ end
21
+
22
+ def test_class_operator_subscript_2
23
+
24
+ # as hash
25
+
26
+ mm = MultiMap[ abc: [ 1 ], def: [ 'd' ] ]
27
+
28
+ assert_not mm.empty?
29
+ assert_equal [ :abc, 1, :def, 'd' ], mm.flatten
30
+ assert_equal 2, mm.size
31
+ end
32
+
33
+ def test_class_operator_subscript_3
34
+
35
+ mm = MultiMap[ [ :abc, 1 ], [ :def, 'd' ] ]
36
+
37
+ assert_not mm.empty?
38
+ assert_equal [ :abc, 1, :def, 'd' ], mm.flatten
39
+ assert_equal 2, mm.size
40
+ end
41
+
42
+ def test_class_operator_subscript_4
43
+
44
+ mm = MultiMap[ [ :abc, 1, 2, 3, 4 ], [ :def, 'd' ] ]
45
+
46
+ assert_not mm.empty?
47
+ assert_equal [ :abc, 1, :abc, 2, :abc, 3, :abc, 4, :def, 'd' ], mm.flatten
48
+ assert_equal 2, mm.size
49
+ end
50
+
51
+ def test_class_operator_subscript_5
52
+
53
+ assert_raise(::TypeError) { MultiMap[:not_a_hash] }
54
+
55
+ assert_raise(::ArgumentError) { MultiMap[ abc: 1 ] }
56
+
57
+ assert_raise(::ArgumentError) { MultiMap[ [ :abc ], 1 ] }
58
+
59
+ assert_raise(::ArgumentError) { MultiMap[ [ :abc ], [] ] }
60
+ end
61
+
62
+ def test_instance_operator_equals
63
+
64
+ mm1 = MultiMap.new
65
+ mm2 = MultiMap.new
66
+
67
+ assert_equal mm1, mm2
68
+
69
+ mm1.push :abc
70
+
71
+ assert_not_equal mm1, mm2
72
+
73
+ mm2.push :abc
74
+
75
+ assert_equal mm1, mm2
76
+
77
+ mm1.push :abc, 1, 2, 3, 4, 5
78
+ mm2.push :abc, 1, 2, 3, 4, 5
79
+
80
+ assert_equal mm1, mm2
81
+ end
82
+
83
+ def test_instance_operator_subscript
84
+
85
+ mm = MultiMap.new
86
+
87
+ assert_nil mm[:abc]
88
+
89
+ mm.push :abc
90
+
91
+ assert_equal [], mm[:abc]
92
+
93
+ mm.push :abc, 1, 2, 3
94
+
95
+ assert_equal [ 1, 2, 3 ], mm[:abc]
96
+ end
97
+
98
+ def test_instance_operator_subscript_assign
99
+
100
+ mm = MultiMap.new
101
+
102
+ assert_nil mm[:abc]
103
+ assert_equal 0, mm.count
104
+ assert mm.empty?
105
+ assert_equal 0, mm.size
106
+
107
+ mm[:abc] = nil
108
+
109
+ assert_equal [], mm[:abc]
110
+ assert_equal 0, mm.count
111
+ assert_not mm.empty?
112
+ assert_equal 1, mm.size
113
+
114
+ mm.store :abc, 1, 2, '3', nil, false
115
+
116
+ assert_equal [ 1, 2, '3', nil, false ], mm[:abc]
117
+ assert_equal 5, mm.count
118
+ assert_not mm.empty?
119
+ assert_equal 1, mm.size
120
+
121
+ mm.store :abc
122
+
123
+ assert_equal [], mm[:abc]
124
+ assert_equal 0, mm.count
125
+ assert_not mm.empty?
126
+ assert_equal 1, mm.size
127
+ end
128
+
129
+ def test_assoc
130
+
131
+ mm = MultiMap.new
132
+
133
+ assert_nil mm.assoc :abc
134
+
135
+ mm.push :abc
136
+
137
+ assert_equal [ :abc, [] ], mm.assoc(:abc)
138
+
139
+ mm.push :abc, 1, 2
140
+
141
+ assert_equal [ :abc, [ 1, 2 ] ], mm.assoc(:abc)
142
+ end
143
+
144
+ def test_clear
145
+
146
+ mm = MultiMap.new
147
+
148
+ mm.push :abc, 'a'
149
+ mm.push :abc, 'b'
150
+ mm.push :abc, 'c'
151
+
152
+ assert_equal 3, mm.count
153
+ assert_not mm.empty?
154
+ assert_equal 1, mm.size
155
+
156
+ mm.clear
157
+
158
+ assert_equal 0, mm.count
159
+ assert mm.empty?
160
+ assert_equal 0, mm.size
161
+ end
162
+
163
+ def test_count_and_delete
164
+
165
+ mm = MultiMap.new
166
+
167
+ assert_equal 0, mm.count
168
+
169
+ mm.push :abc, 1
170
+
171
+ assert_equal 1, mm.count
172
+
173
+ mm.push :abc, 2
174
+
175
+ assert_equal 2, mm.count
176
+
177
+ mm.push :def, 1
178
+
179
+ assert_equal 3, mm.count
180
+
181
+ mm.delete :ghi
182
+
183
+ assert_equal 3, mm.count
184
+
185
+ mm.delete :abc
186
+
187
+ assert_equal 1, mm.count
188
+
189
+ mm.delete :def
190
+
191
+ assert_equal 0, mm.count
192
+ end
193
+
194
+ def test_count
195
+
196
+ test_count_and_delete
197
+ end
198
+
199
+ def test_default
200
+
201
+ end
202
+
203
+ def test_delete
204
+
205
+ test_count_and_delete
206
+ end
207
+
208
+ def test_dup
209
+
210
+ mm1 = MultiMap.new
211
+
212
+ mm1.push :def
213
+ mm1.push :abc, 'a1', 'a2', 'a3', 'a4'
214
+ mm1.push :ghi, 'g1', 'g2'
215
+
216
+ mm2 = mm1.dup
217
+
218
+ assert_eql mm1, mm2
219
+ end
220
+
221
+ def test_each
222
+
223
+ mm = MultiMap.new
224
+
225
+ mm.push :def
226
+ mm.push :abc, 'a1', 'a2', 'a3', 'a4'
227
+ mm.push :ghi, 'g1', 'g2'
228
+
229
+ r = []
230
+
231
+ mm.each do |k, v|
232
+
233
+ r << [k, v]
234
+ end
235
+
236
+ r.sort!
237
+
238
+ assert_equal 6, r.size
239
+ assert_equal [ [ :abc, 'a1' ], [ :abc, 'a2' ], [ :abc, 'a3' ], [ :abc, 'a4' ], [ :ghi, 'g1' ], [ :ghi, 'g2' ] ], r
240
+ end
241
+
242
+ def test_each_with_default
243
+
244
+ mm = MultiMap.new
245
+
246
+ mm.push :def
247
+ mm.push :abc, 'a1', 'a2', 'a3', 'a4'
248
+ mm.push :ghi, 'g1', 'g2'
249
+
250
+ r = []
251
+
252
+ mm.each(:the_default) do |k, v|
253
+
254
+ r << [k, v]
255
+ end
256
+
257
+ r.sort!
258
+
259
+ assert_equal 7, r.size
260
+ assert_equal [ [ :abc, 'a1' ], [ :abc, 'a2' ], [ :abc, 'a3' ], [ :abc, 'a4' ], [ :def, :the_default ], [ :ghi, 'g1' ], [ :ghi, 'g2' ] ], r
261
+ end
262
+
263
+ def test_each_key
264
+
265
+ mm = MultiMap.new
266
+
267
+ mm.push :def
268
+ mm.push :abc, 'a1', 'a2', 'a3', 'a4'
269
+ mm.push :ghi, 'g1', 'g2'
270
+
271
+ r = []
272
+
273
+ mm.each_key do |k|
274
+
275
+ r << k
276
+ end
277
+
278
+ r.sort!
279
+
280
+ assert_equal 3, r.size
281
+ assert_equal [ :abc, :def, :ghi ], r
282
+ end
283
+
284
+ def test_each_value
285
+
286
+ mm = MultiMap.new
287
+
288
+ mm.push :def
289
+ mm.push :abc, 'a1', 'a2', 'a3', 'a4'
290
+ mm.push :ghi, 'g1', 'g2'
291
+
292
+ r = []
293
+
294
+ mm.each_value do |v|
295
+
296
+ r << v
297
+ end
298
+
299
+ r.sort!
300
+
301
+ assert_equal 6, r.size
302
+ assert_equal [ 'a1', 'a2', 'a3', 'a4', 'g1', 'g2' ], r
303
+ end
304
+
305
+ def test_each_with_index
306
+
307
+ mm = MultiMap.new
308
+
309
+ mm.push :def
310
+ mm.push :abc, 'a1', 'a2', 'a3', 'a4'
311
+ mm.push :ghi, 'g1', 'g2'
312
+
313
+ r = []
314
+
315
+ mm.each_with_index do |key, value, index|
316
+
317
+ r << [ key, value, index ]
318
+ end
319
+
320
+ r.sort!
321
+
322
+ assert_equal 6, r.size
323
+ assert_equal [ [ :abc, 'a1', 0 ], [ :abc, 'a2', 1 ], [ :abc, 'a3', 2 ], [ :abc, 'a4', 3 ], [ :ghi, 'g1', 4 ], [ :ghi, 'g2', 5 ] ], r
324
+ end
325
+
326
+ def test_empty
327
+
328
+ mm = MultiMap.new
329
+
330
+ assert mm.empty?
331
+
332
+ mm.push :abc
333
+
334
+ assert_not mm.empty?
335
+ end
336
+
337
+ def test_eql?
338
+
339
+ mm1 = MultiMap.new
340
+ mm2 = MultiMap.new
341
+
342
+ assert mm1.eql? mm1
343
+ assert mm2.eql? mm2
344
+ assert mm1.eql? mm2
345
+
346
+ mm1.push :abc
347
+ mm1.push :def
348
+
349
+ assert mm1.eql? mm1
350
+ assert mm2.eql? mm2
351
+ assert_not mm1.eql? mm2
352
+
353
+ mm2.push :def
354
+ mm2.push :abc
355
+
356
+ assert mm1.eql? mm1
357
+ assert mm2.eql? mm2
358
+ assert mm1.eql? mm2
359
+ end
360
+
361
+ def test_fetch
362
+
363
+ mm = MultiMap.new
364
+
365
+ assert_raise(::KeyError) { mm.fetch(:does_not_exist) }
366
+ assert_equal [2], mm.fetch(:does_not_exist, [2])
367
+ assert_raise(::TypeError) { mm.fetch(:does_not_exist, :wrong_type) }
368
+ assert_raise(::ArgumentError) { mm.fetch(:does_not_exist) { |k| 33 } }
369
+ assert_equal [ 33 ], mm.fetch(:does_not_exist) { |k| [ 33 ] }
370
+ assert_equal [ 34 ], mm.fetch(:does_not_exist) { |k| [ 34 ] }
371
+
372
+ mm.push :abc, 1, 2, 3
373
+
374
+ assert_equal [ 1, 2, 3 ], mm.fetch(:abc)
375
+ assert_equal [ 1, 2, 3 ], mm.fetch(:abc, [ 1 ])
376
+ assert_equal [ 1, 2, 3 ], (mm.fetch(:abc) { |k| [ 33 ] })
377
+ end
378
+
379
+ def test_flatten
380
+
381
+ mm = MultiMap.new
382
+
383
+ assert_equal [], mm.flatten
384
+
385
+ mm.push :abc
386
+
387
+ assert_equal [ :abc, [] ], mm.flatten
388
+
389
+ mm.push :abc, 1, 2, 3
390
+
391
+ assert_equal [ :abc, 1, :abc, 2, :abc, 3 ], mm.flatten
392
+
393
+ mm.push :abc, 4, 5
394
+
395
+ assert_equal [ :abc, 1, :abc, 2, :abc, 3, :abc, 4, :abc, 5 ], mm.flatten
396
+
397
+ mm.push :def
398
+
399
+ assert_equal [ :abc, 1, :abc, 2, :abc, 3, :abc, 4, :abc, 5, :def, [] ], mm.flatten
400
+ end
401
+
402
+ def test_has_key?
403
+
404
+ mm = MultiMap.new
405
+
406
+ assert_not mm.has_key? :abc
407
+
408
+ mm.push :abc
409
+
410
+ assert mm.has_key? :abc
411
+
412
+ mm.delete :abc
413
+
414
+ assert_not mm.has_key? :abc
415
+ end
416
+
417
+ def test_has_value?
418
+
419
+ mm = MultiMap.new
420
+
421
+ assert_not mm.has_value? []
422
+
423
+ mm.push :abc
424
+
425
+ assert mm.has_value? []
426
+
427
+ mm.delete :abc
428
+
429
+ assert_not mm.has_value? []
430
+ end
431
+
432
+ def test_key
433
+
434
+ mm = MultiMap.new
435
+
436
+ assert_nil mm.key []
437
+ assert_nil mm.key :not_defined
438
+
439
+ mm.push :abc
440
+
441
+ assert_equal :abc, mm.key([])
442
+
443
+ mm.push :abc, 1
444
+
445
+ assert_equal :abc, mm.key([ 1 ])
446
+ end
447
+
448
+ def test_length_and_size
449
+
450
+ mm = MultiMap.new
451
+
452
+ assert mm.empty?
453
+ assert_equal 0, mm.size
454
+
455
+ mm.push :abc, 1
456
+
457
+ assert_not mm.empty?
458
+ assert_equal 1, mm.size
459
+
460
+ mm.push :abc, 2
461
+
462
+ assert_not mm.empty?
463
+ assert_equal 1, mm.size
464
+
465
+ mm.push :def, 1
466
+
467
+ assert_not mm.empty?
468
+ assert_equal 2, mm.size
469
+ end
470
+
471
+ def test_length
472
+
473
+ test_length_and_size
474
+ end
475
+
476
+ def test_merge
477
+
478
+ mm1 = MultiMap.new
479
+
480
+ mm1.push :abc, 1, 2, 3
481
+
482
+ assert_equal [ :abc, 1, :abc, 2, :abc, 3 ], mm1.flatten
483
+
484
+ mm2 = MultiMap.new
485
+
486
+ mm2.push :abc, 4, 5
487
+ mm2.push :def, 'a'
488
+
489
+ mm3 = mm1.merge mm2
490
+
491
+ assert_equal [ :abc, 1, :abc, 2, :abc, 3, :abc, 4, :abc, 5, :def, 'a' ], mm3.flatten
492
+ end
493
+
494
+ def test_merge!
495
+
496
+ mm1 = MultiMap.new
497
+
498
+ mm1.push :abc, 1, 2, 3
499
+
500
+ assert_equal [ :abc, 1, :abc, 2, :abc, 3 ], mm1.flatten
501
+
502
+ mm2 = MultiMap.new
503
+
504
+ mm2.push :abc, 4, 5
505
+ mm2.push :def, 'a'
506
+
507
+ mm1.merge! mm2
508
+
509
+ assert_equal [ :abc, 1, :abc, 2, :abc, 3, :abc, 4, :abc, 5, :def, 'a' ], mm1.flatten
510
+ end
511
+
512
+ def test_push
513
+
514
+ mm = MultiMap.new
515
+
516
+ assert_nil mm[:abc]
517
+ assert_equal 0, mm.count
518
+ assert mm.empty?
519
+ assert_equal 0, mm.size
520
+
521
+ mm.push :abc
522
+
523
+ assert_equal [], mm[:abc]
524
+ assert_equal 0, mm.count
525
+ assert_not mm.empty?
526
+ assert_equal 1, mm.size
527
+
528
+ mm.push :abc, 1, 2, '3', nil, false
529
+
530
+ assert_equal [ 1, 2, '3', nil, false ], mm[:abc]
531
+ assert_equal 5, mm.count
532
+ assert_not mm.empty?
533
+ assert_equal 1, mm.size
534
+
535
+ mm.push :def, *(0...10).to_a
536
+
537
+ assert_equal [ 1, 2, '3', nil, false ], mm[:abc]
538
+ assert_equal [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], mm[:def]
539
+ assert_equal 15, mm.count
540
+ assert_not mm.empty?
541
+ assert_equal 2, mm.size
542
+ end
543
+
544
+ def test_shift
545
+
546
+ mm = MultiMap.new
547
+
548
+ assert_nil mm.shift
549
+
550
+ mm.push :abc
551
+
552
+ assert_equal [ :abc, [] ], mm.shift
553
+ end
554
+
555
+ def test_size
556
+
557
+ test_length_and_size
558
+ end
559
+
560
+ def test_store
561
+
562
+ mm = MultiMap.new
563
+
564
+ assert_nil mm[:abc]
565
+ assert_equal 0, mm.count
566
+ assert mm.empty?
567
+ assert_equal 0, mm.size
568
+
569
+ mm.store :abc
570
+
571
+ assert_equal [], mm[:abc]
572
+ assert_equal 0, mm.count
573
+ assert_not mm.empty?
574
+ assert_equal 1, mm.size
575
+
576
+ mm.store :abc, 1, 2, '3', nil, false
577
+
578
+ assert_equal [ 1, 2, '3', nil, false ], mm[:abc]
579
+ assert_equal 5, mm.count
580
+ assert_not mm.empty?
581
+ assert_equal 1, mm.size
582
+
583
+ mm.store :abc
584
+
585
+ assert_equal [], mm[:abc]
586
+ assert_equal 0, mm.count
587
+ assert_not mm.empty?
588
+ assert_equal 1, mm.size
589
+ end
590
+
591
+ def test_to_a
592
+
593
+ mm = MultiMap.new
594
+
595
+ mm.push :abc
596
+
597
+ assert_equal [ :abc, [] ], mm.to_a
598
+
599
+ mm.push :abc, 1, 2, 3, 4, 5
600
+
601
+ assert_equal [ :abc, 1, :abc, 2, :abc, 3, :abc, 4, :abc, 5 ], mm.to_a
602
+
603
+ mm.push :def
604
+
605
+ assert_equal [ :abc, 1, :abc, 2, :abc, 3, :abc, 4, :abc, 5, :def, [] ], mm.to_a
606
+ end
607
+
608
+ def test_to_h
609
+
610
+ mm = MultiMap.new
611
+
612
+ mm.push :abc
613
+
614
+ assert_equal ({ abc: [] }), mm.to_h
615
+
616
+ mm.push :abc, 1, 2, 3, 4, 5
617
+
618
+ assert_equal ({ abc: [ 1, 2, 3, 4, 5 ] }), mm.to_h
619
+
620
+ mm.push :def
621
+
622
+ assert_equal ({ abc: [ 1, 2, 3, 4, 5 ], def: [] }), mm.to_h
623
+ end
624
+
625
+ def test_values
626
+
627
+ mm = MultiMap.new
628
+
629
+ assert_equal [], mm.values
630
+
631
+ mm.store :abc
632
+
633
+ assert_equal [ [] ], mm.values
634
+
635
+ mm.store :abc, 1, 2, '3', nil, false
636
+
637
+ assert_equal [ [ 1, 2, '3', nil, false ] ], mm.values
638
+ end
639
+ end
640
+