Something went wrong. Try again.
Fork of micro-editor/micro
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324package buffer
import ( "math/rand" "strings" "testing"
"github.com/micro-editor/micro/v2/internal/config" ulua "github.com/micro-editor/micro/v2/internal/lua" "github.com/micro-editor/micro/v2/internal/util" "github.com/stretchr/testify/assert" lua "github.com/yuin/gopher-lua")
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)}