Commit 07d622b3

07d622b3b225Merged

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

Description

test: collapse 4 ptr_array_* tests into one ptr_array.rb

Pointer-array (<obj>_ptr_array) coverage — typed-obj [] map, value-class instances in [] / push / << (PR #87 + #91), and slot widening from [nil] * N writes. Four tests; merged. The only class-name collisions were Foo (same definition reused across the two value-class sections — kept once) and Bag (two unrelated shapes — section 1's renamed to MapBag).

4 .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
07d622b3b225
Parent
57409b7d3815

test/ptr_array.rbaddedView file

1+# Pointer-array (`<obj>_ptr_array`) coverage — typed-obj `[]` map,
2+# value-class instances in array literals (and via `push`/`<<`),
3+# and slot widening from `[nil] * N` writes. Was four tests; the
4+# only class-name collisions were Foo (same definition reused
5+# across the two value-class sections — kept once) and Bag (two
6+# unrelated shapes — section 1's Bag renamed to MapBag).
7+
8+# === Section 1: ptr_array map ===
9+# `compile_map_expr` had no `ptr_array` recv branch — `.map { ... }`
10+# on a typed `obj_X[]` ivar/local fell through to the trailing
11+# `"0"` placeholder.
12+class Box
13+ attr_reader :v
14+ def initialize(v); @v = v; end
15+end
16+
17+class MapBag
18+ def initialize
19+ @items = [Box.new(1), Box.new(2), Box.new(3)]
20+ end
21+ def doubled
22+ @items.map { |b| b.v * 2 }
23+ end
24+ def names
25+ @items.map { |b| "v=#{b.v}" }
26+ end
27+end
28+
29+mbag = MapBag.new
30+mints = mbag.doubled
31+puts mints.length # 3
32+puts mints[0] # 2
33+puts mints[2] # 6
34+mnames = mbag.names
35+puts mnames.length # 3
36+puts mnames[0] # v=1
37+puts mnames[2] # v=3
38+
39+# === Section 2 + 3: value-class instances in ptr_array (literal + push) ===
40+# A class with no attr_writers / no mutating methods would normally
41+# be value-type'd, but its instances landing in an array literal or
42+# being push'd into one must demote it to a heap-allocated obj so
43+# `sp_PtrArray_push`'s `void *` arg type-checks. PR #87 covered the
44+# literal case; #91 (follow-up) extended it to push / <<.
45+class Foo
46+ def initialize(x); @x = x; end
47+ attr_reader :x
48+end
49+
50+# Literal form.
51+flit = [Foo.new(1), Foo.new(2), Foo.new(3)]
52+puts flit.length
53+flit.each { |f| puts f.x }
54+
55+# `push` form.
56+fpush = []
57+fpush.push(Foo.new(1))
58+fpush.push(Foo.new(2))
59+puts fpush.length
60+fpush.each { |f| puts f.x }
61+
62+# `<<` form (same code path through the parser).
63+fsh = []
64+fsh << Foo.new(10)
65+fsh << Foo.new(20)
66+fsh << Foo.new(30)
67+puts fsh.length
68+fsh.each { |f| puts f.x }
69+
70+# === Section 4: ptr_array slot widening from [nil] * N ===
71+# `@arr = [nil] * N` followed by `@arr[i] = obj` widens the slot
72+# to `<obj>_ptr_array`. Without the widening, the slot stays at
73+# the default IntArray and the object pointer write is truncated.
74+class Item
75+ def initialize(label)
76+ @label = label
77+ @history = [0] # array ivar keeps Item off the value-type fast path
78+ end
79+ def visit
80+ @history << @history.last + 1
81+ @label
82+ end
83+ def visits
84+ @history.length - 1
85+ end
86+end
87+
88+class Bag
89+ def initialize
90+ @slots = [nil] * 4
91+ @slots[0] = Item.new("a")
92+ @slots[1] = Item.new("b")
93+ @slots[3] = Item.new("d")
94+ end
95+ attr_reader :slots
96+ def visit_at(i)
97+ @slots[i].visit
98+ end
99+end
100+
101+b = Bag.new
102+puts b.visit_at(0)
103+puts b.visit_at(1)
104+puts b.visit_at(3)
105+puts b.slots[0].visits # 1
106+puts b.slots[1].visits # 1
107+puts b.slots[3].visits # 1