File: buffer_test.go

package info (click to toggle)
micro 2.0.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,128 kB
  • sloc: sh: 265; makefile: 77; xml: 53
file content (323 lines) | stat: -rw-r--r-- 6,746 bytes parent folder | download | duplicates (2)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package buffer

import (
	"math/rand"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	lua "github.com/yuin/gopher-lua"
	"github.com/zyedidia/micro/v2/internal/config"
	ulua "github.com/zyedidia/micro/v2/internal/lua"
	"github.com/zyedidia/micro/v2/internal/util"
)

type operation struct {
	start Loc
	end   Loc
	text  []string
}

func init() {
	ulua.L = lua.NewState()
	config.InitRuntimeFiles(false)
	config.InitGlobalSettings()
	config.GlobalSettings["backup"] = false
	config.GlobalSettings["fastdirty"] = true
}

func check(t *testing.T, before []string, operations []operation, after []string) {
	assert := assert.New(t)

	b := NewBufferFromString(strings.Join(before, "\n"), "", BTDefault)

	assert.NotEqual("", b.GetName())
	assert.Equal(false, b.ExternallyModified())
	assert.Equal(false, b.Modified())
	assert.Equal(1, b.NumCursors())

	checkText := func(lines []string) {
		assert.Equal([]byte(strings.Join(lines, "\n")), b.Bytes())
		assert.Equal(len(lines), b.LinesNum())
		for i, s := range lines {
			assert.Equal(s, b.Line(i))
			assert.Equal([]byte(s), b.LineBytes(i))
		}
	}

	checkText(before)

	var cursors []*Cursor

	for _, op := range operations {
		cursor := NewCursor(b, op.start)
		cursor.SetSelectionStart(op.start)
		cursor.SetSelectionEnd(op.end)
		b.AddCursor(cursor)
		cursors = append(cursors, cursor)
	}

	assert.Equal(1+len(operations), b.NumCursors())

	for i, op := range operations {
		cursor := cursors[i]
		b.SetCurCursor(cursor.Num)
		cursor.DeleteSelection()
		b.Insert(cursor.Loc, strings.Join(op.text, "\n"))
	}

	checkText(after)

	// must have exactly two events per operation (delete and insert)
	for range operations {
		b.UndoOneEvent()
		b.UndoOneEvent()
	}

	checkText(before)

	for i, op := range operations {
		cursor := cursors[i]
		if op.start == op.end {
			assert.Equal(op.start, cursor.Loc)
		} else {
			assert.Equal(op.start, cursor.CurSelection[0])
			assert.Equal(op.end, cursor.CurSelection[1])
		}
	}

	for range operations {
		b.RedoOneEvent()
		b.RedoOneEvent()
	}

	checkText(after)

	b.Close()
}

const maxLineLength = 200

var alphabet = []rune(" abcdeäم📚")

func randomString(length int) string {
	runes := make([]rune, length)
	for i := range runes {
		runes[i] = alphabet[rand.Intn(len(alphabet))]
	}
	return string(runes)
}

func randomText(nLines int) string {
	lines := make([]string, nLines)
	for i := range lines {
		lines[i] = randomString(rand.Intn(maxLineLength + 1))
	}
	return strings.Join(lines, "\n")
}

func benchCreateAndClose(testingB *testing.B, nLines int) {
	rand.Seed(int64(nLines))

	text := randomText(nLines)

	testingB.ResetTimer()

	for i := 0; i < testingB.N; i++ {
		b := NewBufferFromString(text, "", BTDefault)
		b.Close()
	}
}

func benchRead(testingB *testing.B, nLines int) {
	rand.Seed(int64(nLines))

	b := NewBufferFromString(randomText(nLines), "", BTDefault)

	testingB.ResetTimer()

	for i := 0; i < testingB.N; i++ {
		b.Bytes()
		for j := 0; j < b.LinesNum(); j++ {
			b.Line(j)
			b.LineBytes(j)
		}
	}

	testingB.StopTimer()

	b.Close()
}

