Project

General

Profile

randombytes-ruby22.diff

hsbt (Hiroshi SHIBATA), 10/29/2014 05:37 AM

View differences:

random.c
304 304
}
305 305

  
306 306
VALUE rb_cRandom;
307
static VALUE rand_default;
307 308
#define id_minus '-'
308 309
#define id_plus  '+'
309 310
static ID id_rand, id_bytes;
......
905 906
    return bytes;
906 907
}
907 908

  
909
/*
910
 * call-seq: Random.bytes(size) -> a_string
911
 *
912
 * Returns a random binary string.  The argument size specified the length of
913
 * the result string.
914
 */
915
static VALUE
916
random_s_bytes(VALUE obj, VALUE len)
917
{
918
    return random_bytes(rand_default, len);
919
}
920

  
908 921
static VALUE
909 922
range_values(VALUE vmax, VALUE *begp, VALUE *endp, int *exclp)
910 923
{
......
1372 1385
    rb_define_method(rb_cRandom, "==", random_equal, 1);
1373 1386

  
1374 1387
    {
1375
	VALUE rand_default = TypedData_Wrap_Struct(rb_cRandom, &random_data_type, &default_rand);
1388
	rand_default = TypedData_Wrap_Struct(rb_cRandom, &random_data_type, &default_rand);
1376 1389
	rb_gc_register_mark_object(rand_default);
1377 1390
	/* Direct access to Ruby's Pseudorandom number generator (PRNG). */
1378 1391
	rb_define_const(rb_cRandom, "DEFAULT", rand_default);
......
1380 1393

  
1381 1394
    rb_define_singleton_method(rb_cRandom, "srand", rb_f_srand, -1);
1382 1395
    rb_define_singleton_method(rb_cRandom, "rand", random_s_rand, -1);
1396
    rb_define_singleton_method(rb_cRandom, "bytes", random_s_bytes, 1);
1383 1397
    rb_define_singleton_method(rb_cRandom, "new_seed", random_seed, 0);
1384 1398
    rb_define_private_method(CLASS_OF(rb_cRandom), "state", random_s_state, 0);
1385 1399
    rb_define_private_method(CLASS_OF(rb_cRandom), "left", random_s_left, 0);
test/ruby/test_rand.rb
347 347
  end
348 348

  
349 349
  def assert_random_bytes(r)
350
    srand(0)
350 351
    assert_equal("", r.bytes(0))
352
    assert_equal("", Random.bytes(0))
351 353
    assert_equal("\xAC".force_encoding("ASCII-8BIT"), r.bytes(1))
352
    assert_equal("/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT"),
353
                 r.bytes(10))
354
    assert_equal("\xAC".force_encoding("ASCII-8BIT"), Random.bytes(1))
355
    assert_equal("/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT"), r.bytes(10))
356
    assert_equal("/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT"), Random.bytes(10))
354 357
  end
355 358

  
356 359
  def test_random_range