File: all_test.go

package info (click to toggle)
golang-modernc-internal 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 148 kB
  • sloc: makefile: 124
file content (182 lines) | stat: -rw-r--r-- 3,607 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2016 The Internal Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package buffer // import "modernc.org/internal/buffer"

import (
	"bytes"
	"fmt"
	"math/rand"
	"os"
	"path"
	"runtime"
	"strings"
	"testing"
)

func caller(s string, va ...interface{}) {
	if s == "" {
		s = strings.Repeat("%v ", len(va))
	}
	_, fn, fl, _ := runtime.Caller(2)
	fmt.Fprintf(os.Stderr, "caller: %s:%d: ", path.Base(fn), fl)
	fmt.Fprintf(os.Stderr, s, va...)
	fmt.Fprintln(os.Stderr)
	_, fn, fl, _ = runtime.Caller(1)
	fmt.Fprintf(os.Stderr, "\tcallee: %s:%d: ", path.Base(fn), fl)
	fmt.Fprintln(os.Stderr)
	os.Stderr.Sync()
}

func dbg(s string, va ...interface{}) {
	if s == "" {
		s = strings.Repeat("%v ", len(va))
	}
	_, fn, fl, _ := runtime.Caller(1)
	fmt.Fprintf(os.Stderr, "dbg %s:%d: ", path.Base(fn), fl)
	fmt.Fprintf(os.Stderr, s, va...)
	fmt.Fprintln(os.Stderr)
	os.Stderr.Sync()
}

func TODO(...interface{}) string { //TODOOK
	_, fn, fl, _ := runtime.Caller(1)
	return fmt.Sprintf("TODO: %s:%d:\n", path.Base(fn), fl) //TODOOK
}

func use(...interface{}) {}

func init() {
	use(caller, dbg, TODO) //TODOOK
}

// ============================================================================

func test(t testing.TB, allocs, goroutines int) {
	ready := make(chan int, goroutines)
	run := make(chan int)
	done := make(chan int, goroutines)
	for i := 0; i < goroutines; i++ {
		go func() {
			a := rand.Perm(allocs)
			ready <- 1
			<-run
			for _, v := range a {
				p := Get(v)
				b := *p
				if g, e := len(b), v; g != e {
					t.Error(g, e)
					break
				}

				Put(p)
			}
			done <- 1
		}()
	}
	for i := 0; i < goroutines; i++ {
		<-ready
	}
	close(run)
	for i := 0; i < goroutines; i++ {
		<-done
	}
}

func Test2(t *testing.T) {
	test(t, 1<<15, 32)
}

func Benchmark1(b *testing.B) {
	const (
		allocs     = 1000
		goroutines = 100
	)
	for i := 0; i < b.N; i++ {
		test(b, allocs, goroutines)
	}
	b.SetBytes(goroutines * (allocs*allocs + allocs) / 2)
}

func TestBytes(t *testing.T) {
	const N = 1e6
	src := make([]byte, N)
	for i := range src {
		src[i] = byte(rand.Int())
	}
	var b Bytes
	for i, v := range src {
		b.WriteByte(v)
		if g, e := b.Len(), i+1; g != e {
			t.Fatal("Len", g, e)
		}
	}

	if !bytes.Equal(b.Bytes(), src) {
		t.Fatal("content differs")
	}

	b.Close()
	if g, e := b.Len(), 0; g != e {
		t.Fatal("Len", g, e)
	}

	if g, e := len(b.Bytes()), 0; g != e {
		t.Fatal("Bytes", g, e)
	}

	s2 := src
	for len(s2) != 0 {
		n := rand.Intn(127)
		if n > len(s2) {
			n = len(s2)
		}
		n2, _ := b.Write(s2[:n])
		if g, e := n2, n; g != e {
			t.Fatal("Write", g, e)
		}

		s2 = s2[n:]
	}

	if !bytes.Equal(b.Bytes(), src) {
		t.Fatal("content differs")
	}

	b.Close()
	s2 = src
	for len(s2) != 0 {
		n := rand.Intn(31)
		if n > len(s2) {
			n = len(s2)
		}
		n2, _ := b.WriteString(string(s2[:n]))
		if g, e := n2, n; g != e {
			t.Fatal("Write", g, e)
		}

		s2 = s2[n:]
	}

	if !bytes.Equal(b.Bytes(), src) {
		t.Fatal("content differs")
	}
}

func benchmarkWriteByte(b *testing.B, n int) {
	for i := 0; i < b.N; i++ {
		var buf Bytes
		for j := 0; j < n; j++ {
			buf.WriteByte(0)
		}
		buf.Close()
	}
	b.SetBytes(int64(n))
}

func BenchmarkWriteByte_1e3(b *testing.B) { benchmarkWriteByte(b, 1e3) }
func BenchmarkWriteByte_1e4(b *testing.B) { benchmarkWriteByte(b, 1e4) }
func BenchmarkWriteByte_1e5(b *testing.B) { benchmarkWriteByte(b, 1e5) }
func BenchmarkWriteByte_1e6(b *testing.B) { benchmarkWriteByte(b, 1e6) }
func BenchmarkWriteByte_1e7(b *testing.B) { benchmarkWriteByte(b, 1e7) }