123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- package door
- import (
- "bytes"
- "fmt"
- "testing"
- "unicode"
- )
- func TestLine(t *testing.T) {
- var textBuff *bytes.Buffer = &bytes.Buffer{}
- textBuff.WriteString("Test Me")
- var line *Line = &Line{Text: textBuff}
- var output string = string(line.Output())
- var expect string = string("Test Me")
- if output != expect {
- t.Errorf("Line: Expected %#v, got %#v", expect, output)
- }
- if line.Update() {
- t.Error("Line: No updater, should return false")
- }
- line.DefaultColor = Color([]int{0})
- line.Width = 8
- output = string(line.Output())
- expect = "\x1b[0mTest Me "
- if output != expect {
- t.Errorf("Line: Expected %#v, got %#v", expect, output)
- }
- // leave the default color, it is ignored when there's a render function
- // line.DefaultColor = ""
- line.RenderF = RenderBlueYellow
- output = string(line.Output())
- var blue string = string(ColorText("BOLD BLUE"))
- var yellow string = string(ColorText("BOLD YELLOW"))
- expect = blue + "T" + yellow + "est " + blue + "M" + yellow + "e "
- if output != expect {
- t.Errorf("Line: Expected %#v, got %#v", expect, output)
- }
- }
- func TestLineUpdate(t *testing.T) {
- var counter int = 0
- uf := func(u *bytes.Buffer) {
- u.Reset()
- fmt.Fprintf(u, "Count: %d", counter)
- }
- var line *Line = &Line{UpdateF: uf}
- line.Update()
- var output string = string(line.Output())
- var expect string = "Count: 0"
- if output != expect {
- t.Errorf("LineUpdate: Expected %#v, got %#v", expect, output)
- }
- if line.Update() {
- t.Error("Unexpected Update: should have returned false. (no change)")
- }
- counter++
- if !line.Update() {
- t.Error("Missing Update: value was changed, Text should have changed")
- }
- output = string(line.Output())
- expect = "Count: 1"
- if output != expect {
- t.Errorf("LineUpdate: Expected %#v, got %#v", expect, output)
- }
- }
- func TestLineUnicode(t *testing.T) {
- Unicode = true
- // code point > FFFF, use \U00000000 (not \u).
- var lineBuff *bytes.Buffer = &bytes.Buffer{}
- lineBuff.WriteString("Howdy \U0001f920")
- var line *Line = &Line{Text: lineBuff}
- var output []byte = line.Output()
- var expect []byte = []byte("Howdy 🤠")
- if bytes.Compare(output, expect) != 0 {
- t.Errorf("LineUnicode: Expected %s, got %s", expect, output)
- }
- if StringLen(expect) != 8 {
- t.Errorf("LineUnicode Strlen: Expected 8, got %d", StringLen(expect))
- }
- // 🤠 = 2 chars.
- line.Width = 9
- output = line.Output()
- expect = []byte("Howdy 🤠 ")
- if bytes.Compare(output, expect) != 0 {
- t.Errorf("LineUnicode: Expected %#v, got %#v", expect, output)
- }
- }
- func TestLineCP437(t *testing.T) {
- Unicode = false
- var tests []string = []string{"\xdb TEXT \xdb",
- "\xf1 F1", "\xf2 F2", "\xf3 F3"}
- for _, test := range tests {
- var lineBuff *bytes.Buffer = &bytes.Buffer{}
- lineBuff.WriteString(test)
- var line *Line = &Line{Text: lineBuff}
- var output string = string(line.Output())
- var expect string = test
- if output != expect {
- t.Errorf("LineCP437: Expected %#v, got %#v", expect, output)
- }
- }
- }
- // Benchmarks for profiling
- // go test -bench=BenchmarkLine -benchmem -memprofile memory.out -cpuprofile cpu.out
- // go tool pprof memory.out
- // go tool pprof cpu.out
- func BenchmarkLine(b *testing.B) {
- Unicode = false
- for n := 0; n < b.N; n++ {
- var lineBuff *bytes.Buffer = &bytes.Buffer{}
- fmt.Fprintf(lineBuff, "Line %d of %d", n, b.N)
- // lineBuff.WriteString(fmt.Sprintf("Line %d of %d", n, b.N))
- var line *Line = &Line{Text: lineBuff}
- line.Output()
- }
- }
- func BenchmarkLineUnicode(b *testing.B) {
- Unicode = true
- for n := 0; n < b.N; n++ {
- var lineBuff *bytes.Buffer = &bytes.Buffer{}
- fmt.Fprintf(lineBuff, "Line %d of %d", n, b.N)
- // lineBuff.WriteString(fmt.Sprintf("Line %d of %d", n, b.N))
- var line *Line = &Line{Text: lineBuff}
- line.Output()
- }
- }
- func BenchmarkLineColor(b *testing.B) {
- Unicode = false
- color := ColorText("BRI WHI ON BLUE")
- for n := 0; n < b.N; n++ {
- var lineBuff *bytes.Buffer = &bytes.Buffer{}
- fmt.Fprintf(lineBuff, "Line %d of %d", n, b.N)
- // lineBuff.WriteString(fmt.Sprintf("Line %d of %d", n, b.N))
- var line *Line = &Line{Text: lineBuff, DefaultColor: color}
- line.Output()
- }
- }
- func BenchmarkLineColorUnicode(b *testing.B) {
- Unicode = true
- color := ColorText("BRI WHI ON BLUE")
- for n := 0; n < b.N; n++ {
- var lineBuff *bytes.Buffer = &bytes.Buffer{}
- fmt.Fprintf(lineBuff, "Line %d of %d", n, b.N)
- // lineBuff.WriteString(fmt.Sprintf("Line %d of %d", n, b.N))
- var line *Line = &Line{Text: lineBuff, DefaultColor: color}
- line.Output()
- }
- }
- // Using RenderUppercase vs. RenderBlueYellow
- // BenchmarkLineRender-4 739462 1709 ns/op 376 B/op 13 allocs/op
- // BenchmarkLineRender-4 862102 1434 ns/op 648 B/op 9 allocs/op
- /*
- // This actually made BenchmarkLineUpdate worse.
- func printd(value int64, write io.Writer) {
- var buffer [32]byte
- var pos int
- if value == 0 {
- write.Write([]byte{'0'})
- return
- }
- for value > 0 {
- buffer[pos] = byte(int64('0') + value%10)
- pos++
- value = value / 10
- }
- for pos != 0 {
- pos--
- write.Write(buffer[pos : pos+1])
- }
- }
- */
- func BenchmarkLineUpdate(b *testing.B) {
- Unicode = false
- var line *Line = &Line{}
- var n int
- line.UpdateF = func(u *bytes.Buffer) {
- u.Reset()
- /*
- u.WriteString("Line ")
- printd(int64(n), u)
- u.WriteString(" of ")
- printd(int64(b.N), u)
- */
- fmt.Fprintf(u, "Line # %d of %d", n, b.N)
- }
- line.Update()
- for n = 0; n < b.N; n++ {
- line.Update()
- line.Output()
- }
- }
- func BenchmarkLineUpdateUnicode(b *testing.B) {
- Unicode = true
- var line *Line = &Line{}
- var n int
- line.UpdateF = func(u *bytes.Buffer) {
- u.Reset()
- /*
- u.WriteString("Line ")
- printd(int64(n), u)
- u.WriteString(" of ")
- printd(int64(b.N), u)
- */
- // № \u2116
- fmt.Fprintf(u, "Line № %d of %d", n, b.N)
- }
- line.Update()
- for n = 0; n < b.N; n++ {
- line.Update()
- line.Output()
- }
- }
- func BenchmarkLineRender(b *testing.B) {
- Unicode = false
- var rf ColorRender = RenderUppercase("RED", "GREEN")
- var line *Line = NewLine("ThIs Is CrAzY TeXt HeRe")
- line.RenderF = rf
- var n int
- for n = 0; n < b.N; n++ {
- line.Output()
- }
- }
- func BenchmarkLineRenderUnicode(b *testing.B) {
- Unicode = true
- var rf ColorRender = RenderUppercase("RED", "GREEN")
- // ₿ /u208f
- var line *Line = NewLine("ThIs Is CrAzY ₿ TeXt HeRe")
- line.RenderF = rf
- var n int
- for n = 0; n < b.N; n++ {
- line.Output()
- }
- }
- // Benchmarks / profiling
- // BenchmarkLineColor-4 2868162 403.9 ns/op 8 B/op 0 allocs/op
- // BenchmarkLineColor-4 2944704 400.0 ns/op 8 B/op 0 allocs/op
- // No change making Color strings to []byte. (reverted change)
- func BenchmarkLineRenderUpdate(b *testing.B) {
- Unicode = false
- var Up = ColorText("BLUE")
- var Down = ColorText("BOLD BLUE")
- var Num = ColorText("BRI GREEN")
- var Sym = ColorText("CYAN")
- var render = func(output *bytes.Buffer, text []byte) {
- output.Reset()
- var last *[]byte
- // var r Render = Render{Line: text}
- for _, letter := range text {
- if unicode.IsUpper(rune(letter)) {
- if last != &Up {
- output.Write(Up)
- last = &Up
- }
- // r.Append(Up, 1)
- } else if unicode.IsLower(rune(letter)) {
- if last != &Down {
- output.Write(Down)
- last = &Down
- }
- // r.Append(Down, 1)
- } else if unicode.IsDigit(rune(letter)) {
- if last != &Num {
- output.Write(Num)
- last = &Num
- }
- // r.Append(Num, 1)
- } else {
- if last != &Sym {
- output.Write(Sym)
- last = &Sym
- }
- //r.Append(Sym, 1)
- }
- output.WriteByte(letter)
- // output.WriteString(string(letter))
- }
- // return output.Bytes()
- // return r.Result
- }
- var up int
- var updater = func(u *bytes.Buffer) {
- u.Reset()
- fmt.Fprintf(u, "The Value: %d", up)
- // u.WriteString("The Value: ")
- // u.WriteString(strconv.Itoa(up))
- up++
- // return fmt.Sprintf("The Value: %d", up)
- }
- var line *Line = &Line{UpdateF: updater,
- RenderF: render,
- Width: 18}
- for i := 0; i < b.N; i++ {
- line.Update()
- line.Output()
- }
- }
- func BenchmarkLineRenderUpdateUnicode(b *testing.B) {
- Unicode = true
- var Up = ColorText("BLUE")
- var Down = ColorText("BOLD BLUE")
- var Num = ColorText("BRI GREEN")
- var Sym = ColorText("CYAN")
- // var unicodeBuff *bytes.Buffer = &bytes.Buffer{}
- var render = func(output *bytes.Buffer, text []byte) {
- output.Reset()
- var last *[]byte
- // var r Render = Render{Line: text}
- for _, letter := range text {
- if unicode.IsUpper(rune(letter)) {
- if last != &Up {
- output.Write(Up)
- last = &Up
- }
- // r.Append(Up, 1)
- } else if unicode.IsLower(rune(letter)) {
- if last != &Down {
- output.Write(Down)
- last = &Down
- }
- // r.Append(Down, 1)
- } else if unicode.IsDigit(rune(letter)) {
- if last != &Num {
- output.Write(Num)
- last = &Num
- }
- // r.Append(Num, 1)
- } else {
- if last != &Sym {
- output.Write(Sym)
- last = &Sym
- }
- //r.Append(Sym, 1)
- }
- output.WriteByte(letter)
- // output.WriteString(string(letter))
- }
- // return output.Bytes()
- // return r.Result
- }
- var up int
- var updater = func(u *bytes.Buffer) {
- u.Reset()
- fmt.Fprintf(u, "The Value: %d", up)
- // u.WriteString("The Value: ")
- // u.WriteString(strconv.Itoa(up))
- up++
- // return fmt.Sprintf("The Value: %d", up)
- }
- var line *Line = &Line{UpdateF: updater,
- RenderF: render,
- Width: 18}
- for i := 0; i < b.N; i++ {
- line.Update()
- line.Output()
- }
- }
|