diff options
author | Benoit Daloze <[email protected]> | 2019-05-28 22:41:48 +0200 |
---|---|---|
committer | Benoit Daloze <[email protected]> | 2019-05-28 22:41:48 +0200 |
commit | a66bc2c01194a9c017c874a30db5b3b6bd95e966 (patch) | |
tree | 598d6375b44fd86f90c3477c73086f6fcf08d76c /spec/ruby/library | |
parent | d070523e7be4b95914adeef9a10401fba7718c5a (diff) |
Update to ruby/spec@9a501a8
Diffstat (limited to 'spec/ruby/library')
-rw-r--r-- | spec/ruby/library/bigdecimal/BigDecimal_spec.rb | 28 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/clone_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/constants_spec.rb | 76 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/dup_spec.rb | 6 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/hash_spec.rb | 30 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/inspect_spec.rb | 7 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/shared/clone.rb | 24 | ||||
-rw-r--r-- | spec/ruby/library/bigdecimal/to_d_spec.rb | 11 | ||||
-rw-r--r-- | spec/ruby/library/objectspace/memsize_of_spec.rb | 30 | ||||
-rw-r--r-- | spec/ruby/library/objectspace/reachable_objects_from_spec.rb | 61 | ||||
-rw-r--r-- | spec/ruby/library/socket/basicsocket/do_not_reverse_lookup_spec.rb | 64 | ||||
-rw-r--r-- | spec/ruby/library/socket/fixtures/classes.rb | 21 | ||||
-rw-r--r-- | spec/ruby/library/socket/socket/tcp_server_loop_spec.rb | 9 | ||||
-rw-r--r-- | spec/ruby/library/socket/socket/udp_server_loop_spec.rb | 14 | ||||
-rw-r--r-- | spec/ruby/library/socket/socket/unix_server_loop_spec.rb | 9 | ||||
-rw-r--r-- | spec/ruby/library/tempfile/open_spec.rb | 8 |
16 files changed, 381 insertions, 23 deletions
diff --git a/spec/ruby/library/bigdecimal/BigDecimal_spec.rb b/spec/ruby/library/bigdecimal/BigDecimal_spec.rb index 2312a4e910..98b3f47703 100644 --- a/spec/ruby/library/bigdecimal/BigDecimal_spec.rb +++ b/spec/ruby/library/bigdecimal/BigDecimal_spec.rb @@ -100,6 +100,34 @@ describe "Kernel#BigDecimal" do neg_inf.should < 0 end + describe "accepts NaN and [+-]Infinity as Float values" do + it "works without an explicit precision" do + BigDecimal(Float::NAN).nan?.should == true + + pos_inf = BigDecimal(Float::INFINITY) + pos_inf.finite?.should == false + pos_inf.should > 0 + pos_inf.should == BigDecimal("+Infinity") + + neg_inf = BigDecimal(-Float::INFINITY) + neg_inf.finite?.should == false + neg_inf.should < 0 + end + + it "works with an explicit precision" do + BigDecimal(Float::NAN, Float::DIG).nan?.should == true + + pos_inf = BigDecimal(Float::INFINITY, Float::DIG) + pos_inf.finite?.should == false + pos_inf.should > 0 + pos_inf.should == BigDecimal("+Infinity") + + neg_inf = BigDecimal(-Float::INFINITY, Float::DIG) + neg_inf.finite?.should == false + neg_inf.should < 0 + end + end + it "allows for [eEdD] as exponent separator" do reference = BigDecimal("12345.67E89") diff --git a/spec/ruby/library/bigdecimal/clone_spec.rb b/spec/ruby/library/bigdecimal/clone_spec.rb new file mode 100644 index 0000000000..b3a1c61d6a --- /dev/null +++ b/spec/ruby/library/bigdecimal/clone_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/clone' + +describe "BigDecimal#dup" do + it_behaves_like :bigdecimal_clone, :clone +end diff --git a/spec/ruby/library/bigdecimal/constants_spec.rb b/spec/ruby/library/bigdecimal/constants_spec.rb new file mode 100644 index 0000000000..1e08c6779b --- /dev/null +++ b/spec/ruby/library/bigdecimal/constants_spec.rb @@ -0,0 +1,76 @@ +require_relative '../../spec_helper' +require 'bigdecimal' + +describe "BigDecimal constants" do + ruby_version_is "2.5" do + it "defines a VERSION value" do + BigDecimal.const_defined?(:VERSION).should be_true + end + end + + it "has a BASE value" do + platform_is wordsize: 64 do + BigDecimal::BASE.should == 1000000000 + end + + platform_is wordsize: 32 do + BigDecimal::BASE.should == 10000 + end + end + + it "has a NaN value" do + BigDecimal::NAN.nan?.should be_true + end + + it "has an INFINITY value" do + BigDecimal::INFINITY.infinite?.should == 1 + end + + describe "exception-related constants" do + [ + [:EXCEPTION_ALL, 0xff], + [:EXCEPTION_INFINITY, 0x01], + [:EXCEPTION_NaN, 0x02], + [:EXCEPTION_UNDERFLOW, 0x04], + [:EXCEPTION_OVERFLOW, 0x01], + [:EXCEPTION_ZERODIVIDE, 0x10] + ].each do |const, value| + it "has a #{const} value" do + BigDecimal.const_get(const).should == value + end + end + end + + describe "rounding-related constants" do + [ + [:ROUND_MODE, 0x100], + [:ROUND_UP, 1], + [:ROUND_DOWN, 2], + [:ROUND_HALF_UP, 3], + [:ROUND_HALF_DOWN, 4], + [:ROUND_CEILING, 5], + [:ROUND_FLOOR, 6], + [:ROUND_HALF_EVEN, 7] + ].each do |const, value| + it "has a #{const} value" do + BigDecimal.const_get(const).should == value + end + end + end + + describe "sign-related constants" do + [ + [:SIGN_NaN, 0], + [:SIGN_POSITIVE_ZERO, 1], + [:SIGN_NEGATIVE_ZERO, -1], + [:SIGN_POSITIVE_FINITE, 2], + [:SIGN_NEGATIVE_FINITE, -2], + [:SIGN_POSITIVE_INFINITE, 3], + [:SIGN_NEGATIVE_INFINITE, -3] + ].each do |const, value| + it "has a #{const} value" do + BigDecimal.const_get(const).should == value + end + end + end +end diff --git a/spec/ruby/library/bigdecimal/dup_spec.rb b/spec/ruby/library/bigdecimal/dup_spec.rb new file mode 100644 index 0000000000..bfabaf6e8b --- /dev/null +++ b/spec/ruby/library/bigdecimal/dup_spec.rb @@ -0,0 +1,6 @@ +require_relative '../../spec_helper' +require_relative 'shared/clone' + +describe "BigDecimal#dup" do + it_behaves_like :bigdecimal_clone, :dup +end diff --git a/spec/ruby/library/bigdecimal/hash_spec.rb b/spec/ruby/library/bigdecimal/hash_spec.rb new file mode 100644 index 0000000000..7581c90f68 --- /dev/null +++ b/spec/ruby/library/bigdecimal/hash_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../spec_helper' +require 'bigdecimal' + +describe "BidDecimal#hash" do + describe "two BigDecimal objects with the same value" do + it "should have the same hash for ordinary values" do + BigDecimal('1.2920').hash.should == BigDecimal('1.2920').hash + end + + it "should have the same hash for infinite values" do + BigDecimal("+Infinity").hash.should == BigDecimal("+Infinity").hash + BigDecimal("-Infinity").hash.should == BigDecimal("-Infinity").hash + end + + it "should have the same hash for NaNs" do + BigDecimal("NaN").hash.should == BigDecimal("NaN").hash + end + + it "should have the same hash for zero values" do + BigDecimal("+0").hash.should == BigDecimal("+0").hash + BigDecimal("-0").hash.should == BigDecimal("-0").hash + end + end + + describe "two BigDecimal objects with numerically equal values" do + it "should have the same hash value" do + BigDecimal("1.2920").hash.should == BigDecimal("1.2920000").hash + end + end +end diff --git a/spec/ruby/library/bigdecimal/inspect_spec.rb b/spec/ruby/library/bigdecimal/inspect_spec.rb index cd2f1a3cd4..18e9ca9a0c 100644 --- a/spec/ruby/library/bigdecimal/inspect_spec.rb +++ b/spec/ruby/library/bigdecimal/inspect_spec.rb @@ -14,4 +14,11 @@ describe "BigDecimal#inspect" do it "looks like this" do @bigdec.inspect.should == "0.12345678e4" end + + it "properly cases non-finite values" do + BigDecimal("NaN").inspect.should == "NaN" + BigDecimal("Infinity").inspect.should == "Infinity" + BigDecimal("+Infinity").inspect.should == "Infinity" + BigDecimal("-Infinity").inspect.should == "-Infinity" + end end diff --git a/spec/ruby/library/bigdecimal/shared/clone.rb b/spec/ruby/library/bigdecimal/shared/clone.rb new file mode 100644 index 0000000000..e58df3a94b --- /dev/null +++ b/spec/ruby/library/bigdecimal/shared/clone.rb @@ -0,0 +1,24 @@ +require 'bigdecimal' + +describe :bigdecimal_clone, shared: true do + before :each do + @obj = BigDecimal("1.2345") + end + + ruby_version_is "" ... "2.5" do + it "copies the BigDecimal's value to a newly allocated object" do + copy = @obj.public_send(@method) + + copy.should_not equal(@obj) + copy.should == @obj + end + end + + ruby_version_is "2.5" do + it "returns self" do + copy = @obj.public_send(@method) + + copy.should equal(@obj) + end + end +end diff --git a/spec/ruby/library/bigdecimal/to_d_spec.rb b/spec/ruby/library/bigdecimal/to_d_spec.rb new file mode 100644 index 0000000000..8e20901fd9 --- /dev/null +++ b/spec/ruby/library/bigdecimal/to_d_spec.rb @@ -0,0 +1,11 @@ +require_relative '../../spec_helper' +require 'bigdecimal' +require 'bigdecimal/util' + + +describe "Float#to_d" do + it "returns appropriate BigDecimal zero for signed zero" do + -0.0.to_d.sign.should == -1 + 0.0.to_d.sign.should == 1 + end +end diff --git a/spec/ruby/library/objectspace/memsize_of_spec.rb b/spec/ruby/library/objectspace/memsize_of_spec.rb new file mode 100644 index 0000000000..06a80d9f9c --- /dev/null +++ b/spec/ruby/library/objectspace/memsize_of_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../spec_helper' +require 'objspace' + +describe "ObjectSpace.memsize_of" do + it "returns 0 for true, false and nil" do + ObjectSpace.memsize_of(true).should == 0 + ObjectSpace.memsize_of(false).should == 0 + ObjectSpace.memsize_of(nil).should == 0 + end + + it "returns 0 for small Integers" do + ObjectSpace.memsize_of(42).should == 0 + end + + it "returns an Integer for an Object" do + obj = Object.new + ObjectSpace.memsize_of(obj).should be_kind_of(Integer) + ObjectSpace.memsize_of(obj).should > 0 + end + + it "is larger if the Object has more instance variables" do + obj = Object.new + before = ObjectSpace.memsize_of(obj) + 100.times do |i| + obj.instance_variable_set(:"@foo#{i}", nil) + end + after = ObjectSpace.memsize_of(obj) + after.should > before + end +end diff --git a/spec/ruby/library/objectspace/reachable_objects_from_spec.rb b/spec/ruby/library/objectspace/reachable_objects_from_spec.rb new file mode 100644 index 0000000000..7e70bc8569 --- /dev/null +++ b/spec/ruby/library/objectspace/reachable_objects_from_spec.rb @@ -0,0 +1,61 @@ +require_relative '../../spec_helper' +require 'objspace' + +describe "ObjectSpace.reachable_objects_from" do + it "returns nil for true and false" do + ObjectSpace.reachable_objects_from(true).should == nil + ObjectSpace.reachable_objects_from(false).should == nil + end + + it "returns nil for nil" do + ObjectSpace.reachable_objects_from(nil).should == nil + end + + it "returns nil for small Integers" do + ObjectSpace.reachable_objects_from(42).should == nil + end + + it "enumerates objects directly reachable from a given object" do + ObjectSpace.reachable_objects_from(['a', 'b', 'c']).should include(Array, 'a', 'b', 'c') + ObjectSpace.reachable_objects_from(Object.new).should == [Object] + end + + it "finds an object stored in an Array" do + obj = Object.new + ary = [obj] + reachable = ObjectSpace.reachable_objects_from(ary) + reachable.should include(obj) + end + + it "finds an object stored in a copy-on-write Array" do + removed = Object.new + obj = Object.new + ary = [removed, obj] + ary.shift + reachable = ObjectSpace.reachable_objects_from(ary) + reachable.should include(obj) + reachable.should_not include(removed) + end + + it "finds an object stored in a Queue" do + require 'thread' + o = Object.new + q = Queue.new + q << o + + reachable = ObjectSpace.reachable_objects_from(q) + reachable = reachable + reachable.flat_map { |r| ObjectSpace.reachable_objects_from(r) } + reachable.should include(o) + end + + it "finds an object stored in a SizedQueue" do + require 'thread' + o = Object.new + q = SizedQueue.new(3) + q << o + + reachable = ObjectSpace.reachable_objects_from(q) + reachable = reachable + reachable.flat_map { |r| ObjectSpace.reachable_objects_from(r) } + reachable.should include(o) + end +end diff --git a/spec/ruby/library/socket/basicsocket/do_not_reverse_lookup_spec.rb b/spec/ruby/library/socket/basicsocket/do_not_reverse_lookup_spec.rb index 85a66275f8..a8800a8493 100644 --- a/spec/ruby/library/socket/basicsocket/do_not_reverse_lookup_spec.rb +++ b/spec/ruby/library/socket/basicsocket/do_not_reverse_lookup_spec.rb @@ -37,3 +37,67 @@ describe "BasicSocket.do_not_reverse_lookup" do @socket.peeraddr[2].should == "127.0.0.1" end end + +describe :socket_do_not_reverse_lookup, shared: true do + it "inherits from BasicSocket.do_not_reverse_lookup when the socket is created" do + @socket = @method.call + reverse = BasicSocket.do_not_reverse_lookup + @socket.do_not_reverse_lookup.should == reverse + + BasicSocket.do_not_reverse_lookup = !reverse + @socket.do_not_reverse_lookup.should == reverse + end + + it "is true when BasicSocket.do_not_reverse_lookup is true" do + BasicSocket.do_not_reverse_lookup = true + @socket = @method.call + @socket.do_not_reverse_lookup.should == true + end + + it "is false when BasicSocket.do_not_reverse_lookup is false" do + BasicSocket.do_not_reverse_lookup = false + @socket = @method.call + @socket.do_not_reverse_lookup.should == false + end + + it "can be changed with #do_not_reverse_lookup=" do + @socket = @method.call + reverse = @socket.do_not_reverse_lookup + @socket.do_not_reverse_lookup = !reverse + @socket.do_not_reverse_lookup.should == !reverse + end +end + +describe "BasicSocket#do_not_reverse_lookup" do + before :each do + @do_not_reverse_lookup = BasicSocket.do_not_reverse_lookup + @server = TCPServer.new('127.0.0.1', 0) + @port = @server.addr[1] + end + + after :each do + @server.close unless @server.closed? + @socket.close if @socket && [email protected]? + BasicSocket.do_not_reverse_lookup = @do_not_reverse_lookup + end + + describe "for an TCPSocket.new socket" do + it_behaves_like :socket_do_not_reverse_lookup, -> { + TCPSocket.new('127.0.0.1', @port) + } + end + + describe "for an TCPServer#accept socket" do + before :each do + @client = TCPSocket.new('127.0.0.1', @port) + end + + after :each do + @client.close if @client && [email protected]? + end + + it_behaves_like :socket_do_not_reverse_lookup, -> { + @server.accept + } + end +end diff --git a/spec/ruby/library/socket/fixtures/classes.rb b/spec/ruby/library/socket/fixtures/classes.rb index 4cfa084333..6cc7ecb389 100644 --- a/spec/ruby/library/socket/fixtures/classes.rb +++ b/spec/ruby/library/socket/fixtures/classes.rb @@ -72,24 +72,11 @@ module SocketSpecs end def self.loop_with_timeout(timeout = TIME_TOLERANCE) - require 'timeout' - time = Process.clock_gettime(Process::CLOCK_MONOTONIC) + start = Process.clock_gettime(Process::CLOCK_MONOTONIC) - loop do - if Process.clock_gettime(Process::CLOCK_MONOTONIC) - time >= timeout - raise TimeoutError, "Did not succeed within #{timeout} seconds" - end - - sleep 0.01 # necessary on OSX; don't know why - yield - end - end - - def self.wait_until_success(timeout = TIME_TOLERANCE) - loop_with_timeout(timeout) do - begin - return yield - rescue + while yield == :retry + if Process.clock_gettime(Process::CLOCK_MONOTONIC) - start >= timeout + raise RuntimeError, "Did not succeed within #{timeout} seconds" end end end diff --git a/spec/ruby/library/socket/socket/tcp_server_loop_spec.rb b/spec/ruby/library/socket/socket/tcp_server_loop_spec.rb index 617e3d445c..15865a028c 100644 --- a/spec/ruby/library/socket/socket/tcp_server_loop_spec.rb +++ b/spec/ruby/library/socket/socket/tcp_server_loop_spec.rb @@ -31,8 +31,13 @@ describe 'Socket.tcp_server_loop' do end end - SocketSpecs.wait_until_success do - @client.connect(Socket.sockaddr_in(@port, '127.0.0.1')) + SocketSpecs.loop_with_timeout do + begin + @client.connect(Socket.sockaddr_in(@port, '127.0.0.1')) + rescue SystemCallError + sleep 0.01 + :retry + end end # At this point the connection has been set up but the thread may not yet diff --git a/spec/ruby/library/socket/socket/udp_server_loop_spec.rb b/spec/ruby/library/socket/socket/udp_server_loop_spec.rb index 95f73a5c35..c6abfb4eb9 100644 --- a/spec/ruby/library/socket/socket/udp_server_loop_spec.rb +++ b/spec/ruby/library/socket/socket/udp_server_loop_spec.rb @@ -35,9 +35,17 @@ describe 'Socket.udp_server_loop' do @client.connect(Socket.sockaddr_in(@port, '127.0.0.1')) SocketSpecs.loop_with_timeout do - SocketSpecs.wait_until_success { @client.write('hello') } - - break if msg + begin + @client.write('hello') + rescue SystemCallError + sleep 0.01 + :retry + else + unless msg + sleep 0.001 + :retry + end + end end msg.should == 'hello' diff --git a/spec/ruby/library/socket/socket/unix_server_loop_spec.rb b/spec/ruby/library/socket/socket/unix_server_loop_spec.rb index 52c535cd45..a2d2ad9fdb 100644 --- a/spec/ruby/library/socket/socket/unix_server_loop_spec.rb +++ b/spec/ruby/library/socket/socket/unix_server_loop_spec.rb @@ -39,7 +39,14 @@ with_feature :unix_socket do end end - @client = SocketSpecs.wait_until_success { Socket.unix(@path) } + SocketSpecs.loop_with_timeout do + begin + @client = Socket.unix(@path) + rescue SystemCallError + sleep 0.01 + :retry + end + end thread.join(2) diff --git a/spec/ruby/library/tempfile/open_spec.rb b/spec/ruby/library/tempfile/open_spec.rb index c4c3d91051..ef2c95376f 100644 --- a/spec/ruby/library/tempfile/open_spec.rb +++ b/spec/ruby/library/tempfile/open_spec.rb @@ -49,6 +49,14 @@ describe "Tempfile.open" do tempfile.binmode?.should be_true end end + + it "uses a blank string for basename when passed no arguments" do + Tempfile.open() do |tempfile| + @tempfile = tempfile + tempfile.closed?.should be_false + end + @tempfile.should_not == nil + end end describe "Tempfile.open when passed a block" do |