github.com/sandwich-go/boost@v1.3.29/misc/hrff/hrff_test.go (about)

     1  // Copyright © 2012-2013 Lawrence E. Bakst. All rights reserved.
     2  
     3  package hrff
     4  
     5  import (
     6  	"flag"
     7  	"fmt"
     8  	"testing"
     9  )
    10  
    11  var _i1 = Int64{V: 2}
    12  var i1 int64
    13  
    14  const ci1 = 1 * 1024 * 1024 * 1024
    15  const ci2 = -2
    16  
    17  var _i2 = Int64{V: 3}
    18  var i2 int64
    19  
    20  var _f1 = Float64{V: 4.5}
    21  var f1 float64
    22  var _f2 = Float64{V: 5.5}
    23  var f2 float64
    24  
    25  func TestHRFF(t *testing.T) {
    26  
    27  	_i1.Set("1k")
    28  	i1 = int64(_i1.V)
    29  	if i1 != 1000 {
    30  		t.Errorf("wanted %d got %d\n", ci1, 1000)
    31  	}
    32  
    33  	_i1.Set("1M")
    34  	i1 = int64(_i1.V)
    35  	if i1 != 1000000 {
    36  		t.Errorf("wanted %d got %d\n", ci1, 1000000)
    37  	}
    38  
    39  	_i1.Set("1000k")
    40  	i1 = int64(_i1.V)
    41  	if i1 != 1000000 {
    42  		t.Errorf("wanted %d got %d\n", ci1, 1000000)
    43  	}
    44  
    45  	_i1.Set("1Gi")
    46  	i1 = int64(_i1.V)
    47  	if i1 != ci1 {
    48  		t.Errorf("wanted %d got %d\n", ci1, i1)
    49  	}
    50  	_f1.Set("123.k")
    51  	f1 = 123. * 1000
    52  	if float64(_f1.V) != f1 {
    53  		t.Errorf("wanted %f got %f\n", f1, _f1)
    54  	}
    55  	_f1.Set("-1.M")
    56  	f1 = -1 * 1000000
    57  	if float64(_f1.V) != f1 {
    58  		t.Errorf("wanted %f got %f\n", f1, _f1)
    59  	}
    60  	_f1.Set(".001m")
    61  	f1 = .000001
    62  	if float64(_f1.V) != f1 {
    63  		t.Errorf("wanted %f got %f\n", f1, _f1)
    64  	}
    65  
    66  }
    67  
    68  func Example001() {
    69  	var i1 = Int64{2000000, "bps"}
    70  	var i2 = Int64{V: 3000}
    71  	var f1 = Float64{-7020000000.1, "B"}
    72  	var f2 = Float64{.000001, "s"}
    73  
    74  	var imm1 = Int64{-40000, ""}
    75  	var imm2 = Int64{1 * 1024 * 1024 * 1024, ""}
    76  
    77  	flag.Var(&i1, "10", "i1")
    78  	flag.Var(&i2, "11", "i2")
    79  	flag.Var(&f1, "12.1", "f1")
    80  	flag.Var(&f2, "12.2", "f2")
    81  
    82  	flag.Parse()
    83  	fmt.Printf("i1=%d, i2=%d, f1=%f, f2=%f, i1=%h, i2=%h, f1=%0.4h, f2=%h, imm1=%h, imm2=%H\n",
    84  		i1.V, i2.V, f1.V, f2.V, i1, i2, f1, f2, imm1, imm2)
    85  	fmt.Printf("%10.3h\n", Int64{V: 0, U: "foobars"})
    86  	// Output: i1=2000000, i2=3000, f1=-7020000000.100000, f2=0.000001, i1=2 Mbps, i2=3 k, f1=-7.0200 GB, f2=1 µs, imm1=-40 k, imm2=1 Gi
    87  	//          0 foobars
    88  }
    89  
    90  func Example002() {
    91  	var size = Int64{3 * 1024 * 1024 * 1024, "B"}
    92  	var speed = Float64{2100000, "bps"}
    93  
    94  	fmt.Printf("size=%H, speed=%0.2h\n", size, speed)
    95  	// Output: size=3 GiB, speed=2.10 Mbps
    96  }
    97  func Example003() {
    98  	var v = Int64{0, "B"}
    99  
   100  	fmt.Printf("v=%h\n", v)
   101  	// Output: v=0 B
   102  }
   103  func Example004() {
   104  	var v = Int64{1, "B"}
   105  
   106  	fmt.Printf("v=%h\n", v)
   107  	// Output: v=1 B
   108  }
   109  
   110  func Example005() {
   111  	var v = Float64{0, "B"}
   112  
   113  	fmt.Printf("v=%h\n", v)
   114  	// Output: v=0 B
   115  }
   116  
   117  func Example006() {
   118  	var v = Float64{1, "B"}
   119  
   120  	fmt.Printf("v=%h\n", v)
   121  	// Output: v=1 B
   122  }
   123  
   124  func Example007() {
   125  	var v = Float64{1024 * 1024 * 1024, "B"}
   126  
   127  	fmt.Printf("v=%h\n", v)
   128  	// Output: v=1 GB
   129  }
   130  
   131  func Example008() {
   132  	var v = Int64{1000, "B"}
   133  	fmt.Printf("v=%D\n", v)
   134  	// Output: v=1,000
   135  }
   136  
   137  func Example009() {
   138  	var v = Int{1000, "B"}
   139  	fmt.Printf("v=%D\n", v)
   140  	// Output: v=1,000
   141  }
   142  
   143  func Example010() {
   144  	var v = Int{-100, "B"}
   145  	fmt.Printf("v=%D\n", v)
   146  	// Output: v=-100
   147  }
   148  
   149  func Example011() {
   150  	var v = Int{-1000, "B"}
   151  	fmt.Printf("v=%D\n", v)
   152  	// Output: v=-1,000
   153  }
   154  
   155  func Example012() {
   156  	var v = Int{1234567, "B"}
   157  	fmt.Printf("v=%D\n", v)
   158  	// Output: v=1,234,567
   159  }
   160  
   161  func Example013() {
   162  	var v = Int{-1234567, "B"}
   163  	fmt.Printf("v=%D\n", v)
   164  	// Output: v=-1,234,567
   165  }
   166  
   167  func Example015a() {
   168  	var v = Float64{0, "B"}
   169  	fmt.Printf("v=%h\n", v)
   170  	// Output: v=0 B
   171  }
   172  
   173  func Example014() {
   174  	fmt.Printf("%h\n", Int64{V: 0, U: "foobars"})
   175  	// Output: 0 foobars
   176  }
   177  
   178  func Example015() {
   179  	fmt.Printf("%h\n", Float64{V: 0, U: "foobars"})
   180  	// Output: 0 foobars
   181  }
   182  
   183  func Example016() {
   184  	fmt.Printf("%h\n", Int64{V: 11, U: "foobars"})
   185  	// Output: 11 foobars
   186  }
   187  
   188  func Example017() {
   189  	fmt.Printf("%h\n", Float64{V: 11, U: "foobars"})
   190  	// Output: 11 foobars
   191  }
   192  
   193  func Example018() {
   194  	fmt.Printf("%h\n", Int64{V: 999, U: "foobars"})
   195  	// Output: 999 foobars
   196  }
   197  
   198  func Example019() {
   199  	fmt.Printf("%h\n", Float64{V: 999, U: "foobars"})
   200  	// Output: 999 foobars
   201  }
   202  
   203  func Example020() {
   204  	fmt.Printf("%h\n", Int64{V: 1000, U: "foobars"})
   205  	// Output: 1 kfoobars
   206  }
   207  
   208  func Example021() {
   209  	fmt.Printf("%h\n", Float64{V: 1000, U: "foobars"})
   210  	// Output: 1 kfoobars
   211  }
   212  
   213  func Example022() {
   214  	fmt.Printf("%D\n", Int64{V: 1000000, U: "foobars"})
   215  	// Output: 1,000,000
   216  }