Skip to content

Commit d3d5ef0

Browse files
committed
1 parent f616e81 commit d3d5ef0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1195
-318
lines changed

spec/ruby/.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ Lint/DuplicateElsifCondition:
9999
Lint/OutOfRangeRegexpRef:
100100
Enabled: false
101101

102+
Lint/InheritException:
103+
Enabled: false
104+
102105
Lint/ElseLayout:
103106
Exclude:
104107
- 'language/if_spec.rb'

spec/ruby/.rubocop_todo.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,6 @@ Lint/IneffectiveAccessModifier:
5050
- 'core/module/fixtures/classes.rb'
5151
- 'language/fixtures/private.rb'
5252

53-
# Offense count: 6
54-
# Cop supports --auto-correct.
55-
# Configuration parameters: EnforcedStyle.
56-
# SupportedStyles: runtime_error, standard_error
57-
Lint/InheritException:
58-
Exclude:
59-
- 'core/enumerator/lazy/fixtures/classes.rb'
60-
- 'core/exception/fixtures/common.rb'
61-
- 'core/module/fixtures/autoload_ex1.rb'
62-
- 'shared/kernel/raise.rb'
63-
6453
# Offense count: 72
6554
# Cop supports --auto-correct.
6655
Lint/LiteralInInterpolation:

spec/ruby/core/array/fill_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@
205205
-> { [].fill('a', obj) }.should raise_error(TypeError)
206206
end
207207

208+
it "raises a TypeError when the length is not numeric" do
209+
-> { [1, 2, 3].fill("x", 1, "foo") }.should raise_error(TypeError, /no implicit conversion of String into Integer/)
210+
-> { [1, 2, 3].fill("x", 1, :"foo") }.should raise_error(TypeError, /no implicit conversion of Symbol into Integer/)
211+
-> { [1, 2, 3].fill("x", 1, Object.new) }.should raise_error(TypeError, /no implicit conversion of Object into Integer/)
212+
end
213+
208214
not_supported_on :opal do
209215
it "raises an ArgumentError or RangeError for too-large sizes" do
210216
error_types = [RangeError, ArgumentError]

spec/ruby/core/array/fixtures/classes.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,68 @@ def self.empty_recursive_array
4040
a
4141
end
4242

43+
# Chi squared critical values for tests with n degrees of freedom at 99% confidence.
44+
# Values obtained from NIST Engineering Statistic Handbook at
45+
# https://www.itl.nist.gov/div898/handbook/eda/section3/eda3674.htm
46+
47+
CHI_SQUARED_CRITICAL_VALUES = [
48+
0,
49+
6.635, 9.210, 11.345, 13.277, 15.086, 16.812, 18.475, 20.090, 21.666, 23.209,
50+
24.725, 26.217, 27.688, 29.141, 30.578, 32.000, 33.409, 34.805, 36.191, 37.566,
51+
38.932, 40.289, 41.638, 42.980, 44.314, 45.642, 46.963, 48.278, 49.588, 50.892,
52+
52.191, 53.486, 54.776, 56.061, 57.342, 58.619, 59.893, 61.162, 62.428, 63.691,
53+
64.950, 66.206, 67.459, 68.710, 69.957, 71.201, 72.443, 73.683, 74.919, 76.154,
54+
77.386, 78.616, 79.843, 81.069, 82.292, 83.513, 84.733, 85.950, 87.166, 88.379,
55+
89.591, 90.802, 92.010, 93.217, 94.422, 95.626, 96.828, 98.028, 99.228, 100.425,
56+
101.621, 102.816, 104.010, 105.202, 106.393, 107.583, 108.771, 109.958, 111.144, 112.329,
57+
113.512, 114.695, 115.876, 117.057, 118.236, 119.414, 120.591, 121.767, 122.942, 124.116,
58+
125.289, 126.462, 127.633, 128.803, 129.973, 131.141, 132.309, 133.476, 134.642, 135.807,
59+
]
60+
61+
def self.measure_sample_fairness(size, samples, iters)
62+
ary = Array.new(size) { |x| x }
63+
(samples).times do |i|
64+
chi_results = []
65+
3.times do
66+
counts = Array.new(size) { 0 }
67+
expected = iters / size
68+
iters.times do
69+
x = ary.sample(samples)[i]
70+
counts[x] += 1
71+
end
72+
chi_squared = 0.0
73+
counts.each do |count|
74+
chi_squared += (((count - expected) ** 2) * 1.0 / expected)
75+
end
76+
chi_results << chi_squared
77+
break if chi_squared <= CHI_SQUARED_CRITICAL_VALUES[size]
78+
end
79+
80+
chi_results.min.should <= CHI_SQUARED_CRITICAL_VALUES[size]
81+
end
82+
end
83+
84+
def self.measure_sample_fairness_large_sample_size(size, samples, iters)
85+
ary = Array.new(size) { |x| x }
86+
counts = Array.new(size) { 0 }
87+
expected = iters * samples / size
88+
iters.times do
89+
ary.sample(samples).each do |sample|
90+
counts[sample] += 1
91+
end
92+
end
93+
chi_squared = 0.0
94+
counts.each do |count|
95+
chi_squared += (((count - expected) ** 2) * 1.0 / expected)
96+
end
97+
98+
# Chi squared critical values for tests with 4 degrees of freedom
99+
# Values obtained from NIST Engineering Statistic Handbook at
100+
# https://www.itl.nist.gov/div898/handbook/eda/section3/eda3674.htm
101+
102+
chi_squared.should <= CHI_SQUARED_CRITICAL_VALUES[size]
103+
end
104+
43105
class MyArray < Array
44106
# The #initialize method has a different signature than Array to help
45107
# catch places in the specs that do not assert the #initialize is not

