Project

General

Profile

Bug #9202 ยป 0001-Array-uniq-uniq-does-not-return-frozen-hash-keys.patch

normalperson (Eric Wong), 12/03/2013 12:10 PM

View differences:

array.c
3905 3905
    long i;
3906 3906

  
3907 3907
    for (i=0; i<RARRAY_LEN(ary); i++) {
3908
	rb_hash_aset(hash, RARRAY_AREF(ary, i), Qtrue);
3908
	VALUE elt = RARRAY_AREF(ary, i);
3909
	rb_hash_aset(hash, elt, elt);
3909 3910
    }
3910 3911
    return hash;
3911 3912
}
......
4056 4057

  
4057 4058
    ary2 = to_ary(ary2);
4058 4059
    hash = ary_add_hash(ary_make_hash(ary1), ary2);
4059
    ary3 = rb_hash_keys(hash);
4060
    ary3 = rb_hash_values(hash);
4060 4061
    ary_recycle_hash(hash);
4061 4062
    return ary3;
4062 4063
}
4063 4064

  
4064 4065
static int
4065
push_key(st_data_t key, st_data_t val, st_data_t ary)
4066
push_val(st_data_t key, st_data_t val, st_data_t ary)
4066 4067
{
4067
    rb_ary_push((VALUE)ary, (VALUE)key);
4068
    rb_ary_push((VALUE)ary, (VALUE)val);
4068 4069
    return ST_CONTINUE;
4069 4070
}
4070 4071

  
......
4137 4138
	    FL_SET_EMBED(ary);
4138 4139
	}
4139 4140
	ary_resize_capa(ary, hash_size);
4140
	st_foreach(rb_hash_tbl_raw(hash), push_key, ary);
4141
	st_foreach(rb_hash_tbl_raw(hash), push_val, ary);
4141 4142
    }
4142 4143
    ary_recycle_hash(hash);
4143 4144

  
......
4176 4177
    }
4177 4178
    else {
4178 4179
	hash = ary_make_hash(ary);
4179
	uniq = rb_hash_keys(hash);
4180
	uniq = rb_hash_values(hash);
4180 4181
    }
4181 4182
    RBASIC_SET_CLASS(uniq, rb_obj_class(ary));
4182 4183
    ary_recycle_hash(hash);
test/ruby/test_array.rb
1507 1507
    assert_equal(d, c)
1508 1508

  
1509 1509
    assert_equal(@cls[1, 2, 3], @cls[1, 2, 3].uniq)
1510

  
1511
    a = %w(a a)
1512
    b = a.uniq
1513
    assert_equal(%w(a a), a)
1514
    assert(a.none?(&:frozen?))
1515
    assert_equal(%w(a), b)
1516
    assert(b.none?(&:frozen?))
1510 1517
  end
1511 1518

  
1512 1519
  def test_uniq_with_block
......
1527 1534
    assert_equal([1,3], a)
1528 1535
    assert_equal([1], b)
1529 1536
    assert_not_same(a, b)
1537

  
1538
    a = %w(a a)
1539
    b = a.uniq {|v| v }
1540
    assert_equal(%w(a a), a)
1541
    assert(a.none?(&:frozen?))
1542
    assert_equal(%w(a), b)
1543
    assert(b.none?(&:frozen?))
1530 1544
  end
1531 1545

  
1532 1546
  def test_uniq!
......
1573 1587
      a.sort_by!{|e| e[:c]}
1574 1588
      a.uniq!   {|e| e[:c]}
1575 1589
    end
1590

  
1591
    a = %w(a a)
1592
    b = a.uniq
1593
    assert_equal(%w(a a), a)
1594
    assert(a.none?(&:frozen?))
1595
    assert_equal(%w(a), b)
1596
    assert(b.none?(&:frozen?))
1576 1597
  end
1577 1598

  
1578 1599
  def test_uniq_bang_with_block
......
1594 1615
    b = a.uniq! {|v| v.even? }
1595 1616
    assert_equal([1,2], a)
1596 1617
    assert_equal(nil, b)
1618

  
1619
    a = %w(a a)
1620
    b = a.uniq! {|v| v }
1621
    assert_equal(%w(a), b)
1622
    assert_same(a, b)
1623
    assert b.none?(&:frozen?)
1597 1624
  end
1598 1625

  
1599 1626
  def test_uniq_bang_with_freeze
......
1622 1649
    assert_equal(@cls[1,2], @cls[1] | @cls[2])
1623 1650
    assert_equal(@cls[1,2], @cls[1, 1] | @cls[2, 2])
1624 1651
    assert_equal(@cls[1,2], @cls[1, 2] | @cls[1, 2])
1652

  
1653
    a = %w(a b c)
1654
    b = %w(a b c d e)
1655
    c = a | b
1656
    assert_equal(c, b)
1657
    assert_not_same(c, b)
1658
    assert_equal(%w(a b c), a)
1659
    assert_equal(%w(a b c d e), b)
1660
    assert(a.none?(&:frozen?))
1661
    assert(b.none?(&:frozen?))
1662
    assert(c.none?(&:frozen?))
1625 1663
  end
1626 1664

  
1627 1665
  def test_combination
1628
-