FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sourcegraph/internal/bytesize/bytesize_test.go at main · Modestzero/sourcegraph · GitHub
Modestzero
/
sourcegraph
Public
forked from
sourcegraph/sourcegraph-public-snapshot
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sourcegraph
/
internal
/
bytesize
/
bytesize_test.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
124 lines (104 loc) · 2.15 KB
Breadcrumbs
sourcegraph
/
internal
/
bytesize
/
bytesize_test.go
Copy path
File metadata and controls
124 lines (104 loc) · 2.15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package
bytesize
import
(
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func
TestParse
(
t
*
testing.
T
) {
tests
:=
[]
struct
{
input
string
want
Bytes
}{
// Happy paths
{
"1 B"
,
1
},
{
"1 KB"
,
1000
},
{
"1 KiB"
,
1024
},
{
"1 MB"
,
1_000_000
},
{
"1 MiB"
,
1024
*
1024
},
{
"1 GB"
,
1_000_000_000
},
{
"1 GiB"
,
1024
*
1024
*
1024
},
// Whitespace
{
" 1 B"
,
1
},
{
" 100 KB"
,
100_000
},
// Various
{
"100 B"
,
100
},
{
"1024 B"
,
1024
},
{
"12 KB"
,
12_000
},
{
"72 KiB"
,
73_728
},
{
"3 MB"
,
3000000
},
{
"3 MiB"
,
3145728
},
{
"7 GB"
,
7_000_000_000
},
{
"7 GiB"
,
7_516_192_768
},
}
for
_
,
tt
:=
range
tests
{
got
,
err
:=
Parse
(
tt
.
input
)
if
err
!=
nil
{
t
.
Errorf
(
"FromString(%q), got error: %s"
,
tt
.
input
,
err
)
}
if
got
!=
tt
.
want
{
t
.
Errorf
(
"FromString(%q) = %d, want %d"
,
tt
.
input
,
got
,
tt
.
want
)
}
}
invalid
:=
[]
string
{
" "
,
"aaa"
,
"1"
,
"1 count"
,
"1 megabyte"
,
"10 horses"
,
"1324 k b"
,
}
for
_
,
tt
:=
range
invalid
{
_
,
err
:=
Parse
(
tt
)
if
err
==
nil
{
t
.
Errorf
(
"Parse(%q) expected error but got nil"
,
tt
)
}
}
}
func
TestParseOverflow
(
t
*
testing.
T
) {
input
:=
fmt
.
Sprintf
(
"%d KB"
, (
maxSize
/
2
))
_
,
err
:=
Parse
(
input
)
if
err
==
nil
{
t
.
Errorf
(
"Parse(%q) expected error, but got none"
,
input
)
}
}
func
TestReadNumber
(
t
*
testing.
T
) {
tests
:=
[]
struct
{
input
string
want
int
len
int
}{
{
"1234 foobar"
,
1234
,
4
},
{
"1234foobar"
,
1234
,
4
},
{
"12 34"
,
12
,
2
},
{
"1f"
,
1
,
1
},
{
"foobar"
,
0
,
0
},
}
for
_
,
tt
:=
range
tests
{
num
,
numLen
,
err
:=
readNumber
(
tt
.
input
)
require
.
NoError
(
t
,
err
)
if
num
!=
tt
.
want
{
t
.
Errorf
(
"readNumber(%q) = %d, want %d"
,
tt
.
input
,
num
,
tt
.
want
)
}
if
numLen
!=
tt
.
len
{
t
.
Errorf
(
"readNumber(%q) length = %d, want %d"
,
tt
.
input
,
numLen
,
tt
.
len
)
}
}
}
func
TestMultiplication
(
t
*
testing.
T
) {
tests
:=
[]
struct
{
inputNumber
int
inputUnit
Bytes
want
Bytes
}{
{
10
,
B
,
10
},
{
25
,
KB
,
25_000
},
{
25_000
,
KB
,
25_000_000
},
}
for
_
,
tt
:=
range
tests
{
got
:=
Bytes
(
tt
.
inputNumber
)
*
tt
.
inputUnit
if
got
!=
tt
.
want
{
t
.
Errorf
(
"FromUnit(%d, %v) = %d, want %d"
,
tt
.
inputNumber
,
tt
.
inputUnit
,
got
,
tt
.
want
)
}
}
}
Back
|
FazBrowse Home
|
New Git URL