зеркало из https://github.com/github/ruby.git
Fix Array#[] with ArithmeticSequence with negative steps (#5739)
* Fix Array#[] with ArithmeticSequence with negative steps Previously, Array#[] when called with an ArithmeticSequence with a negative step did not handle all cases correctly, especially cases involving infinite ranges, inverted ranges, and/or exclusive ends. Fixes [Bug #18247] * Add Array#slice tests for ArithmeticSequence with negative step to test_array Add tests of rb_arithmetic_sequence_beg_len_step C-API function. * Fix ext/-test-/arith_seq/beg_len_step/depend * Rename local variables * Fix a variable name Co-authored-by: Kenta Murata <3959+mrkn@users.noreply.github.com>
This commit is contained in:
Родитель
32d1ce96e0
Коммит
cfb9624460
5
array.c
5
array.c
|
@ -1373,13 +1373,16 @@ ary_make_partial_step(VALUE ary, VALUE klass, long offset, long len, long step)
|
|||
const VALUE *values = RARRAY_CONST_PTR_TRANSIENT(ary);
|
||||
const long orig_len = len;
|
||||
|
||||
if ((step > 0 && step >= len) || (step < 0 && (step < -len))) {
|
||||
if (step > 0 && step >= len) {
|
||||
VALUE result = ary_new(klass, 1);
|
||||
VALUE *ptr = (VALUE *)ARY_EMBED_PTR(result);
|
||||
RB_OBJ_WRITE(result, ptr, values[offset]);
|
||||
ARY_SET_EMBED_LEN(result, 1);
|
||||
return result;
|
||||
}
|
||||
else if (step < 0 && step < -len) {
|
||||
step = -len;
|
||||
}
|
||||
|
||||
long ustep = (step < 0) ? -step : step;
|
||||
len = (len + ustep - 1) / ustep;
|
||||
|
|
|
@ -3802,6 +3802,13 @@ rb_arithmetic_sequence_beg_len_step(VALUE obj, long *begp, long *lenp, long *ste
|
|||
*stepp = step;
|
||||
|
||||
if (step < 0) {
|
||||
if (aseq.exclude_end && !NIL_P(aseq.end)) {
|
||||
/* Handle exclusion before range reversal */
|
||||
aseq.end = LONG2NUM(NUM2LONG(aseq.end) + 1);
|
||||
|
||||
/* Don't exclude the previous beginning */
|
||||
aseq.exclude_end = 0;
|
||||
}
|
||||
VALUE tmp = aseq.begin;
|
||||
aseq.begin = aseq.end;
|
||||
aseq.end = tmp;
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
#include "ruby/ruby.h"
|
||||
|
||||
static VALUE
|
||||
arith_seq_s_beg_len_step(VALUE mod, VALUE obj, VALUE len, VALUE err)
|
||||
{
|
||||
VALUE r;
|
||||
long beg, len2, step;
|
||||
|
||||
r = rb_arithmetic_sequence_beg_len_step(obj, &beg, &len2, &step, NUM2LONG(len), NUM2INT(err));
|
||||
|
||||
return rb_ary_new_from_args(4, r, LONG2NUM(beg), LONG2NUM(len2), LONG2NUM(step));
|
||||
}
|
||||
|
||||
void
|
||||
Init_beg_len_step(void)
|
||||
{
|
||||
VALUE cArithSeq = rb_path2class("Enumerator::ArithmeticSequence");
|
||||
rb_define_singleton_method(cArithSeq, "__beg_len_step__", arith_seq_s_beg_len_step, 3);
|
||||
}
|
|
@ -0,0 +1,161 @@
|
|||
# AUTOGENERATED DEPENDENCIES START
|
||||
beg_len_step.o: $(RUBY_EXTCONF_H)
|
||||
beg_len_step.o: $(arch_hdrdir)/ruby/config.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/assert.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/backward.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/backward/2/assume.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/backward/2/attributes.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/backward/2/bool.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/backward/2/inttypes.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/backward/2/limits.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/backward/2/long_long.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/backward/2/stdalign.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/backward/2/stdarg.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/defines.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/intern.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/abi.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/anyargs.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/char.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/double.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/fixnum.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/gid_t.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/int.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/intptr_t.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/long.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/long_long.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/mode_t.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/off_t.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/pid_t.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/short.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/size_t.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/st_data_t.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/arithmetic/uid_t.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/assume.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/alloc_size.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/artificial.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/cold.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/const.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/constexpr.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/deprecated.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/diagnose_if.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/enum_extensibility.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/error.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/flag_enum.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/forceinline.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/format.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/maybe_unused.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/noalias.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/nodiscard.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/noexcept.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/noinline.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/nonnull.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/noreturn.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/pure.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/restrict.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/returns_nonnull.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/warning.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/attr/weakref.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/cast.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/compiler_is.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/compiler_is/apple.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/compiler_is/clang.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/compiler_is/gcc.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/compiler_is/intel.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/compiler_is/msvc.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/compiler_is/sunpro.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/compiler_since.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/config.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/constant_p.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core/rarray.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core/rbasic.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core/rbignum.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core/rclass.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core/rdata.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core/rfile.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core/rhash.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core/robject.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core/rregexp.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core/rstring.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core/rstruct.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/core/rtypeddata.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/ctype.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/dllexport.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/dosish.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/error.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/eval.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/event.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/fl_type.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/gc.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/glob.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/globals.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/has/attribute.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/has/builtin.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/has/c_attribute.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/has/cpp_attribute.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/has/declspec_attribute.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/has/extension.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/has/feature.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/has/warning.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/array.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/bignum.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/class.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/compar.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/complex.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/cont.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/dir.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/enum.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/enumerator.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/error.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/eval.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/file.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/gc.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/hash.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/io.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/load.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/marshal.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/numeric.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/object.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/parse.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/proc.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/process.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/random.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/range.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/rational.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/re.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/ruby.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/select.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/select/largesize.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/signal.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/sprintf.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/string.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/struct.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/thread.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/time.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/variable.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/intern/vm.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/interpreter.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/iterator.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/memory.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/method.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/module.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/newobj.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/rgengc.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/scan_args.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/special_consts.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/static_assert.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/stdalign.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/stdbool.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/symbol.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/value.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/value_type.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/variable.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/warning_push.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/internal/xmalloc.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/missing.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/ruby.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/st.h
|
||||
beg_len_step.o: $(hdrdir)/ruby/subst.h
|
||||
beg_len_step.o: beg_len_step.c
|
||||
# AUTOGENERATED DEPENDENCIES END
|
|
@ -0,0 +1,2 @@
|
|||
# frozen_string_literal: false
|
||||
create_makefile("-test-/arith_seq/beg_len_step")
|
|
@ -784,6 +784,102 @@ describe :array_slice, shared: true do
|
|||
a.send(@method, (...-9)).should == []
|
||||
end
|
||||
|
||||
ruby_version_is "3.2" do
|
||||
describe "can be sliced with Enumerator::ArithmeticSequence" do
|
||||
it "with infinite/inverted ranges and negative steps" do
|
||||
@array = [0, 1, 2, 3, 4, 5]
|
||||
@array.send(@method, (2..).step(-1)).should == [2, 1, 0]
|
||||
@array.send(@method, (2..).step(-2)).should == [2, 0]
|
||||
@array.send(@method, (2..).step(-3)).should == [2]
|
||||
@array.send(@method, (2..).step(-4)).should == [2]
|
||||
|
||||
@array.send(@method, (-3..).step(-1)).should == [3, 2, 1, 0]
|
||||
@array.send(@method, (-3..).step(-2)).should == [3, 1]
|
||||
@array.send(@method, (-3..).step(-3)).should == [3, 0]
|
||||
@array.send(@method, (-3..).step(-4)).should == [3]
|
||||
@array.send(@method, (-3..).step(-5)).should == [3]
|
||||
|
||||
@array.send(@method, (..0).step(-1)).should == [5, 4, 3, 2, 1, 0]
|
||||
@array.send(@method, (..0).step(-2)).should == [5, 3, 1]
|
||||
@array.send(@method, (..0).step(-3)).should == [5, 2]
|
||||
@array.send(@method, (..0).step(-4)).should == [5, 1]
|
||||
@array.send(@method, (..0).step(-5)).should == [5, 0]
|
||||
@array.send(@method, (..0).step(-6)).should == [5]
|
||||
@array.send(@method, (..0).step(-7)).should == [5]
|
||||
|
||||
@array.send(@method, (...0).step(-1)).should == [5, 4, 3, 2, 1]
|
||||
@array.send(@method, (...0).step(-2)).should == [5, 3, 1]
|
||||
@array.send(@method, (...0).step(-3)).should == [5, 2]
|
||||
@array.send(@method, (...0).step(-4)).should == [5, 1]
|
||||
@array.send(@method, (...0).step(-5)).should == [5]
|
||||
@array.send(@method, (...0).step(-6)).should == [5]
|
||||
|
||||
@array.send(@method, (...1).step(-1)).should == [5, 4, 3, 2]
|
||||
@array.send(@method, (...1).step(-2)).should == [5, 3]
|
||||
@array.send(@method, (...1).step(-3)).should == [5, 2]
|
||||
@array.send(@method, (...1).step(-4)).should == [5]
|
||||
@array.send(@method, (...1).step(-5)).should == [5]
|
||||
|
||||
@array.send(@method, (..-5).step(-1)).should == [5, 4, 3, 2, 1]
|
||||
@array.send(@method, (..-5).step(-2)).should == [5, 3, 1]
|
||||
@array.send(@method, (..-5).step(-3)).should == [5, 2]
|
||||
@array.send(@method, (..-5).step(-4)).should == [5, 1]
|
||||
@array.send(@method, (..-5).step(-5)).should == [5]
|
||||
@array.send(@method, (..-5).step(-6)).should == [5]
|
||||
|
||||
@array.send(@method, (...-5).step(-1)).should == [5, 4, 3, 2]
|
||||
@array.send(@method, (...-5).step(-2)).should == [5, 3]
|
||||
@array.send(@method, (...-5).step(-3)).should == [5, 2]
|
||||
@array.send(@method, (...-5).step(-4)).should == [5]
|
||||
@array.send(@method, (...-5).step(-5)).should == [5]
|
||||
|
||||
@array.send(@method, (4..1).step(-1)).should == [4, 3, 2, 1]
|
||||
@array.send(@method, (4..1).step(-2)).should == [4, 2]
|
||||
@array.send(@method, (4..1).step(-3)).should == [4, 1]
|
||||
@array.send(@method, (4..1).step(-4)).should == [4]
|
||||
@array.send(@method, (4..1).step(-5)).should == [4]
|
||||
|
||||
@array.send(@method, (4...1).step(-1)).should == [4, 3, 2]
|
||||
@array.send(@method, (4...1).step(-2)).should == [4, 2]
|
||||
@array.send(@method, (4...1).step(-3)).should == [4]
|
||||
@array.send(@method, (4...1).step(-4)).should == [4]
|
||||
|
||||
@array.send(@method, (-2..1).step(-1)).should == [4, 3, 2, 1]
|
||||
@array.send(@method, (-2..1).step(-2)).should == [4, 2]
|
||||
@array.send(@method, (-2..1).step(-3)).should == [4, 1]
|
||||
@array.send(@method, (-2..1).step(-4)).should == [4]
|
||||
@array.send(@method, (-2..1).step(-5)).should == [4]
|
||||
|
||||
@array.send(@method, (-2...1).step(-1)).should == [4, 3, 2]
|
||||
@array.send(@method, (-2...1).step(-2)).should == [4, 2]
|
||||
@array.send(@method, (-2...1).step(-3)).should == [4]
|
||||
@array.send(@method, (-2...1).step(-4)).should == [4]
|
||||
|
||||
@array.send(@method, (4..-5).step(-1)).should == [4, 3, 2, 1]
|
||||
@array.send(@method, (4..-5).step(-2)).should == [4, 2]
|
||||
@array.send(@method, (4..-5).step(-3)).should == [4, 1]
|
||||
@array.send(@method, (4..-5).step(-4)).should == [4]
|
||||
@array.send(@method, (4..-5).step(-5)).should == [4]
|
||||
|
||||
@array.send(@method, (4...-5).step(-1)).should == [4, 3, 2]
|
||||
@array.send(@method, (4...-5).step(-2)).should == [4, 2]
|
||||
@array.send(@method, (4...-5).step(-3)).should == [4]
|
||||
@array.send(@method, (4...-5).step(-4)).should == [4]
|
||||
|
||||
@array.send(@method, (-2..-5).step(-1)).should == [4, 3, 2, 1]
|
||||
@array.send(@method, (-2..-5).step(-2)).should == [4, 2]
|
||||
@array.send(@method, (-2..-5).step(-3)).should == [4, 1]
|
||||
@array.send(@method, (-2..-5).step(-4)).should == [4]
|
||||
@array.send(@method, (-2..-5).step(-5)).should == [4]
|
||||
|
||||
@array.send(@method, (-2...-5).step(-1)).should == [4, 3, 2]
|
||||
@array.send(@method, (-2...-5).step(-2)).should == [4, 2]
|
||||
@array.send(@method, (-2...-5).step(-3)).should == [4]
|
||||
@array.send(@method, (-2...-5).step(-4)).should == [4]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "can accept nil...nil ranges" do
|
||||
a = [0, 1, 2, 3, 4, 5]
|
||||
a.send(@method, eval("(nil...nil)")).should == a
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
# frozen_string_literal: false
|
||||
require 'test/unit'
|
||||
|
||||
class Test_ArithSeq < Test::Unit::TestCase
|
||||
def test_beg_len_step
|
||||
assert_separately([], <<-"end;") #do
|
||||
require '-test-/arith_seq/beg_len_step'
|
||||
|
||||
r, = Enumerator::ArithmeticSequence.__beg_len_step__([1, 2, 3], 0, 0)
|
||||
assert_equal(false, r)
|
||||
|
||||
r, = Enumerator::ArithmeticSequence.__beg_len_step__([1, 2, 3], 1, 0)
|
||||
assert_equal(false, r)
|
||||
|
||||
r, = Enumerator::ArithmeticSequence.__beg_len_step__([1, 2, 3], 3, 0)
|
||||
assert_equal(false, r)
|
||||
|
||||
r, = Enumerator::ArithmeticSequence.__beg_len_step__(1..3, 0, 0)
|
||||
assert_equal(nil, r)
|
||||
|
||||
r = Enumerator::ArithmeticSequence.__beg_len_step__(1..3, 1, 0)
|
||||
assert_equal([true, 1, 0, 1], r)
|
||||
|
||||
r = Enumerator::ArithmeticSequence.__beg_len_step__(1..3, 2, 0)
|
||||
assert_equal([true, 1, 1, 1], r)
|
||||
|
||||
r = Enumerator::ArithmeticSequence.__beg_len_step__(1..3, 3, 0)
|
||||
assert_equal([true, 1, 2, 1], r)
|
||||
|
||||
r = Enumerator::ArithmeticSequence.__beg_len_step__(1..3, 4, 0)
|
||||
assert_equal([true, 1, 3, 1], r)
|
||||
|
||||
r = Enumerator::ArithmeticSequence.__beg_len_step__(1..3, 5, 0)
|
||||
assert_equal([true, 1, 3, 1], r)
|
||||
|
||||
r = Enumerator::ArithmeticSequence.__beg_len_step__((-10..10).step(2), 24, 0)
|
||||
assert_equal([true, 14, 0, 2], r)
|
||||
|
||||
r = Enumerator::ArithmeticSequence.__beg_len_step__((-10..10).step(3), 24, 0)
|
||||
assert_equal([true, 14, 0, 3], r)
|
||||
|
||||
r = Enumerator::ArithmeticSequence.__beg_len_step__((-10..10).step(3), 22, 0)
|
||||
assert_equal([true, 12, 0, 3], r)
|
||||
|
||||
r = Enumerator::ArithmeticSequence.__beg_len_step__((-10..10).step(-3), 22, 0)
|
||||
assert_equal([true, 10, 3, -3], r)
|
||||
|
||||
r = Enumerator::ArithmeticSequence.__beg_len_step__(1..3, 0, 1)
|
||||
assert_equal([true, 1, 3, 1], r)
|
||||
end;
|
||||
end
|
||||
end
|
|
@ -1580,6 +1580,96 @@ class TestArray < Test::Unit::TestCase
|
|||
assert_equal_instance(a.values_at(*idx), a.slice((3..90)%2))
|
||||
idx = 90.step(3, -2).to_a
|
||||
assert_equal_instance(a.values_at(*idx), a.slice((90 .. 3)% -2))
|
||||
|
||||
a = [0, 1, 2, 3, 4, 5]
|
||||
assert_equal([2, 1, 0], a.slice((2..).step(-1)))
|
||||
assert_equal([2, 0], a.slice((2..).step(-2)))
|
||||
assert_equal([2], a.slice((2..).step(-3)))
|
||||
assert_equal([2], a.slice((2..).step(-4)))
|
||||
|
||||
assert_equal([3, 2, 1, 0], a.slice((-3..).step(-1)))
|
||||
assert_equal([3, 1], a.slice((-3..).step(-2)))
|
||||
assert_equal([3, 0], a.slice((-3..).step(-3)))
|
||||
assert_equal([3], a.slice((-3..).step(-4)))
|
||||
assert_equal([3], a.slice((-3..).step(-5)))
|
||||
|
||||
assert_equal([5, 4, 3, 2, 1, 0], a.slice((..0).step(-1)))
|
||||
assert_equal([5, 3, 1], a.slice((..0).step(-2)))
|
||||
assert_equal([5, 2], a.slice((..0).step(-3)))
|
||||
assert_equal([5, 1], a.slice((..0).step(-4)))
|
||||
assert_equal([5, 0], a.slice((..0).step(-5)))
|
||||
assert_equal([5], a.slice((..0).step(-6)))
|
||||
assert_equal([5], a.slice((..0).step(-7)))
|
||||
|
||||
assert_equal([5, 4, 3, 2, 1], a.slice((...0).step(-1)))
|
||||
assert_equal([5, 3, 1], a.slice((...0).step(-2)))
|
||||
assert_equal([5, 2], a.slice((...0).step(-3)))
|
||||
assert_equal([5, 1], a.slice((...0).step(-4)))
|
||||
assert_equal([5], a.slice((...0).step(-5)))
|
||||
assert_equal([5], a.slice((...0).step(-6)))
|
||||
|
||||
assert_equal([5, 4, 3, 2], a.slice((...1).step(-1)))
|
||||
assert_equal([5, 3], a.slice((...1).step(-2)))
|
||||
assert_equal([5, 2], a.slice((...1).step(-3)))
|
||||
assert_equal([5], a.slice((...1).step(-4)))
|
||||
assert_equal([5], a.slice((...1).step(-5)))
|
||||
|
||||
assert_equal([5, 4, 3, 2, 1], a.slice((..-5).step(-1)))
|
||||
assert_equal([5, 3, 1], a.slice((..-5).step(-2)))
|
||||
assert_equal([5, 2], a.slice((..-5).step(-3)))
|
||||
assert_equal([5, 1], a.slice((..-5).step(-4)))
|
||||
assert_equal([5], a.slice((..-5).step(-5)))
|
||||
assert_equal([5], a.slice((..-5).step(-6)))
|
||||
|
||||
assert_equal([5, 4, 3, 2], a.slice((...-5).step(-1)))
|
||||
assert_equal([5, 3], a.slice((...-5).step(-2)))
|
||||
assert_equal([5, 2], a.slice((...-5).step(-3)))
|
||||
assert_equal([5], a.slice((...-5).step(-4)))
|
||||
assert_equal([5], a.slice((...-5).step(-5)))
|
||||
|
||||
assert_equal([4, 3, 2, 1], a.slice((4..1).step(-1)))
|
||||
assert_equal([4, 2], a.slice((4..1).step(-2)))
|
||||
assert_equal([4, 1], a.slice((4..1).step(-3)))
|
||||
assert_equal([4], a.slice((4..1).step(-4)))
|
||||
assert_equal([4], a.slice((4..1).step(-5)))
|
||||
|
||||
assert_equal([4, 3, 2], a.slice((4...1).step(-1)))
|
||||
assert_equal([4, 2], a.slice((4...1).step(-2)))
|
||||
assert_equal([4], a.slice((4...1).step(-3)))
|
||||
assert_equal([4], a.slice((4...1).step(-4)))
|
||||
|
||||
assert_equal([4, 3, 2, 1], a.slice((-2..1).step(-1)))
|
||||
assert_equal([4, 2], a.slice((-2..1).step(-2)))
|
||||
assert_equal([4, 1], a.slice((-2..1).step(-3)))
|
||||
assert_equal([4], a.slice((-2..1).step(-4)))
|
||||
assert_equal([4], a.slice((-2..1).step(-5)))
|
||||
|
||||
assert_equal([4, 3, 2], a.slice((-2...1).step(-1)))
|
||||
assert_equal([4, 2], a.slice((-2...1).step(-2)))
|
||||
assert_equal([4], a.slice((-2...1).step(-3)))
|
||||
assert_equal([4], a.slice((-2...1).step(-4)))
|
||||
|
||||
assert_equal([4, 3, 2, 1], a.slice((4..-5).step(-1)))
|
||||
assert_equal([4, 2], a.slice((4..-5).step(-2)))
|
||||
assert_equal([4, 1], a.slice((4..-5).step(-3)))
|
||||
assert_equal([4], a.slice((4..-5).step(-4)))
|
||||
assert_equal([4], a.slice((4..-5).step(-5)))
|
||||
|
||||
assert_equal([4, 3, 2], a.slice((4...-5).step(-1)))
|
||||
assert_equal([4, 2], a.slice((4...-5).step(-2)))
|
||||
assert_equal([4], a.slice((4...-5).step(-3)))
|
||||
assert_equal([4], a.slice((4...-5).step(-4)))
|
||||
|
||||
assert_equal([4, 3, 2, 1], a.slice((-2..-5).step(-1)))
|
||||
assert_equal([4, 2], a.slice((-2..-5).step(-2)))
|
||||
assert_equal([4, 1], a.slice((-2..-5).step(-3)))
|
||||
assert_equal([4], a.slice((-2..-5).step(-4)))
|
||||
assert_equal([4], a.slice((-2..-5).step(-5)))
|
||||
|
||||
assert_equal([4, 3, 2], a.slice((-2...-5).step(-1)))
|
||||
assert_equal([4, 2], a.slice((-2...-5).step(-2)))
|
||||
assert_equal([4], a.slice((-2...-5).step(-3)))
|
||||
assert_equal([4], a.slice((-2...-5).step(-4)))
|
||||
end
|
||||
|
||||
def test_slice_out_of_range
|
||||
|
|
Загрузка…
Ссылка в новой задаче