File: format_test.go

package info (click to toggle)
restic 0.18.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,824 kB
  • sloc: sh: 3,704; makefile: 50; python: 34
file content (104 lines) | stat: -rw-r--r-- 2,002 bytes parent folder | download | duplicates (2)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package ui

import (
	"testing"

	"github.com/restic/restic/internal/test"
)

func TestFormatBytes(t *testing.T) {
	for _, c := range []struct {
		size uint64
		want string
	}{
		{0, "0 B"},
		{1023, "1023 B"},
		{1024, "1.000 KiB"},
		{5<<20 + 1<<19, "5.500 MiB"},
		{1 << 30, "1.000 GiB"},
		{2 << 30, "2.000 GiB"},
		{1<<40 - 1<<36, "960.000 GiB"},
		{1 << 40, "1.000 TiB"},
	} {
		if got := FormatBytes(c.size); got != c.want {
			t.Errorf("want %q, got %q", c.want, got)
		}
	}
}

func TestFormatPercent(t *testing.T) {
	for _, c := range []struct {
		num, denom uint64
		want       string
	}{
		{0, 5, "0.00%"},
		{3, 7, "42.86%"},
		{99, 99, "100.00%"},
	} {
		if got := FormatPercent(c.num, c.denom); got != c.want {
			t.Errorf("want %q, got %q", c.want, got)
		}
	}
}

func TestParseBytes(t *testing.T) {
	for _, tt := range []struct {
		in       string
		expected int64
	}{
		{"1024", 1024},
		{"1024b", 1024},
		{"1024B", 1024},
		{"1k", 1024},
		{"100k", 102400},
		{"100K", 102400},
		{"10M", 10485760},
		{"100m", 104857600},
		{"20G", 21474836480},
		{"10g", 10737418240},
		{"2T", 2199023255552},
		{"2t", 2199023255552},
		{"9223372036854775807", 1<<63 - 1},
	} {
		actual, err := ParseBytes(tt.in)
		test.OK(t, err)
		test.Equals(t, tt.expected, actual)
	}
}

func TestParseBytesInvalid(t *testing.T) {
	for _, s := range []string{
		"",
		" ",
		"foobar",
		"zzz",
		"18446744073709551615", // 1<<64-1.
		"9223372036854775807k", // 1<<63-1 kiB.
		"9999999999999M",
		"99999999999999999999",
	} {
		v, err := ParseBytes(s)
		if err == nil {
			t.Errorf("wanted error for invalid value %q, got nil", s)
		}
		test.Equals(t, int64(0), v)
	}
}

func TestTerminalDisplayWidth(t *testing.T) {
	for _, c := range []struct {
		input string
		want  int
	}{
		{"foo", 3},
		{"aéb", 3},
		{"ab", 3},
		{"a’b", 3},
		{"aあb", 4},
	} {
		if got := TerminalDisplayWidth(c.input); got != c.want {
			t.Errorf("wrong display width for '%s', want %d, got %d", c.input, c.want, got)
		}
	}

}