gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/strutil/quantity/example_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package quantity_test 21 22 import ( 23 "fmt" 24 "math" 25 "testing" 26 "time" 27 28 . "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/strutil/quantity" 31 ) 32 33 func Test(t *testing.T) { TestingT(t) } 34 35 func ExampleFormatAmount_short() { 36 fmt.Printf("%q\n", quantity.FormatAmount(12345, -1)) 37 // Output: "12.3k" 38 } 39 40 func ExampleFormatAmount_long() { 41 for _, amount := range []uint64{ 42 3, 43 13, 95, 44 103, 995, 45 1013, 9995, 46 10009, 99995, 47 } { 48 fmt.Printf("- %5d: 3: %q 5: %q 7: %q\n", 49 amount, 50 quantity.FormatAmount(amount, 3), 51 quantity.FormatAmount(amount, -1), 52 quantity.FormatAmount(amount, 7), 53 ) 54 } 55 // Output: 56 // - 3: 3: " 3" 5: " 3" 7: " 3 " 57 // - 13: 3: " 13" 5: " 13" 7: " 13 " 58 // - 95: 3: " 95" 5: " 95" 7: " 95 " 59 // - 103: 3: "103" 5: " 103" 7: " 103 " 60 // - 995: 3: "995" 5: " 995" 7: " 995 " 61 // - 1013: 3: " 1k" 5: " 1013" 7: " 1013 " 62 // - 9995: 3: "10k" 5: "10.0k" 7: " 9.995k" 63 // - 10009: 3: "10k" 5: "10.0k" 7: "10.009k" 64 // - 99995: 3: ".1M" 5: " 100k" 7: "100.00k" 65 } 66 67 func ExampleFormatBPS() { 68 fmt.Printf("%q\n", quantity.FormatBPS(12345, (10*time.Millisecond).Seconds(), -1)) 69 // Output: "1.23MB/s" 70 } 71 72 func ExampleFormatDuration() { 73 for _, dt := range []time.Duration{ 74 3 * time.Nanosecond, 75 36 * time.Microsecond, 76 430 * time.Millisecond, 77 5155 * time.Millisecond, 78 time.Minute + 2*time.Second, 79 124 * time.Minute / 10, 80 2*time.Hour + 29*time.Minute, 81 10*time.Hour + 9*time.Minute, 82 10*time.Hour + 30*time.Minute, 83 11*time.Hour + 2*time.Minute, 84 30 * time.Hour, 85 345 * time.Hour, 86 357 * time.Hour, 87 4272 * time.Hour, 88 51368 * time.Hour, 89 math.MaxInt64 / 10, 90 math.MaxInt64, 91 } { 92 fmt.Printf("%q\n", quantity.FormatDuration(dt.Seconds())) 93 } 94 fmt.Printf("%q\n", quantity.FormatDuration(float64(math.MaxUint64)*365*24*60*60)) 95 fmt.Printf("%q\n", quantity.FormatDuration(math.MaxFloat64)) 96 97 // Output: 98 // "3.0ns" 99 // " 36µs" 100 // "430ms" 101 // "5.16s" 102 // "1m02s" 103 // "12.4m" 104 // "2h29m" 105 // "10h9m" 106 // "10.5h" 107 // "11h2m" 108 // "1d06h" 109 // "14d9h" 110 // "14.9d" 111 // " 178d" 112 // "5.86y" 113 // "29.2y" 114 // " 292y" 115 // " 18Ey" 116 // "ages!" 117 }