github.com/apcera/util@v0.0.0-20180322191801-7a50bc84ee48/str/str_test.go (about)

     1  // Copyright 2012 Apcera Inc. All rights reserved.
     2  
     3  package str
     4  
     5  import (
     6  	"testing"
     7  )
     8  
     9  func checkFmt(t *testing.T, v int64, expected string) {
    10  	s := FormatIntv(v)
    11  	if s != expected {
    12  		t.Fatalf("Formatting %d generated '%s' when expected '%s'",
    13  			v, s, expected)
    14  	}
    15  }
    16  
    17  func checkFmtv(t *testing.T, v interface{}, expected string) {
    18  	s := FormatIntv(v)
    19  	if s != expected {
    20  		t.Fatalf("Formatting %d generated '%s' when expected '%s'",
    21  			v, s, expected)
    22  	}
    23  }
    24  
    25  func TestFormatInt(t *testing.T) {
    26  
    27  	checkFmt(t, int64(0), "0")
    28  	checkFmt(t, int64(10), "10")
    29  	checkFmt(t, int64(100), "100")
    30  	checkFmt(t, int64(999), "999")
    31  	checkFmt(t, int64(1000), "1,000")
    32  	checkFmt(t, int64(10000), "10,000")
    33  	checkFmt(t, int64(100000), "100,000")
    34  	checkFmt(t, int64(1000000), "1,000,000")
    35  	checkFmt(t, int64(10000000), "10,000,000")
    36  	checkFmt(t, int64(100000000), "100,000,000")
    37  	checkFmt(t, int64(1000000000), "1,000,000,000")
    38  
    39  	checkFmt(t, int64(-10), "-10")
    40  	checkFmt(t, int64(-100), "-100")
    41  	checkFmt(t, int64(-999), "-999")
    42  	checkFmt(t, int64(-1000), "-1,000")
    43  	checkFmt(t, int64(-10000), "-10,000")
    44  	checkFmt(t, int64(-100000), "-100,000")
    45  	checkFmt(t, int64(-1000000), "-1,000,000")
    46  	checkFmt(t, int64(-10000000), "-10,000,000")
    47  	checkFmt(t, int64(-100000000), "-100,000,000")
    48  	checkFmt(t, int64(-1000000000), "-1,000,000,000")
    49  
    50  	// Check min/max
    51  
    52  	var i64 int64
    53  	var u64 uint64
    54  
    55  	i64 = 9223372036854775807
    56  	checkFmtv(t, i64, "9,223,372,036,854,775,807")
    57  
    58  	i64 = -9223372036854775808
    59  	checkFmtv(t, i64, "-9,223,372,036,854,775,808")
    60  
    61  	u64 = 18446744073709551615
    62  	checkFmtv(t, u64, "18,446,744,073,709,551,615")
    63  
    64  }