smalltalk: mandelbrot + literal-array mutability fix
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
This commit is contained in:
@@ -150,9 +150,15 @@
|
||||
((= ty "lit-true") true)
|
||||
((= ty "lit-false") false)
|
||||
((= ty "lit-array")
|
||||
(map
|
||||
(fn (e) (smalltalk-eval-ast e frame))
|
||||
(get ast :elements)))
|
||||
;; map returns an immutable list — Smalltalk arrays must be
|
||||
;; mutable so that `at:put:` works. Build via append! so each
|
||||
;; literal yields a fresh mutable list.
|
||||
(let ((out (list)))
|
||||
(begin
|
||||
(for-each
|
||||
(fn (e) (append! out (smalltalk-eval-ast e frame)))
|
||||
(get ast :elements))
|
||||
out)))
|
||||
((= ty "lit-byte-array") (get ast :elements))
|
||||
((= ty "self") (get frame :self))
|
||||
((= ty "super") (get frame :self))
|
||||
|
||||
@@ -219,4 +219,80 @@
|
||||
^ arr")
|
||||
(list 1 2 3 4))
|
||||
|
||||
;; ── mandelbrot.st ────────────────────────────────────────────────────
|
||||
(define
|
||||
mandel-source
|
||||
"Object subclass: #Mandelbrot
|
||||
instanceVariableNames: ''!
|
||||
|
||||
!Mandelbrot methodsFor: 'iteration'!
|
||||
escapeAt: cx and: cy maxIter: maxIter
|
||||
| zx zy zx2 zy2 i |
|
||||
zx := 0. zy := 0.
|
||||
zx2 := 0. zy2 := 0.
|
||||
i := 0.
|
||||
[(zx2 + zy2 < 4) and: [i < maxIter]] whileTrue: [
|
||||
zy := (zx * zy * 2) + cy.
|
||||
zx := zx2 - zy2 + cx.
|
||||
zx2 := zx * zx.
|
||||
zy2 := zy * zy.
|
||||
i := i + 1].
|
||||
^ i!
|
||||
|
||||
inside: cx and: cy maxIter: maxIter
|
||||
^ (self escapeAt: cx and: cy maxIter: maxIter) >= maxIter! !
|
||||
|
||||
!Mandelbrot methodsFor: 'grid'!
|
||||
countInsideRangeX: x0 to: x1 stepX: dx rangeY: y0 to: y1 stepY: dy maxIter: maxIter
|
||||
| x y count |
|
||||
count := 0.
|
||||
y := y0.
|
||||
[y <= y1] whileTrue: [
|
||||
x := x0.
|
||||
[x <= x1] whileTrue: [
|
||||
(self inside: x and: y maxIter: maxIter) ifTrue: [count := count + 1].
|
||||
x := x + dx].
|
||||
y := y + dy].
|
||||
^ count! !")
|
||||
|
||||
(smalltalk-load mandel-source)
|
||||
|
||||
(st-test "Mandelbrot class registered" (st-class-exists? "Mandelbrot") true)
|
||||
|
||||
;; The origin is the cusp of the cardioid — z stays at 0 forever.
|
||||
(st-test "origin is in the set"
|
||||
(evp "^ Mandelbrot new inside: 0 and: 0 maxIter: 50") true)
|
||||
|
||||
;; (-1, 0) — z₀=0, z₁=-1, z₂=0, … oscillates and stays bounded.
|
||||
(st-test "(-1, 0) is in the set"
|
||||
(evp "^ Mandelbrot new inside: -1 and: 0 maxIter: 50") true)
|
||||
|
||||
;; (1, 0) — escapes after 2 iterations: 0 → 1 → 2, |z|² = 4 ≥ 4.
|
||||
(st-test "(1, 0) escapes quickly"
|
||||
(evp "^ Mandelbrot new escapeAt: 1 and: 0 maxIter: 50") 2)
|
||||
|
||||
;; (2, 0) — escapes immediately: 0 → 2, |z|² = 4 ≥ 4 already.
|
||||
(st-test "(2, 0) escapes after 1 step"
|
||||
(evp "^ Mandelbrot new escapeAt: 2 and: 0 maxIter: 50") 1)
|
||||
|
||||
;; (-2, 0) — z₀=0; iter 1: z₁=-2, |z|²=4, condition `< 4` fails → exits at i=1.
|
||||
(st-test "(-2, 0) escapes after 1 step"
|
||||
(evp "^ Mandelbrot new escapeAt: -2 and: 0 maxIter: 50") 1)
|
||||
|
||||
;; (10, 10) — far outside, escapes on the first step.
|
||||
(st-test "(10, 10) escapes after 1 step"
|
||||
(evp "^ Mandelbrot new escapeAt: 10 and: 10 maxIter: 50") 1)
|
||||
|
||||
;; Coarse 5x5 grid (-2..2 in 1-step increments, no half-steps to keep
|
||||
;; this fast). Membership of (-1,0), (0,0), (-1,-1)? We expect just
|
||||
;; (0,0) and (-1,0) at maxIter 30.
|
||||
;; Actually let's count exact membership at this resolution.
|
||||
(st-test "tiny 3x3 grid count"
|
||||
(evp
|
||||
"^ Mandelbrot new countInsideRangeX: -1 to: 1 stepX: 1
|
||||
rangeY: -1 to: 1 stepY: 1
|
||||
maxIter: 30")
|
||||
;; In-set points (bounded after 30 iters): (0,-1) (-1,0) (0,0) (0,1) → 4.
|
||||
4)
|
||||
|
||||
(list st-test-pass st-test-fail)
|
||||
|
||||
36
lib/smalltalk/tests/programs/mandelbrot.st
Normal file
36
lib/smalltalk/tests/programs/mandelbrot.st
Normal file
@@ -0,0 +1,36 @@
|
||||
"Mandelbrot — escape-time iteration of z := z² + c starting at z₀ = 0.
|
||||
Returns the number of iterations before |z|² exceeds 4, capped at
|
||||
maxIter. Classic-corpus program for the Smalltalk-on-SX runtime."
|
||||
|
||||
Object subclass: #Mandelbrot
|
||||
instanceVariableNames: ''!
|
||||
|
||||
!Mandelbrot methodsFor: 'iteration'!
|
||||
escapeAt: cx and: cy maxIter: maxIter
|
||||
| zx zy zx2 zy2 i |
|
||||
zx := 0. zy := 0.
|
||||
zx2 := 0. zy2 := 0.
|
||||
i := 0.
|
||||
[(zx2 + zy2 < 4) and: [i < maxIter]] whileTrue: [
|
||||
zy := (zx * zy * 2) + cy.
|
||||
zx := zx2 - zy2 + cx.
|
||||
zx2 := zx * zx.
|
||||
zy2 := zy * zy.
|
||||
i := i + 1].
|
||||
^ i!
|
||||
|
||||
inside: cx and: cy maxIter: maxIter
|
||||
^ (self escapeAt: cx and: cy maxIter: maxIter) >= maxIter! !
|
||||
|
||||
!Mandelbrot methodsFor: 'grid'!
|
||||
countInsideRangeX: x0 to: x1 stepX: dx rangeY: y0 to: y1 stepY: dy maxIter: maxIter
|
||||
| x y count |
|
||||
count := 0.
|
||||
y := y0.
|
||||
[y <= y1] whileTrue: [
|
||||
x := x0.
|
||||
[x <= x1] whileTrue: [
|
||||
(self inside: x and: y maxIter: maxIter) ifTrue: [count := count + 1].
|
||||
x := x + dx].
|
||||
y := y + dy].
|
||||
^ count! !
|
||||
Reference in New Issue
Block a user