From eab07daf92c9a8a2836923656d9e8f58583a52ba Mon Sep 17 00:00:00 2001 From: Ryan Lopopolo Date: Tue, 9 Jul 2019 01:41:10 -0700 Subject: Add Range#max and Range#min tests from Ruby Spec --- mrbgems/mruby-range-ext/test/range.rb | 101 ++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) (limited to 'mrbgems/mruby-range-ext/test') 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 -- cgit v1.2.3 From 56929362f58ba5ad3ebe4131a6cc4259e6479dc0 Mon Sep 17 00:00:00 2001 From: Ryan Lopopolo Date: Tue, 9 Jul 2019 01:48:15 -0700 Subject: Fix Range#min fast path with exclusive range --- mrbgems/mruby-range-ext/mrblib/range.rb | 1 - mrbgems/mruby-range-ext/test/range.rb | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) (limited to 'mrbgems/mruby-range-ext/test') diff --git a/mrbgems/mruby-range-ext/mrblib/range.rb b/mrbgems/mruby-range-ext/mrblib/range.rb index e09b2d096..a149a57dc 100644 --- a/mrbgems/mruby-range-ext/mrblib/range.rb +++ b/mrbgems/mruby-range-ext/mrblib/range.rb @@ -58,7 +58,6 @@ class Range return nil if val == last && exclude_end? min = val - min -= 1 if exclude_end? return min end diff --git a/mrbgems/mruby-range-ext/test/range.rb b/mrbgems/mruby-range-ext/test/range.rb index 6b135aeff..b56d6b58e 100644 --- a/mrbgems/mruby-range-ext/test/range.rb +++ b/mrbgems/mruby-range-ext/test/range.rb @@ -87,6 +87,7 @@ end assert('Range#min') do # returns the minimum value in the range when called with no arguments assert_equal 1, (1..10).min + 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 -- cgit v1.2.3 From 8cb37804c16b1506d019081bb5d366b078b0b42a Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Wed, 17 Jul 2019 18:58:21 +0900 Subject: Allow `mruby-range-ext` to work with `MRB_WITHOUT_FLOAT`; ref 2add8641 --- mrbgems/mruby-range-ext/mrblib/range.rb | 4 ++-- mrbgems/mruby-range-ext/test/range.rb | 28 ++++++++++++++++++---------- 2 files changed, 20 insertions(+), 12 deletions(-) (limited to 'mrbgems/mruby-range-ext/test') diff --git a/mrbgems/mruby-range-ext/mrblib/range.rb b/mrbgems/mruby-range-ext/mrblib/range.rb index a149a57dc..7c5787fac 100644 --- a/mrbgems/mruby-range-ext/mrblib/range.rb +++ b/mrbgems/mruby-range-ext/mrblib/range.rb @@ -32,7 +32,7 @@ class Range return super if block # fast path for numerics - if (val.kind_of?(Fixnum) || val.kind_of?(Float)) && (last.kind_of?(Fixnum) || last.kind_of?(Float)) + if val.kind_of?(Numeric) && last.kind_of?(Numeric) raise TypeError if exclude_end? && !last.kind_of?(Fixnum) return nil if val > last return nil if val == last && exclude_end? @@ -52,7 +52,7 @@ class Range return super if block # fast path for numerics - if (val.kind_of?(Fixnum) || val.kind_of?(Float)) && (last.kind_of?(Fixnum) || last.kind_of?(Float)) + if val.kind_of?(Numeric) && last.kind_of?(Numeric) raise TypeError if exclude_end? && !last.kind_of?(Fixnum) return nil if val > last return nil if val == last && exclude_end? diff --git a/mrbgems/mruby-range-ext/test/range.rb b/mrbgems/mruby-range-ext/test/range.rb index b56d6b58e..e2c549d04 100644 --- a/mrbgems/mruby-range-ext/test/range.rb +++ b/mrbgems/mruby-range-ext/test/range.rb @@ -10,6 +10,8 @@ end assert('Range#first') do assert_equal 10, (10..20).first assert_equal [10, 11, 12], (10..20).first(3) + + skip unless Object.const_defined?(:Float) assert_equal [0, 1, 2], (0..Float::INFINITY).first(3) end @@ -23,12 +25,14 @@ end assert('Range#size') do assert_equal 42, (1..42).size assert_equal 41, (1...42).size + assert_nil ('a'..'z').size + + skip unless Object.const_defined?(:Float) assert_equal 6, (1...6.3).size assert_equal 5, (1...6.0).size assert_equal 5, (1.1...6).size assert_equal 15, (1.0..15.9).size assert_equal Float::INFINITY, (0..Float::INFINITY).size - assert_nil ('a'..'z').size end assert('Range#max') do @@ -37,12 +41,6 @@ assert('Range#max') do 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 @@ -52,6 +50,14 @@ assert('Range#max') do # returns the endpoint when the endpoint equals the start point and the range is inclusive assert_equal 5, (5..5).max + skip unless Object.const_defined?(:Float) + + # 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 in a Float range assert_equal nil, (3003.20..908.1111).max end @@ -89,9 +95,6 @@ assert('Range#min') do assert_equal 1, (1..10).min 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 @@ -101,6 +104,11 @@ assert('Range#min') do # returns the endpoint when the endpoint equals the start point and the range is inclusive assert_equal 5, (5..5).max + skip unless Object.const_defined?(:Float) + + # 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 in a Float range assert_equal nil, (3003.20..908.1111).max end -- cgit v1.2.3 From 13f58f9bc2bcff38cd88b7c59c366f8a97d60a08 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Sat, 24 Aug 2019 14:22:10 +0900 Subject: Fix `Range#max` test (`TypeError` is raised) on 32-bit mode --- mrbgems/mruby-range-ext/test/range.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mrbgems/mruby-range-ext/test') diff --git a/mrbgems/mruby-range-ext/test/range.rb b/mrbgems/mruby-range-ext/test/range.rb index e2c549d04..8179cef53 100644 --- a/mrbgems/mruby-range-ext/test/range.rb +++ b/mrbgems/mruby-range-ext/test/range.rb @@ -39,7 +39,7 @@ 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 + assert_equal 1073741823, (0...2**30).max # returns nil when the endpoint is less than the start point assert_equal nil, (100..10).max -- cgit v1.2.3 From 6cfcff6f10c2f3274b36c0025fb807468e79eef2 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Thu, 5 Sep 2019 19:51:32 +0900 Subject: Fix `Range#max` test (`TypeError` is raised) on 32-bit mode with word boxing --- mrbgems/mruby-range-ext/test/range.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mrbgems/mruby-range-ext/test') diff --git a/mrbgems/mruby-range-ext/test/range.rb b/mrbgems/mruby-range-ext/test/range.rb index 8179cef53..865e46d02 100644 --- a/mrbgems/mruby-range-ext/test/range.rb +++ b/mrbgems/mruby-range-ext/test/range.rb @@ -39,7 +39,7 @@ 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 1073741823, (0...2**30).max + assert_equal 536870911, (0...2**29).max # returns nil when the endpoint is less than the start point assert_equal nil, (100..10).max -- cgit v1.2.3