lexer_int_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package jsoniter
  2. import (
  3. "testing"
  4. "bytes"
  5. "encoding/json"
  6. )
  7. func Test_uint64_0(t *testing.T) {
  8. lexer := NewLexer(bytes.NewBufferString("0"), 4096)
  9. val, err := lexer.LexUin64()
  10. if err != nil {
  11. t.Fatal(err)
  12. }
  13. if val != 0 {
  14. t.Fatal(val)
  15. }
  16. }
  17. func Test_uint64_1(t *testing.T) {
  18. lexer := NewLexer(bytes.NewBufferString("1"), 4096)
  19. val, err := lexer.LexUin64()
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. if val != 1 {
  24. t.Fatal(val)
  25. }
  26. }
  27. func Test_uint64_100(t *testing.T) {
  28. lexer := NewLexer(bytes.NewBufferString("100"), 4096)
  29. val, err := lexer.LexUin64()
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. if val != 100 {
  34. t.Fatal(val)
  35. }
  36. }
  37. func Test_uint64_100_comma(t *testing.T) {
  38. lexer := NewLexer(bytes.NewBufferString("100,"), 4096)
  39. val, err := lexer.LexUin64()
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. if val != 100 {
  44. t.Fatal(val)
  45. }
  46. }
  47. func Test_uint64_invalid(t *testing.T) {
  48. lexer := NewLexer(bytes.NewBufferString(","), 4096)
  49. _, err := lexer.LexUin64()
  50. if err == nil {
  51. t.FailNow()
  52. }
  53. }
  54. func Test_int64_100(t *testing.T) {
  55. lexer := NewLexer(bytes.NewBufferString("100"), 4096)
  56. val, err := lexer.LexInt64()
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. if val != 100 {
  61. t.Fatal(val)
  62. }
  63. }
  64. func Test_int64_minus_100(t *testing.T) {
  65. lexer := NewLexer(bytes.NewBufferString("-100"), 4096)
  66. val, err := lexer.LexInt64()
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. if val != -100 {
  71. t.Fatal(val)
  72. }
  73. }
  74. func Benchmark_jsoniter_int(b *testing.B) {
  75. for n := 0; n < b.N; n++ {
  76. lexer := NewLexerWithArray([]byte(`-100`))
  77. lexer.LexInt64()
  78. }
  79. }
  80. func Benchmark_json_int(b *testing.B) {
  81. for n := 0; n < b.N; n++ {
  82. result := int64(0)
  83. json.Unmarshal([]byte(`-100`), &result)
  84. }
  85. }