-
Notifications
You must be signed in to change notification settings - Fork 175
/
utils_test.go
82 lines (75 loc) · 1.91 KB
/
utils_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Package ais_test provides tests of ais package.
/*
* Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
*/
package ais_test
import (
"testing"
"github.com/NVIDIA/aistore/cmn/cos"
)
func TestSizeToStr(t *testing.T) {
type tstruct struct {
val int64
num int
str string
}
tests := []tstruct{
{0, 0, "0B"},
{0, 1, "0B"},
{10, 0, "10B"},
{1000, 0, "1000B"},
{1100, 0, "1KiB"},
{1100, 2, "1.07KiB"},
{1024 * 1000, 0, "1000KiB"},
{1024 * 1025, 0, "1MiB"},
{1024 * 1024 * 1024 * 3, 3, "3.000GiB"},
{1024 * 1024 * 1024 * 1024 * 17, 0, "17TiB"},
{1024 * 1024 * 1024 * 1024 * 1024 * 2, 0, "2048TiB"},
}
for _, tst := range tests {
s := cos.ToSizeIEC(tst.val, tst.num)
if s != tst.str {
t.Errorf("Expected %s got %s", tst.str, s)
}
}
}
func TestStrToSize(t *testing.T) {
type tstruct struct {
val int64
str string
units string
}
tests := []tstruct{
{0, "0", cos.UnitsIEC},
{0, "0B", cos.UnitsIEC},
{10, "10B", cos.UnitsIEC},
{512, "0.5K", cos.UnitsIEC},
{1000, "1000B", cos.UnitsIEC},
{1024 * 1000, "1000KiB", cos.UnitsIEC},
{1024 * 1024, "1MiB", cos.UnitsIEC},
{1024 * 1024 * 2, "2m", cos.UnitsIEC},
{1024 * 1024 * 1024 * 3, "3GiB", cos.UnitsIEC},
{1024 * 1024 * 1024 * 1024 * 17, "17TiB", cos.UnitsIEC},
{1024 * 1024 * 1024 * 1024 * 1024 * 2, "2048TiB", cos.UnitsIEC},
{0, "0", ""},
{0, "0B", ""},
{10, "10B", ""},
{500, "0.5K", ""},
{1000, "1000B", ""}, // suffix takes precedence
{1024 * 1000, "1000KiB", ""},
{1024 * 1024, "1MiB", ""},
{1000 * 1000 * 2, "2m", ""}, // ditto
{1024 * 1024 * 1024 * 3, "3GiB", ""},
{1024 * 1024 * 1024 * 1024 * 17, "17TiB", ""},
{1024 * 1024 * 1024 * 1024 * 1024 * 2, "2048TiB", ""},
}
for _, tst := range tests {
n, e := cos.ParseSize(tst.str, tst.units)
if e != nil {
t.Errorf("Failed to convert %s: %v", tst.str, e)
}
if n != tst.val {
t.Errorf("Expected %d got %d", tst.val, n)
}
}
}