This commit is contained in:
Andrew Konchin 2024-06-07 19:01:53 +03:00 committed by Benoit Daloze
parent e8bd745c17
commit 3ebab4b64d
12 changed files with 1238 additions and 1268 deletions

View File

@ -1,7 +1,7 @@
inherit_from: .rubocop_todo.yml
AllCops:
TargetRubyVersion: 3.0
TargetRubyVersion: 3.1
DisplayCopNames: true
Exclude:
- command_line/fixtures/bad_syntax.rb

View File

@ -60,19 +60,16 @@ module ArraySpecs
def self.measure_sample_fairness(size, samples, iters)
ary = Array.new(size) { |x| x }
expected = iters.fdiv size
(samples).times do |i|
chi_results = []
3.times do
counts = Array.new(size) { 0 }
expected = iters / size
counts = Array.new(size, 0)
iters.times do
x = ary.sample(samples)[i]
counts[x] += 1
end
chi_squared = 0.0
counts.each do |count|
chi_squared += (((count - expected) ** 2) * 1.0 / expected)
end
chi_squared = counts.sum {|count| (count - expected) ** 2} / expected
chi_results << chi_squared
break if chi_squared <= CHI_SQUARED_CRITICAL_VALUES[size]
end
@ -83,17 +80,14 @@ module ArraySpecs
def self.measure_sample_fairness_large_sample_size(size, samples, iters)
ary = Array.new(size) { |x| x }
counts = Array.new(size) { 0 }
expected = iters * samples / size
counts = Array.new(size, 0)
expected = (iters * samples).fdiv size
iters.times do
ary.sample(samples).each do |sample|
counts[sample] += 1
end
end
chi_squared = 0.0
counts.each do |count|
chi_squared += (((count - expected) ** 2) * 1.0 / expected)
end
chi_squared = counts.sum {|count| (count - expected) ** 2} / expected
# Chi squared critical values for tests with 4 degrees of freedom
# Values obtained from NIST Engineering Statistic Handbook at
@ -223,7 +217,8 @@ module ArraySpecs
obj
end
LargeArray = ["test_create_table_with_force_true_does_not_drop_nonexisting_table",
LargeArray = [
"test_create_table_with_force_true_does_not_drop_nonexisting_table",
"test_add_table",
"assert_difference",
"assert_operator",
@ -513,9 +508,11 @@ module ArraySpecs
"assert_respond_to",
"test_change_column_default_to_null",
"assert_same",
"__extend__"]
"__extend__",
]
LargeTestArraySorted = ["test_add_column_not_null_with_default",
LargeTestArraySorted = [
"test_add_column_not_null_with_default",
"test_add_column_with_precision_and_scale",
"test_add_column_with_primary_key_attribute",
"test_add_drop_table_with_prefix_and_suffix",
@ -582,7 +579,8 @@ module ArraySpecs
"test_rename_table_for_sqlite_should_work_with_reserved_words",
"test_rename_table_with_an_index",
"test_schema_migrations_table_name",
"test_target_version_zero_should_run_only_once"]
"test_target_version_zero_should_run_only_once",
]
class PrivateToAry
private

View File

@ -28,6 +28,16 @@ describe "Array#pack with :buffer option" do
TypeError, "buffer must be String, not Array")
end
it "raise FrozenError if buffer is frozen" do
-> { [65].pack("c", buffer: "frozen-string".freeze) }.should raise_error(FrozenError)
end
it "preserves the encoding of the given buffer" do
buffer = ''.encode(Encoding::ISO_8859_1)
[65, 66, 67].pack("ccc", buffer: buffer)
buffer.encoding.should == Encoding::ISO_8859_1
end
context "offset (@) is specified" do
it 'keeps buffer content if it is longer than offset' do
n = [ 65, 66, 67 ]

View File

@ -1,12 +1,12 @@
module IOWaitSpec
module IOSpec
def self.exhaust_write_buffer(io)
written = 0
buf = " " * 4096
begin
while true
written += io.write_nonblock(buf)
end
rescue Errno::EAGAIN, Errno::EWOULDBLOCK
return written
end while true
written
end
end

View File