spec/ruby/core/array/sample_spec.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33

44
describe "Array#sample" do
55
it "samples evenly" do
6-
ary = [0, 1, 2, 3]
7-
3.times do |i|
8-
counts = [0, 0, 0, 0]
9-
4000.times do
10-
counts[ary.sample(3)[i]] += 1
11-
end
12-
counts.each do |count|
13-
(800..1200).should include(count)
14-
end
15-
end
6+
ArraySpecs.measure_sample_fairness(4, 1, 400)
7+
ArraySpecs.measure_sample_fairness(4, 2, 400)
8+
ArraySpecs.measure_sample_fairness(4, 3, 400)
9+
ArraySpecs.measure_sample_fairness(40, 3, 400)
10+
ArraySpecs.measure_sample_fairness(40, 4, 400)
11+
ArraySpecs.measure_sample_fairness(40, 8, 400)
12+
ArraySpecs.measure_sample_fairness(40, 16, 400)
13+
ArraySpecs.measure_sample_fairness_large_sample_size(100, 80, 4000)
1614
end
1715

1816
it "returns nil for an empty Array" do

spec/ruby/core/dir/foreach_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@
4141

4242
it "accepts an encoding keyword for the encoding of the entries" do
4343
dirs = Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: "utf-8").to_a.sort
44-
dirs.each {|dir| dir.encoding.should == Encoding::UTF_8}
44+
dirs.each { |dir| dir.encoding.should == Encoding::UTF_8 }
4545

46-
dirs = Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: Encoding::UTF_16LE).to_a.sort
47-
dirs.each {|dir| dir.encoding.should == Encoding::UTF_16LE}
46+
dirs = Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: Encoding::ISO_8859_1).to_a.sort
47+
dirs.each { |dir| dir.encoding.should == Encoding::ISO_8859_1 }
4848

49-
Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: Encoding::UTF_16LE) do |f|
50-
f.encoding.should == Encoding::UTF_16LE
49+
Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: Encoding::ISO_8859_1) do |f|
50+
f.encoding.should == Encoding::ISO_8859_1
5151
end
5252
end
5353

spec/ruby/core/exception/signal_exception_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393

9494
platform_is_not :windows do
9595
it "runs after at_exit" do
96-
output = ruby_exe(<<-RUBY, exit_status: nil)
96+
output = ruby_exe(<<-RUBY, exit_status: :SIGKILL)
9797
at_exit do
9898
puts "hello"
9999
$stdout.flush
@@ -107,7 +107,7 @@
107107
end
108108

109109
it "cannot be trapped with Signal.trap" do
110-
ruby_exe(<<-RUBY, exit_status: nil)
110+
ruby_exe(<<-RUBY, exit_status: :SIGPROF)
111111
Signal.trap("PROF") {}
112112
raise(SignalException, "PROF")
113113
RUBY
@@ -116,7 +116,7 @@
116116
end
117117

118118
it "self-signals for USR1" do
119-
ruby_exe("raise(SignalException, 'USR1')", exit_status: nil)
119+
ruby_exe("raise(SignalException, 'USR1')", exit_status: :SIGUSR1)
120120
$?.termsig.should == Signal.list.fetch('USR1')
121121
end
122122
end

spec/ruby/core/file/open_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,14 @@
494494
File.open(@file, "w") { |f| f.puts "testing" }
495495
File.size(@file).should > 0
496496
File.open(@file, "rb+") do |f|
497+
f.binmode?.should == true
498+
f.external_encoding.should == Encoding::ASCII_8BIT
499+
f.pos.should == 0
500+
f.should_not.eof?
501+
end
502+
File.open(@file, "r+b") do |f|
503+
f.binmode?.should == true
504+
f.external_encoding.should == Encoding::ASCII_8BIT
497505
f.pos.should == 0
498506
f.should_not.eof?
499507
end

spec/ruby/core/float/divide_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@
3636
-> { 13.0 / "10" }.should raise_error(TypeError)
3737
-> { 13.0 / :symbol }.should raise_error(TypeError)
3838
end
39+
40+
it "divides correctly by Rational numbers" do
41+
(1.2345678901234567 / Rational(1, 10000000000000000000)).should == 1.2345678901234567e+19
42+
end
3943
end

