Debian Codegraph @

debian/vendor/test/numbers/verify.go

raw
1 //
2 // Copyright 2006-2019 WebPKI.org (http://webpki.org).
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 // This program tests the JSON number serializer using both a few discrete
18 // values as well as the 100 million value test suite
19
20 package main
21
22 import (
23 "bufio"
24 "strings"
25 "strconv"
26 "math"
27 "fmt"
28 "os"
29 "webpki.org/jsoncanonicalizer"
30 )
31
32 func check(e error) {
33 if e != nil {
34 panic(e)
35 }
36 }
37
38 // Change the file name to suit your environment
39 const testFile = "c:\\es6\\numbers\\es6testfile100m.txt"
40
41 const invalidNumber = "null"
42
43 var conversionErrors int = 0
44
45 func verify(ieeeHex string, expected string) {
46 for len(ieeeHex) < 16 {
47 ieeeHex = "0" + ieeeHex
48 }
49 ieeeU64, err := strconv.ParseUint(ieeeHex, 16, 64)
50 check(err)
51 ieeeF64 := math.Float64frombits(ieeeU64)
52 es6Created, err := jsoncanonicalizer.NumberToJSON(ieeeF64)
53 if expected == invalidNumber {
54 if err == nil {
55 panic("Missing error")
56 }
57 return
58 } else {
59 check(err);
60 }
61 if es6Created != expected {
62 conversionErrors++
63 fmt.Println("\n" + ieeeHex)
64 fmt.Println(es6Created)
65 fmt.Println(expected)
66 }
67 esParsed, err := strconv.ParseFloat(expected, 64)
68 check(err)
69 if esParsed != ieeeF64 {
70 panic("Parsing error ieeeHex: " + ieeeHex + " expected: " + expected)
71 }
72 }
73
74 func main() {
75 verify("4340000000000001", "9007199254740994")
76 verify("4340000000000002", "9007199254740996")
77 verify("444b1ae4d6e2ef50", "1e+21")
78 verify("3eb0c6f7a0b5ed8d", "0.000001")
79 verify("3eb0c6f7a0b5ed8c", "9.999999999999997e-7")
80 verify("8000000000000000", "0")
81 verify("7fffffffffffffff", invalidNumber)
82 verify("7ff0000000000000", invalidNumber)
83 verify("fff0000000000000", invalidNumber)
84
85 file, err := os.Open(testFile)
86 check(err)
87 defer file.Close()
88 scanner := bufio.NewScanner(file)
89 var lineCount int = 0
90 for scanner.Scan() {
91 lineCount++
92 if lineCount % 1000000 == 0 {
93 fmt.Printf("line: %d\n", lineCount)
94 }
95 line := scanner.Text()
96 comma := strings.IndexByte(line, ',')
97 if comma <= 0 {
98 panic("Missing comma!")
99 }
100 verify(line[:comma], line[comma + 1:])
101 }
102 check(scanner.Err())
103 if conversionErrors == 0 {
104 fmt.Printf("\nSuccessful Operation. Lines read: %d\n", lineCount)
105 } else {
106 fmt.Printf("\n****** ERRORS: %d *******\n", conversionErrors)
107 }
108 }