Project

General

Profile

Feature #14869 ยป hash_eqq.patch

osyo (manga osyo), 07/16/2018 04:35 AM

View differences:

hash.c
2411 2411
    return hash_equal(hash1, hash2, TRUE);
2412 2412
}
2413 2413

  
2414

  
2415
static int
2416
eqq_i(VALUE key, VALUE val1, VALUE arg)
2417
{
2418
    struct equal_data *data = (struct equal_data *)arg;
2419
    st_data_t val2;
2420

  
2421
    if (!st_lookup(data->tbl, key, &val2)) {
2422
	data->result = Qfalse;
2423
	return ST_STOP;
2424
    }
2425
    if (!rb_funcall(val1, idEqq, 1, (VALUE)val2)) {
2426
	data->result = Qfalse;
2427
	return ST_STOP;
2428
    }
2429
    return ST_CONTINUE;
2430
}
2431

  
2432
static VALUE
2433
recursive_eqq(VALUE hash, VALUE dt, int recur)
2434
{
2435
    struct equal_data *data;
2436

  
2437
    if (recur) return Qtrue;	/* Subtle! */
2438
    data = (struct equal_data*)dt;
2439
    data->result = Qtrue;
2440
    rb_hash_foreach(hash, eqq_i, dt);
2441

  
2442
    return data->result;
2443
}
2444

  
2445
static VALUE
2446
rb_hash_eqq(VALUE hash1, VALUE hash2)
2447
{
2448
    struct equal_data data;
2449

  
2450
    if (hash1 == hash2) return Qtrue;
2451
    if (!RB_TYPE_P(hash2, T_HASH)) return Qfalse;
2452
    if (RHASH_EMPTY_P(hash1))
2453
	return RHASH_EMPTY_P(hash2) ? Qtrue : Qfalse;
2454
    if (RHASH_SIZE(hash1) > RHASH_SIZE(hash2))
2455
	return Qfalse;
2456
    if (!RHASH(hash1)->ntbl || !RHASH(hash2)->ntbl)
2457
        return Qtrue;
2458
    if (RHASH(hash1)->ntbl->type != RHASH(hash2)->ntbl->type)
2459
	return Qfalse;
2460

  
2461
    data.tbl = RHASH(hash2)->ntbl;
2462
    return rb_exec_recursive_paired(recursive_eqq, hash1, hash2, (VALUE)&data);
2463
}
2464

  
2414 2465
static int
2415 2466
hash_i(VALUE key, VALUE val, VALUE arg)
2416 2467
{
......
4683 4734
    rb_define_method(rb_cHash, "to_proc", rb_hash_to_proc, 0);
4684 4735

  
4685 4736
    rb_define_method(rb_cHash, "==", rb_hash_equal, 1);
4737
    rb_define_method(rb_cHash, "===", rb_hash_eqq, 1);
4686 4738
    rb_define_method(rb_cHash, "[]", rb_hash_aref, 1);
4687 4739
    rb_define_method(rb_cHash, "hash", rb_hash_hash, 0);
4688 4740
    rb_define_method(rb_cHash, "eql?", rb_hash_eql, 1);
test/ruby/test_hash.rb
1107 1107
    assert_not_send([@cls[], :eql?, o])
1108 1108
  end
1109 1109

  
1110
  def test_eqq
1111
    user = { id: 1, name: "homu", age: 14 }
1112
    assert_operator({ id: 1 }, :===, user)
1113
    assert_operator({ id: 1, age: 14 }, :===, user)
1114
    assert_operator({ id: Integer }, :===, user)
1115
    assert_operator({ name: String, age: Integer }, :===, user)
1116
    assert_operator({ name: /m/ }, :===, user)
1117
    assert_operator({ number: nil }, :===, { number: nil })
1118
    assert_operator({}, :===, {})
1119
    obj = Object.new
1120
    def obj.=== other
1121
      true
1122
    end
1123
    assert_operator({ id: obj }, :===, user)
1124
    a = { name: String }
1125
    a[:a] = a
1126
    assert_operator(a, :===, a)
1127
    b = { name: "homu" }
1128
    b[:a] = b
1129
    assert_operator(a, :===, b)
1130

  
1131
    assert_not_operator({ id: 2 }, :===, user)
1132
    assert_not_operator({ name: Integer }, :===, user)
1133
    assert_not_operator({ number: 42 }, :===, user)
1134
    assert_not_operator({ number: nil }, :===, {})
1135
    assert_not_operator({ number: 42 }, :===, {})
1136
    assert_not_operator({ number: 42 }, :===, 42)
1137
    assert_not_operator({ number: 42 }, :===, [])
1138
    assert_not_operator({}, :===, user)
1139
    assert_not_operator({}, :===, 42)
1140
    assert_not_operator({}, :===, { name: "homu" })
1141
    obj2 = Object.new
1142
    def obj2.=== other
1143
      false
1144
    end
1145
    assert_not_operator({ id: obj2 }, :===, user)
1146
    a = { name: Integer }
1147
    a[:a] = a
1148
    b = { name: "homu" }
1149
    b[:a] = b
1150
    assert_not_operator(a, :===, b)
1151
  end
1152

  
1110 1153
  def test_hash2
1111 1154
    assert_kind_of(Integer, @cls[].hash)
1112 1155
    h = @cls[1=>2]