13569: Accept metric/binary-prefixed units in RAM/scratch configs.
[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         } {
44                 var n ByteSize
45                 err := yaml.Unmarshal([]byte(testcase.in+"\n"), &n)
46                 c.Check(err, check.IsNil)
47                 c.Check(int64(n), check.Equals, testcase.out)
48         }
49         for _, testcase := range []string{
50                 "B", "K", "KB", "KiB", "4BK", "4iB", "4A", "b", "4b", "4mB", "4m", "4mib", "4KIB", "4K iB", "4Ki B", "BB", "4BB",
51                 "400000 EB", // overflows int64
52         } {
53                 var n ByteSize
54                 err := yaml.Unmarshal([]byte(testcase+"\n"), &n)
55                 c.Log(n)
56                 c.Log(err)
57                 c.Check(err, check.NotNil)
58         }
59 }