| 1 | // Copyright 2021 Bret Jordan & Benedikt Thoma, All rights reserved. |
| 2 | // Copyright 2006-2019 WebPKI.org (http://webpki.org). |
| 3 | // |
| 4 | // Use of this source code is governed by an Apache 2.0 license that can be |
| 5 | // found in the LICENSE file in the root of the source tree. |
| 6 | |
| 7 | package jcsscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/ |
| 8 | |
| 9 | import ( |
| 10 | "bufioscip-go gomod github.com/golang/go/src go1.15 bufio/" |
| 11 | "mathscip-go gomod github.com/golang/go/src go1.15 math/" |
| 12 | "osscip-go gomod github.com/golang/go/src go1.15 os/" |
| 13 | "strconvscip-go gomod github.com/golang/go/src go1.15 strconv/" |
| 14 | "stringsscip-go gomod github.com/golang/go/src go1.15 strings/" |
| 15 | "testingscip-go gomod github.com/golang/go/src go1.15 testing/" |
| 16 | |
| 17 | "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/" |
| 18 | ) |
| 19 | |
| 20 | const testFilescip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/testFile. = "/home/test/es6testfile100m.txt" |
| 21 | |
| 22 | const invalidNumberscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/invalidNumber. = "null" |
| 23 | |
| 24 | type numberParsingTestCasescip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase# struct { |
| 25 | descscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#desc. string |
| 26 | ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex. string |
| 27 | expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected. string |
| 28 | } |
| 29 | |
| 30 | func TestNumberToJSONscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/TestNumberToJSON().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) { |
| 31 | r := requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.Newscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/New().(t) |
| 32 | |
| 33 | testCases := []numberParsingTestCasescip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#{ |
| 34 | { |
| 35 | ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex.: "4340000000000001", |
| 36 | expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected.: "9007199254740994", |
| 37 | }, |
| 38 | { |
| 39 | ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex.: "4340000000000002", |
| 40 | expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected.: "9007199254740996", |
| 41 | }, |
| 42 | { |
| 43 | ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex.: "444b1ae4d6e2ef50", |
| 44 | expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected.: "1e+21", |
| 45 | }, |
| 46 | { |
| 47 | ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex.: "3eb0c6f7a0b5ed8d", |
| 48 | expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected.: "0.000001", |
| 49 | }, |
| 50 | { |
| 51 | ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex.: "3eb0c6f7a0b5ed8c", |
| 52 | expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected.: "9.999999999999997e-7", |
| 53 | }, |
| 54 | { |
| 55 | ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex.: "8000000000000000", |
| 56 | expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected.: "0", |
| 57 | }, |
| 58 | { |
| 59 | ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex.: "7fffffffffffffff", |
| 60 | expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected.: invalidNumberscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/invalidNumber., |
| 61 | }, |
| 62 | { |
| 63 | ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex.: "7ff0000000000000", |
| 64 | expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected.: invalidNumberscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/invalidNumber., |
| 65 | }, |
| 66 | { |
| 67 | ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex.: "fff0000000000000", |
| 68 | expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected.: invalidNumberscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/invalidNumber., |
| 69 | }, |
| 70 | } |
| 71 | |
| 72 | if _, err := osscip-go gomod github.com/golang/go/src go1.15 os/.Statscip-go gomod github.com/golang/go/src go1.15 os/Stat().(testFilescip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/testFile.); err == nil { |
| 73 | file, err := osscip-go gomod github.com/golang/go/src go1.15 os/.Openscip-go gomod github.com/golang/go/src go1.15 os/Open().(testFilescip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/testFile.) |
| 74 | r.NoErrorfscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Assertions#NoErrorf().(err, "Failed at reading test sample file: %s\n", err) |
| 75 | |
| 76 | scanner := bufioscip-go gomod github.com/golang/go/src go1.15 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.15 bufio/NewScanner().(file) |
| 77 | var lineCount int |
| 78 | for scanner.Scanscip-go gomod github.com/golang/go/src go1.15 bufio/Scanner#Scan().() { |
| 79 | lineCount++ |
| 80 | line := scanner.Textscip-go gomod github.com/golang/go/src go1.15 bufio/Scanner#Text().() |
| 81 | lineSl := stringsscip-go gomod github.com/golang/go/src go1.15 strings/.Splitscip-go gomod github.com/golang/go/src go1.15 strings/Split().(line, ",") |
| 82 | r.Equalfscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Assertions#Equalf().(len(lineSl), 2, "Failed because a comma is missing in line %v\n", lineCount) |
| 83 | |
| 84 | hex := lineSl[0] |
| 85 | expected := lineSl[1] |
| 86 | t.Runscip-go gomod github.com/golang/go/src go1.15 testing/T#Run().(expected, func(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) { |
| 87 | testNumberParsingscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/testNumberParsing().(t, r, numberParsingTestCasescip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#{ |
| 88 | descscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#desc.: expected, |
| 89 | ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex.: hex, |
| 90 | expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected.: expected, |
| 91 | }) |
| 92 | }) |
| 93 | } |
| 94 | r.NoErrorfscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Assertions#NoErrorf().(err, "Failed at scanning test file: %s\n", err) |
| 95 | file.Closescip-go gomod github.com/golang/go/src go1.15 os/File#Close().() |
| 96 | } else if osscip-go gomod github.com/golang/go/src go1.15 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.15 os/IsNotExist().(err) { |
| 97 | } else { |
| 98 | r.NoErrorfscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Assertions#NoErrorf().(err, "Failed at testing if numbers test file exists: %s\n", err) |
| 99 | } |
| 100 | |
| 101 | for _, tC := range testCases { |
| 102 | t.Runscip-go gomod github.com/golang/go/src go1.15 testing/T#Run().(tC.descscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#desc., func(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#) { |
| 103 | testNumberParsingscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/testNumberParsing().(t, r, tC) |
| 104 | }) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func testNumberParsingscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/testNumberParsing().(t *testingscip-go gomod github.com/golang/go/src go1.15 testing/.Tscip-go gomod github.com/golang/go/src go1.15 testing/T#, r *requirescip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/.Assertionsscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Assertions#, tC numberParsingTestCasescip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#) { |
| 109 | for len(tC.ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex.) < 16 { |
| 110 | tC.ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex. = "0" + tC.ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex. |
| 111 | } |
| 112 | ieeeU64, err := strconvscip-go gomod github.com/golang/go/src go1.15 strconv/.ParseUintscip-go gomod github.com/golang/go/src go1.15 strconv/ParseUint().(tC.ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex., 16, 64) |
| 113 | r.NoErrorfscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Assertions#NoErrorf().(err, "Failed at parsing tC.ieeeHex [%s]: %s\n", tC.ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex., err) |
| 114 | |
| 115 | ieeeF64 := mathscip-go gomod github.com/golang/go/src go1.15 math/.Float64frombitsscip-go gomod github.com/golang/go/src go1.15 math/Float64frombits().(ieeeU64) |
| 116 | es6Created, err := NumberToJSONscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/NumberToJSON().(ieeeF64) |
| 117 | if tC.expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected. == invalidNumberscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/invalidNumber. { |
| 118 | r.Errorfscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Assertions#Errorf().(err, "Failed because parsing number to json did not return an error even though the expected JSON is null\n") |
| 119 | } else { |
| 120 | r.NoErrorfscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Assertions#NoErrorf().(err, "Failed at converting float to json: %s\n", err) |
| 121 | } |
| 122 | |
| 123 | r.Equalfscip-go gomod github.com/stretchr/testify v1.7.0 `github.com/stretchr/testify/require`/Assertions#Equalf().(tC.expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected., es6Created, "Failed because converted number [%s] (tC.ieeeHex [%s]) does not match tC.expected [%s]\n", es6Created, tC.ieeeHexscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#ieeeHex., tC.expectedscip-go gomod github.com/gowebpki/jcs . `github.com/gowebpki/jcs`/numberParsingTestCase#expected.) |
| 124 | } |