mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	enumerator.c: fix for non-integral argument for ArithmeticSequence#last
This fixes a bug of Enumerator::ArithmeticSequence#last in the case that a non-integral argument is passed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64262 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									3ef25ed755
								
							
						
					
					
						commit
						33791cd092
					
				
					 2 changed files with 27 additions and 0 deletions
				
			
		| 
						 | 
					@ -2495,6 +2495,9 @@ arith_seq_last(int argc, VALUE *argv, VALUE self)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    rb_scan_args(argc, argv, "1", &nv);
 | 
					    rb_scan_args(argc, argv, "1", &nv);
 | 
				
			||||||
 | 
					    if (!RB_INTEGER_TYPE_P(nv)) {
 | 
				
			||||||
 | 
					        nv = rb_to_int(nv);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    if (RTEST(rb_int_gt(nv, len))) {
 | 
					    if (RTEST(rb_int_gt(nv, len))) {
 | 
				
			||||||
        nv = len;
 | 
					        nv = len;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -195,6 +195,30 @@ class TestArithmeticSequence < Test::Unit::TestCase
 | 
				
			||||||
    assert_equal([2.0, 0.0, -2.0], seq.last(3))
 | 
					    assert_equal([2.0, 0.0, -2.0], seq.last(3))
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def test_last_with_float
 | 
				
			||||||
 | 
					    res = (1..3).step(2).last(2.0)
 | 
				
			||||||
 | 
					    assert_equal([1, 3], res)
 | 
				
			||||||
 | 
					    assert_instance_of Integer, res[0]
 | 
				
			||||||
 | 
					    assert_instance_of Integer, res[1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    res = (1..3).step(2).last(5.0)
 | 
				
			||||||
 | 
					    assert_equal([1, 3], res)
 | 
				
			||||||
 | 
					    assert_instance_of Integer, res[0]
 | 
				
			||||||
 | 
					    assert_instance_of Integer, res[1]
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def test_last_with_rational
 | 
				
			||||||
 | 
					    res = (1..3).step(2).last(2r)
 | 
				
			||||||
 | 
					    assert_equal([1, 3], res)
 | 
				
			||||||
 | 
					    assert_instance_of Integer, res[0]
 | 
				
			||||||
 | 
					    assert_instance_of Integer, res[1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    res = (1..3).step(2).last(10/2r)
 | 
				
			||||||
 | 
					    assert_equal([1, 3], res)
 | 
				
			||||||
 | 
					    assert_instance_of Integer, res[0]
 | 
				
			||||||
 | 
					    assert_instance_of Integer, res[1]
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def test_to_a
 | 
					  def test_to_a
 | 
				
			||||||
    assert_equal([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 1.step(10).to_a)
 | 
					    assert_equal([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 1.step(10).to_a)
 | 
				
			||||||
    assert_equal([1, 3, 5, 7, 9], 1.step(10, 2).to_a)
 | 
					    assert_equal([1, 3, 5, 7, 9], 1.step(10, 2).to_a)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue