ruby-changes:27971
From: akr <ko1@a...>
Date: Mon, 1 Apr 2013 01:14:27 +0900 (JST)
Subject: [ruby-changes:27971] akr:r40023 (trunk): * include/ruby/ruby.h (FIX2ULONG): Make it consistent with NUM2ULONG.
akr 2013-04-01 01:14:15 +0900 (Mon, 01 Apr 2013) New Revision: 40023 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40023 Log: * include/ruby/ruby.h (FIX2ULONG): Make it consistent with NUM2ULONG. * ext/-test-/num2int/num2int.c: Add utility methods for FIX2XXX tests. * test/-ext-/num2int/test_num2int.rb: Add tests for FIX2XXX. Modified files: trunk/ChangeLog trunk/ext/-test-/num2int/num2int.c trunk/include/ruby/ruby.h trunk/test/-ext-/num2int/test_num2int.rb Index: include/ruby/ruby.h =================================================================== --- include/ruby/ruby.h (revision 40022) +++ include/ruby/ruby.h (revision 40023) @@ -337,7 +337,7 @@ rb_long2int_inline(long n) https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L337 #endif #define FIX2LONG(x) (long)RSHIFT((SIGNED_VALUE)(x),1) -#define FIX2ULONG(x) ((((VALUE)(x))>>1)&LONG_MAX) +#define FIX2ULONG(x) ((unsigned long)FIX2LONG(x)) #define FIXNUM_P(f) (((int)(SIGNED_VALUE)(f))&FIXNUM_FLAG) #define POSFIXABLE(f) ((f) < FIXNUM_MAX+1) #define NEGFIXABLE(f) ((f) >= FIXNUM_MIN) Index: ChangeLog =================================================================== --- ChangeLog (revision 40022) +++ ChangeLog (revision 40023) @@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon Apr 1 01:12:46 2013 Tanaka Akira <akr@f...> + + * include/ruby/ruby.h (FIX2ULONG): Make it consistent with NUM2ULONG. + + * ext/-test-/num2int/num2int.c: Add utility methods for FIX2XXX tests. + + * test/-ext-/num2int/test_num2int.rb: Add tests for FIX2XXX. + Sun Mar 31 17:17:56 2013 Nobuyoshi Nakada <nobu@r...> * proc.c (rb_mod_define_method): consider visibility in define_method. Index: ext/-test-/num2int/num2int.c =================================================================== --- ext/-test-/num2int/num2int.c (revision 40022) +++ ext/-test-/num2int/num2int.c (revision 40023) @@ -98,6 +98,66 @@ print_num2ull(VALUE obj, VALUE num) https://github.com/ruby/ruby/blob/trunk/ext/-test-/num2int/num2int.c#L98 } #endif +static VALUE +print_fix2short(VALUE obj, VALUE num) +{ + char buf[128]; + VALUE str; + + sprintf(buf, "%d", FIX2SHORT(num)); + str = rb_str_new_cstr(buf); + rb_io_write(rb_stdout, str); + return Qnil; +} + +static VALUE +print_fix2int(VALUE obj, VALUE num) +{ + char buf[128]; + VALUE str; + + sprintf(buf, "%d", FIX2INT(num)); + str = rb_str_new_cstr(buf); + rb_io_write(rb_stdout, str); + return Qnil; +} + +static VALUE +print_fix2uint(VALUE obj, VALUE num) +{ + char buf[128]; + VALUE str; + + sprintf(buf, "%u", FIX2UINT(num)); + str = rb_str_new_cstr(buf); + rb_io_write(rb_stdout, str); + return Qnil; +} + +static VALUE +print_fix2long(VALUE obj, VALUE num) +{ + char buf[128]; + VALUE str; + + sprintf(buf, "%ld", FIX2LONG(num)); + str = rb_str_new_cstr(buf); + rb_io_write(rb_stdout, str); + return Qnil; +} + +static VALUE +print_fix2ulong(VALUE obj, VALUE num) +{ + char buf[128]; + VALUE str; + + sprintf(buf, "%lu", FIX2ULONG(num)); + str = rb_str_new_cstr(buf); + rb_io_write(rb_stdout, str); + return Qnil; +} + void Init_num2int(void) { @@ -116,5 +176,13 @@ Init_num2int(void) https://github.com/ruby/ruby/blob/trunk/ext/-test-/num2int/num2int.c#L176 rb_define_singleton_method(cNum2int, "print_num2ll", print_num2ll, 1); rb_define_singleton_method(cNum2int, "print_num2ull", print_num2ull, 1); #endif + + rb_define_singleton_method(cNum2int, "print_fix2short", print_fix2short, 1); + + rb_define_singleton_method(cNum2int, "print_fix2int", print_fix2int, 1); + rb_define_singleton_method(cNum2int, "print_fix2uint", print_fix2uint, 1); + + rb_define_singleton_method(cNum2int, "print_fix2long", print_fix2long, 1); + rb_define_singleton_method(cNum2int, "print_fix2ulong", print_fix2ulong, 1); } Index: test/-ext-/num2int/test_num2int.rb =================================================================== --- test/-ext-/num2int/test_num2int.rb (revision 40022) +++ test/-ext-/num2int/test_num2int.rb (revision 40023) @@ -83,6 +83,36 @@ class TestNum2int < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/num2int/test_num2int.rb#L83 end end + def assert_fix2i_success_internal(exp, func, arg) + mesg = "#{func}(#{arg.inspect})" + method = "print_#{func}".downcase + out = err = nil + assert_nothing_raised(mesg) { + out, err = capture_io { Num2int.send(method, arg) } + } + STDERR.puts err if err && !err.empty? + assert_equal(exp, out, mesg) + end + + def assert_fix2i_success(type, num, result=num) + return if !num.kind_of?(Fixnum) + func = "FIX2#{type}".upcase + assert_fix2i_success_internal(result.to_s, func, num) + end + + def assert_fix2i_error_internal(func, arg) + method = "print_#{func}".downcase + assert_raise(RangeError, "#{func}(#{arg.inspect})") { + Num2int.send(method, arg) + } + end + + def assert_fix2i_error(type, num) + return if !num.kind_of?(Fixnum) + func = "FIX2#{type}".upcase + assert_num2i_error_internal(func, num) + end + def test_num2short assert_num2i_success(:short, SHRT_MIN) assert_num2i_success(:short, SHRT_MAX) @@ -162,6 +192,50 @@ class TestNum2int < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/num2int/test_num2int.rb#L192 assert_num2i_success(:ull, FIXNUM_MAX) assert_num2i_success(:ull, FIXNUM_MAX+1) end if defined?(Num2int.print_num2ull) + + def test_fix2short + assert_fix2i_success(:short, 0) + assert_fix2i_success(:short, SHRT_MAX) + assert_fix2i_success(:short, SHRT_MIN) + assert_fix2i_error(:short, SHRT_MAX+1) + assert_fix2i_error(:short, SHRT_MIN-1) + assert_fix2i_error(:short, FIXNUM_MAX) + assert_fix2i_error(:short, FIXNUM_MIN) + end + + def test_fix2int + assert_fix2i_success(:int, 0) + assert_fix2i_success(:int, INT_MAX) + assert_fix2i_success(:int, INT_MIN) + assert_fix2i_error(:int, INT_MAX+1) + assert_fix2i_error(:int, INT_MIN-1) + assert_fix2i_error(:int, FIXNUM_MAX) if INT_MAX < FIXNUM_MAX + assert_fix2i_error(:int, FIXNUM_MIN) if FIXNUM_MIN < INT_MIN + end + + def test_fix2uint + assert_fix2i_success(:uint, 0) + assert_fix2i_success(:uint, UINT_MAX) + assert_fix2i_success(:uint, INT_MAX) + assert_fix2i_success(:uint, INT_MIN, INT_MAX+1) + assert_fix2i_error(:uint, UINT_MAX+1) + assert_fix2i_error(:uint, INT_MIN-1) + assert_fix2i_error(:uint, FIXNUM_MAX) if UINT_MAX < FIXNUM_MAX + assert_fix2i_error(:uint, FIXNUM_MIN) if FIXNUM_MIN < INT_MIN + end + + def test_fix2long + assert_fix2i_success(:long, 0) + assert_fix2i_success(:long, FIXNUM_MAX) + assert_fix2i_success(:long, FIXNUM_MIN) + end + + def test_fix2ulong + assert_fix2i_success(:ulong, 0) + assert_fix2i_success(:ulong, FIXNUM_MAX) + assert_fix2i_success(:ulong, -1, ULONG_MAX) + end + end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/