You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.2 KiB

  1. package humanize
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "testing"
  7. )
  8. func TestFtoa(t *testing.T) {
  9. testList{
  10. {"200", Ftoa(200), "200"},
  11. {"2", Ftoa(2), "2"},
  12. {"2.2", Ftoa(2.2), "2.2"},
  13. {"2.02", Ftoa(2.02), "2.02"},
  14. {"200.02", Ftoa(200.02), "200.02"},
  15. }.validate(t)
  16. }
  17. func BenchmarkFtoaRegexTrailing(b *testing.B) {
  18. trailingZerosRegex := regexp.MustCompile(`\.?0+$`)
  19. b.ResetTimer()
  20. for i := 0; i < b.N; i++ {
  21. trailingZerosRegex.ReplaceAllString("2.00000", "")
  22. trailingZerosRegex.ReplaceAllString("2.0000", "")
  23. trailingZerosRegex.ReplaceAllString("2.000", "")
  24. trailingZerosRegex.ReplaceAllString("2.00", "")
  25. trailingZerosRegex.ReplaceAllString("2.0", "")
  26. trailingZerosRegex.ReplaceAllString("2", "")
  27. }
  28. }
  29. func BenchmarkFtoaFunc(b *testing.B) {
  30. for i := 0; i < b.N; i++ {
  31. stripTrailingZeros("2.00000")
  32. stripTrailingZeros("2.0000")
  33. stripTrailingZeros("2.000")
  34. stripTrailingZeros("2.00")
  35. stripTrailingZeros("2.0")
  36. stripTrailingZeros("2")
  37. }
  38. }
  39. func BenchmarkFmtF(b *testing.B) {
  40. for i := 0; i < b.N; i++ {
  41. _ = fmt.Sprintf("%f", 2.03584)
  42. }
  43. }
  44. func BenchmarkStrconvF(b *testing.B) {
  45. for i := 0; i < b.N; i++ {
  46. strconv.FormatFloat(2.03584, 'f', 6, 64)
  47. }
  48. }