spec/ruby/core/float/round_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,70 @@
103103
5.55.round(1, half: :up).should eql(5.6)
104104
5.55.round(1, half: :down).should eql(5.5)
105105
5.55.round(1, half: :even).should eql(5.6)
106+
-5.55.round(1, half: nil).should eql(-5.6)
107+
-5.55.round(1, half: :up).should eql(-5.6)
108+
-5.55.round(1, half: :down).should eql(-5.5)
109+
-5.55.round(1, half: :even).should eql(-5.6)
110+
end
111+
112+
it "preserves cases where neighbouring floating pointer number increase the decimal places" do
113+
4.8100000000000005.round(5, half: nil).should eql(4.81)
114+
4.8100000000000005.round(5, half: :up).should eql(4.81)
115+
4.8100000000000005.round(5, half: :down).should eql(4.81)
116+
4.8100000000000005.round(5, half: :even).should eql(4.81)
117+
-4.8100000000000005.round(5, half: nil).should eql(-4.81)
118+
-4.8100000000000005.round(5, half: :up).should eql(-4.81)
119+
-4.8100000000000005.round(5, half: :down).should eql(-4.81)
120+
-4.8100000000000005.round(5, half: :even).should eql(-4.81)
121+
4.81.round(5, half: nil).should eql(4.81)
122+
4.81.round(5, half: :up).should eql(4.81)
123+
4.81.round(5, half: :down).should eql(4.81)
124+
4.81.round(5, half: :even).should eql(4.81)
125+
-4.81.round(5, half: nil).should eql(-4.81)
126+
-4.81.round(5, half: :up).should eql(-4.81)
127+
-4.81.round(5, half: :down).should eql(-4.81)
128+
-4.81.round(5, half: :even).should eql(-4.81)
129+
4.809999999999999.round(5, half: nil).should eql(4.81)
130+
4.809999999999999.round(5, half: :up).should eql(4.81)
131+
4.809999999999999.round(5, half: :down).should eql(4.81)
132+
4.809999999999999.round(5, half: :even).should eql(4.81)
133+
-4.809999999999999.round(5, half: nil).should eql(-4.81)
134+
-4.809999999999999.round(5, half: :up).should eql(-4.81)
135+
-4.809999999999999.round(5, half: :down).should eql(-4.81)
136+
-4.809999999999999.round(5, half: :even).should eql(-4.81)
137+
end
138+
139+
ruby_bug "", ""..."3.3" do
140+
# These numbers are neighbouring floating point numbers round a
141+
# precise value. They test that the rounding modes work correctly
142+
# round that value and precision is not lost which might cause
143+
# incorrect results.
144+
it "does not lose precision during the rounding process" do
145+
767573.1875850001.round(5, half: nil).should eql(767573.18759)
146+
767573.1875850001.round(5, half: :up).should eql(767573.18759)
147+
767573.1875850001.round(5, half: :down).should eql(767573.18759)
148+
767573.1875850001.round(5, half: :even).should eql(767573.18759)
149+
-767573.1875850001.round(5, half: nil).should eql(-767573.18759)
150+
-767573.1875850001.round(5, half: :up).should eql(-767573.18759)
151+
-767573.1875850001.round(5, half: :down).should eql(-767573.18759)
152+
-767573.1875850001.round(5, half: :even).should eql(-767573.18759)
153+
767573.187585.round(5, half: nil).should eql(767573.18759)
154+
767573.187585.round(5, half: :up).should eql(767573.18759)
155+
767573.187585.round(5, half: :down).should eql(767573.18758)
156+
767573.187585.round(5, half: :even).should eql(767573.18758)
157+
-767573.187585.round(5, half: nil).should eql(-767573.18759)
158+
-767573.187585.round(5, half: :up).should eql(-767573.18759)
159+
-767573.187585.round(5, half: :down).should eql(-767573.18758)
160+
-767573.187585.round(5, half: :even).should eql(-767573.18758)
161+
767573.1875849998.round(5, half: nil).should eql(767573.18758)
162+
767573.1875849998.round(5, half: :up).should eql(767573.18758)
163+
767573.1875849998.round(5, half: :down).should eql(767573.18758)
164+
767573.1875849998.round(5, half: :even).should eql(767573.18758)
165+
-767573.1875849998.round(5, half: nil).should eql(-767573.18758)
166+
-767573.1875849998.round(5, half: :up).should eql(-767573.18758)
167+
-767573.1875849998.round(5, half: :down).should eql(-767573.18758)
168+
-767573.1875849998.round(5, half: :even).should eql(-767573.18758)
169+
end
106170
end
107171

108172
it "raises FloatDomainError for exceptional values with a half option" do

0 commit comments

Comments
 (0)