| 1 |
package buffer |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/rand" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/assert" |
| 9 |
lua "github.com/yuin/gopher-lua" |
| 10 |
"github.com/zyedidia/micro/v2/internal/config" |
| 11 |
ulua "github.com/zyedidia/micro/v2/internal/lua" |
| 12 |
"github.com/zyedidia/micro/v2/internal/util" |
| 13 |
) |
| 14 |
|
| 15 |
type operation struct { |
| 16 |
start Loc |
| 17 |
end Loc |
| 18 |
text []string |
| 19 |
} |
| 20 |
|
| 21 |
func init() { |
| 22 |
ulua.L = lua.NewState() |
| 23 |
config.InitRuntimeFiles(false) |
| 24 |
config.InitGlobalSettings() |
| 25 |
config.GlobalSettings["backup"] = false |
| 26 |
config.GlobalSettings["fastdirty"] = true |
| 27 |
} |
| 28 |
|
| 29 |
func check(t *testing.T, before []string, operations []operation, after []string) { |
| 30 |
assert := assert.New(t) |
| 31 |
|
| 32 |
b := NewBufferFromString(strings.Join(before, "\n"), "", BTDefault) |
| 33 |
|
| 34 |
assert.NotEqual("", b.GetName()) |
| 35 |
assert.Equal(false, b.ExternallyModified()) |
| 36 |
assert.Equal(false, b.Modified()) |
| 37 |
assert.Equal(1, b.NumCursors()) |
| 38 |
|
| 39 |
checkText := func(lines []string) { |
| 40 |
assert.Equal([]byte(strings.Join(lines, "\n")), b.Bytes()) |
| 41 |
assert.Equal(len(lines), b.LinesNum()) |
| 42 |
for i, s := range lines { |
| 43 |
assert.Equal(s, b.Line(i)) |
| 44 |
assert.Equal([]byte(s), b.LineBytes(i)) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
checkText(before) |
| 49 |
|
| 50 |
var cursors []*Cursor |
| 51 |
|
| 52 |
for _, op := range operations { |
| 53 |
cursor := NewCursor(b, op.start) |
| 54 |
cursor.SetSelectionStart(op.start) |
| 55 |
cursor.SetSelectionEnd(op.end) |
| 56 |
b.AddCursor(cursor) |
| 57 |
cursors = append(cursors, cursor) |
| 58 |
} |
| 59 |
|
| 60 |
assert.Equal(1+len(operations), b.NumCursors()) |
| 61 |
|
| 62 |
for i, op := range operations { |
| 63 |
cursor := cursors[i] |
| 64 |
b.SetCurCursor(cursor.Num) |
| 65 |
cursor.DeleteSelection() |
| 66 |
b.Insert(cursor.Loc, strings.Join(op.text, "\n")) |
| 67 |
} |
| 68 |
|
| 69 |
checkText(after) |
| 70 |
|
| 71 |
// must have exactly two events per operation (delete and insert) |
| 72 |
for range operations { |
| 73 |
b.UndoOneEvent() |
| 74 |
b.UndoOneEvent() |
| 75 |
} |
| 76 |
|
| 77 |
checkText(before) |
| 78 |
|
| 79 |
for i, op := range operations { |
| 80 |
cursor := cursors[i] |
| 81 |
if op.start == op.end { |
| 82 |
assert.Equal(op.start, cursor.Loc) |
| 83 |
} else { |
| 84 |
assert.Equal(op.start, cursor.CurSelection[0]) |
| 85 |
assert.Equal(op.end, cursor.CurSelection[1]) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
for range operations { |
| 90 |
b.RedoOneEvent() |
| 91 |
b.RedoOneEvent() |
| 92 |
} |
| 93 |
|
| 94 |
checkText(after) |
| 95 |
|
| 96 |
b.Close() |
| 97 |
} |
| 98 |
|
| 99 |
const maxLineLength = 200 |
| 100 |
|
| 101 |
var alphabet = []rune(" abcdeäم📚") |
| 102 |
|
| 103 |
func randomString(length int) string { |
| 104 |
runes := make([]rune, length) |
| 105 |
for i := range runes { |
| 106 |
runes[i] = alphabet[rand.Intn(len(alphabet))] |
| 107 |
} |
| 108 |
return string(runes) |
| 109 |
} |
| 110 |
|
| 111 |
func randomText(nLines int) string { |
| 112 |
lines := make([]string, nLines) |
| 113 |
for i := range lines { |
| 114 |
lines[i] = randomString(rand.Intn(maxLineLength + 1)) |
| 115 |
} |
| 116 |
return strings.Join(lines, "\n") |
| 117 |
} |
| 118 |
|
| 119 |
func benchCreateAndClose(testingB *testing.B, nLines int) { |
| 120 |
rand.Seed(int64(nLines)) |
| 121 |
|
| 122 |
text := randomText(nLines) |
| 123 |
|
| 124 |
testingB.ResetTimer() |
| 125 |
|
| 126 |
for i := 0; i < testingB.N; i++ { |
| 127 |
b := NewBufferFromString(text, "", BTDefault) |
| 128 |
b.Close() |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func benchRead(testingB *testing.B, nLines int) { |
| 133 |
rand.Seed(int64(nLines)) |
| 134 |
|
| 135 |
b := NewBufferFromString(randomText(nLines), "", BTDefault) |
| 136 |
|
| 137 |
testingB.ResetTimer() |
| 138 |
|
| 139 |
for i := 0; i < testingB.N; i++ { |
| 140 |
b.Bytes() |
| 141 |
for j := 0; j < b.LinesNum(); j++ { |
| 142 |
b.Line(j) |
| 143 |
b.LineBytes(j) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
testingB.StopTimer() |
| 148 |
|
| 149 |
b.Close() |
| 150 |
} |
| 151 |
|
| 152 |
func benchEdit(testingB *testing.B, nLines, nCursors int) { |
| 153 |
rand.Seed(int64(nLines + nCursors)) |
| 154 |
|
| 155 |
b := NewBufferFromString(randomText(nLines), "", BTDefault) |
| 156 |
|
| 157 |
regionSize := nLines / nCursors |
| 158 |
|
| 159 |
operations := make([]operation, nCursors) |
| 160 |
for i := range operations { |
| 161 |
startLine := (i * regionSize) + rand.Intn(regionSize-5) |
| 162 |
startColumn := rand.Intn(util.CharacterCountInString(b.Line(startLine)) + 1) |
| 163 |
endLine := startLine + 1 + rand.Intn(5) |
| 164 |
endColumn := rand.Intn(util.CharacterCountInString(b.Line(endLine)) + 1) |
| 165 |
|
| 166 |
operations[i] = operation{ |
| 167 |
start: Loc{startColumn, startLine}, |
| 168 |
end: Loc{endColumn, endLine}, |
| 169 |
text: []string{randomText(2 + rand.Intn(4))}, |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
testingB.ResetTimer() |
| 174 |
|
| 175 |
for i := 0; i < testingB.N; i++ { |
| 176 |
b.SetCursors([]*Cursor{}) |
| 177 |
|
| 178 |
var cursors []*Cursor |
| 179 |
|
| 180 |
for _, op := range operations { |
| 181 |
cursor := NewCursor(b, op.start) |
| 182 |
cursor.SetSelectionStart(op.start) |
| 183 |
cursor.SetSelectionEnd(op.end) |
| 184 |
b.AddCursor(cursor) |
| 185 |
cursors = append(cursors, cursor) |
| 186 |
} |
| 187 |
|
| 188 |
for j, op := range operations { |
| 189 |
cursor := cursors[j] |
| 190 |
b.SetCurCursor(cursor.Num) |
| 191 |
cursor.DeleteSelection() |
| 192 |
b.Insert(cursor.Loc, op.text[0]) |
| 193 |
} |
| 194 |
|
| 195 |
for b.UndoStack.Peek() != nil { |
| 196 |
b.UndoOneEvent() |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
testingB.StopTimer() |
| 201 |
|
| 202 |
b.Close() |
| 203 |
} |
| 204 |
|
| 205 |
func BenchmarkCreateAndClose10Lines(b *testing.B) { |
| 206 |
benchCreateAndClose(b, 10) |
| 207 |
} |
| 208 |
|
| 209 |
func BenchmarkCreateAndClose100Lines(b *testing.B) { |
| 210 |
benchCreateAndClose(b, 100) |
| 211 |
} |
| 212 |
|
| 213 |
func BenchmarkCreateAndClose1000Lines(b *testing.B) { |
| 214 |
benchCreateAndClose(b, 1000) |
| 215 |
} |
| 216 |
|
| 217 |
func BenchmarkCreateAndClose10000Lines(b *testing.B) { |
| 218 |
benchCreateAndClose(b, 10000) |
| 219 |
} |
| 220 |
|
| 221 |
func BenchmarkCreateAndClose100000Lines(b *testing.B) { |
| 222 |
benchCreateAndClose(b, 100000) |
| 223 |
} |
| 224 |
|
| 225 |
func BenchmarkCreateAndClose1000000Lines(b *testing.B) { |
| 226 |
benchCreateAndClose(b, 1000000) |
| 227 |
} |
| 228 |
|
| 229 |
func BenchmarkRead10Lines(b *testing.B) { |
| 230 |
benchRead(b, 10) |
| 231 |
} |
| 232 |
|
| 233 |
func BenchmarkRead100Lines(b *testing.B) { |
| 234 |
benchRead(b, 100) |
| 235 |
} |
| 236 |
|
| 237 |
func BenchmarkRead1000Lines(b *testing.B) { |
| 238 |
benchRead(b, 1000) |
| 239 |
} |
| 240 |
|
| 241 |
func BenchmarkRead10000Lines(b *testing.B) { |
| 242 |
benchRead(b, 10000) |
| 243 |
} |
| 244 |
|
| 245 |
func BenchmarkRead100000Lines(b *testing.B) { |
| 246 |
benchRead(b, 100000) |
| 247 |
} |
| 248 |
|
| 249 |
func BenchmarkRead1000000Lines(b *testing.B) { |
| 250 |
benchRead(b, 1000000) |
| 251 |
} |
| 252 |
|
| 253 |
func BenchmarkEdit10Lines1Cursor(b *testing.B) { |
| 254 |
benchEdit(b, 10, 1) |
| 255 |
} |
| 256 |
|
| 257 |
func BenchmarkEdit100Lines1Cursor(b *testing.B) { |
| 258 |
benchEdit(b, 100, 1) |
| 259 |
} |
| 260 |
|
| 261 |
func BenchmarkEdit100Lines10Cursors(b *testing.B) { |
| 262 |
benchEdit(b, 100, 10) |
| 263 |
} |
| 264 |
|
| 265 |
func BenchmarkEdit1000Lines1Cursor(b *testing.B) { |
| 266 |
benchEdit(b, 1000, 1) |
| 267 |
} |
| 268 |
|
| 269 |
func BenchmarkEdit1000Lines10Cursors(b *testing.B) { |
| 270 |
benchEdit(b, 1000, 10) |
| 271 |
} |
| 272 |
|
| 273 |
func BenchmarkEdit1000Lines100Cursors(b *testing.B) { |
| 274 |
benchEdit(b, 1000, 100) |
| 275 |
} |
| 276 |
|
| 277 |
func BenchmarkEdit10000Lines1Cursor(b *testing.B) { |
| 278 |
benchEdit(b, 10000, 1) |
| 279 |
} |
| 280 |
|
| 281 |
func BenchmarkEdit10000Lines10Cursors(b *testing.B) { |
| 282 |
benchEdit(b, 10000, 10) |
| 283 |
} |
| 284 |
|
| 285 |
func BenchmarkEdit10000Lines100Cursors(b *testing.B) { |
| 286 |
benchEdit(b, 10000, 100) |
| 287 |
} |
| 288 |
|
| 289 |
func BenchmarkEdit10000Lines1000Cursors(b *testing.B) { |
| 290 |
benchEdit(b, 10000, 1000) |
| 291 |
} |
| 292 |
|
| 293 |
func BenchmarkEdit100000Lines1Cursor(b *testing.B) { |
| 294 |
benchEdit(b, 100000, 1) |
| 295 |
} |
| 296 |
|
| 297 |
func BenchmarkEdit100000Lines10Cursors(b *testing.B) { |
| 298 |
benchEdit(b, 100000, 10) |
| 299 |
} |
| 300 |
|
| 301 |
func BenchmarkEdit100000Lines100Cursors(b *testing.B) { |
| 302 |
benchEdit(b, 100000, 100) |
| 303 |
} |
| 304 |
|
| 305 |
func BenchmarkEdit100000Lines1000Cursors(b *testing.B) { |
| 306 |
benchEdit(b, 100000, 1000) |
| 307 |
} |
| 308 |
|
| 309 |
func BenchmarkEdit1000000Lines1Cursor(b *testing.B) { |
| 310 |
benchEdit(b, 1000000, 1) |
| 311 |
} |
| 312 |
|
| 313 |
func BenchmarkEdit1000000Lines10Cursors(b *testing.B) { |
| 314 |
benchEdit(b, 1000000, 10) |
| 315 |
} |
| 316 |
|
| 317 |
func BenchmarkEdit1000000Lines100Cursors(b *testing.B) { |
| 318 |
benchEdit(b, 1000000, 100) |
| 319 |
} |
| 320 |
|
| 321 |
func BenchmarkEdit1000000Lines1000Cursors(b *testing.B) { |
| 322 |
benchEdit(b, 1000000, 1000) |
| 323 |
} |