github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/humanizeutil/humanize_test.go (about) 1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package humanizeutil_test 12 13 import ( 14 "math" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/util/humanizeutil" 18 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 19 ) 20 21 // TestHumanizeBytes verifies both IBytes and ParseBytes. 22 func TestBytes(t *testing.T) { 23 defer leaktest.AfterTest(t)() 24 25 testCases := []struct { 26 value int64 27 exp string 28 expNeg string 29 parseExp int64 30 parseErr string 31 parseErrNeg string 32 }{ 33 {0, "0 B", "0 B", 0, "", ""}, 34 {1024, "1.0 KiB", "-1.0 KiB", 1024, "", ""}, 35 {1024 << 10, "1.0 MiB", "-1.0 MiB", 1024 << 10, "", ""}, 36 {1024 << 20, "1.0 GiB", "-1.0 GiB", 1024 << 20, "", ""}, 37 {1024 << 30, "1.0 TiB", "-1.0 TiB", 1024 << 30, "", ""}, 38 {1024 << 40, "1.0 PiB", "-1.0 PiB", 1024 << 40, "", ""}, 39 {1024 << 50, "1.0 EiB", "-1.0 EiB", 1024 << 50, "", ""}, 40 {int64(math.MaxInt64), "8.0 EiB", "-8.0 EiB", 0, "too large: 8.0 EiB", "too large: -8.0 EiB"}, 41 } 42 43 for i, testCase := range testCases { 44 // Test IBytes. 45 if actual := humanizeutil.IBytes(testCase.value); actual != testCase.exp { 46 t.Errorf("%d: IBytes(%d) actual:%s does not match expected:%s", i, testCase.value, actual, testCase.exp) 47 } 48 // Test negative IBytes. 49 if actual := humanizeutil.IBytes(-testCase.value); actual != testCase.expNeg { 50 t.Errorf("%d: IBytes(%d) actual:%s does not match expected:%s", i, -testCase.value, actual, 51 testCase.expNeg) 52 } 53 // Test ParseBytes. 54 if actual, err := humanizeutil.ParseBytes(testCase.exp); err != nil { 55 if len(testCase.parseErr) > 0 { 56 if testCase.parseErr != err.Error() { 57 t.Errorf("%d: ParseBytes(%s) caused an incorrect error actual:%s, expected:%s", i, testCase.exp, 58 err, testCase.parseErr) 59 } 60 } else { 61 t.Errorf("%d: ParseBytes(%s) caused an unexpected error:%s", i, testCase.exp, err) 62 } 63 } else if actual != testCase.parseExp { 64 t.Errorf("%d: ParseBytes(%s) actual:%d does not match expected:%d", i, testCase.exp, actual, 65 testCase.parseExp) 66 } 67 // Test negative ParseBytes. 68 if actual, err := humanizeutil.ParseBytes(testCase.expNeg); err != nil { 69 if len(testCase.parseErrNeg) > 0 { 70 if testCase.parseErrNeg != err.Error() { 71 t.Errorf("%d: ParseBytes(%s) caused an incorrect error actual:%s, expected:%s", i, testCase.expNeg, 72 err, testCase.parseErrNeg) 73 } 74 } else { 75 t.Errorf("%d: ParseBytes(%s) caused an unexpected error:%s", i, testCase.expNeg, err) 76 } 77 } else if actual != -testCase.parseExp { 78 t.Errorf("%d: ParseBytes(%s) actual:%d does not match expected:%d", i, testCase.expNeg, actual, 79 -testCase.parseExp) 80 } 81 } 82 83 // Some extra error cases for good measure. 84 testFailCases := []struct { 85 value string 86 expected string 87 }{ 88 {"", "parsing \"\": invalid syntax"}, // our error 89 {"1 ZB", "unhandled size name: zb"}, // humanize's error 90 {"-1 ZB", "unhandled size name: zb"}, // humanize's error 91 {"1 ZiB", "unhandled size name: zib"}, // humanize's error 92 {"-1 ZiB", "unhandled size name: zib"}, // humanize's error 93 {"100 EiB", "too large: 100 EiB"}, // humanize's error 94 {"-100 EiB", "too large: 100 EiB"}, // humanize's error 95 {"10 EiB", "too large: 10 EiB"}, // our error 96 {"-10 EiB", "too large: -10 EiB"}, // our error 97 } 98 for i, testCase := range testFailCases { 99 if _, err := humanizeutil.ParseBytes(testCase.value); err.Error() != testCase.expected { 100 t.Errorf("%d: ParseBytes(%s) caused an incorrect error actual:%s, expected:%s", i, testCase.value, err, 101 testCase.expected) 102 } 103 } 104 }