jsoniter_any_float_test.go 1021 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package jsoniter
  2. import (
  3. "testing"
  4. "github.com/json-iterator/go/require"
  5. )
  6. var floatConvertMap = map[string]float64{
  7. "null": 0,
  8. "true": 1,
  9. "false": 0,
  10. `"true"`: 0,
  11. `"false"`: 0,
  12. "123": 123,
  13. `"123true"`: 0,
  14. `"-123true"`: 0,
  15. "0": 0,
  16. `"0"`: 0,
  17. "-1": -1,
  18. "1.1": 1.1,
  19. "0.0": 0,
  20. "-1.1": -1.1,
  21. `"+1.1"`: 1.1,
  22. `""`: 0,
  23. "[1,2]": 1,
  24. "[]": 0,
  25. "{}": 0,
  26. `{"abc":1}`: 0,
  27. }
  28. func Test_read_any_to_float(t *testing.T) {
  29. should := require.New(t)
  30. for k, v := range floatConvertMap {
  31. any := Get([]byte(k))
  32. should.Equal(float64(v), any.ToFloat64(), "the original val is "+k)
  33. }
  34. for k, v := range floatConvertMap {
  35. any := Get([]byte(k))
  36. should.Equal(float32(v), any.ToFloat32(), "the original val is "+k)
  37. }
  38. }
  39. func Test_read_float_as_any(t *testing.T) {
  40. should := require.New(t)
  41. any := Get([]byte("12.3"))
  42. should.Equal(float64(12.3), any.ToFloat64())
  43. should.Equal("12.3", any.ToString())
  44. should.True(any.ToBool())
  45. }