1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Move spec/rubyspec to spec/ruby for consistency

* Other ruby implementations use the spec/ruby directory.
  [Misc #13792] [ruby-core:82287]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2017-09-20 20:18:52 +00:00
parent 75bfc6440d
commit 1d15d5f080
4370 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/to_i', __FILE__)
describe "Integer#ceil" do
it_behaves_like(:integer_to_i, :ceil)
end

View file

@ -0,0 +1,239 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#chr without argument" do
it "returns a String" do
17.chr.should be_an_instance_of(String)
end
it "returns a new String for each call" do
82.chr.should_not equal(82.chr)
end
it "raises a RangeError is self is less than 0" do
lambda { -1.chr }.should raise_error(RangeError)
lambda { -bignum_value.chr }.should raise_error(RangeError)
end
describe "when Encoding.default_internal is nil" do
describe "and self is between 0 and 127 (inclusive)" do
it "returns a US-ASCII String" do
(0..127).each do |c|
c.chr.encoding.should == Encoding::US_ASCII
end
end
it "returns a String encoding self interpreted as a US-ASCII codepoint" do
(0..127).each do |c|
c.chr.bytes.to_a.should == [c]
end
end
end
describe "and self is between 128 and 255 (inclusive)" do
it "returns an ASCII-8BIT String" do
(128..255).each do |c|
c.chr.encoding.should == Encoding::ASCII_8BIT
end
end
it "returns a String containing self interpreted as a byte" do
(128..255).each do |c|
c.chr.bytes.to_a.should == [c]
end
end
end
it "raises a RangeError is self is greater than 255" do
lambda { 256.chr }.should raise_error(RangeError)
lambda { bignum_value.chr }.should raise_error(RangeError)
end
end
describe "when Encoding.default_internal is not nil" do
before do
@default_internal = Encoding.default_internal
end
after do
Encoding.default_internal = @default_internal
end
describe "and self is between 0 and 127 (inclusive)" do
it "returns a US-ASCII String" do
(0..127).each do |c|
Encoding.default_internal = Encoding::UTF_8
c.chr.encoding.should == Encoding::US_ASCII
Encoding.default_internal = Encoding::SHIFT_JIS
c.chr.encoding.should == Encoding::US_ASCII
end
end
it "returns a String encoding self interpreted as a US-ASCII codepoint" do
(0..127).each do |c|
Encoding.default_internal = Encoding::UTF_8
c.chr.bytes.to_a.should == [c]
Encoding.default_internal = Encoding::SHIFT_JIS
c.chr.bytes.to_a.should == [c]
end
end
end
describe "and self is between 128 and 255 (inclusive)" do
it "returns an ASCII-8BIT String" do
(128..255).each do |c|
Encoding.default_internal = Encoding::UTF_8
c.chr.encoding.should == Encoding::ASCII_8BIT
Encoding.default_internal = Encoding::SHIFT_JIS
c.chr.encoding.should == Encoding::ASCII_8BIT
end
end
it "returns a String containing self interpreted as a byte" do
(128..255).each do |c|
Encoding.default_internal = Encoding::UTF_8
c.chr.bytes.to_a.should == [c]
Encoding.default_internal = Encoding::SHIFT_JIS
c.chr.bytes.to_a.should == [c]
end
end
end
describe "and self is greater than 255" do
it "returns a String with the default internal encoding" do
Encoding.default_internal = Encoding::UTF_8
0x0100.chr.encoding.should == Encoding::UTF_8
0x3000.chr.encoding.should == Encoding::UTF_8
Encoding.default_internal = Encoding::SHIFT_JIS
0x8140.chr.encoding.should == Encoding::SHIFT_JIS
0xFC4B.chr.encoding.should == Encoding::SHIFT_JIS
end
it "returns a String encoding self interpreted as a codepoint in the default internal encoding" do
Encoding.default_internal = Encoding::UTF_8
0x0100.chr.bytes.to_a.should == [0xC4, 0x80]
0x3000.chr.bytes.to_a.should == [0xE3, 0x80, 0x80]
Encoding.default_internal = Encoding::SHIFT_JIS
0x8140.chr.bytes.to_a.should == [0x81, 0x40] # Smallest assigned CP932 codepoint greater than 255
0xFC4B.chr.bytes.to_a.should == [0xFC, 0x4B] # Largest assigned CP932 codepoint
end
# #5864
it "raises RangeError if self is invalid as a codepoint in the default internal encoding" do
[ [0x0100, "US-ASCII"],
[0x0100, "ASCII-8BIT"],
[0x0100, "EUC-JP"],
[0xA1A0, "EUC-JP"],
[0x0100, "ISO-8859-9"],
[620, "TIS-620"]
].each do |integer, encoding_name|
Encoding.default_internal = Encoding.find(encoding_name)
lambda { integer.chr }.should raise_error(RangeError)
end
end
end
end
end
describe "Integer#chr with an encoding argument" do
it "returns a String" do
900.chr(Encoding::UTF_8).should be_an_instance_of(String)
end
it "returns a new String for each call" do
8287.chr(Encoding::UTF_8).should_not equal(8287.chr(Encoding::UTF_8))
end
it "accepts a String as an argument" do
lambda { 0xA4A2.chr('euc-jp') }.should_not raise_error
end
it "converts a String to an Encoding as Encoding.find does" do
['utf-8', 'UTF-8', 'Utf-8'].each do |encoding|
7894.chr(encoding).encoding.should == Encoding::UTF_8
end
end
# http://redmine.ruby-lang.org/issues/4869
it "raises a RangeError is self is less than 0" do
lambda { -1.chr(Encoding::UTF_8) }.should raise_error(RangeError)
lambda { -bignum_value.chr(Encoding::EUC_JP) }.should raise_error(RangeError)
end
it "returns a String with the specified encoding" do
0x0000.chr(Encoding::US_ASCII).encoding.should == Encoding::US_ASCII
0x007F.chr(Encoding::US_ASCII).encoding.should == Encoding::US_ASCII
0x0000.chr(Encoding::ASCII_8BIT).encoding.should == Encoding::ASCII_8BIT
0x007F.chr(Encoding::ASCII_8BIT).encoding.should == Encoding::ASCII_8BIT
0x0080.chr(Encoding::ASCII_8BIT).encoding.should == Encoding::ASCII_8BIT
0x00FF.chr(Encoding::ASCII_8BIT).encoding.should == Encoding::ASCII_8BIT
0x0000.chr(Encoding::UTF_8).encoding.should == Encoding::UTF_8
0x007F.chr(Encoding::UTF_8).encoding.should == Encoding::UTF_8
0x0080.chr(Encoding::UTF_8).encoding.should == Encoding::UTF_8
0x00FF.chr(Encoding::UTF_8).encoding.should == Encoding::UTF_8
0x0100.chr(Encoding::UTF_8).encoding.should == Encoding::UTF_8
0x3000.chr(Encoding::UTF_8).encoding.should == Encoding::UTF_8
0x0000.chr(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS
0x007F.chr(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS
0x00A1.chr(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS
0x00DF.chr(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS
0x8140.chr(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS
0xFC4B.chr(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS
end
it "returns a String encoding self interpreted as a codepoint in the specified encoding" do
0x0000.chr(Encoding::US_ASCII).bytes.to_a.should == [0x00]
0x007F.chr(Encoding::US_ASCII).bytes.to_a.should == [0x7F]
0x0000.chr(Encoding::ASCII_8BIT).bytes.to_a.should == [0x00]
0x007F.chr(Encoding::ASCII_8BIT).bytes.to_a.should == [0x7F]
0x0080.chr(Encoding::ASCII_8BIT).bytes.to_a.should == [0x80]
0x00FF.chr(Encoding::ASCII_8BIT).bytes.to_a.should == [0xFF]
0x0000.chr(Encoding::UTF_8).bytes.to_a.should == [0x00]
0x007F.chr(Encoding::UTF_8).bytes.to_a.should == [0x7F]
0x0080.chr(Encoding::UTF_8).bytes.to_a.should == [0xC2, 0x80]
0x00FF.chr(Encoding::UTF_8).bytes.to_a.should == [0xC3, 0xBF]
0x0100.chr(Encoding::UTF_8).bytes.to_a.should == [0xC4, 0x80]
0x3000.chr(Encoding::UTF_8).bytes.to_a.should == [0xE3, 0x80, 0x80]
0x0000.chr(Encoding::SHIFT_JIS).bytes.to_a.should == [0x00]
0x007F.chr(Encoding::SHIFT_JIS).bytes.to_a.should == [0x7F]
0x00A1.chr(Encoding::SHIFT_JIS).bytes.to_a.should == [0xA1]
0x00DF.chr(Encoding::SHIFT_JIS).bytes.to_a.should == [0xDF]
0x8140.chr(Encoding::SHIFT_JIS).bytes.to_a.should == [0x81, 0x40] # Smallest assigned CP932 codepoint greater than 255
0xFC4B.chr(Encoding::SHIFT_JIS).bytes.to_a.should == [0xFC, 0x4B] # Largest assigned CP932 codepoint
end
# #5864
it "raises RangeError if self is invalid as a codepoint in the specified encoding" do
[ [0x80, "US-ASCII"],
[0x0100, "ASCII-8BIT"],
[0x0100, "EUC-JP"],
[0xA1A0, "EUC-JP"],
[0xA1, "EUC-JP"],
[0x80, "SHIFT_JIS"],
[0xE0, "SHIFT_JIS"],
[0x0100, "ISO-8859-9"],
[620, "TIS-620"],
[0xD800, "UTF-8"],
[0xDBFF, "UTF-8"],
[0xDC00, "UTF-8"],
[0xDFFF, "UTF-8"],
[0xD800, "UTF-16"],
[0xDBFF, "UTF-16"],
[0xDC00, "UTF-16"],
[0xDFFF, "UTF-16"],
].each do |integer, encoding_name|
lambda { integer.chr(encoding_name) }.should raise_error(RangeError)
end
end
end

View file

@ -0,0 +1,20 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#denominator" do
# The Numeric child classes override this method, so their behaviour is
# specified in the appropriate place
before :each do
@numbers = [
20, # Integer
-2709, # Negative Integer
99999999**99, # Bignum
-99999**621, # Negative BigNum
0,
1
]
end
it "returns 1" do
@numbers.each {|number| number.denominator.should == 1}
end
end

View file

@ -0,0 +1,69 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#downto [stop] when self and stop are Fixnums" do
it "does not yield when stop is greater than self" do
result = []
5.downto(6) { |x| result << x }
result.should == []
end
it "yields once when stop equals self" do
result = []
5.downto(5) { |x| result << x }
result.should == [5]
end
it "yields while decreasing self until it is less than stop" do
result = []
5.downto(2) { |x| result << x }
result.should == [5, 4, 3, 2]
end
it "yields while decreasing self until it less than ceil for a Float endpoint" do
result = []
9.downto(1.3) {|i| result << i}
3.downto(-1.3) {|i| result << i}
result.should == [9, 8, 7, 6, 5, 4, 3, 2, 3, 2, 1, 0, -1]
end
it "raises an ArgumentError for invalid endpoints" do
lambda {1.downto("A") {|x| p x } }.should raise_error(ArgumentError)
lambda {1.downto(nil) {|x| p x } }.should raise_error(ArgumentError)
end
describe "when no block is given" do
it "returns an Enumerator" do
result = []
enum = 5.downto(2)
enum.each { |i| result << i }
result.should == [5, 4, 3, 2]
end
describe "returned Enumerator" do
describe "size" do
it "raises an ArgumentError for invalid endpoints" do
enum = 1.downto("A")
lambda { enum.size }.should raise_error(ArgumentError)
enum = 1.downto(nil)
lambda { enum.size }.should raise_error(ArgumentError)
end
it "returns self - stop + 1" do
10.downto(5).size.should == 6
10.downto(1).size.should == 10
10.downto(0).size.should == 11
0.downto(0).size.should == 1
-3.downto(-5).size.should == 3
end
it "returns 0 when stop > self" do
4.downto(5).size.should == 0
-5.downto(0).size.should == 0
-5.downto(-3).size.should == 0
end
end
end
end
end

View file

@ -0,0 +1,20 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#even?" do
it "returns true for a Fixnum when it is an even number" do
(-2).even?.should be_true
(-1).even?.should be_false
0.even?.should be_true
1.even?.should be_false
2.even?.should be_true
end
it "returns true for a Bignum when it is an even number" do
bignum_value(0).even?.should be_true
bignum_value(1).even?.should be_false
(-bignum_value(0)).even?.should be_true
(-bignum_value(1)).even?.should be_false
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/to_i', __FILE__)
describe "Integer#floor" do
it_behaves_like(:integer_to_i, :floor)
end

View file

@ -0,0 +1,58 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#gcd" do
it "returns self if equal to the argument" do
1.gcd(1).should == 1
398.gcd(398).should == 398
end
it "returns an Integer" do
36.gcd(6).should be_kind_of(Integer)
4.gcd(20981).should be_kind_of(Integer)
end
it "returns the greatest common divisor of self and argument" do
10.gcd(5).should == 5
200.gcd(20).should == 20
end
it "returns a positive integer even if self is negative" do
-12.gcd(6).should == 6
-100.gcd(100).should == 100
end
it "returns a positive integer even if the argument is negative" do
12.gcd(-6).should == 6
100.gcd(-100).should == 100
end
it "returns a positive integer even if both self and argument are negative" do
-12.gcd(-6).should == 6
-100.gcd(-100).should == 100
end
it "accepts a Bignum argument" do
bignum = 9999**99
bignum.should be_kind_of(Bignum)
99.gcd(bignum).should == 99
end
it "works if self is a Bignum" do
bignum = 9999**99
bignum.should be_kind_of(Bignum)
bignum.gcd(99).should == 99
end
it "raises an ArgumentError if not given an argument" do
lambda { 12.gcd }.should raise_error(ArgumentError)
end
it "raises an ArgumentError if given more than one argument" do
lambda { 12.gcd(30, 20) }.should raise_error(ArgumentError)
end
it "raises a TypeError unless the argument is an Integer" do
lambda { 39.gcd(3.8) }.should raise_error(TypeError)
lambda { 45872.gcd([]) }.should raise_error(TypeError)
end
end

View file

@ -0,0 +1,53 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#gcdlcm" do
it "returns [self, self] if self is equal to the argument" do
1.gcdlcm(1).should == [1, 1]
398.gcdlcm(398).should == [398, 398]
end
it "returns an Array" do
36.gcdlcm(6).should be_kind_of(Array)
4.gcdlcm(20981).should be_kind_of(Array)
end
it "returns a two-element Array" do
36.gcdlcm(876).size.should == 2
29.gcdlcm(17).size.should == 2
end
it "returns the greatest common divisor of self and argument as the first element" do
10.gcdlcm(5)[0].should == 10.gcd(5)
200.gcdlcm(20)[0].should == 200.gcd(20)
end
it "returns the least common multiple of self and argument as the last element" do
10.gcdlcm(5)[1].should == 10.lcm(5)
200.gcdlcm(20)[1].should == 200.lcm(20)
end
it "accepts a Bignum argument" do
bignum = 91999**99
bignum.should be_kind_of(Bignum)
99.gcdlcm(bignum).should == [99.gcd(bignum), 99.lcm(bignum)]
end
it "works if self is a Bignum" do
bignum = 9999**89
bignum.should be_kind_of(Bignum)
bignum.gcdlcm(99).should == [bignum.gcd(99), bignum.lcm(99)]
end
it "raises an ArgumentError if not given an argument" do
lambda { 12.gcdlcm }.should raise_error(ArgumentError)
end
it "raises an ArgumentError if given more than one argument" do
lambda { 12.gcdlcm(30, 20) }.should raise_error(ArgumentError)
end
it "raises a TypeError unless the argument is an Integer" do
lambda { 39.gcdlcm(3.8) }.should raise_error(TypeError)
lambda { 45872.gcdlcm([]) }.should raise_error(TypeError)
end
end

View file

@ -0,0 +1,15 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer" do
it "includes Comparable" do
Integer.include?(Comparable).should == true
end
end
describe "Integer#integer?" do
it "returns true for Integers" do
0.integer?.should == true
-1.integer?.should == true
bignum_value.integer?.should == true
end
end

View file

@ -0,0 +1,58 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#lcm" do
it "returns self if equal to the argument" do
1.lcm(1).should == 1
398.lcm(398).should == 398
end
it "returns an Integer" do
36.lcm(6).should be_kind_of(Integer)
4.lcm(20981).should be_kind_of(Integer)
end
it "returns the least common multiple of self and argument" do
200.lcm(2001).should == 400200
99.lcm(90).should == 990
end
it "returns a positive integer even if self is negative" do
-12.lcm(6).should == 12
-100.lcm(100).should == 100
end
it "returns a positive integer even if the argument is negative" do
12.lcm(-6).should == 12
100.lcm(-100).should == 100
end
it "returns a positive integer even if both self and argument are negative" do
-12.lcm(-6).should == 12
-100.lcm(-100).should == 100
end
it "accepts a Bignum argument" do
bignum = 9999**99
bignum.should be_kind_of(Bignum)
99.lcm(bignum).should == bignum
end
it "works if self is a Bignum" do
bignum = 9999**99
bignum.should be_kind_of(Bignum)
bignum.lcm(99).should == bignum
end
it "raises an ArgumentError if not given an argument" do
lambda { 12.lcm }.should raise_error(ArgumentError)
end
it "raises an ArgumentError if given more than one argument" do
lambda { 12.lcm(30, 20) }.should raise_error(ArgumentError)
end
it "raises a TypeError unless the argument is an Integer" do
lambda { 39.lcm(3.8) }.should raise_error(TypeError)
lambda { 45872.lcm([]) }.should raise_error(TypeError)
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/next', __FILE__)
describe "Integer#next" do
it_behaves_like(:integer_next, :next)
end

View file

@ -0,0 +1,18 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#numerator" do
before :all do
@numbers = [
0,
29871,
99999999999999**99,
72628191273,
].map{|n| [-n, n]}.flatten
end
it "returns self" do
@numbers.each do |number|
number.numerator.should == number
end
end
end

View file

@ -0,0 +1,18 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#odd?" do
it "returns true when self is an odd number" do
(-2).odd?.should be_false
(-1).odd?.should be_true
0.odd?.should be_false
1.odd?.should be_true
2.odd?.should be_false
bignum_value(0).odd?.should be_false
bignum_value(1).odd?.should be_true
(-bignum_value(0)).odd?.should be_false
(-bignum_value(1)).odd?.should be_true
end
end

View file

@ -0,0 +1,17 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#ord" do
it "returns self" do
20.ord.should eql(20)
40.ord.should eql(40)
0.ord.should eql(0)
(-10).ord.should eql(-10)
?a.ord.should eql(97)
?Z.ord.should eql(90)
bignum_value.ord.should eql(bignum_value)
(-bignum_value).ord.should eql(-bignum_value)
end
end

View file

@ -0,0 +1,11 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#pred" do
it "returns the Integer equal to self - 1" do
0.pred.should eql(-1)
-1.pred.should eql(-2)
bignum_value.pred.should eql(bignum_value(-1))
fixnum_min.pred.should eql(fixnum_min - 1)
20.pred.should eql(19)
end
end

View file

@ -0,0 +1,39 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#rationalize" do
before :all do
@numbers = [
0,
29871,
99999999999999**99,
-72628191273,
]
end
it "returns a Rational object" do
@numbers.each do |number|
number.rationalize.should be_an_instance_of(Rational)
end
end
it "uses self as the numerator" do
@numbers.each do |number|
number.rationalize.numerator.should == number
end
end
it "uses 1 as the denominator" do
@numbers.each do |number|
number.rationalize.denominator.should == 1
end
end
it "ignores a single argument" do
1.rationalize(0.1).should == Rational(1,1)
end
it "raises ArgumentError when passed more than one argument" do
lambda { 1.rationalize(0.1, 0.1) }.should raise_error(ArgumentError)
lambda { 1.rationalize(0.1, 0.1, 2) }.should raise_error(ArgumentError)
end
end

View file

@ -0,0 +1,77 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/to_i', __FILE__)
describe "Integer#round" do
it_behaves_like(:integer_to_i, :round)
ruby_version_is ""..."2.5" do
it "rounds itself as a float if passed a positive precision" do
[2, -4, 10**70, -10**100].each do |v|
v.round(42).should eql(v.to_f)
end
end
end
ruby_version_is "2.5" do
it "returns itself if passed a positive precision" do
[2, -4, 10**70, -10**100].each do |v|
v.round(42).should eql(v)
end
end
end
it "returns itself if passed zero" do
[2, -4, 10**70, -10**100].each do |v|
v.round(0).should eql(v)
end
end
# redmine:5228
it "returns itself rounded if passed a negative value" do
+249.round(-2).should eql(+200)
-249.round(-2).should eql(-200)
(+25 * 10**70 - 1).round(-71).should eql(+20 * 10**70)
(-25 * 10**70 + 1).round(-71).should eql(-20 * 10**70)
end
it "returns itself rounded to nearest if passed a negative value" do
+250.round(-2).should eql(+300)
-250.round(-2).should eql(-300)
(+25 * 10**70).round(-71).should eql(+30 * 10**70)
(-25 * 10**70).round(-71).should eql(-30 * 10**70)
end
platform_is_not wordsize: 32 do
it "raises a RangeError when passed a big negative value" do
lambda { 42.round(fixnum_min) }.should raise_error(RangeError)
end
end
it "raises a RangeError when passed Float::INFINITY" do
lambda { 42.round(Float::INFINITY) }.should raise_error(RangeError)
end
it "raises a RangeError when passed a beyond signed int" do
lambda { 42.round(1<<31) }.should raise_error(RangeError)
end
it "raises a TypeError when passed a String" do
lambda { 42.round("4") }.should raise_error(TypeError)
end
it "raises a TypeError when its argument cannot be converted to an Integer" do
lambda { 42.round(nil) }.should raise_error(TypeError)
end
it "calls #to_int on the argument to convert it to an Integer" do
obj = mock("Object")
obj.should_receive(:to_int).and_return(0)
42.round(obj)
end
it "raises a TypeError when #to_int does not return an Integer" do
obj = mock("Object")
obj.stub!(:to_int).and_return([])
lambda { 42.round(obj) }.should raise_error(TypeError)
end
end

View file

@ -0,0 +1,25 @@
describe :integer_next, shared: true do
it "returns the next larger positive Fixnum" do
2.send(@method).should == 3
end
it "returns the next larger negative Fixnum" do
(-2).send(@method).should == -1
end
it "returns the next larger positive Bignum" do
bignum_value.send(@method).should == bignum_value(1)
end
it "returns the next larger negative Bignum" do
(-bignum_value(1)).send(@method).should == -bignum_value
end
it "overflows a Fixnum to a Bignum" do
fixnum_max.send(@method).should == fixnum_max + 1
end
it "underflows a Bignum to a Fixnum" do
(fixnum_min - 1).send(@method).should == fixnum_min
end
end

View file

@ -0,0 +1,8 @@
describe :integer_to_i, shared: true do
it "returns self" do
10.send(@method).should eql(10)
(-15).send(@method).should eql(-15)
bignum_value.send(@method).should eql(bignum_value)
(-bignum_value).send(@method).should eql(-bignum_value)
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/next', __FILE__)
describe "Integer#succ" do
it_behaves_like(:integer_next, :succ)
end

View file

@ -0,0 +1,79 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#times" do
it "returns self" do
5.times {}.should == 5
9.times {}.should == 9
9.times { |n| n - 2 }.should == 9
end
it "yields each value from 0 to self - 1" do
a = []
9.times { |i| a << i }
-2.times { |i| a << i }
a.should == [0, 1, 2, 3, 4, 5, 6, 7, 8]
end
it "skips the current iteration when encountering 'next'" do
a = []
3.times do |i|
next if i == 1
a << i
end
a.should == [0, 2]
end
it "skips all iterations when encountering 'break'" do
a = []
5.times do |i|
break if i == 3
a << i
end
a.should == [0, 1, 2]
end
it "skips all iterations when encountering break with an argument and returns that argument" do
9.times { break 2 }.should == 2
end
it "executes a nested while loop containing a break expression" do
a = [false]
b = 1.times do |i|
while true
a.shift or break
end
end
a.should == []
b.should == 1
end
it "executes a nested #times" do
a = 0
b = 3.times do |i|
2.times { a += 1 }
end
a.should == 6
b.should == 3
end
it "returns an Enumerator" do
result = []
enum = 3.times
enum.each { |i| result << i }
result.should == [0, 1, 2]
end
describe "when no block is given" do
describe "returned Enumerator" do
describe "size" do
it "returns self" do
5.times.size.should == 5
10.times.size.should == 10
0.times.size.should == 0
end
end
end
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/to_i', __FILE__)
describe "Integer#to_i" do
it_behaves_like(:integer_to_i, :to_i)
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/to_i', __FILE__)
describe "Integer#to_int" do
it_behaves_like(:integer_to_i, :to_int)
end

View file

@ -0,0 +1,26 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#to_r" do
it "returns a Rational object" do
309.to_r.should be_an_instance_of(Rational)
end
it "constructs a rational number with self as the numerator" do
34.to_r.numerator.should == 34
end
it "constructs a rational number with 1 as the denominator" do
298.to_r.denominator.should == 1
end
it "works even if self is a Bignum" do
bignum = 99999**999
bignum.should be_an_instance_of(Bignum)
bignum.to_r.should == Rational(bignum, 1)
end
it "raises an ArgumentError if given any arguments" do
lambda { 287.to_r(2) }.should raise_error(ArgumentError)
lambda { 9102826.to_r(309, [], 71) }.should raise_error(ArgumentError)
end
end

View file

@ -0,0 +1,6 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/to_i', __FILE__)
describe "Integer#truncate" do
it_behaves_like(:integer_to_i, :truncate)
end

View file

@ -0,0 +1,69 @@
require File.expand_path('../../../spec_helper', __FILE__)
describe "Integer#upto [stop] when self and stop are Fixnums" do
it "does not yield when stop is less than self" do
result = []
5.upto(4) { |x| result << x }
result.should == []
end
it "yields once when stop equals self" do
result = []
5.upto(5) { |x| result << x }
result.should == [5]
end
it "yields while increasing self until it is less than stop" do
result = []
2.upto(5) { |x| result << x }
result.should == [2, 3, 4, 5]
end
it "yields while increasing self until it is greater than floor of a Float endpoint" do
result = []
9.upto(13.3) {|i| result << i}
-5.upto(-1.3) {|i| result << i}
result.should == [9,10,11,12,13,-5,-4,-3,-2]
end
it "raises an ArgumentError for non-numeric endpoints" do
lambda { 1.upto("A") {|x| p x} }.should raise_error(ArgumentError)
lambda { 1.upto(nil) {|x| p x} }.should raise_error(ArgumentError)
end
describe "when no block is given" do
it "returns an Enumerator" do
result = []
enum = 2.upto(5)
enum.each { |i| result << i }
result.should == [2, 3, 4, 5]
end
describe "returned Enumerator" do
describe "size" do
it "raises an ArgumentError for non-numeric endpoints" do
enum = 1.upto("A")
lambda { enum.size }.should raise_error(ArgumentError)
enum = 1.upto(nil)
lambda { enum.size }.should raise_error(ArgumentError)
end
it "returns stop - self + 1" do
5.upto(10).size.should == 6
1.upto(10).size.should == 10
0.upto(10).size.should == 11
0.upto(0).size.should == 1
-5.upto(-3).size.should == 3
end
it "returns 0 when stop < self" do
5.upto(4).size.should == 0
0.upto(-5).size.should == 0
-3.upto(-5).size.should == 0
end
end
end
end
end