Project

General

Profile

Feature #14550 ยป date-step.patch

ksss (Yuki Kurihara), 02/24/2018 02:34 PM

View differences:

ext/date/date_core.c
20 20

  
21 21
#define USE_PACK
22 22

  
23
static ID id_cmp, id_le_p, id_ge_p, id_eqeq_p;
23
static ID id_cmp, id_le_p, id_ge_p, id_eqeq_p, id_to, id_by;
24 24
static VALUE cDate, cDateTime;
25 25
static VALUE half_days_in_day, day_in_nanoseconds;
26 26
static double positive_inf, negative_inf;
......
6153 6153
static VALUE
6154 6154
d_lite_step(int argc, VALUE *argv, VALUE self)
6155 6155
{
6156
    VALUE limit, step, date;
6156
    VALUE limit, step, hash, date;
6157 6157

  
6158
    rb_scan_args(argc, argv, "11", &limit, &step);
6158
    RETURN_ENUMERATOR(self, argc, argv);
6159 6159

  
6160
    if (argc < 2)
6160
    argc = rb_scan_args(argc, argv, "02:", &limit, &step, &hash);
6161

  
6162
    if (!NIL_P(hash)) {
6163
	ID keys[2];
6164
	VALUE values[2];
6165
	keys[0] = id_to;
6166
	keys[1] = id_by;
6167
	rb_get_kwargs(hash, keys, 0, 2, values);
6168
	if (values[0] != Qundef) {
6169
	    if (argc > 0) rb_raise(rb_eArgError, "to is given twice");
6170
	    limit = values[0];
6171
	}
6172
	if (values[1] != Qundef) {
6173
	    if (argc > 1) rb_raise(rb_eArgError, "step is given twice");
6174
	    step = values[1];
6175
	}
6176
    }
6177

  
6178
    if (NIL_P(step)) {
6161 6179
	step = INT2FIX(1);
6180
    }
6162 6181

  
6163 6182
#if 0
6164 6183
    if (f_zero_p(step))
6165 6184
	rb_raise(rb_eArgError, "step can't be 0");
6166 6185
#endif
6167 6186

  
6168
    RETURN_ENUMERATOR(self, argc, argv);
6169

  
6170 6187
    date = self;
6171 6188
    switch (FIX2INT(f_cmp(step, INT2FIX(0)))) {
6172 6189
      case -1:
......
9664 9681
			       date_s_test_unit_conv, 0);
9665 9682
    de_define_singleton_method(cDate, "test_all", date_s_test_all, 0);
9666 9683
#endif
9684

  
9685
    id_to = rb_intern("to");
9686
    id_by = rb_intern("by");
9667 9687
}
9668 9688

  
9669 9689
/*
test/date/test_date_arith.rb
245 245
    end
246 246
    assert_equal(4, i)
247 247

  
248
    i = 0
249
    p.step(q, by: 2) do
250
      i += 1
251
    end
252
    assert_equal(4, i)
253

  
254
    i = 0
255
    p.step(to: q, by: 2) do
256
      i += 1
257
    end
258
    assert_equal(4, i)
259

  
248 260
    i = 0
249 261
    p.step(q) do
250 262
      i += 1
251 263
    end
252 264
    assert_equal(8, i)
265

  
266
    i = 0
267
    p.step(to: q) do
268
      i += 1
269
    end
270
    assert_equal(8, i)
271

  
272
    assert_raise(ArgumentError) { p.step(q, to: q) { } }
273
    assert_raise(ArgumentError) { p.step(q, 1, by: 1) { } }
274
    assert_raise(ArgumentError) { p.step(q, foo: nil) { } }
253 275
  end
254 276

  
255 277
  def test_step__noblock
256 278
    p = Date.new(2001,1,14)
257 279
    q = Date.new(2001,1,21)
280

  
258 281
    e = p.step(q, 2)
259 282
    assert_equal(4, e.to_a.size)
260 283

  
284
    e = p.step(q, by: 2)
285
    assert_equal(4, e.to_a.size)
286

  
287
    e = p.step(to: q, by: 2)
288
    assert_equal(4, e.to_a.size)
289

  
261 290
    e = p.step(q)
262 291
    assert_equal(8, e.to_a.size)
292

  
293
    e = p.step(to: q)
294
    assert_equal(8, e.to_a.size)
263 295
  end
264 296

  
265 297
end