PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/libgo/go/strconv/atof_test.go

https://gitlab.com/lobl.pavel/gcc-6.2.0
Go | 430 lines | 329 code | 46 blank | 55 comment | 64 complexity | 4d184ca03b093fb2b5aa63cf579a2cba MD5 | raw file
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package strconv_test
  5. import (
  6. "math"
  7. "math/rand"
  8. "reflect"
  9. . "strconv"
  10. "strings"
  11. "testing"
  12. "time"
  13. )
  14. type atofTest struct {
  15. in string
  16. out string
  17. err error
  18. }
  19. var atoftests = []atofTest{
  20. {"", "0", ErrSyntax},
  21. {"1", "1", nil},
  22. {"+1", "1", nil},
  23. {"1x", "0", ErrSyntax},
  24. {"1.1.", "0", ErrSyntax},
  25. {"1e23", "1e+23", nil},
  26. {"1E23", "1e+23", nil},
  27. {"100000000000000000000000", "1e+23", nil},
  28. {"1e-100", "1e-100", nil},
  29. {"123456700", "1.234567e+08", nil},
  30. {"99999999999999974834176", "9.999999999999997e+22", nil},
  31. {"100000000000000000000001", "1.0000000000000001e+23", nil},
  32. {"100000000000000008388608", "1.0000000000000001e+23", nil},
  33. {"100000000000000016777215", "1.0000000000000001e+23", nil},
  34. {"100000000000000016777216", "1.0000000000000003e+23", nil},
  35. {"-1", "-1", nil},
  36. {"-0.1", "-0.1", nil},
  37. {"-0", "-0", nil},
  38. {"1e-20", "1e-20", nil},
  39. {"625e-3", "0.625", nil},
  40. // NaNs
  41. {"nan", "NaN", nil},
  42. {"NaN", "NaN", nil},
  43. {"NAN", "NaN", nil},
  44. // Infs
  45. {"inf", "+Inf", nil},
  46. {"-Inf", "-Inf", nil},
  47. {"+INF", "+Inf", nil},
  48. {"-Infinity", "-Inf", nil},
  49. {"+INFINITY", "+Inf", nil},
  50. {"Infinity", "+Inf", nil},
  51. // largest float64
  52. {"1.7976931348623157e308", "1.7976931348623157e+308", nil},
  53. {"-1.7976931348623157e308", "-1.7976931348623157e+308", nil},
  54. // next float64 - too large
  55. {"1.7976931348623159e308", "+Inf", ErrRange},
  56. {"-1.7976931348623159e308", "-Inf", ErrRange},
  57. // the border is ...158079
  58. // borderline - okay
  59. {"1.7976931348623158e308", "1.7976931348623157e+308", nil},
  60. {"-1.7976931348623158e308", "-1.7976931348623157e+308", nil},
  61. // borderline - too large
  62. {"1.797693134862315808e308", "+Inf", ErrRange},
  63. {"-1.797693134862315808e308", "-Inf", ErrRange},
  64. // a little too large
  65. {"1e308", "1e+308", nil},
  66. {"2e308", "+Inf", ErrRange},
  67. {"1e309", "+Inf", ErrRange},
  68. // way too large
  69. {"1e310", "+Inf", ErrRange},
  70. {"-1e310", "-Inf", ErrRange},
  71. {"1e400", "+Inf", ErrRange},
  72. {"-1e400", "-Inf", ErrRange},
  73. {"1e400000", "+Inf", ErrRange},
  74. {"-1e400000", "-Inf", ErrRange},
  75. // denormalized
  76. {"1e-305", "1e-305", nil},
  77. {"1e-306", "1e-306", nil},
  78. {"1e-307", "1e-307", nil},
  79. {"1e-308", "1e-308", nil},
  80. {"1e-309", "1e-309", nil},
  81. {"1e-310", "1e-310", nil},
  82. {"1e-322", "1e-322", nil},
  83. // smallest denormal
  84. {"5e-324", "5e-324", nil},
  85. {"4e-324", "5e-324", nil},
  86. {"3e-324", "5e-324", nil},
  87. // too small
  88. {"2e-324", "0", nil},
  89. // way too small
  90. {"1e-350", "0", nil},
  91. {"1e-400000", "0", nil},
  92. // try to overflow exponent
  93. {"1e-4294967296", "0", nil},
  94. {"1e+4294967296", "+Inf", ErrRange},
  95. {"1e-18446744073709551616", "0", nil},
  96. {"1e+18446744073709551616", "+Inf", ErrRange},
  97. // Parse errors
  98. {"1e", "0", ErrSyntax},
  99. {"1e-", "0", ErrSyntax},
  100. {".e-1", "0", ErrSyntax},
  101. {"1\x00.2", "0", ErrSyntax},
  102. // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
  103. {"2.2250738585072012e-308", "2.2250738585072014e-308", nil},
  104. // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
  105. {"2.2250738585072011e-308", "2.225073858507201e-308", nil},
  106. // A very large number (initially wrongly parsed by the fast algorithm).
  107. {"4.630813248087435e+307", "4.630813248087435e+307", nil},
  108. // A different kind of very large number.
  109. {"22.222222222222222", "22.22222222222222", nil},
  110. {"2." + strings.Repeat("2", 4000) + "e+1", "22.22222222222222", nil},
  111. // Exactly halfway between 1 and math.Nextafter(1, 2).
  112. // Round to even (down).
  113. {"1.00000000000000011102230246251565404236316680908203125", "1", nil},
  114. // Slightly lower; still round down.
  115. {"1.00000000000000011102230246251565404236316680908203124", "1", nil},
  116. // Slightly higher; round up.
  117. {"1.00000000000000011102230246251565404236316680908203126", "1.0000000000000002", nil},
  118. // Slightly higher, but you have to read all the way to the end.
  119. {"1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1", "1.0000000000000002", nil},
  120. }
  121. var atof32tests = []atofTest{
  122. // Exactly halfway between 1 and the next float32.
  123. // Round to even (down).
  124. {"1.000000059604644775390625", "1", nil},
  125. // Slightly lower.
  126. {"1.000000059604644775390624", "1", nil},
  127. // Slightly higher.
  128. {"1.000000059604644775390626", "1.0000001", nil},
  129. // Slightly higher, but you have to read all the way to the end.
  130. {"1.000000059604644775390625" + strings.Repeat("0", 10000) + "1", "1.0000001", nil},
  131. // largest float32: (1<<128) * (1 - 2^-24)
  132. {"340282346638528859811704183484516925440", "3.4028235e+38", nil},
  133. {"-340282346638528859811704183484516925440", "-3.4028235e+38", nil},
  134. // next float32 - too large
  135. {"3.4028236e38", "+Inf", ErrRange},
  136. {"-3.4028236e38", "-Inf", ErrRange},
  137. // the border is 3.40282356779...e+38
  138. // borderline - okay
  139. {"3.402823567e38", "3.4028235e+38", nil},
  140. {"-3.402823567e38", "-3.4028235e+38", nil},
  141. // borderline - too large
  142. {"3.4028235678e38", "+Inf", ErrRange},
  143. {"-3.4028235678e38", "-Inf", ErrRange},
  144. // Denormals: less than 2^-126
  145. {"1e-38", "1e-38", nil},
  146. {"1e-39", "1e-39", nil},
  147. {"1e-40", "1e-40", nil},
  148. {"1e-41", "1e-41", nil},
  149. {"1e-42", "1e-42", nil},
  150. {"1e-43", "1e-43", nil},
  151. {"1e-44", "1e-44", nil},
  152. {"6e-45", "6e-45", nil}, // 4p-149 = 5.6e-45
  153. {"5e-45", "6e-45", nil},
  154. // Smallest denormal
  155. {"1e-45", "1e-45", nil}, // 1p-149 = 1.4e-45
  156. {"2e-45", "1e-45", nil},
  157. // 2^92 = 8388608p+69 = 4951760157141521099596496896 (4.9517602e27)
  158. // is an exact power of two that needs 8 decimal digits to be correctly
  159. // parsed back.
  160. // The float32 before is 16777215p+68 = 4.95175986e+27
  161. // The halfway is 4.951760009. A bad algorithm that thinks the previous
  162. // float32 is 8388607p+69 will shorten incorrectly to 4.95176e+27.
  163. {"4951760157141521099596496896", "4.9517602e+27", nil},
  164. }
  165. type atofSimpleTest struct {
  166. x float64
  167. s string
  168. }
  169. var (
  170. atofRandomTests []atofSimpleTest
  171. benchmarksRandomBits [1024]string
  172. benchmarksRandomNormal [1024]string
  173. )
  174. func init() {
  175. // The atof routines return NumErrors wrapping
  176. // the error and the string. Convert the table above.
  177. for i := range atoftests {
  178. test := &atoftests[i]
  179. if test.err != nil {
  180. test.err = &NumError{"ParseFloat", test.in, test.err}
  181. }
  182. }
  183. for i := range atof32tests {
  184. test := &atof32tests[i]
  185. if test.err != nil {
  186. test.err = &NumError{"ParseFloat", test.in, test.err}
  187. }
  188. }
  189. // Generate random inputs for tests and benchmarks
  190. rand.Seed(time.Now().UnixNano())
  191. if testing.Short() {
  192. atofRandomTests = make([]atofSimpleTest, 100)
  193. } else {
  194. atofRandomTests = make([]atofSimpleTest, 10000)
  195. }
  196. for i := range atofRandomTests {
  197. n := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
  198. x := math.Float64frombits(n)
  199. s := FormatFloat(x, 'g', -1, 64)
  200. atofRandomTests[i] = atofSimpleTest{x, s}
  201. }
  202. for i := range benchmarksRandomBits {
  203. bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
  204. x := math.Float64frombits(bits)
  205. benchmarksRandomBits[i] = FormatFloat(x, 'g', -1, 64)
  206. }
  207. for i := range benchmarksRandomNormal {
  208. x := rand.NormFloat64()
  209. benchmarksRandomNormal[i] = FormatFloat(x, 'g', -1, 64)
  210. }
  211. }
  212. func testAtof(t *testing.T, opt bool) {
  213. oldopt := SetOptimize(opt)
  214. for i := 0; i < len(atoftests); i++ {
  215. test := &atoftests[i]
  216. out, err := ParseFloat(test.in, 64)
  217. outs := FormatFloat(out, 'g', -1, 64)
  218. if outs != test.out || !reflect.DeepEqual(err, test.err) {
  219. t.Errorf("ParseFloat(%v, 64) = %v, %v want %v, %v",
  220. test.in, out, err, test.out, test.err)
  221. }
  222. if float64(float32(out)) == out {
  223. out, err := ParseFloat(test.in, 32)
  224. out32 := float32(out)
  225. if float64(out32) != out {
  226. t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32))
  227. continue
  228. }
  229. outs := FormatFloat(float64(out32), 'g', -1, 32)
  230. if outs != test.out || !reflect.DeepEqual(err, test.err) {
  231. t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v",
  232. test.in, out32, err, test.out, test.err, out)
  233. }
  234. }
  235. }
  236. for _, test := range atof32tests {
  237. out, err := ParseFloat(test.in, 32)
  238. out32 := float32(out)
  239. if float64(out32) != out {
  240. t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32))
  241. continue
  242. }
  243. outs := FormatFloat(float64(out32), 'g', -1, 32)
  244. if outs != test.out || !reflect.DeepEqual(err, test.err) {
  245. t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v",
  246. test.in, out32, err, test.out, test.err, out)
  247. }
  248. }
  249. SetOptimize(oldopt)
  250. }
  251. func TestAtof(t *testing.T) { testAtof(t, true) }
  252. func TestAtofSlow(t *testing.T) { testAtof(t, false) }
  253. func TestAtofRandom(t *testing.T) {
  254. for _, test := range atofRandomTests {
  255. x, _ := ParseFloat(test.s, 64)
  256. switch {
  257. default:
  258. t.Errorf("number %s badly parsed as %b (expected %b)", test.s, x, test.x)
  259. case x == test.x:
  260. case math.IsNaN(test.x) && math.IsNaN(x):
  261. }
  262. }
  263. t.Logf("tested %d random numbers", len(atofRandomTests))
  264. }
  265. var roundTripCases = []struct {
  266. f float64
  267. s string
  268. }{
  269. // Issue 2917.
  270. // This test will break the optimized conversion if the
  271. // FPU is using 80-bit registers instead of 64-bit registers,
  272. // usually because the operating system initialized the
  273. // thread with 80-bit precision and the Go runtime didn't
  274. // fix the FP control word.
  275. {8865794286000691 << 39, "4.87402195346389e+27"},
  276. {8865794286000692 << 39, "4.8740219534638903e+27"},
  277. }
  278. func TestRoundTrip(t *testing.T) {
  279. for _, tt := range roundTripCases {
  280. old := SetOptimize(false)
  281. s := FormatFloat(tt.f, 'g', -1, 64)
  282. if s != tt.s {
  283. t.Errorf("no-opt FormatFloat(%b) = %s, want %s", tt.f, s, tt.s)
  284. }
  285. f, err := ParseFloat(tt.s, 64)
  286. if f != tt.f || err != nil {
  287. t.Errorf("no-opt ParseFloat(%s) = %b, %v want %b, nil", tt.s, f, err, tt.f)
  288. }
  289. SetOptimize(true)
  290. s = FormatFloat(tt.f, 'g', -1, 64)
  291. if s != tt.s {
  292. t.Errorf("opt FormatFloat(%b) = %s, want %s", tt.f, s, tt.s)
  293. }
  294. f, err = ParseFloat(tt.s, 64)
  295. if f != tt.f || err != nil {
  296. t.Errorf("opt ParseFloat(%s) = %b, %v want %b, nil", tt.s, f, err, tt.f)
  297. }
  298. SetOptimize(old)
  299. }
  300. }
  301. // TestRoundTrip32 tries a fraction of all finite positive float32 values.
  302. func TestRoundTrip32(t *testing.T) {
  303. step := uint32(997)
  304. if testing.Short() {
  305. step = 99991
  306. }
  307. count := 0
  308. for i := uint32(0); i < 0xff<<23; i += step {
  309. f := math.Float32frombits(i)
  310. if i&1 == 1 {
  311. f = -f // negative
  312. }
  313. s := FormatFloat(float64(f), 'g', -1, 32)
  314. parsed, err := ParseFloat(s, 32)
  315. parsed32 := float32(parsed)
  316. switch {
  317. case err != nil:
  318. t.Errorf("ParseFloat(%q, 32) gave error %s", s, err)
  319. case float64(parsed32) != parsed:
  320. t.Errorf("ParseFloat(%q, 32) = %v, not a float32 (nearest is %v)", s, parsed, parsed32)
  321. case parsed32 != f:
  322. t.Errorf("ParseFloat(%q, 32) = %b (expected %b)", s, parsed32, f)
  323. }
  324. count++
  325. }
  326. t.Logf("tested %d float32's", count)
  327. }
  328. func BenchmarkAtof64Decimal(b *testing.B) {
  329. for i := 0; i < b.N; i++ {
  330. ParseFloat("33909", 64)
  331. }
  332. }
  333. func BenchmarkAtof64Float(b *testing.B) {
  334. for i := 0; i < b.N; i++ {
  335. ParseFloat("339.7784", 64)
  336. }
  337. }
  338. func BenchmarkAtof64FloatExp(b *testing.B) {
  339. for i := 0; i < b.N; i++ {
  340. ParseFloat("-5.09e75", 64)
  341. }
  342. }
  343. func BenchmarkAtof64Big(b *testing.B) {
  344. for i := 0; i < b.N; i++ {
  345. ParseFloat("123456789123456789123456789", 64)
  346. }
  347. }
  348. func BenchmarkAtof64RandomBits(b *testing.B) {
  349. for i := 0; i < b.N; i++ {
  350. ParseFloat(benchmarksRandomBits[i%1024], 64)
  351. }
  352. }
  353. func BenchmarkAtof64RandomFloats(b *testing.B) {
  354. for i := 0; i < b.N; i++ {
  355. ParseFloat(benchmarksRandomNormal[i%1024], 64)
  356. }
  357. }
  358. func BenchmarkAtof32Decimal(b *testing.B) {
  359. for i := 0; i < b.N; i++ {
  360. ParseFloat("33909", 32)
  361. }
  362. }
  363. func BenchmarkAtof32Float(b *testing.B) {
  364. for i := 0; i < b.N; i++ {
  365. ParseFloat("339.778", 32)
  366. }
  367. }
  368. func BenchmarkAtof32FloatExp(b *testing.B) {
  369. for i := 0; i < b.N; i++ {
  370. ParseFloat("12.3456e32", 32)
  371. }
  372. }
  373. var float32strings [4096]string
  374. func BenchmarkAtof32Random(b *testing.B) {
  375. n := uint32(997)
  376. for i := range float32strings {
  377. n = (99991*n + 42) % (0xff << 23)
  378. float32strings[i] = FormatFloat(float64(math.Float32frombits(n)), 'g', -1, 32)
  379. }
  380. b.ResetTimer()
  381. for i := 0; i < b.N; i++ {
  382. ParseFloat(float32strings[i%4096], 32)
  383. }
  384. }