From 836b436b004eccd86b4576736cbc7f2420778ec2 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 8 Sep 2026 12:24:32 +0200 Subject: [PATCH 1/2] enumerator: Enumerator::Product class, Chain/ArithmeticSequence constructors, break-early take_while (core/enumerator +49) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reference: ruby/ruby v3_4_0 enumerator.c (enum_product_initialize, enum_product_init_copy, enum_product_each/product_each_i, enum_product_total_size, enum_product_rewind, inspect_enum_product; arith_seq_eq/arith_seq_hash; the missing ArithmeticSequence allocator/new). - Enumerator::Product is now a real class backed by productSources on the enumerator: .new/#initialize store the enumerables, .allocate yields an uninitialized product, #initialize/#initialize_copy are private, #each walks the Cartesian product via #each_entry (driven through enumRunEach so #to_a/#take/#next work, infinite sources included), #size follows enum_product_total_size (0/nil/INFINITY/Integer rules), #rewind rewinds each source in order, #inspect renders the enumerables with a self-reference guard ("..."). Enumerator.product now builds the same object (top-level product_spec stays 12/12). - Enumerator::Chain gains .allocate + private #initialize and an "uninitialized" #inspect. - Enumerator::ArithmeticSequence.new raises NoMethodError and .allocate raises TypeError (MRI defines neither); #== / #hash key on the begin/end/step/exclude_end? tuple. - Enumerator#take_while breaks at the first falsy element (MRI's rb_iter_break), so it terminates on unbounded sources — the generic Enumerable#take_while loops to the end and hung produce_spec. core/enumerator: fail 22->16, error 40->11, produce_spec FILEFAIL cleared (pass 348->397). Remaining gaps are harness/object-model limits (shared before-hook merge, native-method arity, subclass identity, the OpEq fast-path gate) noted in the PR. Co-Authored-By: Claude Opus 4.8 --- internal/vm/enumerator.go | 338 ++++++++++++++++++++++++++++++-------- 1 file changed, 272 insertions(+), 66 deletions(-) diff --git a/internal/vm/enumerator.go b/internal/vm/enumerator.go index cadd98ae..a12ff9b7 100644 --- a/internal/vm/enumerator.go +++ b/internal/vm/enumerator.go @@ -38,9 +38,14 @@ type Enumerator struct { produceInit object.Value produceHas bool - // isProduct marks an Enumerator::Product (built by Enumerator.product): a plain - // generator Enumerator whose only distinction is the class it reports. - isProduct bool + // isProduct marks an Enumerator::Product (built by Enumerator.product or + // Enumerator::Product.new): #each walks the Cartesian product of productSources, + // consuming each through #each_entry. productSources is nil for an allocated but + // never-#initialize-d product (rendered "uninitialized"); an initialized product + // holds a non-nil (possibly empty) slice — Enumerator.product with no arguments + // yields the single empty tuple [[]]. + isProduct bool + productSources []object.Value // isArithSeq marks an Enumerator::ArithmeticSequence (built by Numeric#step // without a block): a step-driven Enumerator that additionally answers @@ -89,7 +94,13 @@ type Enumerator struct { // raises when it is iterated. Every real constructor sets at least one of these, // so an all-zero Enumerator is exactly the allocated-but-uninitialized one. func (e *Enumerator) uninitialized() bool { - return e.recv == nil && e.block == nil && e.produceBlk == nil && !e.isChain + if e.isProduct { + return e.productSources == nil + } + if e.isChain { + return e.chainParts == nil + } + return e.recv == nil && e.block == nil && e.produceBlk == nil } // forPull returns a copy of e carrying its definition and none of its @@ -121,18 +132,37 @@ func (y *yielder) Truthy() bool { return true } // #). (MRI's #to_s shows the object address, which we // can't reproduce deterministically, so ToS reuses Inspect.) func (e *Enumerator) Inspect() string { - if e.uninitialized() { - // Only Enumerator.allocate produces an uninitialized enumerator, and it - // carries none of the subclass flags, so the class name is always plain. - return "#" + // Enumerator::Product renders its enumerables, guarding against a product that + // contains itself (MRI's rb_exec_recursive → "...") via the shared repr guard. + if e.isProduct { + if e.productSources == nil { + return "#" + } + if !object.ReprEnter(e) { + return "#" + } + defer object.ReprLeave(e) + parts := make([]string, len(e.productSources)) + for i, p := range e.productSources { + parts[i] = p.Inspect() + } + return "#" } if e.isChain { + if e.chainParts == nil { + return "#" + } parts := make([]string, len(e.chainParts)) for i, p := range e.chainParts { parts[i] = p.Inspect() } return "#" } + if e.uninitialized() { + // A plain Enumerator.allocate carries none of the subclass flags, so the + // class name here is always the bare "Enumerator". + return "#" + } // A generator (Enumerator.new) or produce enumerator has no driven receiver; // MRI shows an internal Generator/Producer object there (its address, which we // can't reproduce), so render a stable placeholder and default the method name. @@ -176,6 +206,9 @@ func (vm *VM) registerEnumerator() { } // Enumerator::Chain — a subclass whose instances chain several enumerables. + // .new and #initialize both store the parts; .allocate yields an uninitialized + // chain (chainParts nil), which #inspect renders "uninitialized" and #initialize + // later fills in. #initialize is private (MRI keeps it so). vm.cEnumeratorChain = newClass("Enumerator::Chain", vm.cEnumerator) vm.cEnumeratorChain.consts = vm.cEnumerator.consts vm.cEnumerator.consts["Chain"] = vm.cEnumeratorChain @@ -183,8 +216,26 @@ func (vm *VM) registerEnumerator() { native: func(_ *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { return newChain(append([]object.Value{}, args...)) }} + vm.cEnumeratorChain.smethods["allocate"] = &Method{name: "allocate", owner: vm.cEnumeratorChain, + native: func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { + return &Enumerator{isChain: true} + }} + vm.cEnumeratorChain.define("initialize", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + e := self.(*Enumerator) + if isFrozen(e) { + vm.raiseFrozen(e) + } + parts := append([]object.Value{}, args...) + *e = Enumerator{isChain: true, chainParts: parts, entered: make([]bool, len(parts)), methodValueState: e.methodValueState} + return e + }) + vm.setInstanceVisibility(vm.cEnumeratorChain, "initialize", visPrivate) - // Enumerator::Product — the subclass Enumerator.product returns. + // Enumerator::Product — the Cartesian product Enumerator.product and + // Enumerator::Product.new return. .new/#initialize store the enumerables; + // .allocate yields an uninitialized product; #initialize/#initialize_copy are + // private; #size/#rewind/#each read productSources so a post-hoc + // #initialize_copy (or #rewind) is reflected. vm.cEnumeratorProduct = newClass("Enumerator::Product", vm.cEnumerator) vm.cEnumeratorProduct.consts = vm.cEnumerator.consts vm.cEnumerator.consts["Product"] = vm.cEnumeratorProduct @@ -192,6 +243,31 @@ func (vm *VM) registerEnumerator() { native: func(vm *VM, _ object.Value, args []object.Value, blk *Proc) object.Value { return vm.enumProduct(args, blk) }} + vm.cEnumeratorProduct.smethods["new"] = &Method{name: "new", owner: vm.cEnumeratorProduct, + native: func(_ *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + e := &Enumerator{isProduct: true} + productInit(e, args) + return e + }} + vm.cEnumeratorProduct.smethods["allocate"] = &Method{name: "allocate", owner: vm.cEnumeratorProduct, + native: func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { + return &Enumerator{isProduct: true} + }} + vm.cEnumeratorProduct.define("initialize", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + e := self.(*Enumerator) + if isFrozen(e) { + vm.raiseFrozen(e) + } + productInit(e, args) + return e + }) + vm.setInstanceVisibility(vm.cEnumeratorProduct, "initialize", visPrivate) + vm.cEnumeratorProduct.define("initialize_copy", productInitCopy) + vm.setInstanceVisibility(vm.cEnumeratorProduct, "initialize_copy", visPrivate) + vm.cEnumeratorProduct.define("size", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + return vm.enumProductSize(self.(*Enumerator)) + }) + vm.cEnumeratorProduct.define("rewind", productRewind) // Enumerator::ArithmeticSequence — the subclass Numeric#step (and #step on an // Integer/Float) returns when called without a block. It is a step-driven @@ -200,6 +276,48 @@ func (vm *VM) registerEnumerator() { vm.cArithSeq = newClass("Enumerator::ArithmeticSequence", vm.cEnumerator) vm.cArithSeq.consts = vm.cEnumerator.consts vm.cEnumerator.consts["ArithmeticSequence"] = vm.cArithSeq + // MRI defines neither .new (an instance only ever comes from Numeric#step / + // Range#step) nor an allocator for ArithmeticSequence: .new raises NoMethodError + // and .allocate raises TypeError. Override the inherited Enumerator ones to match. + vm.cArithSeq.smethods["new"] = &Method{name: "new", owner: vm.cArithSeq, + native: func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { + raise("NoMethodError", "undefined method 'new' for class Enumerator::ArithmeticSequence") + return object.NilV + }} + vm.cArithSeq.smethods["allocate"] = &Method{name: "allocate", owner: vm.cArithSeq, + native: func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { + raise("TypeError", "allocator undefined for Enumerator::ArithmeticSequence") + return object.NilV + }} + // #== / #hash key on the defining triple (begin, end, step) plus exclude_end?, + // so two sequences built by different constructors (1.step(10,100) and + // (1..10).step(100)) compare and hash equal — MRI's arith_seq_eq / arith_seq_hash. + vm.cArithSeq.define("==", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + e := self.(*Enumerator) + o, ok := args[0].(*Enumerator) + if !ok || !o.isArithSeq { + return object.False + } + eq := vm.send(e.asBegin, "==", []object.Value{o.asBegin}, nil).Truthy() && + vm.send(e.asEnd, "==", []object.Value{o.asEnd}, nil).Truthy() && + vm.send(e.asStep, "==", []object.Value{o.asStep}, nil).Truthy() && + e.asExcl == o.asExcl + return object.Bool(eq) + }) + vm.cArithSeq.define("hash", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + e := self.(*Enumerator) + h := int64(1) + for _, v := range []object.Value{e.asBegin, e.asEnd, e.asStep} { + hv := vm.send(v, "hash", nil, nil) + if i, ok := hv.(object.Integer); ok { + h = h*31 + int64(i) + } + } + if e.asExcl { + h = h*31 + 1 + } + return object.IntValue(h) + }) vm.cArithSeq.define("begin", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return self.(*Enumerator).asBegin }) @@ -523,6 +641,38 @@ func (vm *VM) registerEnumerator() { d("take", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { return object.NewArrayFromSlice(vm.enumTake(self.(*Enumerator), int(intArg(args[0])))) }) + // #take_while collects elements while the block is truthy and stops at the + // first falsy one. MRI's rb_iter_break makes it terminate even for an unbounded + // source (Enumerator.produce, Array#cycle); the generic Enumerable#take_while + // keeps iterating to the end, which would hang here — so Enumerator overrides it + // to break early (via the enumStop unwind #take already uses). + d("take_while", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { + e := self.(*Enumerator) + if blk == nil { + return enumFor(self, "take_while") + } + out := []object.Value{} + collect := &Proc{native: func(_ *VM, cargs []object.Value) object.Value { + v := enumPack(cargs) + if !vm.callBlock(blk, []object.Value{v}).Truthy() { + panic(enumStop{}) + } + out = append(out, v) + return object.NilV + }} + func() { + defer func() { + if r := recover(); r != nil { + if _, ok := r.(enumStop); ok { + return + } + panic(r) + } + }() + vm.enumRunEach(e, collect) + }() + return object.NewArrayFromSlice(out) + }) } // enumProduce builds the enumerator for Enumerator.produce(initial, size:) { … }. @@ -557,9 +707,8 @@ func (vm *VM) enumProduce(args []object.Value, blk *Proc) object.Value { } // enumProduct implements Enumerator.product. It returns an Enumerator::Product -// over the Cartesian product of its enumerable arguments, each consumed through -// #each_entry lazily and afresh at every outer step — so an infinite source -// yields an infinite product and a one-shot source is drained once. Keyword +// over the Cartesian product of its enumerable arguments (stored on the +// returned enumerator so #size/#rewind/#inspect can read them). Keyword // arguments are rejected. With a block it iterates the product and returns nil. func (vm *VM) enumProduct(args []object.Value, blk *Proc) object.Value { if h := trailingKwHash(args); h != nil { @@ -569,68 +718,123 @@ func (vm *VM) enumProduct(args []object.Value, blk *Proc) object.Value { } raise("ArgumentError", "unknown keywords: %s", strings.Join(bad, ", ")) } - sources := append([]object.Value{}, args...) + e := &Enumerator{isProduct: true} + productInit(e, args) + if blk == nil { + return e + } + vm.enumProductEach(e, blk) + return object.NilV +} - // #size is the product of the sources' sizes: 1 for no sources, Float::INFINITY - // when any is infinite, and nil when any is unknown. - var size object.Value = object.IntValue(1) - for _, e := range sources { - if !vm.respondsToDynamic(e, "size") { - size = object.NilV - break - } - s := vm.send(e, "size", nil, nil) - if object.IsNil(s) { - size = object.NilV - break - } - size = productSize(size, s) +// productInit configures e as an initialized Enumerator::Product over args, +// resetting any prior state. append onto a fresh slice yields a non-nil (so +// "initialized", even for no arguments) copy that the product owns. +func productInit(e *Enumerator, args []object.Value) { + *e = Enumerator{isProduct: true, productSources: append([]object.Value{}, args...), methodValueState: e.methodValueState} +} + +// productInitCopy is Enumerator::Product#initialize_copy: it replaces the +// receiver's enumerables with the source's. It is a no-op for a self-copy (even +// when frozen), then rejects a frozen receiver, a source of a different class, +// and an uninitialized source — mirroring MRI's OBJ_INIT_COPY plus the +// "uninitialized product" guard. +func productInitCopy(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + e := self.(*Enumerator) + if len(args) != 1 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 1)", len(args)) + } + o, ok := args[0].(*Enumerator) + if ok && e == o { + return e // self-copy: nothing to do, even on a frozen receiver + } + if isFrozen(e) { + vm.raiseFrozen(e) } + if !ok || vm.classOf(e) != vm.classOf(o) { + raise("TypeError", "initialize_copy should take same class object") + } + if o.productSources == nil { + raise("ArgumentError", "uninitialized product") + } + e.productSources = o.productSources + e.extFiber, e.peeked, e.ended, e.feedSet = nil, false, false, false + return e +} - gen := &Proc{native: func(vm *VM, gargs []object.Value) object.Value { - y := gargs[0] - var combine func(rest, prefix []object.Value) - combine = func(rest, prefix []object.Value) { - if len(rest) == 0 { - vm.send(y, "<<", []object.Value{object.NewArrayFromSlice(append([]object.Value{}, prefix...))}, nil) - return - } - first, tail := rest[0], rest[1:] - step := &Proc{native: func(vm *VM, a []object.Value) object.Value { - // each_entry yields one entry per element; a multi-value yield keeps - // only its first value, a bare yield contributes nil — as MRI does. - var x object.Value = object.NilV - if len(a) > 0 { - x = a[0] - } - combine(tail, append(append([]object.Value{}, prefix...), x)) - return object.NilV - }} - vm.send(first, "each_entry", nil, step) +// productRewind is Enumerator::Product#rewind: it rewinds each enumerable that +// responds to #rewind, in forward order, and drops any external-iteration state. +func productRewind(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + e := self.(*Enumerator) + e.extFiber, e.peeked, e.ended, e.feedSet = nil, false, false, false + for _, src := range e.productSources { + if vm.respondsTo(src, "rewind") { + vm.send(src, "rewind", nil, nil) } - combine(sources, nil) - return object.NilV - }} + } + return e +} - e := &Enumerator{isProduct: true, block: gen, sizeSpec: size, sizeSpecSet: true} - if blk == nil { - return e +// enumProductEach walks the Cartesian product of e.productSources, consuming +// each source through #each_entry afresh at every outer step (so an infinite +// source yields an infinite product), and forwards each combination (an Array) +// to blk. It returns e. +func (vm *VM) enumProductEach(e *Enumerator, blk *Proc) object.Value { + var combine func(rest, prefix []object.Value) + combine = func(rest, prefix []object.Value) { + if len(rest) == 0 { + vm.callBlock(blk, []object.Value{object.NewArrayFromSlice(append([]object.Value{}, prefix...))}) + return + } + first, tail := rest[0], rest[1:] + step := &Proc{native: func(vm *VM, a []object.Value) object.Value { + // each_entry yields one entry per element; a multi-value yield keeps only + // its first value, a bare yield contributes nil — as MRI's product does. + var x object.Value = object.NilV + if len(a) > 0 { + x = a[0] + } + combine(tail, append(append([]object.Value{}, prefix...), x)) + return object.NilV + }} + vm.send(first, "each_entry", nil, step) } - vm.send(e, "each", nil, blk) - return object.NilV + combine(e.productSources, nil) + return e } -// productSize multiplies two finite-or-infinite #size values under -// Enumerator.product's rules: an infinite factor makes the product infinite; -// otherwise the two Integer sizes multiply. The caller stops at the first nil -// (unknown) size, so neither argument here is nil. -func productSize(a, b object.Value) object.Value { - if isInfFloat(a) || isInfFloat(b) { - return object.Float(math.Inf(1)) - } - an, _ := object.BigOf(a) - bn, _ := object.BigOf(b) - return object.NormInt(new(big.Int).Mul(an, bn)) +// enumProductSize is Enumerator::Product#size: the product of the sources' +// sizes. It returns 0 as soon as any source is empty; nil when any source lacks +// #size, reports nil, or reports a non-Integer finite size (including NaN); +// Float::INFINITY when any reports an infinite size; otherwise the Integer +// product. Mirrors MRI's enum_product_total_size. +func (vm *VM) enumProductSize(e *Enumerator) object.Value { + total := big.NewInt(1) + for _, src := range e.productSources { + if !vm.respondsToDynamic(src, "size") { + return object.NilV + } + switch s := vm.send(src, "size", nil, nil).(type) { + case object.Integer: + if s == 0 { + return object.IntValue(0) + } + total.Mul(total, big.NewInt(int64(s))) + case *object.Bignum: + if s.I.Sign() == 0 { + return object.IntValue(0) + } + total.Mul(total, s.I) + case object.Float: + if math.IsInf(float64(s), 1) { + return object.Float(math.Inf(1)) + } + return object.NilV // a finite (or NaN) Float size is not multiplied + default: + return object.NilV // nil, or a value with no integer meaning + } + } + return object.NormInt(total) } // isInfFloat reports whether v is a positive-infinite Float. @@ -655,6 +859,8 @@ func (vm *VM) enumResolveSize(v object.Value) object.Value { // forwards to recv.meth. The return value is the source's finish value. func (vm *VM) enumRunEach(e *Enumerator, blk *Proc) object.Value { switch { + case e.isProduct: + return vm.enumProductEach(e, blk) case e.isChain: for i, part := range e.chainParts { e.entered[i] = true From 2040982b470d7b092591606174b7e956293139c1 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 8 Sep 2026 12:45:06 +0200 Subject: [PATCH 2/2] enumerator: whitebox tests for the wave-20 Product/Chain/ArithmeticSequence/take_while surface (100% coverage) Covers every new branch via eval() snippets checked byte-for-byte against ruby 4.0.6: Enumerator::Product .new/.allocate/#initialize/#initialize_copy (replace, self-copy, frozen, uninitialized-source, different-class and arity guards)/#rewind/#size (Integer/empty-0/no-size-nil/finite-Float-nil/Symbol-nil/ Bignum/INFINITY)/#inspect (normal, uninitialized, self-referential); Enumerator::Chain .allocate + private #initialize + "uninitialized" #inspect; Enumerator::ArithmeticSequence .new (NoMethodError) / .allocate (TypeError) / #== / #hash; Enumerator#take_while breaking early on unbounded sources. Also simplifies #uninitialized (Inspect handles Product/Chain before it is reached) and drops an unreachable Bignum-zero branch in #size (0 always normalises to an Integer). go tool cover -func shows enumerator.go at 100%. Co-Authored-By: Claude Opus 4.8 --- internal/vm/enumerator.go | 14 +- .../vm/enumerator_wave20_internal_test.go | 120 ++++++++++++++++++ 2 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 internal/vm/enumerator_wave20_internal_test.go diff --git a/internal/vm/enumerator.go b/internal/vm/enumerator.go index a12ff9b7..9393a19e 100644 --- a/internal/vm/enumerator.go +++ b/internal/vm/enumerator.go @@ -93,13 +93,10 @@ type Enumerator struct { // source: MRI renders such an enumerator as "#" and // raises when it is iterated. Every real constructor sets at least one of these, // so an all-zero Enumerator is exactly the allocated-but-uninitialized one. +// It is only consulted for a plain Enumerator: Inspect handles the Product and +// Chain subclasses (whose uninitialized form is productSources/chainParts nil) +// before reaching here. func (e *Enumerator) uninitialized() bool { - if e.isProduct { - return e.productSources == nil - } - if e.isChain { - return e.chainParts == nil - } return e.recv == nil && e.block == nil && e.produceBlk == nil } @@ -821,9 +818,8 @@ func (vm *VM) enumProductSize(e *Enumerator) object.Value { } total.Mul(total, big.NewInt(int64(s))) case *object.Bignum: - if s.I.Sign() == 0 { - return object.IntValue(0) - } + // A Bignum is never zero (0 normalises to an Integer), so no zero + // short-circuit is needed here. total.Mul(total, s.I) case object.Float: if math.IsInf(float64(s), 1) { diff --git a/internal/vm/enumerator_wave20_internal_test.go b/internal/vm/enumerator_wave20_internal_test.go new file mode 100644 index 00000000..87d71531 --- /dev/null +++ b/internal/vm/enumerator_wave20_internal_test.go @@ -0,0 +1,120 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +// TestEnumeratorProductClass covers the Enumerator::Product class surface added +// beyond Enumerator.product: .new / .allocate / #initialize, the private +// #initialize_copy (replace, self-copy, frozen, uninitialized-source, +// different-class and arity guards), #rewind, #size across every branch, and +// #inspect (normal, uninitialized, self-referential). Verified against ruby 4.0.6. +func TestEnumeratorProductClass(t *testing.T) { + cases := []struct{ src, want string }{ + // .new / #each store and walk the enumerables. + {`p Enumerator::Product.new([1, 2], [:a, :b]).each.to_a`, `[[1, :a], [1, :b], [2, :a], [2, :b]]`}, + {`p Enumerator::Product.new.each.to_a`, `[[]]`}, + {`p Enumerator::Product.new([1, 2], [:a, :b]).class`, `Enumerator::Product`}, + // #each with a block returns self; yields each element to multi-arg blocks. + {`e = Enumerator::Product.new([1, 2], [:a, :b]); p e.each {}.equal?(e)`, `true`}, + {`acc = []; Enumerator::Product.new([1, 2], [:a, :b]).each { |x, y| acc << y }; p acc`, `[:a, :b, :a, :b]`}, + {`acc = []; Enumerator::Product.new([], [1]).each { |x| acc << x }; p acc`, `[]`}, + // #inspect: normal, uninitialized (from .allocate), and self-referential. + {`p Enumerator::Product.new([1, 2], [:a, :b]).inspect`, `"#"`}, + {`p Enumerator::Product.allocate.inspect`, `"#"`}, + {`a = [1, 2]; r = Enumerator::Product.new(a); a << r; p r.inspect`, `"#]]>"`}, + // #initialize is private; .allocate + send(:initialize, ...) returns self. + {`p Enumerator::Product.private_instance_methods(false).include?(:initialize)`, `true`}, + {`u = Enumerator::Product.allocate; p u.send(:initialize, 0..1, 2..3).equal?(u)`, `true`}, + {`u = Enumerator::Product.allocate.freeze; begin; u.send(:initialize, 0..1); rescue => e; p e.class; end`, `FrozenError`}, + // #initialize_copy: replace, return self, self-copy no-op (even frozen), + // frozen receiver, uninitialized source, different class, non-Enumerator, arity. + {`a = Enumerator::Product.new([true]); b = Enumerator::Product.new([1, 2], [:a, :b]); a.send(:initialize_copy, b); p a.each.to_a`, `[[1, :a], [1, :b], [2, :a], [2, :b]]`}, + {`a = Enumerator::Product.new([true]); b = Enumerator::Product.new([1]); p a.send(:initialize_copy, b).equal?(a)`, `true`}, + {`p Enumerator::Product.private_instance_methods(false).include?(:initialize_copy)`, `true`}, + {`a = Enumerator::Product.new(1..2); p a.send(:initialize_copy, a).equal?(a)`, `true`}, + {`a = Enumerator::Product.new(1..2).freeze; p a.send(:initialize_copy, a).equal?(a)`, `true`}, + {`a = Enumerator::Product.new(1..2).freeze; b = Enumerator::Product.new(3..4); begin; a.send(:initialize_copy, b); rescue => e; p e.class; end`, `FrozenError`}, + {`a = Enumerator::Product.new(1..2); u = Enumerator::Product.allocate; begin; a.send(:initialize_copy, u); rescue => e; p e.message; end`, `"uninitialized product"`}, + {`a = Enumerator::Product.new(1..2); begin; a.send(:initialize_copy, "x"); rescue => e; p e.class; end`, `TypeError`}, + {`a = Enumerator::Product.new(1..2); begin; a.send(:initialize_copy, [1].chain([2])); rescue => e; p e.class; end`, `TypeError`}, + {`a = Enumerator::Product.new(1..2); begin; a.send(:initialize_copy); rescue => e; p e.class; end`, `ArgumentError`}, + // #rewind rewinds each source that responds and returns self. + {`o = Object.new; def o.each_entry; end; e = Enumerator::Product.new([1, 2].each, o); e.rewind; p e.rewind.equal?(e)`, `true`}, + // #size across every branch: Integer product, empty (0), no #size (nil), + // non-Integer finite Float (nil), Symbol (nil), Bignum, and infinite (INFINITY). + {`p Enumerator::Product.new(1..2, 1..3).size`, `6`}, + {`p Enumerator::Product.new(1...1, [1, 2]).size`, `0`}, + {`o = Object.new; def o.each_entry; end; p Enumerator::Product.new(1..2, o).size`, `nil`}, + {`o = Object.new; def o.size; 1.0; end; p Enumerator::Product.new(1..2, o).size`, `nil`}, + {`o = Object.new; def o.size; :sym; end; p Enumerator::Product.new(1..2, o).size`, `nil`}, + {`o = Object.new; def o.size; 10 ** 20; end; p Enumerator::Product.new(1..2, o).size`, `200000000000000000000`}, + {`p Enumerator::Product.new(1.., [:a]).size`, `Infinity`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +} + +// TestEnumeratorChainAndArithSeqWave20 covers the Chain constructors added +// (.allocate + private #initialize and an "uninitialized" #inspect) and the +// ArithmeticSequence surface (.new raises NoMethodError, .allocate raises +// TypeError, #== and #hash key on the begin/end/step/exclude_end? tuple). It also +// covers the plain-Enumerator "uninitialized" #inspect. Verified against ruby 4.0.6. +func TestEnumeratorChainAndArithSeqWave20(t *testing.T) { + cases := []struct{ src, want string }{ + // Chain: uninitialized and initialized #inspect, private #initialize. + {`p Enumerator::Chain.allocate.inspect`, `"#"`}, + {`p Enumerator::Chain.new(1..2, 3..4).inspect`, `"#"`}, + {`p Enumerator::Chain.new.inspect`, `"#"`}, + {`p Enumerator::Chain.private_instance_methods(false).include?(:initialize)`, `true`}, + {`u = Enumerator::Chain.allocate; p u.send(:initialize, 1..2).equal?(u)`, `true`}, + {`u = Enumerator::Chain.allocate; u.send(:initialize, 1..2, 3..4); p u.to_a`, `[1, 2, 3, 4]`}, + {`u = Enumerator::Chain.allocate.freeze; begin; u.send(:initialize); rescue => e; p e.class; end`, `FrozenError`}, + // A plain Enumerator from .allocate renders "uninitialized". + {`p Enumerator.allocate.inspect`, `"#"`}, + // ArithmeticSequence: no .new, no .allocate. + {`begin; Enumerator::ArithmeticSequence.new; rescue => e; p e.class; end`, `NoMethodError`}, + {`begin; Enumerator::ArithmeticSequence.allocate; rescue => e; p e.message; end`, `"allocator undefined for Enumerator::ArithmeticSequence"`}, + // #== keys on begin/end/step/exclude_end? (dispatched via #send to bypass the + // == operator's identity fast path), and #hash is a stable Integer. + {`p 1.step(10).send(:==, 1.step(10))`, `true`}, + {`p 1.step(10, 100).send(:==, (1..10).step(100))`, `true`}, + {`p 1.step(10).send(:==, 1.step(11))`, `false`}, + {`p 1.step(10).send(:==, 1.step(10, 2))`, `false`}, + {`p (1..10).step.send(:==, (1...10).step)`, `false`}, + {`p 1.step(10).send(:==, 5)`, `false`}, + {`p 1.step(10).hash.is_a?(Integer)`, `true`}, + {`p 1.step(10).hash == 1.step(10).hash`, `true`}, + {`p((1...10).step.hash.is_a?(Integer))`, `true`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +} + +// TestEnumeratorTakeWhileBreaksEarly covers Enumerator#take_while: it terminates +// on an unbounded source (produce / cycle) by breaking at the first falsy element, +// returns an Enumerator without a block, collects while truthy for a finite +// source, and propagates a non-break exception from the block. Verified against +// ruby 4.0.6. +func TestEnumeratorTakeWhileBreaksEarly(t *testing.T) { + cases := []struct{ src, want string }{ + {`p Enumerator.produce(0) { |x| x + 1 }.take_while { |x| x < 3 }`, `[0, 1, 2]`}, + {`p [1, 2, 3].cycle.take_while { |x| x < 3 }`, `[1, 2]`}, + {`p [1, 2, 3, 4].each.take_while { |x| x < 3 }`, `[1, 2]`}, + {`p [1, 2, 3].each.take_while.class`, `Enumerator`}, + {`begin; [1, 2, 3].each.take_while { raise "boom" }; rescue => e; p e.message; end`, `"boom"`}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want+"\n" { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n") + } + } +}