Merge branch '21448-menu-reorder' into 21224-project-details
[arvados.git] / sdk / go / arvados / byte_size_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package arvados
6
7 import (
8         "github.com/ghodss/yaml"
9         check "gopkg.in/check.v1"
10 )
11
12 var _ = check.Suite(&ByteSizeSuite{})
13
14 type ByteSizeSuite struct{}
15
16 func (s *ByteSizeSuite) TestUnmarshal(c *check.C) {
17         for _, testcase := range []struct {
18                 in  string
19                 out int64
20         }{
21                 {"0", 0},
22                 {"5", 5},
23                 {"5B", 5},
24                 {"5 B", 5},
25                 {" 4 KiB ", 4096},
26                 {"0K", 0},
27                 {"0Ki", 0},
28                 {"0 KiB", 0},
29                 {"4K", 4000},
30                 {"4KB", 4000},
31                 {"4Ki", 4096},
32                 {"4KiB", 4096},
33                 {"4MB", 4000000},
34                 {"4MiB", 4194304},
35                 {"4GB", 4000000000},
36                 {"4 GiB", 4294967296},
37                 {"4TB", 4000000000000},
38                 {"4TiB", 4398046511104},
39                 {"4PB", 4000000000000000},
40                 {"4PiB", 4503599627370496},
41                 {"4EB", 4000000000000000000},
42                 {"4EiB", 4611686018427387904},
43                 {"4.5EiB", 5188146770730811392},
44                 {"1.5 GB", 1500000000},
45                 {"1.5 GiB", 1610612736},
46                 {"1.234 GiB", 1324997410}, // rounds down from 1324997410.816
47                 {"1e2 KB", 100000},
48                 {"20E-1 KiB", 2048},
49                 {"1E0EB", 1000000000000000000},
50                 {"1E-1EB", 100000000000000000},
51                 {"1E-1EiB", 115292150460684704},
52                 {"4.5E15 K", 4500000000000000000},
53         } {
54                 var n ByteSize
55                 err := yaml.Unmarshal([]byte(testcase.in+"\n"), &n)
56                 c.Logf("%v => %v: %v", testcase.in, testcase.out, n)
57                 c.Check(err, check.IsNil)
58                 c.Check(int64(n), check.Equals, testcase.out)
59         }
60         for _, testcase := range []string{
61                 "B", "K", "KB", "KiB", "4BK", "4iB", "4A", "b", "4b", "4mB", "4m", "4mib", "4KIB", "4K iB", "4Ki B", "BB", "4BB",
62                 "400000 EB", // overflows int64
63                 "4.11e4 EB", // ok as float64, but overflows int64
64         } {
65                 var n ByteSize
66                 err := yaml.Unmarshal([]byte(testcase+"\n"), &n)
67                 c.Logf("%s => error: %v", testcase, err)
68                 c.Check(err, check.NotNil)
69         }
70 }
71
72 func (s *ByteSizeSuite) TestMarshalByteSizeOrPercent(c *check.C) {
73         for _, testcase := range []struct {
74                 in  ByteSizeOrPercent
75                 out string
76         }{
77                 {0, "0"},
78                 {-1, "1%"},
79                 {-100, "100%"},
80                 {8, "8"},
81         } {
82                 out, err := yaml.Marshal(&testcase.in)
83                 c.Check(err, check.IsNil)
84                 c.Check(string(out), check.Equals, testcase.out+"\n")
85         }
86 }
87
88 func (s *ByteSizeSuite) TestUnmarshalByteSizeOrPercent(c *check.C) {
89         for _, testcase := range []struct {
90                 in  string
91                 out int64
92         }{
93                 {"0", 0},
94                 {"100", 100},
95                 {"0%", 0},
96                 {"1%", -1},
97                 {"100%", -100},
98                 {"8 GB", 8000000000},
99         } {
100                 var n ByteSizeOrPercent
101                 err := yaml.Unmarshal([]byte(testcase.in+"\n"), &n)
102                 c.Logf("%v => %v: %v", testcase.in, testcase.out, n)
103                 c.Check(err, check.IsNil)
104                 c.Check(int64(n), check.Equals, testcase.out)
105         }
106         for _, testcase := range []string{
107                 "1000%", "101%", "-1%",
108                 "%", "-%", "%%", "%1",
109                 "400000 EB",
110                 "4.11e4 EB",
111         } {
112                 var n ByteSizeOrPercent
113                 err := yaml.Unmarshal([]byte(testcase+"\n"), &n)
114                 c.Logf("%s => error: %v", testcase, err)
115                 c.Check(err, check.NotNil)
116         }
117 }