github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/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  	"time"
    26  
    27  	"github.com/snapcore/snapd/strutil/quantity"
    28  )
    29  
    30  func ExampleFormatAmount_short() {
    31  	fmt.Printf("%q\n", quantity.FormatAmount(12345, -1))
    32  	// Output: "12.3k"
    33  }
    34  
    35  func ExampleFormatAmount_long() {
    36  	for _, amount := range []uint64{
    37  		3,
    38  		13, 95,
    39  		103, 995,
    40  		1013, 9995,
    41  		10009, 99995,
    42  	} {
    43  		fmt.Printf("- %5d: 3: %q  5: %q  7: %q\n",
    44  			amount,
    45  			quantity.FormatAmount(amount, 3),
    46  			quantity.FormatAmount(amount, -1),
    47  			quantity.FormatAmount(amount, 7),
    48  		)
    49  	}
    50  	// Output:
    51  	// -     3: 3: "  3"  5: "    3"  7: "     3 "
    52  	// -    13: 3: " 13"  5: "   13"  7: "    13 "
    53  	// -    95: 3: " 95"  5: "   95"  7: "    95 "
    54  	// -   103: 3: "103"  5: "  103"  7: "   103 "
    55  	// -   995: 3: "995"  5: "  995"  7: "   995 "
    56  	// -  1013: 3: " 1k"  5: " 1013"  7: "  1013 "
    57  	// -  9995: 3: "10k"  5: "10.0k"  7: " 9.995k"
    58  	// - 10009: 3: "10k"  5: "10.0k"  7: "10.009k"
    59  	// - 99995: 3: ".1M"  5: " 100k"  7: "100.00k"
    60  }
    61  
    62  func ExampleFormatBPS() {
    63  	fmt.Printf("%q\n", quantity.FormatBPS(12345, (10*time.Millisecond).Seconds(), -1))
    64  	// Output: "1.23MB/s"
    65  }
    66  
    67  func ExampleFormatDuration() {
    68  	for _, dt := range []time.Duration{
    69  		3 * time.Nanosecond,
    70  		36 * time.Microsecond,
    71  		430 * time.Millisecond,
    72  		5155 * time.Millisecond,
    73  		time.Minute + 2*time.Second,
    74  		124 * time.Minute / 10,
    75  		2*time.Hour + 29*time.Minute,
    76  		10*time.Hour + 9*time.Minute,
    77  		10*time.Hour + 30*time.Minute,
    78  		11*time.Hour + 2*time.Minute,
    79  		30 * time.Hour,
    80  		345 * time.Hour,
    81  		357 * time.Hour,
    82  		4272 * time.Hour,
    83  		51368 * time.Hour,
    84  		math.MaxInt64 / 10,
    85  		math.MaxInt64,
    86  	} {
    87  		fmt.Printf("%q\n", quantity.FormatDuration(dt.Seconds()))
    88  	}
    89  	fmt.Printf("%q\n", quantity.FormatDuration(float64(math.MaxUint64)*365*24*60*60))
    90  	fmt.Printf("%q\n", quantity.FormatDuration(math.MaxFloat64))
    91  
    92  	// Output:
    93  	// "3.0ns"
    94  	// " 36µs"
    95  	// "430ms"
    96  	// "5.16s"
    97  	// "1m02s"
    98  	// "12.4m"
    99  	// "2h29m"
   100  	// "10h9m"
   101  	// "10.5h"
   102  	// "11h2m"
   103  	// "1d06h"
   104  	// "14d9h"
   105  	// "14.9d"
   106  	// " 178d"
   107  	// "5.86y"
   108  	// "29.2y"
   109  	// " 292y"
   110  	// " 18Ey"
   111  	// "ages!"
   112  }