ruby-changes:7420
From: nobu <ko1@a...>
Date: Sat, 30 Aug 2008 14:39:28 +0900 (JST)
Subject: [ruby-changes:7420] Ruby:r18939 (mvm): * merged from trunk r18923:18938.
nobu 2008-08-30 14:36:09 +0900 (Sat, 30 Aug 2008) New Revision: 18939 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=18939 Log: * merged from trunk r18923:18938. Modified files: branches/mvm/.merged-trunk-revision branches/mvm/ChangeLog branches/mvm/bignum.c branches/mvm/complex.c branches/mvm/encoding.c branches/mvm/ext/iconv/iconv.c branches/mvm/file.c branches/mvm/include/ruby/intern.h branches/mvm/io.c branches/mvm/lib/cgi.rb branches/mvm/lib/rake.rb branches/mvm/numeric.c branches/mvm/object.c branches/mvm/pack.c branches/mvm/parse.y branches/mvm/rational.c branches/mvm/test/ruby/test_complex.rb branches/mvm/test/ruby/test_m17n.rb branches/mvm/test/ruby/test_rational.rb branches/mvm/transcode.c branches/mvm/version.h Index: mvm/complex.c =================================================================== --- mvm/complex.c (revision 18938) +++ mvm/complex.c (revision 18939) @@ -756,6 +756,13 @@ } static VALUE +nucomp_rect(VALUE self) +{ + get_dat1(self); + return rb_assoc_new(dat->real, dat->image); +} + +static VALUE nucomp_polar(VALUE self) { return rb_assoc_new(f_abs(self), f_arg(self)); @@ -1011,14 +1018,15 @@ #define underscores_pat *rb_vm_specific_ptr(vmkey_underscores_pat) #define an_underscore *rb_vm_specific_ptr(vmkey_an_underscore) +#define WS "\\s*" #define DIGITS "(?:\\d(?:_\\d|\\d)*)" #define NUMERATOR "(?:" DIGITS "?\\.)?" DIGITS "(?:[eE][-+]?" DIGITS ")?" #define DENOMINATOR DIGITS #define NUMBER "[-+]?" NUMERATOR "(?:\\/" DENOMINATOR ")?" #define NUMBERNOS NUMERATOR "(?:\\/" DENOMINATOR ")?" -#define PATTERN0 "\\A(" NUMBER ")@(" NUMBER ")" -#define PATTERN1 "\\A([-+])?(" NUMBER ")?[iIjJ]" -#define PATTERN2 "\\A(" NUMBER ")(([-+])(" NUMBERNOS ")?[iIjJ])?" +#define PATTERN0 "\\A" WS "(" NUMBER ")@(" NUMBER ")" WS +#define PATTERN1 "\\A" WS "([-+])?(" NUMBER ")?[iIjJ]" WS +#define PATTERN2 "\\A" WS "(" NUMBER ")(([-+])(" NUMBERNOS ")?[iIjJ])?" WS static void make_patterns(void) @@ -1057,9 +1065,6 @@ an_underscore = rb_str_new2("_"); } -#define id_strip rb_intern("strip") -#define f_strip(x) rb_funcall(x, id_strip, 0) - #define id_match rb_intern("match") #define f_match(x,y) rb_funcall(x, id_match, 1, y) @@ -1086,7 +1091,7 @@ { VALUE s; - s = f_strip(self); + s = self; if (RSTRING_LEN(s) == 0) return rb_assoc_new(Qnil, self); @@ -1305,6 +1310,12 @@ } static VALUE +numeric_rect(VALUE self) +{ + return rb_assoc_new(self, ZERO); +} + +static VALUE numeric_polar(VALUE self) { return rb_assoc_new(f_abs(self), f_arg(self)); @@ -1374,10 +1385,8 @@ rb_funcall(rb_cComplex, rb_intern("private_class_method"), 1, ID2SYM(rb_intern("new"))); -#if 0 + rb_define_singleton_method(rb_cComplex, "rectangular", nucomp_s_new, -1); rb_define_singleton_method(rb_cComplex, "rect", nucomp_s_new, -1); - rb_define_singleton_method(rb_cComplex, "rectangular", nucomp_s_new, -1); -#endif rb_define_singleton_method(rb_cComplex, "polar", nucomp_s_polar, 2); rb_define_global_function(COMPLEX_NAME, nucomp_f_complex, -1); @@ -1416,12 +1425,13 @@ rb_define_method(rb_cComplex, "coerce", nucomp_coerce, 1); rb_define_method(rb_cComplex, "abs", nucomp_abs, 0); -#if 0 rb_define_method(rb_cComplex, "magnitude", nucomp_abs, 0); -#endif rb_define_method(rb_cComplex, "abs2", nucomp_abs2, 0); rb_define_method(rb_cComplex, "arg", nucomp_arg, 0); rb_define_method(rb_cComplex, "angle", nucomp_arg, 0); + rb_define_method(rb_cComplex, "phase", nucomp_arg, 0); + rb_define_method(rb_cComplex, "rectangular", nucomp_rect, 0); + rb_define_method(rb_cComplex, "rect", nucomp_rect, 0); rb_define_method(rb_cComplex, "polar", nucomp_polar, 0); rb_define_method(rb_cComplex, "conjugate", nucomp_conjugate, 0); rb_define_method(rb_cComplex, "conj", nucomp_conjugate, 0); @@ -1473,6 +1483,9 @@ rb_define_method(rb_cNumeric, "imag", numeric_image, 0); rb_define_method(rb_cNumeric, "arg", numeric_arg, 0); rb_define_method(rb_cNumeric, "angle", numeric_arg, 0); + rb_define_method(rb_cNumeric, "phase", numeric_arg, 0); + rb_define_method(rb_cNumeric, "rectangular", numeric_rect, 0); + rb_define_method(rb_cNumeric, "rect", numeric_rect, 0); rb_define_method(rb_cNumeric, "polar", numeric_polar, 0); rb_define_method(rb_cNumeric, "conjugate", numeric_conjugate, 0); rb_define_method(rb_cNumeric, "conj", numeric_conjugate, 0); Index: mvm/encoding.c =================================================================== --- mvm/encoding.c (revision 18938) +++ mvm/encoding.c (revision 18939) @@ -879,13 +879,13 @@ { rb_encoding *enc; - if (BUILTIN_TYPE(str1) != T_STRING && BUILTIN_TYPE(str1) != T_REGEXP) { - rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Regexp)", - rb_obj_classname(str1)); + if (SPECIAL_CONST_P(str1) || TYPE(str1) != T_STRING && TYPE(str1) != T_REGEXP) { + rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Regexp)", + rb_obj_classname(str1)); } - if (BUILTIN_TYPE(str2) != T_STRING && BUILTIN_TYPE(str2) != T_REGEXP) { - rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Regexp)", - rb_obj_classname(str2)); + if (SPECIAL_CONST_P(str2) || TYPE(str2) != T_STRING && TYPE(str2) != T_REGEXP) { + rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Regexp)", + rb_obj_classname(str2)); } if (!enc_capable(str1)) return Qnil; if (!enc_capable(str2)) return Qnil; Index: mvm/include/ruby/intern.h =================================================================== --- mvm/include/ruby/intern.h (revision 18938) +++ mvm/include/ruby/intern.h (revision 18939) @@ -318,6 +318,8 @@ /* file.c */ VALUE rb_file_s_expand_path(int, VALUE *); VALUE rb_file_expand_path(VALUE, VALUE); +VALUE rb_file_s_absolute_path(int, VALUE *); +VALUE rb_file_absolute_path(VALUE, VALUE); void rb_file_const(const char*, VALUE); int rb_find_file_ext(VALUE*, const char* const*); VALUE rb_find_file(VALUE); Index: mvm/ChangeLog =================================================================== --- mvm/ChangeLog (revision 18938) +++ mvm/ChangeLog (revision 18939) @@ -1,3 +1,71 @@ +Sat Aug 30 10:46:44 2008 Koichi Sasada <ko1@a...> + + * ext/iconv/iconv.c: remove include pragma for "ruby/intern.h". + +Sat Aug 30 10:43:03 2008 2008 Koichi Sasada <ko1@a...> + + * parse.y: remove include pragma for "ruby/intern.h". + +Sat Aug 30 10:41:57 2008 Nobuyoshi Nakada <nobu@r...> + + * lib/rake.rb (Rake::Application#help): fix for OPTIONAL_ARGUMENT and + multiple short options. [ruby-dev:36051] + +Sat Aug 30 08:59:25 2008 Nobuyoshi Nakada <nobu@r...> + + * include/ruby/intern.h (rb_file_s_absolute_path), + (rb_file_absolute_path): prototyped. + +Sat Aug 30 08:09:36 2008 NARUSE, Yui <naruse@r...> + + * encoding.c (enc_compatible_p): use TYPE not BUILTIN_TYPE. + [ruby-dev:36048] + +Sat Aug 30 07:48:48 2008 NARUSE, Yui <naruse@r...> + + * encoding.c (enc_compatible_p): add SPECIAL_CONST_P check. + [ruby-dev:36048] + + * test/ruby/test_m17n.rb (test_compatible): fix test. + +Sat Aug 30 02:48:11 2008 Yukihiro Matsumoto <matz@r...> + + * object.c (rb_obj_freeze): update rdoc to mention RuntimeError + (not TypeError any longer) would be raised. [ruby-dev:35982] + +Sat Aug 30 01:55:30 2008 Yukihiro Matsumoto <matz@r...> + + * bignum.c (bigdivmod): remove redundant code. a patch from + TOYOFUKU Chikanobu <nobu_toyofuku at nifty.com> in [ruby-dev:36044]. + +Sat Aug 30 01:37:20 2008 Yukihiro Matsumoto <matz@r...> + + * lib/cgi.rb (CGI#initialize): remove unused constants. a patch + from Takeyuki Fujioka in [ruby-dev:36046]. + +Fri Aug 29 22:29:41 2008 Tadayoshi Funaba <tadf@d...> + + * complex.c ({nucomp,numeric}_rect): new. + + * complex.c: added some aliases + (::rectangular, ::rect, #rectangular, #rect, #phase, + #magnitude). + + * complex.c (string_to_c_internal): should not strip any null + bytes. + + * rational.c (string_to_r_internal): ditto. + + * rational.c (i_gcd): reverted to nurat 0.0.2's one. + + * numeric.c: added an alias (#magnitude). + + * bignum.c: ditto. + + * test/ruby/test_complex.rb: added assertions. + + * test/ruby/test_rational.rb: ditto. + Fri Aug 29 19:46:02 2008 Koichi Sasada <ko1@a...> * gc.c, include/ruby/ruby.h: rename T_DEFERRED to T_ZOMBIE. Index: mvm/object.c =================================================================== --- mvm/object.c (revision 18938) +++ mvm/object.c (revision 18939) @@ -749,7 +749,7 @@ * obj.freeze => obj * * Prevents further modifications to <i>obj</i>. A - * <code>TypeError</code> will be raised if modification is attempted. + * <code>RuntimeError</code> will be raised if modification is attempted. * There is no way to unfreeze a frozen object. See also * <code>Object#frozen?</code>. * @@ -759,7 +759,7 @@ * * <em>produces:</em> * - * prog.rb:3:in `<<': can't modify frozen array (TypeError) + * prog.rb:3:in `<<': can't modify frozen array (RuntimeError) * from prog.rb:3 */ Index: mvm/io.c =================================================================== --- mvm/io.c (revision 18938) +++ mvm/io.c (revision 18939) @@ -3906,8 +3906,6 @@ struct sysopen_struct { #if USE_OPENAT int base; -#else - VALUE fullpath; #endif const char *fname; int flag; @@ -3931,42 +3929,47 @@ struct sysopen_struct data; #if USE_OPENAT - data->base = GET_THREAD()->cwd.fd; -#else - if (ruby_absolute_path_p(fname)) { - data->fullpath = Qnil; - } - else { - data->fullpath = rb_file_expand_path(rb_str_new2(fname), Qnil); - fname = RSTRING_PTR(data->fullpath); - } + data.base = GET_THREAD()->cwd.fd; #endif - data->fname = fname; - data->flag = flags; - data->mode = mode; - return (int)rb_thread_blocking_region(sysopen_func, data, RB_UBF_DFL, 0); + data.fname = fname; + data.flag = flags; + data.mode = mode; + return (int)rb_thread_blocking_region(sysopen_func, &data, RB_UBF_DFL, 0); } static int rb_sysopen(char *fname, int flags, mode_t mode) { int fd; - struct sysopen_struct data; +#if !USE_OPENAT + volatile VALUE fullpath; +#endif #ifdef O_BINARY flags |= O_BINARY; #endif +#if !USE_OPENAT + if (!ruby_absolute_path_p(fname)) { + fullpath = rb_file_absolute_path(rb_str_new2(fname), Qnil); + fname = RSTRING_PTR(fullpath); + } +#endif + fd = rb_sysopen_internal(fname, flags, mode); if (fd < 0) { if (errno == EMFILE || errno == ENFILE) { rb_gc(); - fd = rb_sysopen_internal(&data); + fd = rb_sysopen_internal(fname, flags, mode); } if (fd < 0) { rb_sys_fail(fname); } } + +#if !USE_OPENAT + RB_GC_GUARD(fullpath); +#endif UPDATE_MAXFD(fd); return fd; } @@ -4123,7 +4126,7 @@ } #undef RB_DO_OPENAT fptr->fd = fd; - fptr->path = strdup(fullpath); + fptr->pathv = fname; if (rb_block_given_p()) { return rb_ensure(rb_yield, newio, io_close, newio); } @@ -5944,7 +5947,7 @@ argf_of(argf).current_file = prep_io(fr, FMODE_READABLE, rb_cFile, fn); } if (argf_of(argf).binmode) rb_io_binmode(argf_of(argf).current_file); - if (argf_of(argf).enc) { + if (argf_of(argf).encs.enc) { rb_io_t *fptr; GetOpenFile(argf_of(argf).current_file, fptr); Index: mvm/pack.c =================================================================== --- mvm/pack.c (revision 18938) +++ mvm/pack.c (revision 18939) @@ -1153,7 +1153,7 @@ b64_xtable_once(volatile signed char *init, rb_thread_lock_t *lock) { signed char t; - while ((t = *init) < 0 && (ruby_native_thread_yield(), 1)); + while ((t = *init) < 0) ruby_native_thread_yield(); if (t > 0) return 0; ruby_native_thread_lock(lock); if (*init <= 0) return 1; Index: mvm/lib/cgi.rb =================================================================== --- mvm/lib/cgi.rb (revision 18938) +++ mvm/lib/cgi.rb (revision 18939) @@ -2298,11 +2298,6 @@ Apache.request.setup_cgi_env end - (class << self; self; end).class_eval do - const_set(:CGI_PARAMS, [1]) - const_set(:CGI_COOKIES, [2]) - end - extend QueryExtension @multipart = false Index: mvm/lib/rake.rb =================================================================== --- mvm/lib/rake.rb (revision 18938) +++ mvm/lib/rake.rb (revision 18939) @@ -1984,13 +1984,18 @@ puts puts "Options are ..." puts - OPTIONS.sort.each do |long, short, mode, desc| - if mode == GetoptLong::REQUIRED_ARGUMENT + OPTIONS.sort.each do |long, *short, mode, desc| + case mode + when GetoptLong::REQUIRED_ARGUMENT if desc =~ /\b([A-Z]{2,})\b/ long = long + "=#{$1}" end + when GetoptLong::OPTIONAL_ARGUMENT + if desc =~ /\b([A-Z]{2,})\b/ + long = long + "[=#{$1}]" + end end - printf " %-20s (%s)\n", long, short + printf " %-20s (%s)\n", long, short.join(", ") printf " %s\n", desc end end Index: mvm/parse.y =================================================================== --- mvm/parse.y (revision 18938) +++ mvm/parse.y (revision 18939) @@ -16,7 +16,6 @@ #define YYSTACK_USE_ALLOCA 0 #include "ruby/ruby.h" -#include "ruby/intern.h" #include "ruby/node.h" #include "ruby/st.h" #include "ruby/encoding.h" Index: mvm/ext/iconv/iconv.c =================================================================== --- mvm/ext/iconv/iconv.c (revision 18938) +++ mvm/ext/iconv/iconv.c (revision 18939) @@ -18,7 +18,6 @@ #include <iconv.h> #include <assert.h> #include "ruby/st.h" -#include "ruby/intern.h" #include "ruby/encoding.h" /* Index: mvm/.merged-trunk-revision =================================================================== --- mvm/.merged-trunk-revision (revision 18938) +++ mvm/.merged-trunk-revision (revision 18939) @@ -1 +1 @@ -18923 +18938 Index: mvm/numeric.c =================================================================== --- mvm/numeric.c (revision 18938) +++ mvm/numeric.c (revision 18939) @@ -3134,6 +3134,7 @@ rb_define_method(rb_cNumeric, "modulo", num_modulo, 1); rb_define_method(rb_cNumeric, "remainder", num_remainder, 1); rb_define_method(rb_cNumeric, "abs", num_abs, 0); + rb_define_method(rb_cNumeric, "magnitude", num_abs, 0); rb_define_method(rb_cNumeric, "to_int", num_to_int, 0); rb_define_method(rb_cNumeric, "scalar?", num_scalar_p, 0); @@ -3195,6 +3196,7 @@ rb_define_method(rb_cFixnum, "**", fix_pow, 1); rb_define_method(rb_cFixnum, "abs", fix_abs, 0); + rb_define_method(rb_cFixnum, "magnitude", fix_abs, 0); rb_define_method(rb_cFixnum, "==", fix_equal, 1); rb_define_method(rb_cFixnum, "<=>", fix_cmp, 1); @@ -3262,6 +3264,7 @@ rb_define_method(rb_cFloat, "hash", flo_hash, 0); rb_define_method(rb_cFloat, "to_f", flo_to_f, 0); rb_define_method(rb_cFloat, "abs", flo_abs, 0); + rb_define_method(rb_cFloat, "magnitude", flo_abs, 0); rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0); rb_define_method(rb_cFloat, "to_i", flo_truncate, 0); Index: mvm/version.h =================================================================== --- mvm/version.h (revision 18938) +++ mvm/version.h (revision 18939) @@ -1,7 +1,7 @@ #define RUBY_VERSION "1.9.0" -#define RUBY_RELEASE_DATE "2008-08-29" +#define RUBY_RELEASE_DATE "2008-08-30" #define RUBY_VERSION_CODE 190 -#define RUBY_RELEASE_CODE 20080829 +#define RUBY_RELEASE_CODE 20080830 #define RUBY_PATCHLEVEL 0 #define RUBY_VERSION_MAJOR 1 @@ -9,7 +9,7 @@ #define RUBY_VERSION_TEENY 0 #define RUBY_RELEASE_YEAR 2008 #define RUBY_RELEASE_MONTH 8 -#define RUBY_RELEASE_DAY 29 +#define RUBY_RELEASE_DAY 30 #ifdef RUBY_EXTERN RUBY_EXTERN const char ruby_version[]; Index: mvm/bignum.c =================================================================== --- mvm/bignum.c (revision 18938) +++ mvm/bignum.c (revision 18939) @@ -1801,9 +1801,8 @@ if (divp) *divp = bigadd(*divp, rb_int2big(1), 0); if (modp) *modp = bigadd(mod, y, 1); } - else { - if (divp) *divp = *divp; - if (modp) *modp = mod; + else if (modp) { + *modp = mod; } } @@ -2717,6 +2716,7 @@ rb_define_method(rb_cBignum, "hash", rb_big_hash, 0); rb_define_method(rb_cBignum, "to_f", rb_big_to_f, 0); rb_define_method(rb_cBignum, "abs", rb_big_abs, 0); + rb_define_method(rb_cBignum, "magnitude", rb_big_abs, 0); rb_define_method(rb_cBignum, "size", rb_big_size, 0); rb_define_method(rb_cBignum, "odd?", rb_big_odd_p, 0); rb_define_method(rb_cBignum, "even?", rb_big_even_p, 0); Index: mvm/test/ruby/test_m17n.rb =================================================================== --- mvm/test/ruby/test_m17n.rb (revision 18938) +++ mvm/test/ruby/test_m17n.rb (revision 18939) @@ -1280,7 +1280,7 @@ end def test_compatible - assert_equal(nil, Encoding.compatible?("",0), "moved from btest/knownbug") + assert_raise(TypeError) {Encoding.compatible?("",0)} end def test_force_encoding Index: mvm/test/ruby/test_complex.rb =================================================================== --- mvm/test/ruby/test_complex.rb (revision 18938) +++ mvm/test/ruby/test_complex.rb (revision 18939) @@ -7,6 +7,10 @@ def test_compsub c = ComplexSub.__send__(:new, 1) cc = ComplexSub.__send__(:convert, 1) + + assert_kind_of(Numeric, c) + assert_kind_of(Numeric, cc) + if defined?(ComplexSub::Unify) assert_instance_of(Fixnum, c) assert_instance_of(Fixnum, cc) @@ -181,10 +185,12 @@ assert_equal(4, c.real) assert_equal(5, c.image) - c = Complex(-0.0,-0.0) + if -0.0.to_s == '-0.0' + c = Complex(-0.0,-0.0) - assert_equal('-0.0', c.real.to_s) - assert_equal('-0.0', c.image.to_s) + assert_equal('-0.0', c.real.to_s) + assert_equal('-0.0', c.image.to_s) + end c = Complex.__send__(:new, 4) @@ -198,11 +204,13 @@ assert_equal(5, c.image) assert_equal(c.imag, c.image) - c = Complex.__send__(:new, -0.0,-0.0) + if -0.0.to_s == '-0.0' + c = Complex.__send__(:new, -0.0,-0.0) - assert_equal('-0.0', c.real.to_s) - assert_equal('-0.0', c.image.to_s) - assert_equal(c.imag.to_s, c.image.to_s) + assert_equal('-0.0', c.real.to_s) + assert_equal('-0.0', c.image.to_s) + assert_equal(c.imag.to_s, c.image.to_s) + end c = Complex.__send__(:new!, 4) @@ -275,6 +283,15 @@ assert_equal(Complex(1,1), Complex(1,1).nonzero?) end + def rect + assert_equal([1,2], Complex.rectangular(1,2).rectangular) + assert_equal([1,2], Complex.rect(1,2).rect) + end + + def polar + assert_equal([1,2], Complex.polar(1,2).polar) + end + def test_uplus assert_equal(Complex(1), +Complex(1)) assert_equal(Complex(-1), +Complex(-1)) @@ -503,10 +520,23 @@ assert_equal(false, Complex(1) == '') end + def test_unify + if defined?(Complex::Unify) + assert_instance_of(Fixnum, Complex(1,2) + Complex(-1,-2)) + assert_instance_of(Fixnum, Complex(1,2) - Complex(1,2)) + assert_instance_of(Fixnum, Complex(1,2) * 0) + assert_instance_of(Fixnum, Complex(1,2) / Complex(1,2)) + assert_instance_of(Fixnum, Complex(1,2).div(Complex(1,2))) + assert_instance_of(Fixnum, Complex(1,2).quo(Complex(1,2))) +# assert_instance_of(Fixnum, Complex(1,2) ** 0) # mathn's bug + end + end + def test_math c = Complex(1,2) assert_in_delta(2.236, c.abs, 0.001) + assert_in_delta(2.236, c.magnitude, 0.001) assert_equal(5, c.abs2) assert_equal(c.abs, Math.sqrt(c * c.conj)) @@ -516,6 +546,7 @@ assert_in_delta(1.107, c.arg, 0.001) assert_in_delta(1.107, c.angle, 0.001) + assert_in_delta(1.107, c.phase, 0.001) r = c.polar assert_in_delta(2.236, r[0], 0.001) @@ -598,6 +629,7 @@ def test_parse assert_equal(Complex(0), ''.to_c) assert_equal(Complex(0), ' '.to_c) + assert_equal(Complex(5), "\f\n\r\t\v5\0".to_c) assert_equal(Complex(5), '5'.to_c) assert_equal(Complex(-5), '-5'.to_c) assert_equal(Complex(5,3), '5+3i'.to_c) @@ -705,6 +737,7 @@ assert_raise(ArgumentError){ Complex('5+3_i')} assert_raise(ArgumentError){ Complex('5+3i_')} assert_raise(ArgumentError){ Complex('5+3ix')} + assert_raise(ArgumentError){ Complex("5\0")} if defined?(Rational) && defined?(''.to_r) assert_equal(Complex(Rational(1,5)), '1/5'.to_c) @@ -818,18 +851,37 @@ assert_equal(0, 1.1.image) assert_equal(0, 1.1.imag) + assert_equal(1, 1.magnitude) + assert_equal(1, -1.magnitude) + assert_equal(1, 1.0.magnitude) + assert_equal(1, -1.0.magnitude) + assert_equal(0, 1.arg) assert_equal(0, 1.angle) + assert_equal(0, 1.phase) assert_equal(0, 1.0.arg) assert_equal(0, 1.0.angle) + assert_equal(0, 1.0.phase) assert_equal(Math::PI, -1.arg) assert_equal(Math::PI, -1.angle) + assert_equal(Math::PI, -1.phase) assert_equal(Math::PI, -1.0.arg) assert_equal(Math::PI, -1.0.angle) + assert_equal(Math::PI, -1.0.phase) + assert_equal([1,0], 1.rect) + assert_equal([-1,0], -1.rect) + assert_equal([1,0], 1.rectangular) + assert_equal([-1,0], -1.rectangular) + + assert_equal([1.0,0], 1.0.rect) + assert_equal([-1.0,0], -1.0.rect) + assert_equal([1.0,0], 1.0.rectangular) + assert_equal([-1.0,0], -1.0.rectangular) + assert_equal([1,0], 1.polar) assert_equal([1, Math::PI], -1.polar) @@ -846,46 +898,14 @@ assert_equal(1.1, 1.1.conj) assert_equal(-1.1, -1.1.conj) - assert_equal(1, 1.numerator) - assert_equal(9, 9.numerator) - assert_equal(1, 1.denominator) - assert_equal(1, 9.denominator) - - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') - assert_equal(1.0, 1.0.numerator) - assert_equal(9.0, 9.0.numerator) - assert_equal(1.0, 1.0.denominator) - assert_equal(1.0, 9.0.denominator) - end - -=begin - if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') - assert_equal(Rational(1,9), 9.reciprocal) - assert_equal(Rational(1,9), 9.0.reciprocal) - assert_equal(Rational(1,9), 9.inverse) - assert_equal(Rational(1,9), 9.0.inverse) - end -=end - if defined?(Rational) - assert_equal(Rational(1,2), 1.quo(2)) - assert_equal(Rational(5000000000), 10000000000.quo(2)) - assert_equal(0.5, 1.0.quo(2)) - assert_equal(Rational(1,4), Rational(1,2).quo(2)) assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2)) else - assert_equal(0.5, 1.quo(2)) - assert_equal(5000000000.0, 10000000000.quo(2)) - assert_equal(0.5, 1.0.quo(2)) assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2)) end =begin if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') - assert_equal(Rational(1,2), 1.rdiv(2)) - assert_equal(Rational(5000000000), 10000000000.rdiv(2)) - assert_equal(Rational(1,2), 1.0.rdiv(2)) - assert_equal(Rational(1,4), Rational(1,2).rdiv(2)) assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2)) end =end @@ -1008,132 +1028,6 @@ end -=begin - def test_canonicalize - f = defined?(Complex::Unify) - Complex.const_set(:Unify, true) unless f - - assert_same(1, Complex.instance_eval { new(1, 0) }) - assert_not_same(1.0, Complex.instance_eval { new(1.0, 0) }) - assert_equal(Complex(1.0, 0), Complex.instance_eval { new(1.0, 0) }) - - Complex.instance_eval { remove_const(:Unify) } unless f - end - - def test_polar - c = Complex.polar(2, 2) - assert_in_delta(2*Math.cos(2), c.real , 0.001) - assert_in_delta(2*Math.sin(2), c.image, 0.001) - - c = Complex.polar(1, Complex(0, 1)) - assert_in_delta(1/Math::E, c.real , 0.001) - assert_in_delta( 0, c.image, 0.001) - end - - def test_generic? - assert_equal(true, Complex.generic?(1)) - assert_equal(true, Complex.generic?(2**100)) - assert_equal(true, Complex.generic?(Rational(1, 2))) - assert_equal(true, Complex.generic?(1.0)) - assert_equal(false, Complex.generic?(Complex(1, 1))) - end - - def test_new_bang2 - o = Object.new - def o.to_i; 1; end - assert_equal(Complex(1, 1), Complex.instance_eval { new!(o, o) }) - end - - def test_denominator - f = defined?(Complex::Unify) - unify_val = f && Complex::Unify - Complex.instance_eval { remove_const(:Unify) } if f - - dummy_rational = Class.new(Rational) - o1 = dummy_rational.instance_eval { new(1, 1) } - o2 = dummy_rational.instance_eval { new(1, 1) } - d1 = d2 = nil - class << o1; self; end.instance_eval { define_method(:denominator) { d1 } rescue nil } - class << o2; self; end.instance_eval { define_method(:denominator) { d2 } rescue nil } - # o1.denominator returns d1 and o1.denominator returns d2 - - c = Complex(o1, o2) - - d1 = d2 = 0 - assert_equal(0, c.denominator) - - d1 = d2 = -1 - assert_equal(1, c.denominator) - - d1 = d2 = 256 - assert_equal(256, c.denominator) - - d1, d2 = 512, 256 - assert_equal(512, c.denominator) - - d1, d2 = 256, 512 - assert_equal(512, c.denominator) - - d1, d2 = -(2**100), -(3**100) - assert_equal(6**100, c.denominator) - - d1, d2 = 1, 2**100 - assert_equal(2**100, c.denominator) - - Complex.const_set(:Unify, unify_val) if f - end - - def test_abs - b = 2**100 - def b.*(x); self; end rescue nil - def b.+(x); -1; end rescue nil - assert_equal(Complex(0, 1), Complex(b, 1).abs) - - def b.+(x); Complex(0, 1); end rescue nil - c = Complex(b, 1).abs - assert_in_delta(1/Math.sqrt(2), c.real , 0.001) - assert_in_delta(1/Math.sqrt(2), c.image, 0.001) - - def b.+(x); Complex(0, -1); end rescue nil - c = Complex(b, 1).abs - assert_in_delta( 1/Math.sqrt(2), c.real , 0.001) - assert_in_delta(-1/Math.sqrt(2), c.image, 0.001) - - inf = 1.0/0.0 - nan = inf/inf - assert_raise(Errno::EDOM, Errno::ERANGE) { Complex(1, nan).abs } - end - - def test_coerce - c = Complex(6, 3) - assert_equal(Complex(42, 0), c.coerce(42).first) - assert_raise(TypeError) { c.coerce(Object.new) } - - o = Object.new - def o.coerce(x); [x.real, x.image]; end - assert_equal(9, c + o) - assert_equal(3, c - o) - assert_equal(18, c * o) - assert_equal(2, c / o) - assert_equal(216, c ** o) - end - - def test_add2 - assert_equal(Complex(2**100, 1), Complex(0, 1) + 2**100) - end - - def test_mul2 - assert_equal(Complex(0.0, 0.0), Complex(1.0, 1.0) * 0) - assert_equal(Complex(0, 0), Complex(0, 0) * (2**100)) - end - - def test_expt2 - assert_equal(Complex(1, 0), Complex(2, 2) ** 0) - assert_equal(Complex(0, -1), Complex(0, 1) ** (2**100-1)) - assert_equal(Complex(1, 0), Complex(1, 0) ** Rational(1, 2**100)) - end -=end - def test_fixed_bug if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID') assert_equal(Complex(1), 1 ** Complex(1)) Index: mvm/test/ruby/test_rational.rb =================================================================== --- mvm/test/ruby/test_rational.rb (revision 18938) +++ mvm/test/ruby/test_rational.rb (revision 18939) @@ -7,6 +7,10 @@ def test_ratsub c = RationalSub.__send__(:new, 1) cc = RationalSub.__send__(:convert, 1) + + assert_kind_of(Numeric, c) + assert_kind_of(Numeric, cc) + if defined?(RationalSub::Unify) assert_instance_of(Fixnum, c) assert_instance_of(Fixnum, cc) @@ -29,6 +33,21 @@ end end + def test_eql_p + c = Rational(0) + c2 = Rational(0) + c3 = Rational(1) + + assert_equal(true, c.eql?(c2)) + assert_equal(false, c.eql?(c3)) + + if defined?(Rational::Unify) + assert_equal(true, c.eql?(0)) + else + assert_equal(false, c.eql?(0)) + end + end + def test_hash assert_instance_of(Fixnum, Rational(1,2).hash) @@ -180,10 +199,16 @@ c = Rational(Rational(1,2),Rational(1,2)) assert_equal(Rational.__send__(:new, 1), c) - assert_equal(Rational.__send__(:new, 1),Rational(1)) - assert_equal(1.1.to_r,Rational(1.1)) - assert_equal(Rational.__send__(:new, 1),Rational('1')) + assert_equal(Rational.__send__(:new, 3),Rational(3)) + assert_equal(Rational.__send__(:new, 1),Rational(3,3)) + assert_equal(3.3.to_r,Rational(3.3)) + assert_equal(1,Rational(3.3,3.3)) + assert_equal(Rational.__send__(:new, 3),Rational('3')) + assert_equal(Rational.__send__(:new, 1),Rational('3.0','3.0')) + assert_equal(Rational.__send__(:new, 1),Rational('3/3','3/3')) assert_raise(ArgumentError){Rational(nil)} + assert_raise(ArgumentError){Rational('')} + assert_raise(ArgumentError){Rational(Object.new)} end def test_attr @@ -335,6 +360,9 @@ assert_equal(Rational(1,4), c / 2) assert_equal(0.25, c / 2.0) + + assert_raise(ZeroDivisionError){Rational(1, 3) / 0} + assert_raise(ZeroDivisionError){Rational(1, 3) / Rational(0)} end def assert_eql(exp, act, *args) @@ -387,6 +415,41 @@ end end + def test_modulo + c = Rational(1,2) + c2 = Rational(2,3) + + assert_eql(Rational(1,2), c.modulo(c2)) + assert_eql(Rational(1,2), c.modulo(2)) + assert_eql(0.5, c.modulo(2.0)) + + c = Rational(301,100) + c2 = Rational(7,5) + + assert_equal(Rational(21,100), c.modulo(c2)) + assert_equal(Rational(-119,100), c.modulo(-c2)) + assert_equal(Rational(119,100), (-c).modulo(c2)) + assert_equal(Rational(-21,100), (-c).modulo(-c2)) + + c = Rational(301,100) + c2 = Rational(2) + + assert_equal(Rational(101,100), c.modulo(c2)) + assert_equal(Rational(-99,100), c.modulo(-c2)) + assert_equal(Rational(99,100), (-c).modulo(c2)) + assert_equal(Rational(-101,100), (-c).modulo(-c2)) + + unless defined?(Rational::Unify) + c = Rational(11) + c2 = Rational(3) + + assert_equal(2, c.modulo(c2)) + assert_equal(-1, c.modulo(-c2)) + assert_equal(1, (-c).modulo(c2)) + assert_equal(-2, (-c).modulo(-c2)) + end + end + def test_divmod c = Rational(1,2) c2 = Rational(2,3) @@ -457,7 +520,44 @@ assert_equal(3, (-c).quot(-c2)) end end +=end + def test_remainder + c = Rational(1,2) + c2 = Rational(2,3) + + assert_eql(Rational(1,2), c.remainder(c2)) + assert_eql(Rational(1,2), c.remainder(2)) + assert_eql(0.5, c.remainder(2.0)) + + c = Rational(301,100) + c2 = Rational(7,5) + + assert_equal(Rational(21,100), c.remainder(c2)) + assert_equal(Rational(21,100), c.remainder(-c2)) + assert_equal(Rational(-21,100), (-c).remainder(c2)) + assert_equal(Rational(-21,100), (-c).remainder(-c2)) + + c = Rational(301,100) + c2 = Rational(2) + + assert_equal(Rational(101,100), c.remainder(c2)) + assert_equal(Rational(101,100), c.remainder(-c2)) + assert_equal(Rational(-101,100), (-c).remainder(c2)) + assert_equal(Rational(-101,100), (-c).remainder(-c2)) + + unless defined?(Rational::Unify) + c = Rational(11) + c2 = Rational(3) + + assert_equal(2, c.remainder(c2)) + assert_equal(2, c.remainder(-c2)) + assert_equal(-2, (-c).remainder(c2)) + assert_equal(-2, (-c).remainder(-c2)) + end + end + +=begin def test_quotrem c = Rational(1,2) c2 = Rational(2,3) @@ -696,6 +796,33 @@ end end + def test_math + assert_equal(Rational(1,2), Rational(1,2).abs) + assert_equal(Rational(1,2), Rational(-1,2).abs) + if defined?(Complex) && !Complex.instance_variable_get('@RCS_ID') + assert_equal(Rational(1,2), Rational(1,2).magnitude) + assert_equal(Rational(1,2), Rational(-1,2).magnitude) + end + + assert_equal(1, Rational(1,2).numerator) + assert_equal(2, Rational(1,2).denominator) + end + + def test_trunc + [[Rational(13, 5), [ 2, 3, 2, 3]], # 2.6 + [Rational(5, 2), [ 2, 3, 2, 3]], # 2.5 + [Rational(12, 5), [ 2, 3, 2, 2]], # 2.4 + [Rational(-12,5), [-3, -2, -2, -2]], # -2.4 + [Rational(-5, 2), [-3, -2, -2, -3]], # -2.5 + [Rational(-13, 5), [-3, -2, -2, -3]], # -2.6 + ].each do |i, a| + assert_equal(a[0], i.floor) + assert_equal(a[1], i.ceil) + assert_equal(a[2], i.truncate) + assert_equal(a[3], i.round) + end + end + def test_to_s c = Rational(1,2) @@ -739,6 +866,7 @@ def test_parse assert_equal(Rational(0), ''.to_r) assert_equal(Rational(0), ' '.to_r) + assert_equal(Rational(5), "\f\n\r\t\v5\0".to_r) assert_equal(Rational(5), '5'.to_r) assert_equal(Rational(-5), '-5'.to_r) assert_equal(Rational(5,3), '5/3'.to_r) @@ -816,6 +944,7 @@ assert_raise(ArgumentError){ Rational('5/3_')} assert_raise(ArgumentError){ Rational('5/3.3')} assert_raise(ArgumentError){ Rational('5/3x')} + assert_raise(ArgumentError){ Rational("5\0")} end =begin @@ -885,48 +1014,27 @@ assert_eql(c, c.prec(Rational)) end + def test_gcdlcm + assert_equal(7, 91.gcd(-49)) + assert_equal(5, 5.gcd(0)) + assert_equal(5, 0.gcd(5)) + assert_equal(70, 14.lcm(35)) + assert_equal(0, 5.lcm(0)) + assert_equal(0, 0.lcm(5)) + assert_equal([5,0], 0.gcdlcm(5)) + assert_equal([5,0], 5.gcdlcm(0)) + + assert_equal(1, 1073741827.gcd(1073741789)) + assert_equal(1152921470247108503, 1073741827.lcm(1073741789)) + + assert_equal(1, 1073741789.gcd(1073741827)) + assert_equal(1152921470247108503, 1073741789.lcm(1073741827)) + end + def test_supp assert_equal(true, 1.scalar?) assert_equal(true, 1.1.scalar?) - if defined?(Complex) - assert_equal(1, 1.real) - assert_equal(0, 1.image) - assert_equal(0, 1.imag) - - assert_equal(1.1, 1.1.real) - assert_equal(0, 1.1.image) - assert_equal(0, 1.1.imag) - - assert_equal(0, 1.arg) - assert_equal(0, 1.angle) - - assert_equal(0, 1.0.arg) - assert_equal(0, 1.0.angle) - - assert_equal(Math::PI, -1.arg) - assert_equal(Math::PI, -1.angle) - - assert_equal(Math::PI, -1.0.arg) - assert_equal(Math::PI, -1.0.angle) - - assert_equal([1,0], 1.polar) - assert_equal([1, Math::PI], -1.polar) - - assert_equal([1.0,0], 1.0.polar) - assert_equal([1.0, Math::PI], -1.0.polar) - - assert_equal(1, 1.conjugate) - assert_equal(-1, -1.conjugate) - assert_equal(1, 1.conj) - assert_equal(-1, -1.conj) - - assert_equal(1.1, 1.1.conjugate) - assert_equal(-1.1, -1.1.conjugate) - assert_equal(1.1, 1.1.conj) - assert_equal(-1.1, -1.1.conj) - end - assert_equal(1, 1.numerator) assert_equal(9, 9.numerator) assert_equal(1, 1.denominator) @@ -955,110 +1063,6 @@ assert_equal(0.25, Rational(1,2).fdiv(2)) end -=begin - def test_zero_div - assert_raise(ZeroDivisionError) { Rational(1, 0) } - assert_raise(ZeroDivisionError) { Rational(1, 1) / 0 } - end - - def test_gcd - assert_equal(0, Rational(0, 2**100)) - end - - def test_unify2 - f = defined?(Rational::Unify) - Rational.const_set(:Unify, true) unless f - - assert_same(42, Rational(84, 2)) - assert_same(1, Rational(1, 2) + Rational(1, 2)) - - Rational.instance_eval { remove_const(:Unify) } unless f - end - - def test_coerce - r = Rational(7, 3) - assert_equal(Rational(42, 1), r.coerce(42).first) - assert_raise(TypeError) { r.coerce(Object.new) } - - o = Object.new - def o.coerce(x); [x.numerator, x.denominator]; end - assert_equal(10, r + o) - assert_equal(4, r - o) - assert_equal(21, r * o) - assert_equal(2, r / o) - assert_equal(343, r ** o) - assert_equal(1, r <=> o) - - b = 2**100 - def b.<=>(x); 0; end rescue nil - assert_equal(1, r ** b) - b = 2**100 - def b.**(x); -1; end rescue nil - assert_equal(-1, Rational(1, b)**3) - end - - def test_modulo_remainder - assert_equal(Rational(1, 2), Rational(5, 2).modulo(1)) - assert_equal(Rational(1, 2), Rational(5, 2).modulo(2)) - assert_equal(Rational(5, 2), Rational(5, 2).modulo(3)) - assert_equal(Rational(5, 6), Rational(5, 2).modulo(Rational(5, 3))) - assert_equal(Rational(1, 2), Rational(-5, 2).modulo(1)) - assert_equal(Rational(-1, 2), Rational(5, 2).modulo(-1)) - assert_equal(Rational(-1, 2), Rational(-5, 2).modulo(-1)) - - assert_equal(Rational(1, 2), Rational(5, 2).remainder(1)) - assert_equal(Rational(1, 2), Rational(5, 2).remainder(2)) - assert_equal(Rational(5, 2), Rational(5, 2).remainder(3)) - assert_equal(Rational(5, 6), Rational(5, 2).remainder(Rational(5, 3))) - assert_equal(Rational(-1, 2), Rational(-5, 2).remainder(1)) - assert_equal(Rational(1, 2), Rational(5, 2).remainder(-1)) - assert_equal(Rational(-1, 2), Rational(-5, 2).remainder(-1)) - end - - def test_abs - assert_equal(Rational(1, 2), Rational(1, 2).abs) - assert_equal(Rational(1, 2), Rational(-1, 2).abs) - end - - def test_floor_ceil_truncate_round - assert_equal( 2, Rational( 5, 2).floor) - assert_equal(-3, Rational(-5, 2).floor) - assert_equal( 3, Rational( 5, 2).ceil) - assert_equal(-2, Rational(-5, 2).ceil) - assert_equal( 2, Rational( 5, 2).truncate) - assert_equal(-2, Rational(-5, 2).truncate) - assert_equal( 3, Rational( 5, 2).round) - assert_equal(-3, Rational(-5, 2).round) - assert_equal( 1, Rational( 4, 3).round) - assert_equal(-1, Rational(-4, 3).round) - assert_equal( 2, Rational( 5, 3).round) - assert_equal(-2, Rational(-5, 3).round) - end - - def test_convert - assert_equal(Rational(1, 2), Rational(Complex(1, 0), 2)) - assert_raise(RangeError) { Rational(Complex(1, 1), 1) } - assert_equal(Rational(1, 2), Rational(1, Complex(2, 0))) - assert_raise(RangeError) { Rational(1, Complex(2, 1)) } - assert_equal(Rational(1, 2), Rational(0.25, 0.5)) - assert_equal(Rational(1, 2), Rational('1', '2')) - end - - def test_add2 - assert_equal(Rational(2**100, 3), Rational(0, 1) + Rational(2**100, 3)) - assert_equal(Rational(2, 3**100), Rational(0, 1) + Rational(2, 3**100)) - end - - def test_div2 - assert_raise(ZeroDivisionError) { Rational(1, 1) / Rational(0, 1) } - end - - def test_to_f2 - assert_equal(1, Rational(2**5000,3).to_f.infinite?) - assert_equal(0, Rational(1, 2**5000).to_f) - end -=end - def test_fixed_bug if defined?(Rational::Unify) assert_instance_of(Fixnum, Rational(1,2) ** 0) # mathn's bug Index: mvm/rational.c =================================================================== --- mvm/rational.c (revision 18938) +++ mvm/rational.c (revision 18939) @@ -225,8 +225,6 @@ inline static long i_gcd(long x, long y) { - long b; - if (x < 0) x = -x; if (y < 0) @@ -237,32 +235,12 @@ if (y == 0) return x; - b = 0; - while ((x & 1) == 0 && (y & 1) == 0) { - b += 1; - x >>= 1; - y >>= 1; + while (x > 0) { + long t = x; + x = y % x; + y = t; } - - while ((x & 1) == 0) - x >>= 1; - - while ((y & 1) == 0) - y >>= 1; - - while (x != y) { - if (y > x) { - long t; - t = x; - x = y; - y = t; - } - x -= y; - while ((x & 1) == 0) - x >>= 1; - } - - return x << b; + return y; } inline static VALUE @@ -1234,10 +1212,11 @@ #define underscores_pat *rb_vm_specific_ptr(vmkey_underscores_pat) #define an_underscore *rb_vm_specific_ptr(vmkey_an_underscore) +#define WS "\\s*" #define DIGITS "(?:\\d(?:_\\d|\\d)*)" #define NUMERATOR "(?:" DIGITS "?\\.)?" DIGITS "(?:[eE][-+]?" DIGITS ")?" #define DENOMINATOR DIGITS -#define PATTERN "\\A([-+])?(" NUMERATOR ")(?:\\/(" DENOMINATOR "))?" +#define PATTERN "\\A" WS "([-+])?(" NUMERATOR ")(?:\\/(" DENOMINATOR "))?" WS static void make_patterns(void) @@ -1272,9 +1251,6 @@ rb_global_variable(&an_underscore); } -#define id_strip rb_intern("strip") -#define f_strip(x) rb_funcall(x, id_strip, 0) - #define id_match rb_intern("match") #define f_match(x,y) rb_funcall(x, id_match, 1, y) @@ -1294,7 +1270,7 @@ { VALUE s, m; - s = f_strip(self); + s = self; if (RSTRING_LEN(s) == 0) return rb_assoc_new(Qnil, self); Index: mvm/file.c =================================================================== --- mvm/file.c (revision 18938) +++ mvm/file.c (revision 18939) @@ -2976,9 +2976,7 @@ */ VALUE -rb_file_s_absolute_path(argc, argv) - int argc; - VALUE *argv; +rb_file_s_absolute_path(int argc, VALUE *argv) { VALUE fname, dname; Index: mvm/transcode.c =================================================================== --- mvm/transcode.c (revision 18938) +++ mvm/transcode.c (revision 18939) @@ -2506,6 +2506,17 @@ * puts ec.convert("\u3042").dump #=> "\xA4\xA2" * puts ec.finish.dump #=> "" * + * ec = Encoding::Converter.new("euc-jp", "utf-8") + * puts ec.convert("\xA4").dump #=> "" + * puts ec.convert("\xA2").dump #=> "\xE3\x81\x82" + * puts ec.finish.dump #=> "" + * + * ec = Encoding::Converter.new("utf-8", "iso-2022-jp") + * puts ec.convert("\xE3").dump #=> "".force_encoding("ISO-2022-JP") + * puts ec.convert("\x81").dump #=> "".force_encoding("ISO-2022-JP") + * puts ec.convert("\x82").dump #=> "\e$B$\"".force_encoding("ISO-2022-JP") + * puts ec.finish.dump #=> "\e(B".force_encoding("ISO-2022-JP") + * * If a conversion error occur, * Encoding::ConversionUndefined or * Encoding::InvalidByteSequence is raised. -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/