1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
8 "github.com/ghodss/yaml"
9 check "gopkg.in/check.v1"
12 var _ = check.Suite(&ByteSizeSuite{})
14 type ByteSizeSuite struct{}
16 func (s *ByteSizeSuite) TestUnmarshal(c *check.C) {
17 for _, testcase := range []struct {
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
49 {"1E0EB", 1000000000000000000},
50 {"1E-1EB", 100000000000000000},
51 {"1E-1EiB", 115292150460684704},
52 {"4.5E15 K", 4500000000000000000},
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)
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
66 err := yaml.Unmarshal([]byte(testcase+"\n"), &n)
67 c.Logf("%s => error: %v", testcase, err)
68 c.Check(err, check.NotNil)
72 func (s *ByteSizeSuite) TestMarshalByteSizeOrPercent(c *check.C) {
73 for _, testcase := range []struct {
82 out, err := yaml.Marshal(&testcase.in)
83 c.Check(err, check.IsNil)
84 c.Check(string(out), check.Equals, testcase.out+"\n")
88 func (s *ByteSizeSuite) TestUnmarshalByteSizeOrPercent(c *check.C) {
89 for _, testcase := range []struct {
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)
106 for _, testcase := range []string{
107 "1000%", "101%", "-1%",
108 "%", "-%", "%%", "%1",
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)