@ -427,7 +427,7 @@ describe "The 'case'-construct" do
:bar
end
RUBY
}.should complain(/warning: duplicated .when' clause with line \d+ is ignored/, verbose: true)
}.should complain(/warning: (duplicated .when' clause with line \d+ is ignored|'when' clause on line \d+ duplicates 'when' clause on line \d+ and is ignored)/, verbose: true)
end
end

View File

@ -0,0 +1,75 @@
describe "Pattern matching" do
before :each do
ScratchPad.record []
end
describe "Ruby 3.1 improvements" do
ruby_version_is "3.1" do
it "can omit parentheses in one line pattern matching" do
[1, 2] => a, b
[a, b].should == [1, 2]
{a: 1} => a:
a.should == 1
end
it "supports pinning instance variables" do
@a = /a/
case 'abc'
in ^@a
true
end.should == true
end
it "supports pinning class variables" do
result = nil
Module.new do
result = module_eval(<<~RUBY)
@@a = 0..10
case 2
in ^@@a
true
end
RUBY
end
result.should == true
end
it "supports pinning global variables" do
$a = /a/
case 'abc'
in ^$a
true
end.should == true
end
it "supports pinning expressions" do
case 'abc'
in ^(/a/)
true
end.should == true
case 0
in ^(0 + 0)
true
end.should == true
end
it "supports pinning expressions in array pattern" do
case [3]
in [^(1 + 2)]
true
end.should == true
end
it "supports pinning expressions in hash pattern" do
case {name: '2.6', released_at: Time.new(2018, 12, 25)}
in {released_at: ^(Time.new(2010)..Time.new(2020))}
true
end.should == true
end
end
end
end

File diff suppressed because it is too large Load Diff

View File

@ -24,4 +24,23 @@ describe "IO#wait_readable" do
it "waits for the IO to become readable with the given large timeout" do
@io.wait_readable(365 * 24 * 60 * 60).should == @io
end
it "can be interrupted" do
rd, wr = IO.pipe
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
t = Thread.new do
rd.wait_readable(10)
end
Thread.pass until t.stop?
t.kill
t.join
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
(finish - start).should < 9
ensure
rd.close
wr.close
end
end

View File

@ -1,5 +1,5 @@
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative '../../fixtures/io'
ruby_version_is ''...'3.2' do
require 'io/wait'
@ -55,7 +55,7 @@ describe "IO#wait" do
end
it "waits for the WRITABLE event to be ready" do
written_bytes = IOWaitSpec.exhaust_write_buffer(@w)
written_bytes = IOSpec.exhaust_write_buffer(@w)
@w.wait(IO::WRITABLE, 0).should == nil
@r.read(written_bytes)
@ -67,7 +67,7 @@ describe "IO#wait" do
end
it "returns nil when the WRITABLE event is not ready during the timeout" do
IOWaitSpec.exhaust_write_buffer(@w)
IOSpec.exhaust_write_buffer(@w)
@w.wait(IO::WRITABLE, 0).should == nil
end
@ -92,7 +92,7 @@ describe "IO#wait" do
end
it "changes thread status to 'sleep' when waits for WRITABLE event" do
written_bytes = IOWaitSpec.exhaust_write_buffer(@w)
IOSpec.exhaust_write_buffer(@w)
t = Thread.new { @w.wait(IO::WRITABLE, 10) }
sleep 1
@ -100,6 +100,37 @@ describe "IO#wait" do
t.kill
t.join # Thread#kill doesn't wait for the thread to end
end
it "can be interrupted when waiting for READABLE event" do
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
t = Thread.new do
@r.wait(IO::READABLE, 10)
end
Thread.pass until t.stop?
t.kill
t.join # Thread#kill doesn't wait for the thread to end
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
(finish - start).should < 9
end
it "can be interrupted when waiting for WRITABLE event" do
IOSpec.exhaust_write_buffer(@w)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
t = Thread.new do
@w.wait(IO::WRITABLE, 10)
end
Thread.pass until t.stop?
t.kill
t.join # Thread#kill doesn't wait for the thread to end
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
(finish - start).should < 9
end
end
context "[timeout, mode] passed" do

View File

@ -1,4 +1,5 @@
require_relative '../../spec_helper'
require_relative '../../fixtures/io'
ruby_version_is ''...'3.2' do
require 'io/wait'
@ -17,4 +18,24 @@ describe "IO#wait_writable" do
# Represents one year and is larger than a 32-bit int
STDOUT.wait_writable(365 * 24 * 60 * 60).should == STDOUT
end
it "can be interrupted" do
rd, wr = IO.pipe
IOSpec.exhaust_write_buffer(wr)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
t = Thread.new do
wr.wait_writable(10)
end
Thread.pass until t.stop?
t.kill
t.join
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
(finish - start).should < 9
ensure
rd.close unless rd.closed?
wr.close unless wr.closed?
end
end

View File

@ -88,6 +88,30 @@ describe 'RbConfig::CONFIG' do
end
end
end
guard -> { %w[aarch64 arm64].include? RbConfig::CONFIG['host_cpu'] } do
it "['host_cpu'] returns CPU architecture properly for AArch64" do
platform_is :darwin do
RbConfig::CONFIG['host_cpu'].should == 'arm64'
end
platform_is_not :darwin do
RbConfig::CONFIG['host_cpu'].should == 'aarch64'
end
end
end
guard -> { platform_is(:linux) || platform_is(:darwin) } do
it "['host_os'] returns a proper OS name or platform" do
platform_is :darwin do
RbConfig::CONFIG['host_os'].should.match?(/darwin/)
end
platform_is :linux do
RbConfig::CONFIG['host_os'].should.match?(/linux/)
end
end
end
end
describe "RbConfig::TOPDIR" do
@ -99,3 +123,32 @@ describe "RbConfig::TOPDIR" do
end
end
end
describe "RUBY_PLATFORM" do
it "RUBY_PLATFORM contains a proper CPU architecture" do
RUBY_PLATFORM.should.include? RbConfig::CONFIG['host_cpu']
end
guard -> { platform_is(:linux) || platform_is(:darwin) } do
it "RUBY_PLATFORM contains OS name" do
# don't use RbConfig::CONFIG['host_os'] as far as it could be slightly different, e.g. linux-gnu
platform_is(:linux) do
RUBY_PLATFORM.should.include? 'linux'
end
platform_is(:darwin) do
RUBY_PLATFORM.should.include? 'darwin'
end
end
end
end
describe "RUBY_DESCRIPTION" do
it "contains version" do
RUBY_DESCRIPTION.should.include? RUBY_VERSION
end
it "contains RUBY_PLATFORM" do
RUBY_DESCRIPTION.should.include? RUBY_PLATFORM
end
end

View File

@ -1,4 +1,5 @@
require_relative 'spec_helper'
require_relative '../../fixtures/io'
load_extension('io')
@ -279,6 +280,22 @@ describe "C-API IO function" do
it "raises an IOError if the IO is not initialized" do
-> { @o.rb_io_maybe_wait_writable(0, IO.allocate, nil) }.should raise_error(IOError, "uninitialized stream")
end
it "can be interrupted" do
IOSpec.exhaust_write_buffer(@w_io)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
t = Thread.new do
@o.rb_io_maybe_wait_writable(0, @w_io, 10)
end
Thread.pass until t.stop?
t.kill
t.join
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
(finish - start).should < 9
end
end
end
@ -355,6 +372,21 @@ describe "C-API IO function" do
thr.join
end
it "can be interrupted" do
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
t = Thread.new do
@o.rb_io_maybe_wait_readable(0, @r_io, 10, false)
end
Thread.pass until t.stop?
t.kill
t.join
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
(finish - start).should < 9
end
it "raises an IOError if the IO is closed" do
@r_io.close
-> { @o.rb_io_maybe_wait_readable(0, @r_io, nil, false) }.should raise_error(IOError, "closed stream")
@ -438,6 +470,37 @@ describe "C-API IO function" do
it "raises an IOError if the IO is not initialized" do
-> { @o.rb_io_maybe_wait(0, IO.allocate, IO::WRITABLE, nil) }.should raise_error(IOError, "uninitialized stream")
end
it "can be interrupted when waiting for READABLE event" do
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
t = Thread.new do
@o.rb_io_maybe_wait(0, @r_io, IO::READABLE, 10)
end
Thread.pass until t.stop?
t.kill
t.join
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
(finish - start).should < 9
end
it "can be interrupted when waiting for WRITABLE event" do
IOSpec.exhaust_write_buffer(@w_io)
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
t = Thread.new do
@o.rb_io_maybe_wait(0, @w_io, IO::WRITABLE, 10)
end
Thread.pass until t.stop?
t.kill
t.join
finish = Process.clock_gettime(Process::CLOCK_MONOTONIC)
(finish - start).should < 9
end
end
end