# _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ | # |_|\___||___/\__| # # for facets/enumerable/collect.rb require 'facets/enumerable/collect.rb' require 'test/unit' require 'set' class TestEnumerable < Test::Unit::TestCase def test_filter_collect e = [3,4] a = [1,2,3,4].filter_collect { |n| throw(:skip) if n < 3 n } assert_equal( e, a ) end def test_compact_collect a = [1,2,nil,4].compact_collect { |e| e } assert_equal( [1,2,4], a ) end def test_filter_collect e = [3,4] a = [1,2,3,4].filter_collect { |n| throw(:skip) if n < 3 n } assert_equal( e, a ) end def test_compact_collect a = [1,2,nil,4].compact_collect { |e| e } assert_equal( [1,2,4], a ) end def test_injecting r = [1,2,3,4,5].injecting([]){ |a,i| a << i % 2 } e = [1,0,1,0,1] assert_equal(e, r) end def test_injecting_equal r = [].injecting([]){ |a,i| a << i % 2 } e = [] assert_equal(e, r) end # def test_op_mod # a = [:A,:B,:C] # assert_equal( a[1], a/1 ) # assert_equal( :B, a/1 ) # end # # def test_op_div # a = [:A,:B,:C] # assert_equal( a[1], a/1 ) # assert_equal( :B, a/1 ) # end def test_group_by_for_array a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] r = {0=>[0, 2, 4, 6, 8], 1=>[1, 3, 5, 7, 9]} assert_equal(r, a.group_by{|e| e%2}.each{|k, v| v.sort!}) h = {0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5, 6=>6, 7=>7, 8=>8, 9=>9} r = {0=>[[0, 0], [2, 2], [4, 4], [6, 6], [8, 8]], 1=>[[1, 1], [3, 3], [5, 5], [7, 7], [9, 9]]} assert_equal(r, h.group_by{|k, v| v%2}.each{|k, v| v.sort!}) x = (1..5).group_by{ |n| n % 3 } o = { 0 => [3], 1 => [1, 4], 2 => [2,5] } assert_equal( o, x ) x = ["I had", 1, "dollar and", 50, "cents"].group_by{ |e| e.class } o = { String => ["I had","dollar and","cents"], Fixnum => [1,50] } assert_equal( o, x ) end def test_cluster_by a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] r = [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]] assert_equal(r, a.cluster_by{|e| e%2}.each{|a| a.sort!}) h = {0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5, 6=>6, 7=>7, 8=>8, 9=>9} r = [[[0, 0], [2, 2], [4, 4], [6, 6], [8, 8]], [[1, 1], [3, 3], [5, 5], [7, 7], [9, 9]]] assert_equal(r, h.cluster_by{|k, v| v%2}.each{|a| a.sort!}) end def test_cluster_by_empty r = [].cluster_by{ |a| a } assert_equal([], r) end def test_each_by_01 x = [] [1,2,3,4].each_by{ |a,b| x << [a,b] } o = [[1,2],[3,4]] assert_equal( o, x ) end def test_each_by_02 x = [] [1,2,3,4,5].each_by{ |a,b,c| x << [a,b,c] } o = [[1,2,3],[4,5,nil]] assert_equal( o, x ) end def test_each_by_03 x = [] [1,2,3,4].each_by(2){ |a,b| x << [a,b] } o = [[1,2],[3,4]] assert_equal( o, x ) end def test_each_by_04 x = [] [1,2,3,4,5,6,7,8].each_by(4){ |*a| x << a } o = [ [[1,2,3,4]],[[5,6,7,8]] ] assert_equal( o, x ) end def test_each_by_05 x = [] [1,2,3,4,5,6].each_by(3){ |*a| x << a } o = [ [[1,2,3]],[[4,5,6]] ] assert_equal( o, x ) end def test_each_by_06 a = [1,2,3,4,5,6] r = [] a.each_by(2){ |x,y| r << [x,y] } assert_equal( [[1,2],[3,4],[5,6]], r ) end def test_each_by_07 a = [1,2,3,4,5,6] r = [] a.each_by(3){ |*e| r << e } assert_equal( [ [[1,2,3]], [[4,5,6]] ], r ) end def test_each_pair r = [] a = [1,2,3,4] a.each_pair{ |a,b| r << [a,b] } assert_equal( [[1,2],[3,4]], r ) end def test_eachn x = [] [1,2,3,4].eachn{ |a,b| x << [a,b] } o = [[1,2],[3,4]] assert_equal( o, x ) x = [] [1,2,3,4,5].eachn{ |a,b,c| x << [a,b,c] } o = [[1,2,3],[4,5,nil]] assert_equal( o, x ) end def test_collect_with_index a = [1,2,3].collect_with_index{ |e,i| e*i } assert_equal( [0,2,6], a ) end end