...

Source file src/github.com/dustin/go-humanize/si_test.go

Documentation: github.com/dustin/go-humanize

     1  package humanize
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  )
     7  
     8  func TestSI(t *testing.T) {
     9  	tests := []struct {
    10  		name      string
    11  		num       float64
    12  		formatted string
    13  	}{
    14  		{"e-30", 1e-30, "1 qF"},
    15  		{"e-27", 1e-27, "1 rF"},
    16  		{"e-24", 1e-24, "1 yF"},
    17  		{"e-21", 1e-21, "1 zF"},
    18  		{"e-18", 1e-18, "1 aF"},
    19  		{"e-15", 1e-15, "1 fF"},
    20  		{"e-12", 1e-12, "1 pF"},
    21  		{"e-12", 2.2345e-12, "2.2345 pF"},
    22  		{"e-12", 2.23e-12, "2.23 pF"},
    23  		{"e-11", 2.23e-11, "22.3 pF"},
    24  		{"e-10", 2.2e-10, "220 pF"},
    25  		{"e-9", 2.2e-9, "2.2 nF"},
    26  		{"e-8", 2.2e-8, "22 nF"},
    27  		{"e-7", 2.2e-7, "220 nF"},
    28  		{"e-6", 2.2e-6, "2.2 µF"},
    29  		{"e-6", 1e-6, "1 µF"},
    30  		{"e-5", 2.2e-5, "22 µF"},
    31  		{"e-4", 2.2e-4, "220 µF"},
    32  		{"e-3", 2.2e-3, "2.2 mF"},
    33  		{"e-2", 2.2e-2, "22 mF"},
    34  		{"e-1", 2.2e-1, "220 mF"},
    35  		{"e+0", 2.2e-0, "2.2 F"},
    36  		{"e+0", 2.2, "2.2 F"},
    37  		{"e+1", 2.2e+1, "22 F"},
    38  		{"0", 0, "0 F"},
    39  		{"e+1", 22, "22 F"},
    40  		{"e+2", 2.2e+2, "220 F"},
    41  		{"e+2", 220, "220 F"},
    42  		{"e+3", 2.2e+3, "2.2 kF"},
    43  		{"e+3", 2200, "2.2 kF"},
    44  		{"e+4", 2.2e+4, "22 kF"},
    45  		{"e+4", 22000, "22 kF"},
    46  		{"e+5", 2.2e+5, "220 kF"},
    47  		{"e+6", 2.2e+6, "2.2 MF"},
    48  		{"e+6", 1e+6, "1 MF"},
    49  		{"e+7", 2.2e+7, "22 MF"},
    50  		{"e+8", 2.2e+8, "220 MF"},
    51  		{"e+9", 2.2e+9, "2.2 GF"},
    52  		{"e+10", 2.2e+10, "22 GF"},
    53  		{"e+11", 2.2e+11, "220 GF"},
    54  		{"e+12", 2.2e+12, "2.2 TF"},
    55  		{"e+15", 2.2e+15, "2.2 PF"},
    56  		{"e+18", 2.2e+18, "2.2 EF"},
    57  		{"e+21", 2.2e+21, "2.2 ZF"},
    58  		{"e+24", 2.2e+24, "2.2 YF"},
    59  		{"e+27", 2.2e+27, "2.2 RF"},
    60  		{"e+30", 2.2e+30, "2.2 QF"},
    61  
    62  		// special case
    63  		{"1F", 1000 * 1000, "1 MF"},
    64  		{"1F", 1e6, "1 MF"},
    65  
    66  		// negative number
    67  		{"-100 F", -100, "-100 F"},
    68  	}
    69  
    70  	for _, test := range tests {
    71  		got := SI(test.num, "F")
    72  		if got != test.formatted {
    73  			t.Errorf("On %v (%v), got %v, wanted %v",
    74  				test.name, test.num, got, test.formatted)
    75  		}
    76  
    77  		gotf, gotu, err := ParseSI(test.formatted)
    78  		if err != nil {
    79  			t.Errorf("Error parsing %v (%v): %v", test.name, test.formatted, err)
    80  			continue
    81  		}
    82  
    83  		if math.Abs(1-(gotf/test.num)) > 0.01 {
    84  			t.Errorf("On %v (%v), got %v, wanted %v (±%v)",
    85  				test.name, test.formatted, gotf, test.num,
    86  				math.Abs(1-(gotf/test.num)))
    87  		}
    88  		if gotu != "F" {
    89  			t.Errorf("On %v (%v), expected unit F, got %v",
    90  				test.name, test.formatted, gotu)
    91  		}
    92  	}
    93  
    94  	// Parse error
    95  	gotf, gotu, err := ParseSI("x1.21JW") // 1.21 jigga whats
    96  	if err == nil {
    97  		t.Errorf("Expected error on x1.21JW, got %v %v", gotf, gotu)
    98  	}
    99  }
   100  
   101  func TestSIWithDigits(t *testing.T) {
   102  	tests := []struct {
   103  		name      string
   104  		num       float64
   105  		digits    int
   106  		formatted string
   107  	}{
   108  		{"e-12", 2.234e-12, 0, "2 pF"},
   109  		{"e-12", 2.234e-12, 1, "2.2 pF"},
   110  		{"e-12", 2.234e-12, 2, "2.23 pF"},
   111  		{"e-12", 2.234e-12, 3, "2.234 pF"},
   112  		{"e-12", 2.234e-12, 4, "2.234 pF"},
   113  	}
   114  
   115  	for _, test := range tests {
   116  		got := SIWithDigits(test.num, test.digits, "F")
   117  		if got != test.formatted {
   118  			t.Errorf("On %v (%v), got %v, wanted %v",
   119  				test.name, test.num, got, test.formatted)
   120  		}
   121  	}
   122  }
   123  
   124  func BenchmarkParseSI(b *testing.B) {
   125  	for i := 0; i < b.N; i++ {
   126  		ParseSI("2.2346ZB")
   127  	}
   128  }
   129  
   130  // There was a report that zeroes were being truncated incorrectly
   131  func TestBug106(t *testing.T) {
   132  	tests := []struct{
   133  		in float64
   134  		want string
   135  	}{
   136  		{20.0, "20 U"},
   137  		{200.0, "200 U"},
   138  	}
   139  
   140  	for _, test := range tests {
   141  		if got :=SIWithDigits(test.in, 0, "U") ;  got != test.want {
   142  			t.Errorf("on %f got %v, want %v", test.in, got, test.want);
   143  		}
   144  	}
   145  }
   146  

View as plain text