func benchEdit(testingB *testing.B, nLines, nCursors int) {
	rand.Seed(int64(nLines + nCursors))

	b := NewBufferFromString(randomText(nLines), "", BTDefault)

	regionSize := nLines / nCursors

	operations := make([]operation, nCursors)
	for i := range operations {
		startLine := (i * regionSize) + rand.Intn(regionSize-5)
		startColumn := rand.Intn(util.CharacterCountInString(b.Line(startLine)) + 1)
		endLine := startLine + 1 + rand.Intn(5)
		endColumn := rand.Intn(util.CharacterCountInString(b.Line(endLine)) + 1)

		operations[i] = operation{
			start: Loc{startColumn, startLine},
			end:   Loc{endColumn, endLine},
			text:  []string{randomText(2 + rand.Intn(4))},
		}
	}

	testingB.ResetTimer()

	for i := 0; i < testingB.N; i++ {
		b.SetCursors([]*Cursor{})

		var cursors []*Cursor

		for _, op := range operations {
			cursor := NewCursor(b, op.start)
			cursor.SetSelectionStart(op.start)
			cursor.SetSelectionEnd(op.end)
			b.AddCursor(cursor)
			cursors = append(cursors, cursor)
		}

		for j, op := range operations {
			cursor := cursors[j]
			b.SetCurCursor(cursor.Num)
			cursor.DeleteSelection()
			b.Insert(cursor.Loc, op.text[0])
		}

		for b.UndoStack.Peek() != nil {
			b.UndoOneEvent()
		}
	}

	testingB.StopTimer()

	b.Close()
}

func BenchmarkCreateAndClose10Lines(b *testing.B) {
	benchCreateAndClose(b, 10)
}

func BenchmarkCreateAndClose100Lines(b *testing.B) {
	benchCreateAndClose(b, 100)
}

func BenchmarkCreateAndClose1000Lines(b *testing.B) {
	benchCreateAndClose(b, 1000)
}

func BenchmarkCreateAndClose10000Lines(b *testing.B) {
	benchCreateAndClose(b, 10000)
}

func BenchmarkCreateAndClose100000Lines(b *testing.B) {
	benchCreateAndClose(b, 100000)
}

func BenchmarkCreateAndClose1000000Lines(b *testing.B) {
	benchCreateAndClose(b, 1000000)
}

func BenchmarkRead10Lines(b *testing.B) {
	benchRead(b, 10)
}

func BenchmarkRead100Lines(b *testing.B) {
	benchRead(b, 100)
}

func BenchmarkRead1000Lines(b *testing.B) {
	benchRead(b, 1000)
}

func BenchmarkRead10000Lines(b *testing.B) {
	benchRead(b, 10000)
}

func BenchmarkRead100000Lines(b *testing.B) {
	benchRead(b, 100000)
}

func BenchmarkRead1000000Lines(b *testing.B) {
	benchRead(b, 1000000)
}

func BenchmarkEdit10Lines1Cursor(b *testing.B) {
	benchEdit(b, 10, 1)
}

func BenchmarkEdit100Lines1Cursor(b *testing.B) {
	benchEdit(b, 100, 1)
}

func BenchmarkEdit100Lines10Cursors(b *testing.B) {
	benchEdit(b, 100, 10)
}

func BenchmarkEdit1000Lines1Cursor(b *testing.B) {
	benchEdit(b, 1000, 1)
}

func BenchmarkEdit1000Lines10Cursors(b *testing.B) {
	benchEdit(b, 1000, 10)
}

func BenchmarkEdit1000Lines100Cursors(b *testing.B) {
	benchEdit(b, 1000, 100)
}

func BenchmarkEdit10000Lines1Cursor(b *testing.B) {
	benchEdit(b, 10000, 1)
}

func BenchmarkEdit10000Lines10Cursors(b *testing.B) {
	benchEdit(b, 10000, 10)
}

func BenchmarkEdit10000Lines100Cursors(b *testing.B) {
	benchEdit(b, 10000, 100)
}

func BenchmarkEdit10000Lines1000Cursors(b *testing.B) {
	benchEdit(b, 10000, 1000)
}

func BenchmarkEdit100000Lines1Cursor(b *testing.B) {
	benchEdit(b, 100000, 1)
}

func BenchmarkEdit100000Lines10Cursors(b *testing.B) {
	benchEdit(b, 100000, 10)
}

func BenchmarkEdit100000Lines100Cursors(b *testing.B) {
	benchEdit(b, 100000, 100)
}

func BenchmarkEdit100000Lines1000Cursors(b *testing.B) {
	benchEdit(b, 100000, 1000)
}

func BenchmarkEdit1000000Lines1Cursor(b *testing.B) {
	benchEdit(b, 1000000, 1)
}

func BenchmarkEdit1000000Lines10Cursors(b *testing.B) {
	benchEdit(b, 1000000, 10)
}

func BenchmarkEdit1000000Lines100Cursors(b *testing.B) {
	benchEdit(b, 1000000, 100)
}

func BenchmarkEdit1000000Lines1000Cursors(b *testing.B) {
	benchEdit(b, 1000000, 1000)
}