...

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

Documentation: github.com/dustin/go-humanize

     1  package humanize
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"reflect"
     7  	"regexp"
     8  	"strconv"
     9  	"strings"
    10  	"testing"
    11  	"testing/quick"
    12  )
    13  
    14  func TestFtoa(t *testing.T) {
    15  	testList{
    16  		{"200", Ftoa(200), "200"},
    17  		{"20", Ftoa(20.0), "20"},
    18  		{"2", Ftoa(2), "2"},
    19  		{"2.2", Ftoa(2.2), "2.2"},
    20  		{"2.02", Ftoa(2.02), "2.02"},
    21  		{"200.02", Ftoa(200.02), "200.02"},
    22  	}.validate(t)
    23  }
    24  
    25  func TestFtoaWithDigits(t *testing.T) {
    26  	testList{
    27  		{"1.23, 0", FtoaWithDigits(1.23, 0), "1"},
    28  		{"20, 0", FtoaWithDigits(20.0, 0), "20"},
    29  		{"1.23, 1", FtoaWithDigits(1.23, 1), "1.2"},
    30  		{"1.23, 2", FtoaWithDigits(1.23, 2), "1.23"},
    31  		{"1.23, 3", FtoaWithDigits(1.23, 3), "1.23"},
    32  	}.validate(t)
    33  }
    34  
    35  func TestStripTrailingDigits(t *testing.T) {
    36  	err := quick.Check(func(s string, digits int) bool {
    37  		stripped := stripTrailingDigits(s, digits)
    38  
    39  		// A stripped string will always be a prefix of its original string
    40  		if !strings.HasPrefix(s, stripped) {
    41  			return false
    42  		}
    43  
    44  		if strings.ContainsRune(s, '.') {
    45  			// If there is a dot, the part on the left of the dot will never change
    46  			a := strings.Split(s, ".")
    47  			b := strings.Split(stripped, ".")
    48  			if a[0] != b[0] {
    49  				return false
    50  			}
    51  		} else {
    52  			// If there's no dot in the input, the output will always be the same as the input.
    53  			if stripped != s {
    54  				return false
    55  			}
    56  		}
    57  
    58  		return true
    59  	}, &quick.Config{
    60  		MaxCount: 10000,
    61  		Values: func(v []reflect.Value, r *rand.Rand) {
    62  			rdigs := func(n int) string {
    63  				digs := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
    64  				var rv []rune
    65  				for i := 0; i < n; i++ {
    66  					rv = append(rv, digs[r.Intn(len(digs))])
    67  				}
    68  				return string(rv)
    69  			}
    70  
    71  			ls := r.Intn(20)
    72  			rs := r.Intn(20)
    73  			jc := "."
    74  			if rs == 0 {
    75  				jc = ""
    76  			}
    77  			s := rdigs(ls) + jc + rdigs(rs)
    78  			digits := r.Intn(len(s) + 1)
    79  
    80  			v[0] = reflect.ValueOf(s)
    81  			v[1] = reflect.ValueOf(digits)
    82  		},
    83  	})
    84  
    85  	if err != nil {
    86  		t.Error(err)
    87  	}
    88  }
    89  
    90  func BenchmarkFtoaRegexTrailing(b *testing.B) {
    91  	trailingZerosRegex := regexp.MustCompile(`\.?0+$`)
    92  
    93  	b.ResetTimer()
    94  	for i := 0; i < b.N; i++ {
    95  		trailingZerosRegex.ReplaceAllString("2.00000", "")
    96  		trailingZerosRegex.ReplaceAllString("2.0000", "")
    97  		trailingZerosRegex.ReplaceAllString("2.000", "")
    98  		trailingZerosRegex.ReplaceAllString("2.00", "")
    99  		trailingZerosRegex.ReplaceAllString("2.0", "")
   100  		trailingZerosRegex.ReplaceAllString("2", "")
   101  	}
   102  }
   103  
   104  func BenchmarkFtoaFunc(b *testing.B) {
   105  	for i := 0; i < b.N; i++ {
   106  		stripTrailingZeros("2.00000")
   107  		stripTrailingZeros("2.0000")
   108  		stripTrailingZeros("2.000")
   109  		stripTrailingZeros("2.00")
   110  		stripTrailingZeros("2.0")
   111  		stripTrailingZeros("2")
   112  	}
   113  }
   114  
   115  func BenchmarkFmtF(b *testing.B) {
   116  	for i := 0; i < b.N; i++ {
   117  		_ = fmt.Sprintf("%f", 2.03584)
   118  	}
   119  }
   120  
   121  func BenchmarkStrconvF(b *testing.B) {
   122  	for i := 0; i < b.N; i++ {
   123  		strconv.FormatFloat(2.03584, 'f', 6, 64)
   124  	}
   125  }
   126  

View as plain text