1 # frozen_string_literal: false
5 class TC_Set < Test::Unit::TestCase
10 assert_nothing_raised {
16 assert_equal(0, Set[].size)
17 assert_equal(1, Set[nil].size)
18 assert_equal(1, Set[[]].size)
19 assert_equal(1, Set[[nil]].size)
22 assert_equal(Set.new([2,4,6]), set)
26 assert_nothing_raised {
33 assert_raise(ArgumentError) {
36 assert_raise(ArgumentError) {
39 assert_raise(ArgumentError) {
43 assert_equal(0, Set.new().size)
44 assert_equal(0, Set.new(nil).size)
45 assert_equal(0, Set.new([]).size)
46 assert_equal(1, Set.new([nil]).size)
51 assert_equal(false, set.empty?)
52 assert_equal(3, set.size)
56 s = Set.new(ary) { |o| o * 2 }
57 assert_equal([2,4,6], s.sort)
64 assert_equal(Set.new, set2)
71 assert_not_same(set1, set2)
73 assert_equal(set1, set2)
77 assert_not_equal(set1, set2)
81 assert_equal(0, Set[].size)
82 assert_equal(2, Set[1,2].size)
83 assert_equal(2, Set[1,2,1].size)
87 assert_equal(true, Set[].empty?)
88 assert_equal(false, Set[1, 2].empty?)
96 assert_equal(true, set.empty?)
101 ret = set.replace('a'..'c')
103 assert_same(set, ret)
104 assert_equal(Set['a','b','c'], set)
107 assert_raise(ArgumentError) {
110 assert_equal(Set[1,2], set)
117 assert_equal([1,2,3], ary.sort)
139 assert_not_same(set2, set1)
140 assert_equal(set3, set2)
146 assert_same(orig_set1, set1)
147 assert_equal(set3, set1)
149 # test3; multiple occurrences of a set in an set
151 set2 = Set[set1, Set[set1, 4], 3]
153 assert_nothing_raised {
157 assert_equal(Set.new(1..4), set2)
164 assert_raise(ArgumentError) {
168 # test5; miscellaneous
170 set = Set[Set[empty, "a"],Set[empty, "b"]]
172 assert_nothing_raised {
176 set1 = empty.merge(Set["no_more", set])
178 assert_nil(Set.new(0..31).flatten!)
180 x = Set[Set[],Set[1,2]].flatten!
189 assert_equal(true, set.include?(1))
190 assert_equal(true, set.include?(2))
191 assert_equal(true, set.include?(3))
192 assert_equal(false, set.include?(0))
193 assert_equal(false, set.include?(nil))
195 set = Set["1",nil,"2",nil,"0","1",false]
196 assert_equal(true, set.include?(nil))
197 assert_equal(true, set.include?(false))
198 assert_equal(true, set.include?("1"))
199 assert_equal(false, set.include?(0))
200 assert_equal(false, set.include?(true))
206 assert_equal(true, set === 1)
207 assert_equal(true, set === 2)
208 assert_equal(true, set === 3)
209 assert_equal(false, set === 0)
210 assert_equal(false, set === nil)
212 set = Set["1",nil,"2",nil,"0","1",false]
213 assert_equal(true, set === nil)
214 assert_equal(true, set === false)
215 assert_equal(true, set === "1")
216 assert_equal(false, set === 0)
217 assert_equal(false, set === true)
223 assert_raise(ArgumentError) {
227 assert_raise(ArgumentError) {
231 assert_raise(ArgumentError) {
235 [Set, Set2].each { |klass|
236 assert_equal(true, set.superset?(klass[]), klass.name)
237 assert_equal(true, set.superset?(klass[1,2]), klass.name)
238 assert_equal(true, set.superset?(klass[1,2,3]), klass.name)
239 assert_equal(false, set.superset?(klass[1,2,3,4]), klass.name)
240 assert_equal(false, set.superset?(klass[1,4]), klass.name)
242 assert_equal(true, set >= klass[1,2,3], klass.name)
243 assert_equal(true, set >= klass[1,2], klass.name)
245 assert_equal(true, Set[].superset?(klass[]), klass.name)
249 def test_proper_superset?
252 assert_raise(ArgumentError) {
253 set.proper_superset?()
256 assert_raise(ArgumentError) {
257 set.proper_superset?(2)
260 assert_raise(ArgumentError) {
261 set.proper_superset?([2])
264 [Set, Set2].each { |klass|
265 assert_equal(true, set.proper_superset?(klass[]), klass.name)
266 assert_equal(true, set.proper_superset?(klass[1,2]), klass.name)
267 assert_equal(false, set.proper_superset?(klass[1,2,3]), klass.name)
268 assert_equal(false, set.proper_superset?(klass[1,2,3,4]), klass.name)
269 assert_equal(false, set.proper_superset?(klass[1,4]), klass.name)
271 assert_equal(false, set > klass[1,2,3], klass.name)
272 assert_equal(true, set > klass[1,2], klass.name)
274 assert_equal(false, Set[].proper_superset?(klass[]), klass.name)
281 assert_raise(ArgumentError) {
285 assert_raise(ArgumentError) {
289 assert_raise(ArgumentError) {
293 [Set, Set2].each { |klass|
294 assert_equal(true, set.subset?(klass[1,2,3,4]), klass.name)
295 assert_equal(true, set.subset?(klass[1,2,3]), klass.name)
296 assert_equal(false, set.subset?(klass[1,2]), klass.name)
297 assert_equal(false, set.subset?(klass[]), klass.name)
299 assert_equal(true, set <= klass[1,2,3], klass.name)
300 assert_equal(true, set <= klass[1,2,3,4], klass.name)
302 assert_equal(true, Set[].subset?(klass[1]), klass.name)
303 assert_equal(true, Set[].subset?(klass[]), klass.name)
307 def test_proper_subset?
310 assert_raise(ArgumentError) {
314 assert_raise(ArgumentError) {
315 set.proper_subset?(2)
318 assert_raise(ArgumentError) {
319 set.proper_subset?([2])
322 [Set, Set2].each { |klass|
323 assert_equal(true, set.proper_subset?(klass[1,2,3,4]), klass.name)
324 assert_equal(false, set.proper_subset?(klass[1,2,3]), klass.name)
325 assert_equal(false, set.proper_subset?(klass[1,2]), klass.name)
326 assert_equal(false, set.proper_subset?(klass[]), klass.name)
328 assert_equal(false, set < klass[1,2,3], klass.name)
329 assert_equal(true, set < klass[1,2,3,4], klass.name)
331 assert_equal(false, Set[].proper_subset?(klass[]), klass.name)
335 def test_spacecraft_operator
338 assert_nil(set <=> 2)
340 assert_nil(set <=> set.to_a)
342 [Set, Set2].each { |klass|
343 assert_equal(-1, set <=> klass[1,2,3,4], klass.name)
344 assert_equal( 0, set <=> klass[3,2,1] , klass.name)
345 assert_equal(nil, set <=> klass[1,2,4] , klass.name)
346 assert_equal(+1, set <=> klass[2,3] , klass.name)
347 assert_equal(+1, set <=> klass[] , klass.name)
349 assert_equal(0, Set[] <=> klass[], klass.name)
353 def assert_intersect(expected, set, other)
356 assert_send([set, :intersect?, other])
357 assert_send([set, :intersect?, other.to_a])
358 assert_send([other, :intersect?, set])
359 assert_not_send([set, :disjoint?, other])
360 assert_not_send([set, :disjoint?, other.to_a])
361 assert_not_send([other, :disjoint?, set])
363 assert_not_send([set, :intersect?, other])
364 assert_not_send([set, :intersect?, other.to_a])
365 assert_not_send([other, :intersect?, set])
366 assert_send([set, :disjoint?, other])
367 assert_send([set, :disjoint?, other.to_a])
368 assert_send([other, :disjoint?, set])
370 assert_raise(expected) {
371 set.intersect?(other)
373 assert_raise(expected) {
377 raise ArgumentError, "%s: unsupported expected value: %s" % [__method__, expected.inspect]
384 assert_intersect(ArgumentError, set, 3)
385 assert_intersect(true, set, Set[2,4,6])
387 assert_intersect(true, set, set)
388 assert_intersect(true, set, Set[2,4])
389 assert_intersect(true, set, Set[5,6,7])
390 assert_intersect(true, set, Set[1,2,6,8,4])
392 assert_intersect(false, set, Set[])
393 assert_intersect(false, set, Set[0,2])
394 assert_intersect(false, set, Set[0,2,6])
395 assert_intersect(false, set, Set[0,2,6,8,10])
397 # Make sure set hasn't changed
398 assert_equal(Set[3,4,5], set)
402 ary = [1,3,5,7,10,20]
405 ret = set.each { |o| }
406 assert_same(set, ret)
409 assert_instance_of(Enumerator, e)
411 assert_nothing_raised {
413 ary.delete(o) or raise "unexpected element: #{o}"
416 ary.empty? or raise "forgotten elements: #{ary.join(', ')}"
419 assert_equal(6, e.size)
421 assert_equal(7, e.size)
428 assert_same(set, ret)
429 assert_equal(Set[1,2,3], set)
433 assert_equal(Set[1,2,3], set)
436 assert_same(set, ret)
437 assert_equal(Set[1,2,3,4], set)
440 assert_same(set, ret)
441 assert_equal(Set[1,2,3,4,5], set)
448 assert_same(set, ret)
449 assert_equal(Set[1,2,3], set)
453 assert_equal(Set[1,2,3], set)
456 assert_equal(set, ret)
457 assert_equal(Set[1,3], set)
460 assert_equal(set, ret)
461 assert_equal(Set[3], set)
466 ret = set.delete_if { |i| i > 10 }
467 assert_same(set, ret)
468 assert_equal(Set.new(1..10), set)
471 ret = set.delete_if { |i| i % 3 == 0 }
472 assert_same(set, ret)
473 assert_equal(Set[1,2,4,5,7,8,10], set)
477 assert_equal(set.size, enum.size)
478 assert_same(set, enum.each { |i| i % 3 == 0 })
479 assert_equal(Set[1,2,4,5,7,8,10], set)
484 ret = set.keep_if { |i| i <= 10 }
485 assert_same(set, ret)
486 assert_equal(Set.new(1..10), set)
489 ret = set.keep_if { |i| i % 3 != 0 }
490 assert_same(set, ret)
491 assert_equal(Set[1,2,4,5,7,8,10], set)
495 assert_equal(set.size, enum.size)
496 assert_same(set, enum.each { |i| i % 3 != 0 })
497 assert_equal(Set[1,2,4,5,7,8,10], set)
501 set = Set[1,2,3,'a','b','c',-1..1,2..4]
503 ret = set.collect! { |i|
514 assert_same(set, ret)
515 assert_equal(Set[2,4,6,'A','B','C',nil], set)
517 set = Set[1,2,3,'a','b','c',-1..1,2..4]
520 assert_equal(set.size, enum.size)
521 assert_same(set, enum.each { |i|
531 assert_equal(Set[2,4,6,'A','B','C',nil], set)
537 ret = set.reject! { |i| i > 10 }
539 assert_equal(Set.new(1..10), set)
541 ret = set.reject! { |i| i % 3 == 0 }
542 assert_same(set, ret)
543 assert_equal(Set[1,2,4,5,7,8,10], set)
547 assert_equal(set.size, enum.size)
548 assert_same(set, enum.each { |i| i % 3 == 0 })
549 assert_equal(Set[1,2,4,5,7,8,10], set)
554 ret = set.select! { |i| i <= 10 }
555 assert_equal(nil, ret)
556 assert_equal(Set.new(1..10), set)
559 ret = set.select! { |i| i % 3 != 0 }
560 assert_same(set, ret)
561 assert_equal(Set[1,2,4,5,7,8,10], set)
565 assert_equal(set.size, enum.size)
566 assert_equal(nil, enum.each { |i| i <= 10 })
567 assert_equal(Set.new(1..10), set)
572 ret = set.filter! { |i| i <= 10 }
573 assert_equal(nil, ret)
574 assert_equal(Set.new(1..10), set)
577 ret = set.filter! { |i| i % 3 != 0 }
578 assert_same(set, ret)
579 assert_equal(Set[1,2,4,5,7,8,10], set)
583 assert_equal(set.size, enum.size)
584 assert_equal(nil, enum.each { |i| i <= 10 })
585 assert_equal(Set.new(1..10), set)
591 ret = set.merge([2,4,6])
592 assert_same(set, ret)
593 assert_equal(Set[1,2,3,4,6], set)
599 ret = set.subtract([2,4,6])
600 assert_same(set, ret)
601 assert_equal(Set[1,3], set)
608 assert_not_same(set, ret)
609 assert_equal(Set[1,2,3,4,6], ret)
616 assert_not_same(set, ret)
617 assert_equal(Set[1,3], ret)
624 assert_not_same(set, ret)
625 assert_equal(Set[2,4], ret)
630 ret = set ^ [2,4,5,5]
631 assert_not_same(set, ret)
632 assert_equal(Set[1,3,5], ret)
639 assert_equal(set1, set1)
640 assert_equal(set1, set2)
641 assert_not_equal(Set[1], [1])
643 set1 = Class.new(Set)["a", "b"]
644 set1.add(set1).reset # Make recursive
645 set2 = Set["a", "b", Set["a", "b", set1]]
647 assert_equal(set1, set2)
649 assert_not_equal(Set[Exception.new,nil], Set[Exception.new,Exception.new], "[ruby-dev:26127]")
654 ret = set.classify { |i| i % 3 }
656 assert_equal(3, ret.size)
657 assert_instance_of(Hash, ret)
658 ret.each_value { |value| assert_instance_of(Set, value) }
659 assert_equal(Set[3,6,9], ret[0])
660 assert_equal(Set[1,4,7,10], ret[1])
661 assert_equal(Set[2,5,8], ret[2])
666 assert_equal(set.size, enum.size)
667 ret = enum.each { |i| i % 3 }
668 assert_equal(3, ret.size)
669 assert_instance_of(Hash, ret)
670 ret.each_value { |value| assert_instance_of(Set, value) }
671 assert_equal(Set[3,6,9], ret[0])
672 assert_equal(Set[1,4,7,10], ret[1])
673 assert_equal(Set[2,5,8], ret[2])
678 ret = set.divide { |i| i % 3 }
680 assert_equal(3, ret.size)
682 ret.each { |s| n += s.size }
683 assert_equal(set.size, n)
684 assert_equal(set, ret.flatten)
686 set = Set[7,10,5,11,1,3,4,9,0]
687 ret = set.divide { |a,b| (a - b).abs == 1 }
689 assert_equal(4, ret.size)
691 ret.each { |s| n += s.size }
692 assert_equal(set.size, n)
693 assert_equal(set, ret.flatten)
696 assert_equal(Set[0,1], s)
698 assert_equal(Set[3,4,5], s)
700 assert_equal(Set[7], s)
702 assert_equal(Set[9,10,11], s)
704 raise "unexpected group: #{s.inspect}"
710 ret = enum.each { |i| i % 3 }
712 assert_equal(set.size, enum.size)
713 assert_equal(3, ret.size)
715 ret.each { |s| n += s.size }
716 assert_equal(set.size, n)
717 assert_equal(set, ret.flatten)
721 orig = set = Set[1,2,3]
722 assert_equal false, set.frozen?
724 assert_same orig, set.freeze
725 assert_equal true, set.frozen?
726 assert_raise(FrozenError) {
729 assert_equal 4, set.size
737 assert_not_predicate set2, :frozen?
738 assert_nothing_raised {
743 def test_freeze_clone
748 assert_predicate set2, :frozen?
749 assert_raise(FrozenError) {
754 def test_freeze_clone_false
757 set2 = set1.clone(freeze: false)
759 assert_not_predicate set2, :frozen?
761 assert_equal Set[1,2,3,5], set2
762 assert_equal Set[1,2,3], set1
763 end if Kernel.instance_method(:initialize_clone).arity != 1
766 assert_equal('123', Set[1, 2, 3].join)
767 assert_equal('1 & 2 & 3', Set[1, 2, 3].join(' & '))
772 assert_equal('#<Set: {1, 2}>', set1.inspect)
774 set2 = Set[Set[0], 1, 2, set1]
775 assert_equal('#<Set: {#<Set: {0}>, 1, 2, #<Set: {1, 2}>}>', set2.inspect)
778 assert_equal('#<Set: {#<Set: {0}>, 1, 2, #<Set: {1, 2, #<Set: {...}>}>}>', set2.inspect)
783 assert_equal('#<Set: {1, 2}>', set1.to_s)
785 set2 = Set[Set[0], 1, 2, set1]
786 assert_equal('#<Set: {#<Set: {0}>, 1, 2, #<Set: {1, 2}>}>', set2.to_s)
789 assert_equal('#<Set: {#<Set: {0}>, 1, 2, #<Set: {1, 2, #<Set: {...}>}>}>', set2.to_s)
792 def test_compare_by_identity
796 array = [a1, b1, c, a2, b2]
798 iset = Set.new.compare_by_identity
799 assert_send([iset, :compare_by_identity?])
801 assert_equal(5, iset.size)
802 assert_equal(array.map(&:object_id).sort, iset.map(&:object_id).sort)
805 assert_not_send([set, :compare_by_identity?])
807 assert_equal(3, set.size)
808 assert_equal(array.uniq.sort, set.sort)
812 [Set, Class.new(Set)].each { |klass|
815 set = klass.new([a, b])
820 assert_equal(klass.new([a]), set, klass.name)
825 class TC_Enumerable < Test::Unit::TestCase
827 ary = [2,5,4,3,2,1,3]
830 assert_instance_of(Set, set)
831 assert_equal([1,2,3,4,5], set.sort)
833 set = ary.to_set { |o| o * -2 }
834 assert_instance_of(Set, set)
835 assert_equal([-10,-8,-6,-4,-2], set.sort)
837 assert_same set, set.to_set
838 assert_not_same set, set.to_set { |o| o }