int_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package strconv // import "github.com/tdewolff/parse/strconv"
  2. import (
  3. "math"
  4. "math/rand"
  5. "testing"
  6. "github.com/tdewolff/test"
  7. )
  8. func TestParseInt(t *testing.T) {
  9. intTests := []struct {
  10. i string
  11. expected int64
  12. }{
  13. {"5", 5},
  14. {"99", 99},
  15. {"999", 999},
  16. {"-5", -5},
  17. {"+5", 5},
  18. {"9223372036854775807", 9223372036854775807},
  19. {"9223372036854775808", 0},
  20. {"-9223372036854775807", -9223372036854775807},
  21. {"-9223372036854775808", -9223372036854775808},
  22. {"-9223372036854775809", 0},
  23. {"18446744073709551620", 0},
  24. {"a", 0},
  25. }
  26. for _, tt := range intTests {
  27. i, _ := ParseInt([]byte(tt.i))
  28. test.That(t, i == tt.expected, "return", tt.expected, "for", tt.i)
  29. }
  30. }
  31. func TestLenInt(t *testing.T) {
  32. lenIntTests := []struct {
  33. number int64
  34. expected int
  35. }{
  36. {0, 1},
  37. {1, 1},
  38. {10, 2},
  39. {99, 2},
  40. // coverage
  41. {100, 3},
  42. {1000, 4},
  43. {10000, 5},
  44. {100000, 6},
  45. {1000000, 7},
  46. {10000000, 8},
  47. {100000000, 9},
  48. {1000000000, 10},
  49. {10000000000, 11},
  50. {100000000000, 12},
  51. {1000000000000, 13},
  52. {10000000000000, 14},
  53. {100000000000000, 15},
  54. {1000000000000000, 16},
  55. {10000000000000000, 17},
  56. {100000000000000000, 18},
  57. {1000000000000000000, 19},
  58. }
  59. for _, tt := range lenIntTests {
  60. test.That(t, LenInt(tt.number) == tt.expected, "return", tt.expected, "for", tt.number)
  61. }
  62. }
  63. ////////////////////////////////////////////////////////////////
  64. var num []int64
  65. func TestMain(t *testing.T) {
  66. for j := 0; j < 1000; j++ {
  67. num = append(num, rand.Int63n(1000))
  68. }
  69. }
  70. func BenchmarkLenIntLog(b *testing.B) {
  71. n := 0
  72. for i := 0; i < b.N; i++ {
  73. for j := 0; j < 1000; j++ {
  74. n += int(math.Log10(math.Abs(float64(num[j])))) + 1
  75. }
  76. }
  77. }
  78. func BenchmarkLenIntSwitch(b *testing.B) {
  79. n := 0
  80. for i := 0; i < b.N; i++ {
  81. for j := 0; j < 1000; j++ {
  82. n += LenInt(num[j])
  83. }
  84. }
  85. }