github.com/sverrirab/mandelbrot-go@v0.0.0-20210314152707-e4e8adc43eaa/mandelbrot_test.go (about) 1 package main 2 3 import "testing" 4 5 func TestEscape(t *testing.T) { 6 data := []struct { 7 c complex128 8 e int 9 }{ 10 {-0.5-0.5i, 63}, 11 {-0.75+0.1i, 32}, 12 {0.75-0.25i, 2}, 13 {-2.01, 0}, 14 {1.99+1.99i, 0}, 15 {2.2i, 0}, 16 } 17 18 for _, d := range data { 19 r := escape(d.c) 20 if d.e != r { 21 t.Errorf("TestEscape: unexpected result for %g: %d != %d", d.c, d.e, r) 22 } 23 } 24 } 25 26 func BenchmarkEscape(b *testing.B) { 27 for i := 0; i < b.N; i++ { 28 escape(0.35+0.21i) 29 } 30 } 31 32 func BenchmarkGenerate(b *testing.B) { 33 for i := 0; i < 10; i++ { 34 center := 1.5+0i + 0.1+0.1i * complex(float64(i), 2*float64(i)); 35 generate(320, 200, center, 0.5) 36 } 37 } 38 39 // Below is an example of an alternative "escape" implementation. This might work faster on 40 // some devices (switching to 64 floating point would possibly help more). 41 // switch this function out for the original escape and rerun the benchmark to test. 42 /* 43 func escape(c complex128) int { 44 z := c 45 for i := 0; i < _MaxEscape-1; i++ { 46 if real(z)*real(z)+imag(z)*imag(z) > 4 { 47 return i 48 } 49 z = z*z + c 50 } 51 return _MaxEscape - 1 52 } 53 */