* 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.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40023 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-03-31 16:14:15 +00:00
Родитель c68234f7d1
Коммит a7526797a6
4 изменённых файлов: 151 добавлений и 1 удалений

Просмотреть файл

@ -1,3 +1,11 @@
Mon Apr 1 01:12:46 2013 Tanaka Akira <akr@fsij.org>
* 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@ruby-lang.org>
* proc.c (rb_mod_define_method): consider visibility in define_method.

Просмотреть файл

@ -98,6 +98,66 @@ print_num2ull(VALUE obj, VALUE num)
}
#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)
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);
}

Просмотреть файл

@ -337,7 +337,7 @@ rb_long2int_inline(long n)
#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)

Просмотреть файл

@ -83,6 +83,36 @@ class TestNum2int < Test::Unit::TestCase
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
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