1package pb
2
3import (
4	"github.com/fatih/color"
5	"testing"
6)
7
8var testColorString = color.RedString("red") +
9	color.GreenString("hello") +
10	"simple" +
11	color.WhiteString("進捗")
12
13func TestUtilCellCount(t *testing.T) {
14	if e, l := 18, CellCount(testColorString); l != e {
15		t.Errorf("Invalid length %d, expected %d", l, e)
16	}
17}
18
19func TestUtilStripString(t *testing.T) {
20	if r, e := StripString("12345", 4), "1234"; r != e {
21		t.Errorf("Invalid result '%s', expected '%s'", r, e)
22	}
23
24	if r, e := StripString("12345", 5), "12345"; r != e {
25		t.Errorf("Invalid result '%s', expected '%s'", r, e)
26	}
27	if r, e := StripString("12345", 10), "12345"; r != e {
28		t.Errorf("Invalid result '%s', expected '%s'", r, e)
29	}
30
31	s := color.RedString("1") + "23"
32	e := color.RedString("1") + "2"
33	if r := StripString(s, 2); r != e {
34		t.Errorf("Invalid result '%s', expected '%s'", r, e)
35	}
36	return
37}
38
39func TestUtilRound(t *testing.T) {
40	if v := round(4.4); v != 4 {
41		t.Errorf("Unexpected result: %v", v)
42	}
43	if v := round(4.501); v != 5 {
44		t.Errorf("Unexpected result: %v", v)
45	}
46}
47
48func TestUtilFormatBytes(t *testing.T) {
49	inputs := []struct {
50		v int64
51		s bool
52		e string
53	}{
54		{v: 1000, s: false, e: "1000 B"},
55		{v: 1024, s: false, e: "1.00 KiB"},
56		{v: 3*_MiB + 140*_KiB, s: false, e: "3.14 MiB"},
57		{v: 2 * _GiB, s: false, e: "2.00 GiB"},
58		{v: 2048 * _GiB, s: false, e: "2.00 TiB"},
59
60		{v: 999, s: true, e: "999 B"},
61		{v: 1024, s: true, e: "1.02 kB"},
62		{v: 3*_MB + 140*_kB, s: true, e: "3.14 MB"},
63		{v: 2 * _GB, s: true, e: "2.00 GB"},
64		{v: 2048 * _GB, s: true, e: "2.05 TB"},
65	}
66
67	for _, input := range inputs {
68		actual := formatBytes(input.v, input.s)
69		if actual != input.e {
70			t.Errorf("Expected {%s} was {%s}", input.e, actual)
71		}
72	}
73}
74
75func BenchmarkUtilsCellCount(b *testing.B) {
76	b.ReportAllocs()
77	for i := 0; i < b.N; i++ {
78		CellCount(testColorString)
79	}
80}
81