Project

General

Profile

randombytes.diff

marcandre (Marc-Andre Lafortune), 06/28/2011 12:58 AM

View differences:

NEWS
79 79
  * Random
80 80
    * extended method:
81 81
      * Random.rand supports range argument
82
    * new method:
83
      * Random.bytes
82 84

  
83 85
  * String
84 86
    * extended method:
random.c
964 964
    return bytes;
965 965
}
966 966

  
967
/*
968
 * call-seq: Random.bytes(size) -> a_string
969
 *
970
 * Returns a random binary string.  The argument size specified the length of
971
 * the result string.
972
 */
973
static VALUE
974
random_s_bytes(VALUE obj, VALUE len)
975
{
976
    return random_bytes(rb_Random_DEFAULT, len);
977
}
978

  
967 979
static VALUE
968 980
range_values(VALUE vmax, VALUE *begp, VALUE *endp, int *exclp)
969 981
{
......
1346 1358

  
1347 1359
    rb_define_singleton_method(rb_cRandom, "srand", rb_f_srand, -1);
1348 1360
    rb_define_singleton_method(rb_cRandom, "rand", random_s_rand, -1);
1361
    rb_define_singleton_method(rb_cRandom, "bytes", random_s_bytes, 1);
1349 1362
    rb_define_singleton_method(rb_cRandom, "new_seed", random_seed, 0);
1350 1363
    rb_define_private_method(CLASS_OF(rb_cRandom), "state", random_s_state, 0);
1351 1364
    rb_define_private_method(CLASS_OF(rb_cRandom), "left", random_s_left, 0);
test/ruby/test_rand.rb
340 340
  end
341 341

  
342 342
  def test_random_bytes
343
    srand(0)
343 344
    r = Random.new(0)
344 345
    assert_equal("", r.bytes(0))
346
    assert_equal("", Random.bytes(0))
345 347
    assert_equal("\xAC".force_encoding("ASCII-8BIT"), r.bytes(1))
346
    assert_equal("/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT"),                                                                                                                              r.bytes(10))
348
    assert_equal("\xAC".force_encoding("ASCII-8BIT"), Random.bytes(1))
349
    assert_equal("/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT"), r.bytes(10))
350
    assert_equal("/\xAA\xC4\x97u\xA6\x16\xB7\xC0\xCC".force_encoding("ASCII-8BIT"), Random.bytes(10))
347 351
  end
348 352

  
349 353
  def test_random_range