| 1 | package jsonparserscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ |
| 2 | |
| 3 | import ( |
| 4 | "strconvscip-go gomod github.com/golang/go/src go1.13 strconv/" |
| 5 | "testingscip-go gomod github.com/golang/go/src go1.13 testing/" |
| 6 | "unsafescip-go gomod github.com/golang/go/src go1.13 unsafe/" |
| 7 | ) |
| 8 | |
| 9 | type ParseIntTestscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest# struct { |
| 10 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in. string |
| 11 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out. int64 |
| 12 | isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr. bool |
| 13 | isOverflowscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isOverflow. bool |
| 14 | } |
| 15 | |
| 16 | var parseIntTestsscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/parseIntTests. = []ParseIntTestscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#{ |
| 17 | { |
| 18 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "0", |
| 19 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: 0, |
| 20 | }, |
| 21 | { |
| 22 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "1", |
| 23 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: 1, |
| 24 | }, |
| 25 | { |
| 26 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "-1", |
| 27 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: -1, |
| 28 | }, |
| 29 | { |
| 30 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "12345", |
| 31 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: 12345, |
| 32 | }, |
| 33 | { |
| 34 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "-12345", |
| 35 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: -12345, |
| 36 | }, |
| 37 | { |
| 38 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "9223372036854775807", // = math.MaxInt64 |
| 39 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: 9223372036854775807, |
| 40 | }, |
| 41 | { |
| 42 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "-9223372036854775808", // = math.MinInt64 |
| 43 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: -9223372036854775808, |
| 44 | }, |
| 45 | { |
| 46 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "-92233720368547758081", |
| 47 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: 0, |
| 48 | isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr.: true, |
| 49 | isOverflowscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isOverflow.: true, |
| 50 | }, |
| 51 | { |
| 52 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "18446744073709551616", // = 2^64 |
| 53 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: 0, |
| 54 | isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr.: true, |
| 55 | isOverflowscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isOverflow.: true, |
| 56 | }, |
| 57 | { |
| 58 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "9223372036854775808", // = math.MaxInt64 - 1 |
| 59 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: 0, |
| 60 | isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr.: true, |
| 61 | isOverflowscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isOverflow.: true, |
| 62 | }, |
| 63 | { |
| 64 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "-9223372036854775809", // = math.MaxInt64 - 1 |
| 65 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: 0, |
| 66 | isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr.: true, |
| 67 | isOverflowscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isOverflow.: true, |
| 68 | }, |
| 69 | { |
| 70 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "", |
| 71 | isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr.: true, |
| 72 | }, |
| 73 | { |
| 74 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "abc", |
| 75 | isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr.: true, |
| 76 | }, |
| 77 | { |
| 78 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "12345x", |
| 79 | isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr.: true, |
| 80 | }, |
| 81 | { |
| 82 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "123e5", |
| 83 | isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr.: true, |
| 84 | }, |
| 85 | { |
| 86 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "9223372036854775807x", |
| 87 | isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr.: true, |
| 88 | }, |
| 89 | { |
| 90 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "27670116110564327410", |
| 91 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: 0, |
| 92 | isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr.: true, |
| 93 | isOverflowscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isOverflow.: true, |
| 94 | }, |
| 95 | { |
| 96 | inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.: "-27670116110564327410", |
| 97 | outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.: 0, |
| 98 | isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr.: true, |
| 99 | isOverflowscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isOverflow.: true, |
| 100 | }, |
| 101 | } |
| 102 | |
| 103 | // Verifies: SYS-REQ-015 [boundary] |
| 104 | // MCDC SYS-REQ-015: N/A |
| 105 | func TestBytesParseIntscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/TestBytesParseInt().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 106 | for _, test := range parseIntTestsscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/parseIntTests. { |
| 107 | out, ok, overflow := parseIntscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/parseInt().([]byte(test.inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in.)) |
| 108 | if overflow != test.isOverflowscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isOverflow. { |
| 109 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("Test '%s' error return did not overflow expectation (obtained %t, expected %t)", test.inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in., overflow, test.isOverflowscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isOverflow.) |
| 110 | } |
| 111 | if ok != !test.isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr. { |
| 112 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("Test '%s' error return did not match expectation (obtained %t, expected %t)", test.inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in., !ok, test.isErrscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#isErr.) |
| 113 | } else if ok && out != test.outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out. { |
| 114 | t.Errorfscip-go gomod github.com/golang/go/src go1.13 testing/common#Errorf().("Test '%s' did not return the expected value (obtained %d, expected %d)", test.inscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#in., out, test.outscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/ParseIntTest#out.) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Verifies: SYS-REQ-015 [example] |
| 120 | // MCDC SYS-REQ-015: N/A |
| 121 | func BenchmarkParseIntscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/BenchmarkParseInt().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 122 | bytes := []byte("123") |
| 123 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 124 | parseIntscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/parseInt().(bytes) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Alternative implementation using unsafe and delegating to strconv.ParseInt |
| 129 | // Verifies: SYS-REQ-015 [example] |
| 130 | // MCDC SYS-REQ-015: N/A |
| 131 | func BenchmarkParseIntUnsafeSlowerscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/BenchmarkParseIntUnsafeSlower().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 132 | bytes := []byte("123") |
| 133 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 134 | strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.13 strconv/ParseInt().(*(*string)(unsafescip-go gomod github.com/golang/go/src go1.13 unsafe/.Pointer(&bytes)), 10, 64) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Old implementation that did not check for overflows. |
| 139 | // Verifies: SYS-REQ-015 [example] |
| 140 | // MCDC SYS-REQ-015: N/A |
| 141 | func BenchmarkParseIntOverflowsscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/BenchmarkParseIntOverflows().(b *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Bscip-go gomod github.com/golang/go/src go1.13 testing/B#) { |
| 142 | bytes := []byte("123") |
| 143 | for i := 0; i < b.Nscip-go gomod github.com/golang/go/src go1.13 testing/B#N.; i++ { |
| 144 | parseIntOverflowsscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/parseIntOverflows().(bytes) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Test helper for SYS-REQ-015. |
| 149 | func parseIntOverflowsscip-go gomod github.com/buger/jsonparser . `github.com/buger/jsonparser`/parseIntOverflows().(bytes []byte) (v int64, ok bool) { |
| 150 | if len(bytes) == 0 { |
| 151 | return 0, false |
| 152 | } |
| 153 | |
| 154 | var neg bool = false |
| 155 | if bytes[0] == '-' { |
| 156 | neg = true |
| 157 | bytes = bytes[1:] |
| 158 | } |
| 159 | |
| 160 | for _, c := range bytes { |
| 161 | if c >= '0' && c <= '9' { |
| 162 | v = (10 * v) + int64(c-'0') |
| 163 | } else { |
| 164 | return 0, false |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if neg { |
| 169 | return -v, true |
| 170 | } else { |
| 171 | return v, true |
| 172 | } |
| 173 | } |