-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlb_test.go
65 lines (56 loc) · 1.38 KB
/
lb_test.go
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
package lb_test
import (
"strings"
"testing"
"unicode"
"unicode/utf8"
lb "github.com/aslrousta/line-breaking"
)
var (
alice = "Alice was beginning to get very tired of sitting by her sister" +
" on the bank, and of having nothing to do: once or twice she had" +
" peeped into the book her sister was reading, but it had no pictures" +
" or conversations in it, 'and what is the use of a book,' thought" +
" Alice 'without pictures or conversation?'"
)
type Word string
func (w Word) Direction() lb.Direction {
r, _ := utf8.DecodeRuneInString(string(w))
if unicode.Is(unicode.Arabic, r) {
return lb.RightToLeft
}
return lb.LeftToRight
}
func (w Word) Width() float32 {
return float32(utf8.RuneCountInString(string(w)))
}
func BenchmarkGreedy(b *testing.B) {
para := splitWords(alice)
for i := 0; i < b.N; i++ {
lb.Greedy(para, &lb.Options{
TextWidth: 50,
TextDirection: lb.LeftToRight,
GlueWidth: 1,
GlueExpand: 1,
})
}
}
func BenchmarkKnuthPlass(b *testing.B) {
para := splitWords(alice)
for i := 0; i < b.N; i++ {
lb.KnuthPlass(para, &lb.Options{
TextWidth: 50,
TextDirection: lb.LeftToRight,
GlueWidth: 1,
GlueExpand: 1,
})
}
}
func splitWords(text string) []lb.Box {
words := strings.Split(text, " ")
result := make([]lb.Box, 0, len(words))
for _, w := range words {
result = append(result, Word(w))
}
return result
}