Skip to content

Commit ab79607

Browse files
Add specs for case/when and if with out-of-int-range Integer literals
Add specs covering case/when and if with Integer literals outside 32-bit int range (10_000_000_000, -3_000_000_000) and beyond 64-bit long range (1 << 100). Existing case/when specs only exercised small Integers, so optimized implementations whose codepaths diverge at the int/long boundary were not covered.
1 parent 680fc69 commit ab79607

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

language/case_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,41 @@ def bar; @calls << :bar; end
2727
@calls.should == [:foo, :bar]
2828
end
2929

30+
it "matches an Integer literal whose value does not fit in a 32-bit int" do
31+
big = 10_000_000_000
32+
case big
33+
when 10_000_000_000; true
34+
else false
35+
end.should == true
36+
37+
case -3_000_000_000
38+
when -3_000_000_000; true
39+
else false
40+
end.should == true
41+
end
42+
43+
it "matches an arbitrary-precision Integer literal" do
44+
huge = 1 << 100
45+
case huge
46+
when 1 << 100; true
47+
else false
48+
end.should == true
49+
end
50+
51+
it "dispatches correctly with mixed small and large Integer literals" do
52+
pick = -> x {
53+
case x
54+
when 1 << 100 then :beyond_long
55+
when 10_000_000_000 then :beyond_int
56+
when 1 then :fits_int
57+
else :other
58+
end
59+
}
60+
61+
[1 << 100, 10_000_000_000, 1, :nope].map(&pick).should ==
62+
[:beyond_long, :beyond_int, :fits_int, :other]
63+
end
64+
3065
it "evaluates the body of the when clause whose range expression includes the case target expression" do
3166
case 5
3267
when 21..30; false

language/if_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,31 @@
147147
end.should == 123
148148
end
149149

150+
it "compares against an Integer literal whose value does not fit in a 32-bit int" do
151+
x = 10_000_000_000
152+
if x == 10_000_000_000
153+
:match
154+
else
155+
:miss
156+
end.should == :match
157+
158+
y = -3_000_000_000
159+
if y == -3_000_000_000
160+
:match
161+
else
162+
:miss
163+
end.should == :match
164+
end
165+
166+
it "compares against an arbitrary-precision Integer literal" do
167+
x = 1 << 100
168+
if x == 1 << 100
169+
:match
170+
else
171+
:miss
172+
end.should == :match
173+
end
174+
150175
it "allows starting else-body on the same line" do
151176
if false
152177
123

0 commit comments

Comments
 (0)