20318: Comment about error handling.
[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("%v => error: %v", n, err)
68                 c.Check(err, check.NotNil)
69         }
70 }