Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

Location:
EcnlProtoTool/trunk/mruby-2.1.1
Files:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-range-ext/test/range.rb

    r331 r439  
    1111  assert_equal 10, (10..20).first
    1212  assert_equal [10, 11, 12], (10..20).first(3)
     13
     14  skip unless Object.const_defined?(:Float)
    1315  assert_equal [0, 1, 2], (0..Float::INFINITY).first(3)
    1416end
     
    2426  assert_equal 42, (1..42).size
    2527  assert_equal 41, (1...42).size
     28  assert_nil ('a'..'z').size
     29
     30  skip unless Object.const_defined?(:Float)
    2631  assert_equal 6, (1...6.3).size
    2732  assert_equal 5, (1...6.0).size
     
    2934  assert_equal 15, (1.0..15.9).size
    3035  assert_equal Float::INFINITY, (0..Float::INFINITY).size
    31   assert_nil ('a'..'z').size
    3236end
     37
     38assert('Range#max') do
     39  # returns the maximum value in the range when called with no arguments
     40  assert_equal 10, (1..10).max
     41  assert_equal 9, (1...10).max
     42  assert_equal 536870911, (0...2**29).max
     43
     44  # returns nil when the endpoint is less than the start point
     45  assert_equal nil, (100..10).max
     46
     47  # returns nil when the endpoint equals the start point and the range is exclusive
     48  assert_equal nil, (5...5).max
     49
     50  # returns the endpoint when the endpoint equals the start point and the range is inclusive
     51  assert_equal 5, (5..5).max
     52
     53  skip unless Object.const_defined?(:Float)
     54
     55  # returns the maximum value in the Float range when called with no arguments
     56  assert_equal 908.1111, (303.20..908.1111).max
     57
     58  # raises TypeError when called on an exclusive range and a non Integer value
     59  assert_raise(TypeError) { (303.20...908.1111).max }
     60
     61  # returns nil when the endpoint is less than the start point in a Float range
     62  assert_equal nil, (3003.20..908.1111).max
     63end
     64
     65assert('Range#max given a block') do
     66  # passes each pair of values in the range to the block
     67  acc = []
     68  (1..10).max do |a, b|
     69    acc << a
     70    acc << b
     71    a
     72  end
     73  (1..10).each do |value|
     74    assert_true acc.include?(value)
     75  end
     76
     77  # passes each pair of elements to the block in reversed order
     78  acc = []
     79  (1..5).max do |a, b|
     80    acc << [a, b]
     81    a
     82  end
     83  assert_equal [[2, 1], [3, 2], [4, 3], [5, 4]], acc
     84
     85  # returns the element the block determines to be the maximum
     86  assert_equal 1, ((1..3).max { |_a, _b| -3 })
     87
     88  # returns nil when the endpoint is less than the start point
     89  assert_equal nil, ((100..10).max { |x, y| x <=> y })
     90  assert_equal nil, ((5...5).max { |x, y| x <=> y })
     91end
     92
     93assert('Range#min') do
     94  # returns the minimum value in the range when called with no arguments
     95  assert_equal 1, (1..10).min
     96  assert_equal 1, (1...10).min
     97
     98  # returns nil when the start point is greater than the endpoint
     99  assert_equal nil, (100..10).min
     100
     101  # returns nil when the endpoint equals the start point and the range is exclusive
     102  assert_equal nil, (5...5).max
     103
     104  # returns the endpoint when the endpoint equals the start point and the range is inclusive
     105  assert_equal 5, (5..5).max
     106
     107  skip unless Object.const_defined?(:Float)
     108
     109  # returns the minimum value in the Float range when called with no arguments
     110  assert_equal 303.20, (303.20..908.1111).min
     111
     112  # returns nil when the start point is greater than the endpoint in a Float range
     113  assert_equal nil, (3003.20..908.1111).max
     114end
     115
     116assert('Range#min given a block') do
     117  # passes each pair of values in the range to the block
     118  acc = []
     119  (1..10).min do |a, b|
     120    acc << a
     121    acc << b
     122    a
     123  end
     124  (1..10).each do |value|
     125    assert_true acc.include?(value)
     126  end
     127
     128  # passes each pair of elements to the block in reversed order
     129  acc = []
     130  (1..5).min do |a, b|
     131    acc << [a, b]
     132    a
     133  end
     134  assert_equal [[2, 1], [3, 1], [4, 1], [5, 1]], acc
     135
     136  # returns the element the block determines to be the minimum
     137  assert_equal 3, ((1..3).min { |_a, _b| -3 })
     138
     139  # returns nil when the start point is greater than the endpoint
     140  assert_equal nil, ((100..10).min { |x, y| x <=> y })
     141  assert_equal nil, ((5...5).min { |x, y| x <=> y })
     142end
Note: See TracChangeset for help on using the changeset viewer.