github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/ais/utils_test.go (about) 1 // Package ais_test provides tests of ais package. 2 /* 3 * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package ais_test 6 7 import ( 8 "testing" 9 10 "github.com/NVIDIA/aistore/cmn/cos" 11 ) 12 13 func TestSizeToStr(t *testing.T) { 14 type tstruct struct { 15 val int64 16 num int 17 str string 18 } 19 tests := []tstruct{ 20 {0, 0, "0B"}, 21 {0, 1, "0B"}, 22 {10, 0, "10B"}, 23 {1000, 0, "1000B"}, 24 {1100, 0, "1KiB"}, 25 {1100, 2, "1.07KiB"}, 26 {1024 * 1000, 0, "1000KiB"}, 27 {1024 * 1025, 0, "1MiB"}, 28 {1024 * 1024 * 1024 * 3, 3, "3.000GiB"}, 29 {1024 * 1024 * 1024 * 1024 * 17, 0, "17TiB"}, 30 {1024 * 1024 * 1024 * 1024 * 1024 * 2, 0, "2048TiB"}, 31 } 32 33 for _, tst := range tests { 34 s := cos.ToSizeIEC(tst.val, tst.num) 35 if s != tst.str { 36 t.Errorf("Expected %s got %s", tst.str, s) 37 } 38 } 39 } 40 41 func TestStrToSize(t *testing.T) { 42 type tstruct struct { 43 val int64 44 str string 45 units string 46 } 47 tests := []tstruct{ 48 {0, "0", cos.UnitsIEC}, 49 {0, "0B", cos.UnitsIEC}, 50 {10, "10B", cos.UnitsIEC}, 51 {512, "0.5K", cos.UnitsIEC}, 52 {1000, "1000B", cos.UnitsIEC}, 53 {1024 * 1000, "1000KiB", cos.UnitsIEC}, 54 {1024 * 1024, "1MiB", cos.UnitsIEC}, 55 {1024 * 1024 * 2, "2m", cos.UnitsIEC}, 56 {1024 * 1024 * 1024 * 3, "3GiB", cos.UnitsIEC}, 57 {1024 * 1024 * 1024 * 1024 * 17, "17TiB", cos.UnitsIEC}, 58 {1024 * 1024 * 1024 * 1024 * 1024 * 2, "2048TiB", cos.UnitsIEC}, 59 60 {0, "0", ""}, 61 {0, "0B", ""}, 62 {10, "10B", ""}, 63 {500, "0.5K", ""}, 64 {1000, "1000B", ""}, // suffix takes precedence 65 {1024 * 1000, "1000KiB", ""}, 66 {1024 * 1024, "1MiB", ""}, 67 {1000 * 1000 * 2, "2m", ""}, // ditto 68 {1024 * 1024 * 1024 * 3, "3GiB", ""}, 69 {1024 * 1024 * 1024 * 1024 * 17, "17TiB", ""}, 70 {1024 * 1024 * 1024 * 1024 * 1024 * 2, "2048TiB", ""}, 71 } 72 73 for _, tst := range tests { 74 n, e := cos.ParseSize(tst.str, tst.units) 75 if e != nil { 76 t.Errorf("Failed to convert %s: %v", tst.str, e) 77 } 78 if n != tst.val { 79 t.Errorf("Expected %d got %d", tst.val, n) 80 } 81 } 82 }