diff options
| author | Ryan Lopopolo <[email protected]> | 2019-07-09 01:41:10 -0700 |
|---|---|---|
| committer | Ryan Lopopolo <[email protected]> | 2019-07-09 01:41:10 -0700 |
| commit | eab07daf92c9a8a2836923656d9e8f58583a52ba (patch) | |
| tree | 03aac1f676cd10d5f8690b14dbac7797bdfc1b7f | |
| parent | d9c530379343ef0dd0f314c982ddeedea8eb1a60 (diff) | |
| download | mruby-eab07daf92c9a8a2836923656d9e8f58583a52ba.tar.gz mruby-eab07daf92c9a8a2836923656d9e8f58583a52ba.zip | |
Add Range#max and Range#min tests from Ruby Spec
| -rw-r--r-- | mrbgems/mruby-range-ext/test/range.rb | 101 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/test/range.rb | 18 |
2 files changed, 117 insertions, 2 deletions
diff --git a/mrbgems/mruby-range-ext/test/range.rb b/mrbgems/mruby-range-ext/test/range.rb index efcbdabe4..6b135aeff 100644 --- a/mrbgems/mruby-range-ext/test/range.rb +++ b/mrbgems/mruby-range-ext/test/range.rb @@ -30,3 +30,104 @@ assert('Range#size') do assert_equal Float::INFINITY, (0..Float::INFINITY).size assert_nil ('a'..'z').size end + +assert('Range#max') do + # returns the maximum value in the range when called with no arguments + assert_equal 10, (1..10).max + assert_equal 9, (1...10).max + assert_equal 4294967295, (0...2**32).max + + # returns the maximum value in the Float range when called with no arguments + assert_equal 908.1111, (303.20..908.1111).max + + # raises TypeError when called on an exclusive range and a non Integer value + assert_raise(TypeError) { (303.20...908.1111).max } + + # returns nil when the endpoint is less than the start point + assert_equal nil, (100..10).max + + # returns nil when the endpoint equals the start point and the range is exclusive + assert_equal nil, (5...5).max + + # returns the endpoint when the endpoint equals the start point and the range is inclusive + assert_equal 5, (5..5).max + + # returns nil when the endpoint is less than the start point in a Float range + assert_equal nil, (3003.20..908.1111).max +end + +assert('Range#max given a block') do + # passes each pair of values in the range to the block + acc = [] + (1..10).max do |a, b| + acc << a + acc << b + a + end + (1..10).each do |value| + assert_true acc.include?(value) + end + + # passes each pair of elements to the block in reversed order + acc = [] + (1..5).max do |a, b| + acc << [a, b] + a + end + assert_equal [[2, 1], [3, 2], [4, 3], [5, 4]], acc + + # returns the element the block determines to be the maximum + assert_equal 1, ((1..3).max { |_a, _b| -3 }) + + # returns nil when the endpoint is less than the start point + assert_equal nil, ((100..10).max { |x, y| x <=> y }) + assert_equal nil, ((5...5).max { |x, y| x <=> y }) +end + +assert('Range#min') do + # returns the minimum value in the range when called with no arguments + assert_equal 1, (1..10).min + + # returns the minimum value in the Float range when called with no arguments + assert_equal 303.20, (303.20..908.1111).min + + # returns nil when the start point is greater than the endpoint + assert_equal nil, (100..10).min + + # returns nil when the endpoint equals the start point and the range is exclusive + assert_equal nil, (5...5).max + + # returns the endpoint when the endpoint equals the start point and the range is inclusive + assert_equal 5, (5..5).max + + # returns nil when the start point is greater than the endpoint in a Float range + assert_equal nil, (3003.20..908.1111).max +end + +assert('Range#min given a block') do + # passes each pair of values in the range to the block + acc = [] + (1..10).min do |a, b| + acc << a + acc << b + a + end + (1..10).each do |value| + assert_true acc.include?(value) + end + + # passes each pair of elements to the block in reversed order + acc = [] + (1..5).min do |a, b| + acc << [a, b] + a + end + assert_equal [[2, 1], [3, 1], [4, 1], [5, 1]], acc + + # returns the element the block determines to be the minimum + assert_equal 3, ((1..3).min { |_a, _b| -3 }) + + # returns nil when the start point is greater than the endpoint + assert_equal nil, ((100..10).min { |x, y| x <=> y }) + assert_equal nil, ((5...5).min { |x, y| x <=> y }) +end diff --git a/mrbgems/mruby-string-ext/test/range.rb b/mrbgems/mruby-string-ext/test/range.rb index 4b42cd3a6..80c286850 100644 --- a/mrbgems/mruby-string-ext/test/range.rb +++ b/mrbgems/mruby-string-ext/test/range.rb @@ -1,12 +1,26 @@ assert('Range#max') do - # string Range depends on String#upto + # returns the maximum value in the range when called with no arguments assert_equal 'l', ('f'..'l').max assert_equal 'e', ('a'...'f').max + + # returns nil when the endpoint is less than the start point assert_equal nil, ('z'..'l').max end +assert('Range#max given a block') do + # returns nil when the endpoint is less than the start point + assert_equal nil, (('z'..'l').max { |x, y| x <=> y }) +end + assert('Range#min') do - # string Range depends on String#upto + # returns the minimum value in the range when called with no arguments assert_equal 'f', ('f'..'l').min + + # returns nil when the start point is greater than the endpoint assert_equal nil, ('z'..'l').min end + +assert('Range#min given a block') do + # returns nil when the start point is greater than the endpoint + assert_equal nil, (('z'..'l').min { |x, y| x <=> y }) +end |
