Commit 9fe821c2

9fe821c2c615Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 10 files · ~2 modified · -8 deleted

Description

test: collapse 5 range_* tests into one range.rb

Range methods coverage. Was five separate tests covering the basic surface, cover?/min/max/count, Range#map, inclusive-vs- exclusive (slicing / for-in / case-when), and step without block param. No class collisions; the only top-level def (classify in the inclusive/exclusive section) is unique. Local names use per-section prefixes where the original tests reused generic names like r, sum, i.

5 .rb (counting the existing range.rb) → 1. ~5s saved on Windows test step at the ~1.2s/test rate.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
9fe821c2c615
Parent
9dcaf5fbd5ea

test/range.rbmodifiedView file

@@ -1,16 +1,123 @@
1-r = (1..10)
2-puts r.first # 1
3-puts r.last # 10
4-puts r.include?(5) # true
5-puts r.include?(11) # false
1+# Range methods — first / last / include? / to_a / each, cover? /
2+# min / max / count, Range#map, inclusive-vs-exclusive distinction,
3+# step without block param. Was five separate tests; merged here.
4+# No class collisions. The only top-level def (`classify` in the
5+# inclusive/exclusive section) is unique within the file. Local
6+# names use per-section prefixes where the original tests reused
7+# generic names like `r`, `sum`, `i`.
68
7-arr = r.to_a
8-puts arr.length # 10
9-puts arr.sum # 55
9+# === Basic Range methods ===
10+r0 = (1..10)
11+puts r0.first # 1
12+puts r0.last # 10
13+puts r0.include?(5) # true
14+puts r0.include?(11) # false
15+
16+arr0 = r0.to_a
17+puts arr0.length # 10
18+puts arr0.sum # 55
1019
1120 total = 0
12-r.each do |i|
13- total += i
21+r0.each do |x|
22+ total += x
1423 end
15-puts total # 55
24+puts total # 55
1625 puts "done"
26+
27+# === cover? / min / max / count ===
28+puts (1..10).cover?(5)
29+puts (1..10).cover?(1)
30+puts (1..10).cover?(10)
31+puts (1..10).cover?(0)
32+puts (1..10).cover?(11)
33+puts (-5..5).cover?(0)
34+puts (-5..5).cover?(-5)
35+puts (-5..5).cover?(6)
36+
37+puts (1..10).min
38+puts (1..10).max
39+puts (-5..5).min
40+puts (-5..5).max
41+puts (100..200).min
42+puts (100..200).max
43+
44+puts (1..10).count
45+puts (1..1).count
46+puts (-5..5).count
47+puts (100..200).count
48+
49+puts (1...10).count
50+puts (1...1).count
51+puts (-5...5).count
52+
53+# === Range#map ===
54+puts (0..3).map { |i| i * 10 }.length # 4
55+puts (0..3).map { |i| i * 10 }[2] # 20
56+puts (0...3).map { |i| i }.length # 3
57+puts (0...3).map { |i| i }[2] # 2
58+puts ((1..3).map { |i| "x#{i}" }.join(",")) # x1,x2,x3
59+puts ((0...3).map { |i| i * 0.5 }.length) # 3
60+
61+# === Inclusive vs exclusive (slicing / for-in / Range#to_a / case-when) ===
62+ia = [10, 20, 30, 40, 50]
63+puts ia[1..3].length # 3 (inclusive)
64+puts ia[1...3].length # 2 (exclusive)
65+puts ia[1...3][0] # 20
66+puts ia[1...3][1] # 30
67+
68+sa = "a,b,c,d,e".split(",")
69+puts sa[0..2].join(":") # a:b:c
70+puts sa[0...2].join(":") # a:b
71+
72+fa = [1.5, 2.5, 3.5, 4.5]
73+puts fa[0..2].length # 3
74+puts fa[0...2].length # 2
75+puts fa[0...3].sum # 7.5
76+
77+s = "abcdef"
78+puts s[1..3] # bcd
79+puts s[1...3] # bc
80+
81+sum_inc = 0
82+for ix in 1..3
83+ sum_inc = sum_inc + ix
84+end
85+puts sum_inc # 6
86+
87+sum_exc = 0
88+for ix in 1...3
89+ sum_exc = sum_exc + ix
90+end
91+puts sum_exc # 3
92+
93+puts (1..3).to_a.length # 3
94+puts (1...3).to_a.length # 2
95+
96+def classify(n)
97+ case n
98+ when 0...10 then "small"
99+ when 10..99 then "medium"
100+ else "large"
101+ end
102+end
103+puts classify(5)
104+puts classify(9)
105+puts classify(10)
106+puts classify(99)
107+puts classify(100)
108+
109+# === step without block param ===
110+# `Integer#step` with a do-block that omits its parameter used a
111+# synthesized `_i` index that wasn't declared. Two paramless `step`
112+# blocks in the same function also redefined it.
113+sum_step = 0
114+1.step(10, 1) do
115+ sum_step += 1
116+end
117+1.step(5, 1) do
118+ sum_step += 10
119+end
120+1.step(5, 1) do
121+ sum_step += 100
122+end
123+puts sum_step