golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/foreach/foreach_test.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package foreach_test 6 7 import ( 8 "bytes" 9 "fmt" 10 "strings" 11 "testing" 12 13 "golang.org/x/build/internal/foreach" 14 ) 15 16 func ExampleLine() { 17 v := []byte(`line 1 18 line 2 19 line 3 20 21 22 after two blank lines 23 last line`) 24 foreach.Line(v, func(b []byte) error { 25 fmt.Printf("%q\n", b) 26 return nil 27 }) 28 29 // Output: 30 // "line 1" 31 // "line 2" 32 // "line 3" 33 // "" 34 // "" 35 // "after two blank lines" 36 // "last line" 37 } 38 39 func TestLineAllocs(t *testing.T) { 40 v := bytes.Repeat([]byte(`waefaweflk 41 awfkjlawfe 42 wfaweflkjawfewfawef 43 44 awefwaejflk 45 awfljwfael 46 afjwewefk`), 1000) 47 allocs := testing.AllocsPerRun(1000, func() { 48 foreach.Line(v, func([]byte) error { return nil }) 49 }) 50 if allocs > 0.1 { 51 t.Errorf("got allocs = %v; want zero", allocs) 52 } 53 } 54 55 func TestLineStrAllocs(t *testing.T) { 56 s := strings.Repeat(`waefaweflk 57 awfkjlawfe 58 wfaweflkjawfewfawef 59 60 awefwaejflk 61 awfljwfael 62 afjwewefk`, 1000) 63 allocs := testing.AllocsPerRun(1000, func() { 64 foreach.LineStr(s, func(string) error { return nil }) 65 }) 66 if allocs > 0.1 { 67 t.Errorf("got allocs = %v; want zero", allocs) 68 } 69 }