github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/internal/native/avx/native_test.go (about) 1 // +build amd64 2 3 4 // Code generated by Makefile, DO NOT EDIT. 5 6 // Code generated by Makefile, DO NOT EDIT. 7 8 /* 9 * Copyright 2021 ByteDance Inc. 10 * 11 * Licensed under the Apache License, Version 2.0 (the "License"); 12 * you may not use this file except in compliance with the License. 13 * You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, software 18 * distributed under the License is distributed on an "AS IS" BASIS, 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 * See the License for the specific language governing permissions and 21 * limitations under the License. 22 */ 23 24 package avx 25 26 import ( 27 `encoding/hex` 28 `fmt` 29 `math` 30 `strings` 31 `testing` 32 `unsafe` 33 34 `github.com/bytedance/sonic/internal/native/types` 35 `github.com/bytedance/sonic/internal/rt` 36 `github.com/davecgh/go-spew/spew` 37 `github.com/stretchr/testify/assert` 38 `github.com/stretchr/testify/require` 39 ) 40 41 func TestNative_Value(t *testing.T) { 42 var v types.JsonState 43 s := ` -12345` 44 p := (*rt.GoString)(unsafe.Pointer(&s)) 45 x := value(p.Ptr, p.Len, 0, &v, 0) 46 assert.Equal(t, 9, x) 47 assert.Equal(t, types.V_INTEGER, v.Vt) 48 assert.Equal(t, int64(-12345), v.Iv) 49 assert.Equal(t, 3, v.Ep) 50 } 51 52 func TestNative_Value_OutOfBound(t *testing.T) { 53 var v types.JsonState 54 mem := []byte{'"', '"'} 55 s := rt.Mem2Str(mem[:1]) 56 p := (*rt.GoString)(unsafe.Pointer(&s)) 57 x := value(p.Ptr, p.Len, 0, &v, 0) 58 assert.Equal(t, 1, x) 59 assert.Equal(t, -int(types.ERR_EOF), int(v.Vt)) 60 } 61 62 func TestNative_Quote(t *testing.T) { 63 s := "hello\b\f\n\r\t\\\"\u666fworld" 64 d := make([]byte, 256) 65 dp := (*rt.GoSlice)(unsafe.Pointer(&d)) 66 sp := (*rt.GoString)(unsafe.Pointer(&s)) 67 rv := quote(sp.Ptr, sp.Len, dp.Ptr, &dp.Len, 0) 68 if rv < 0 { 69 require.NoError(t, types.ParsingError(-rv)) 70 } 71 assert.Equal(t, len(s), rv) 72 assert.Equal(t, 35, len(d)) 73 assert.Equal(t, `hello\u0008\u000c\n\r\t\\\"景world`, string(d)) 74 } 75 76 func TestNative_QuoteNoMem(t *testing.T) { 77 s := "hello\b\f\n\r\t\\\"\u666fworld" 78 d := make([]byte, 10) 79 dp := (*rt.GoSlice)(unsafe.Pointer(&d)) 80 sp := (*rt.GoString)(unsafe.Pointer(&s)) 81 rv := quote(sp.Ptr, sp.Len, dp.Ptr, &dp.Len, 0) 82 assert.Equal(t, -6, rv) 83 assert.Equal(t, 5, len(d)) 84 assert.Equal(t, `hello`, string(d)) 85 } 86 87 func TestNative_DoubleQuote(t *testing.T) { 88 s := "hello\b\f\n\r\t\\\"\u666fworld" 89 d := make([]byte, 256) 90 dp := (*rt.GoSlice)(unsafe.Pointer(&d)) 91 sp := (*rt.GoString)(unsafe.Pointer(&s)) 92 rv := quote(sp.Ptr, sp.Len, dp.Ptr, &dp.Len, types.F_DOUBLE_UNQUOTE) 93 if rv < 0 { 94 require.NoError(t, types.ParsingError(-rv)) 95 } 96 assert.Equal(t, len(s), rv) 97 assert.Equal(t, 44, len(d)) 98 assert.Equal(t, `hello\\u0008\\u000c\\n\\r\\t\\\\\\\"景world`, string(d)) 99 } 100 101 func TestNative_Unquote(t *testing.T) { 102 s := `hello\b\f\n\r\t\\\"\u2333world` 103 d := make([]byte, 0, len(s)) 104 ep := -1 105 dp := (*rt.GoSlice)(unsafe.Pointer(&d)) 106 sp := (*rt.GoString)(unsafe.Pointer(&s)) 107 rv := unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, 0) 108 if rv < 0 { 109 require.NoError(t, types.ParsingError(-rv)) 110 } 111 dp.Len = rv 112 assert.Equal(t, -1, ep) 113 assert.Equal(t, "hello\b\f\n\r\t\\\"\u2333world", string(d)) 114 } 115 116 func TestNative_UnquoteError(t *testing.T) { 117 s := `asdf\` 118 d := make([]byte, 0, len(s)) 119 ep := -1 120 dp := (*rt.GoSlice)(unsafe.Pointer(&d)) 121 sp := (*rt.GoString)(unsafe.Pointer(&s)) 122 rv := unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, 0) 123 assert.Equal(t, -int(types.ERR_EOF), rv) 124 assert.Equal(t, 5, ep) 125 s = `asdf\gqwer` 126 d = make([]byte, 0, len(s)) 127 ep = -1 128 dp = (*rt.GoSlice)(unsafe.Pointer(&d)) 129 sp = (*rt.GoString)(unsafe.Pointer(&s)) 130 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, 0) 131 assert.Equal(t, -int(types.ERR_INVALID_ESCAPE), rv) 132 assert.Equal(t, 5, ep) 133 s = `asdf\u1gggqwer` 134 d = make([]byte, 0, len(s)) 135 ep = -1 136 dp = (*rt.GoSlice)(unsafe.Pointer(&d)) 137 sp = (*rt.GoString)(unsafe.Pointer(&s)) 138 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, 0) 139 assert.Equal(t, -int(types.ERR_INVALID_CHAR), rv) 140 assert.Equal(t, 7, ep) 141 s = `asdf\ud800qwer` 142 d = make([]byte, 0, len(s)) 143 ep = -1 144 dp = (*rt.GoSlice)(unsafe.Pointer(&d)) 145 sp = (*rt.GoString)(unsafe.Pointer(&s)) 146 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, 0) 147 assert.Equal(t, -int(types.ERR_INVALID_UNICODE), rv) 148 assert.Equal(t, 6, ep) 149 s = `asdf\\ud800qwer` 150 d = make([]byte, 0, len(s)) 151 ep = -1 152 dp = (*rt.GoSlice)(unsafe.Pointer(&d)) 153 sp = (*rt.GoString)(unsafe.Pointer(&s)) 154 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, types.F_DOUBLE_UNQUOTE) 155 assert.Equal(t, -int(types.ERR_INVALID_UNICODE), rv) 156 assert.Equal(t, 7, ep) 157 s = `asdf\ud800\ud800qwer` 158 d = make([]byte, 0, len(s)) 159 ep = -1 160 dp = (*rt.GoSlice)(unsafe.Pointer(&d)) 161 sp = (*rt.GoString)(unsafe.Pointer(&s)) 162 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, 0) 163 assert.Equal(t, -int(types.ERR_INVALID_UNICODE), rv) 164 assert.Equal(t, 12, ep) 165 s = `asdf\\ud800\\ud800qwer` 166 d = make([]byte, 0, len(s)) 167 ep = -1 168 dp = (*rt.GoSlice)(unsafe.Pointer(&d)) 169 sp = (*rt.GoString)(unsafe.Pointer(&s)) 170 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, types.F_DOUBLE_UNQUOTE) 171 assert.Equal(t, -int(types.ERR_INVALID_UNICODE), rv) 172 assert.Equal(t, 14, ep) 173 } 174 175 func TestNative_DoubleUnquote(t *testing.T) { 176 s := `hello\\b\\f\\n\\r\\t\\\\\\\"\\u2333world` 177 d := make([]byte, 0, len(s)) 178 ep := -1 179 dp := (*rt.GoSlice)(unsafe.Pointer(&d)) 180 sp := (*rt.GoString)(unsafe.Pointer(&s)) 181 rv := unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, types.F_DOUBLE_UNQUOTE) 182 if rv < 0 { 183 require.NoError(t, types.ParsingError(-rv)) 184 } 185 dp.Len = rv 186 assert.Equal(t, -1, ep) 187 assert.Equal(t, "hello\b\f\n\r\t\\\"\u2333world", string(d)) 188 } 189 190 func TestNative_UnquoteUnicodeReplacement(t *testing.T) { 191 s := `hello\ud800world` 192 d := make([]byte, 0, len(s)) 193 ep := -1 194 dp := (*rt.GoSlice)(unsafe.Pointer(&d)) 195 sp := (*rt.GoString)(unsafe.Pointer(&s)) 196 rv := unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, types.F_UNICODE_REPLACE) 197 if rv < 0 { 198 require.NoError(t, types.ParsingError(-rv)) 199 } 200 dp.Len = rv 201 assert.Equal(t, -1, ep) 202 assert.Equal(t, "hello\ufffdworld", string(d)) 203 s = `hello\ud800\ud800world` 204 d = make([]byte, 0, len(s)) 205 ep = -1 206 dp = (*rt.GoSlice)(unsafe.Pointer(&d)) 207 sp = (*rt.GoString)(unsafe.Pointer(&s)) 208 rv = unquote(sp.Ptr, sp.Len, dp.Ptr, &ep, types.F_UNICODE_REPLACE) 209 if rv < 0 { 210 require.NoError(t, types.ParsingError(-rv)) 211 } 212 dp.Len = rv 213 assert.Equal(t, -1, ep) 214 assert.Equal(t, "hello\ufffd\ufffdworld", string(d)) 215 } 216 217 func TestNative_HTMLEscape(t *testing.T) { 218 s := "hello\u2029\u2028<&>world" 219 d := make([]byte, 256) 220 dp := (*rt.GoSlice)(unsafe.Pointer(&d)) 221 sp := (*rt.GoString)(unsafe.Pointer(&s)) 222 rv := html_escape(sp.Ptr, sp.Len, dp.Ptr, &dp.Len) 223 if rv < 0 { 224 require.NoError(t, types.ParsingError(-rv)) 225 } 226 assert.Equal(t, len(s), rv) 227 assert.Equal(t, 40, len(d)) 228 assert.Equal(t, `hello\u2029\u2028\u003c\u0026\u003eworld`, string(d)) 229 } 230 231 func TestNative_HTMLEscapeNoMem(t *testing.T) { 232 s := "hello\u2029\u2028<&>world" 233 d := make([]byte, 10) 234 dp := (*rt.GoSlice)(unsafe.Pointer(&d)) 235 sp := (*rt.GoString)(unsafe.Pointer(&s)) 236 rv := html_escape(sp.Ptr, sp.Len, dp.Ptr, &dp.Len) 237 assert.Equal(t, -6, rv) 238 assert.Equal(t, 5, len(d)) 239 assert.Equal(t, `hello`, string(d)) 240 } 241 242 func TestNative_Vstring(t *testing.T) { 243 var v types.JsonState 244 i := 0 245 s := `test"test\n2"` 246 vstring(&s, &i, &v, 0) 247 assert.Equal(t, 5, i) 248 assert.Equal(t, -1, v.Ep) 249 assert.Equal(t, int64(0), v.Iv) 250 vstring(&s, &i, &v, 0) 251 assert.Equal(t, 13, i) 252 assert.Equal(t, 9, v.Ep) 253 assert.Equal(t, int64(5), v.Iv) 254 } 255 256 func TestNative_Vstring_ValidUnescapedChars(t *testing.T) { 257 var v types.JsonState 258 valid := uint64(types.F_VALIDATE_STRING) 259 i := 0 260 s := "test\x1f\"" 261 vstring(&s, &i, &v, valid) 262 assert.Equal(t, -int(types.ERR_INVALID_CHAR), int(v.Vt)) 263 } 264 265 func TestNative_VstringEscapeEOF(t *testing.T) { 266 var v types.JsonState 267 i := 0 268 s := `xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"x` 269 vstring(&s, &i, &v, 0) 270 assert.Equal(t, 95, i) 271 assert.Equal(t, 63, v.Ep) 272 assert.Equal(t, int64(0), v.Iv) 273 } 274 275 func TestNative_VstringHangUpOnRandomData(t *testing.T) { 276 v, e := hex.DecodeString( 277 "228dc61efd54ef80a908fb6026b7f2d5f92a257ba8b347c995f259eb8685376a" + 278 "8c4500262d9c308b3f3ec2577689cf345d9f86f9b5d18d3e463bec5c22df2d2e" + 279 "4506010eba1dae7278", 280 ) 281 assert.Nil(t, e) 282 p := 1 283 s := rt.Mem2Str(v) 284 var js types.JsonState 285 vstring(&s, &p, &js, 0) 286 fmt.Printf("js: %s\n", spew.Sdump(js)) 287 } 288 289 func TestNative_Vnumber(t *testing.T) { 290 var v types.JsonState 291 i := 0 292 s := "1234" 293 vnumber(&s, &i, &v) 294 assert.Equal(t, 4, i) 295 assert.Equal(t, 0, v.Ep) 296 assert.Equal(t, int64(1234), v.Iv) 297 assert.Equal(t, types.V_INTEGER, v.Vt) 298 i = 0 299 s = "1.234" 300 vnumber(&s, &i, &v) 301 assert.Equal(t, 5, i) 302 assert.Equal(t, 0, v.Ep) 303 assert.Equal(t, 1.234, v.Dv) 304 assert.Equal(t, types.V_DOUBLE, v.Vt) 305 i = 0 306 s = "1.234e5" 307 vnumber(&s, &i, &v) 308 assert.Equal(t, 7, i) 309 assert.Equal(t, 0, v.Ep) 310 assert.Equal(t, 1.234e5, v.Dv) 311 assert.Equal(t, types.V_DOUBLE, v.Vt) 312 i = 0 313 s = "0.0125" 314 vnumber(&s, &i, &v) 315 assert.Equal(t, 6, i) 316 assert.Equal(t, 0, v.Ep) 317 assert.Equal(t, 0.0125, v.Dv) 318 assert.Equal(t, types.V_DOUBLE, v.Vt) 319 i = 0 320 s = "100000000000000000000" 321 vnumber(&s, &i, &v) 322 assert.Equal(t, 21, i) 323 assert.Equal(t, 0, v.Ep) 324 assert.Equal(t, 100000000000000000000.0, v.Dv) 325 assert.Equal(t, types.V_DOUBLE, v.Vt) 326 i = 0 327 s = "999999999999999900000" 328 vnumber(&s, &i, &v) 329 assert.Equal(t, 21, i) 330 assert.Equal(t, 0, v.Ep) 331 assert.Equal(t, 999999999999999900000.0, v.Dv) 332 assert.Equal(t, types.V_DOUBLE, v.Vt) 333 i = 0 334 s = "-1.234" 335 vnumber(&s, &i, &v) 336 assert.Equal(t, 6, i) 337 assert.Equal(t, 0, v.Ep) 338 assert.Equal(t, -1.234, v.Dv) 339 assert.Equal(t, types.V_DOUBLE, v.Vt) 340 } 341 342 func TestNative_Vsigned(t *testing.T) { 343 var v types.JsonState 344 i := 0 345 s := "1234" 346 vsigned(&s, &i, &v) 347 assert.Equal(t, 4, i) 348 assert.Equal(t, 0, v.Ep) 349 assert.Equal(t, int64(1234), v.Iv) 350 assert.Equal(t, types.V_INTEGER, v.Vt) 351 i = 0 352 s = "-1234" 353 vsigned(&s, &i, &v) 354 assert.Equal(t, 5, i) 355 assert.Equal(t, 0, v.Ep) 356 assert.Equal(t, int64(-1234), v.Iv) 357 assert.Equal(t, types.V_INTEGER, v.Vt) 358 i = 0 359 s = "9223372036854775807" 360 vsigned(&s, &i, &v) 361 assert.Equal(t, 19, i) 362 assert.Equal(t, 0, v.Ep) 363 assert.Equal(t, int64(math.MaxInt64), v.Iv) 364 assert.Equal(t, types.V_INTEGER, v.Vt) 365 i = 0 366 s = "-9223372036854775808" 367 vsigned(&s, &i, &v) 368 assert.Equal(t, 20, i) 369 assert.Equal(t, 0, v.Ep) 370 assert.Equal(t, int64(math.MinInt64), v.Iv) 371 assert.Equal(t, types.V_INTEGER, v.Vt) 372 i = 0 373 s = "9223372036854775808" 374 vsigned(&s, &i, &v) 375 assert.Equal(t, 18, i) 376 assert.Equal(t, 0, v.Ep) 377 assert.Equal(t, types.ValueType(-int(types.ERR_INTEGER_OVERFLOW)), v.Vt) 378 i = 0 379 s = "-9223372036854775809" 380 vsigned(&s, &i, &v) 381 assert.Equal(t, 19, i) 382 assert.Equal(t, 0, v.Ep) 383 assert.Equal(t, types.ValueType(-int(types.ERR_INTEGER_OVERFLOW)), v.Vt) 384 i = 0 385 s = "1.234" 386 vsigned(&s, &i, &v) 387 assert.Equal(t, 1, i) 388 assert.Equal(t, 0, v.Ep) 389 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt) 390 i = 0 391 s = "0.0125" 392 vsigned(&s, &i, &v) 393 assert.Equal(t, 1, i) 394 assert.Equal(t, 0, v.Ep) 395 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt) 396 i = 0 397 s = "-1234e5" 398 vsigned(&s, &i, &v) 399 assert.Equal(t, 5, i) 400 assert.Equal(t, 0, v.Ep) 401 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt) 402 i = 0 403 s = "-1234e-5" 404 vsigned(&s, &i, &v) 405 assert.Equal(t, 5, i) 406 assert.Equal(t, 0, v.Ep) 407 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt) 408 } 409 410 func TestNative_Vunsigned(t *testing.T) { 411 var v types.JsonState 412 i := 0 413 s := "1234" 414 vunsigned(&s, &i, &v) 415 assert.Equal(t, 4, i) 416 assert.Equal(t, 0, v.Ep) 417 assert.Equal(t, int64(1234), v.Iv) 418 assert.Equal(t, types.V_INTEGER, v.Vt) 419 i = 0 420 s = "18446744073709551615" 421 vunsigned(&s, &i, &v) 422 assert.Equal(t, 20, i) 423 assert.Equal(t, 0, v.Ep) 424 assert.Equal(t, ^int64(0), v.Iv) 425 assert.Equal(t, types.V_INTEGER, v.Vt) 426 i = 0 427 s = "18446744073709551616" 428 vunsigned(&s, &i, &v) 429 assert.Equal(t, 19, i) 430 assert.Equal(t, 0, v.Ep) 431 assert.Equal(t, types.ValueType(-int(types.ERR_INTEGER_OVERFLOW)), v.Vt) 432 i = 0 433 s = "-1234" 434 vunsigned(&s, &i, &v) 435 assert.Equal(t, 0, i) 436 assert.Equal(t, 0, v.Ep) 437 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt) 438 i = 0 439 s = "1.234" 440 vunsigned(&s, &i, &v) 441 assert.Equal(t, 1, i) 442 assert.Equal(t, 0, v.Ep) 443 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt) 444 i = 0 445 s = "0.0125" 446 vunsigned(&s, &i, &v) 447 assert.Equal(t, 1, i) 448 assert.Equal(t, 0, v.Ep) 449 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt) 450 i = 0 451 s = "1234e5" 452 vunsigned(&s, &i, &v) 453 assert.Equal(t, 4, i) 454 assert.Equal(t, 0, v.Ep) 455 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt) 456 i = 0 457 s = "-1234e5" 458 vunsigned(&s, &i, &v) 459 assert.Equal(t, 0, i) 460 assert.Equal(t, 0, v.Ep) 461 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt) 462 i = 0 463 s = "-1.234e5" 464 vunsigned(&s, &i, &v) 465 assert.Equal(t, 0, i) 466 assert.Equal(t, 0, v.Ep) 467 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt) 468 i = 0 469 s = "-1.234e-5" 470 vunsigned(&s, &i, &v) 471 assert.Equal(t, 0, i) 472 assert.Equal(t, 0, v.Ep) 473 assert.Equal(t, types.ValueType(-int(types.ERR_INVALID_NUMBER_FMT)), v.Vt) 474 } 475 476 func TestNative_SkipOne(t *testing.T) { 477 p := 0 478 s := ` {"asdf": [null, true, false, 1, 2.0, -3]}, 1234.5` 479 q := skip_one(&s, &p, &types.StateMachine{}, uint64(0)) 480 assert.Equal(t, 42, p) 481 assert.Equal(t, 1, q) 482 p = 0 483 s = `1 2.5 -3 "asdf\nqwer" true false null {} []` 484 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0)) 485 assert.Equal(t, 1, p) 486 assert.Equal(t, 0, q) 487 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0)) 488 assert.Equal(t, 5, p) 489 assert.Equal(t, 2, q) 490 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0)) 491 assert.Equal(t, 8, p) 492 assert.Equal(t, 6, q) 493 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0)) 494 assert.Equal(t, 21, p) 495 assert.Equal(t, 9, q) 496 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0)) 497 assert.Equal(t, 26, p) 498 assert.Equal(t, 22, q) 499 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0)) 500 assert.Equal(t, 32, p) 501 assert.Equal(t, 27, q) 502 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0)) 503 assert.Equal(t, 37, p) 504 assert.Equal(t, 33, q) 505 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0)) 506 assert.Equal(t, 40, p) 507 assert.Equal(t, 38, q) 508 q = skip_one(&s, &p, &types.StateMachine{}, uint64(0)) 509 assert.Equal(t, 43, p) 510 assert.Equal(t, 41, q) 511 } 512 513 func TestNative_SkipOne_Error(t *testing.T) { 514 for _, s := range([]string{ 515 "-", "+", "0.", "0. ", "+1", "0.0e ", "9e+", "0e-", 516 "tru", "fals", "nul", "trux", "fals ", 517 `"asdf`, `"\\\"`, 518 }) { 519 p := 0 520 q := skip_one(&s, &p, &types.StateMachine{}, uint64(0)) 521 assert.True(t, q < 0) 522 } 523 } 524 525 func TestNative_SkipArray(t *testing.T) { 526 p := 0 527 s := `null, true, false, 1, 2.0, -3, {"asdf": "wqer"}],` 528 skip_array(&s, &p, &types.StateMachine{}, uint64(0)) 529 assert.Equal(t, p, 48) 530 } 531 532 func TestNative_SkipObject(t *testing.T) { 533 p := 0 534 s := `"asdf": "wqer"},` 535 skip_object(&s, &p, &types.StateMachine{}, uint64(0)) 536 assert.Equal(t, p, 15) 537 } 538 539 func TestNative_SkipNumber(t *testing.T) { 540 p := 0 541 s := `-1.23e+12` 542 q := skip_number(&s, &p) 543 assert.Equal(t, 9, p) 544 assert.Equal(t, 0, q) 545 } 546 547 func TestNative_SkipNumberInJson(t *testing.T) { 548 p := 0x13 549 s := "{\"h\":\"1.00000\",\"i\":true,\"pass3\":1}" 550 q := skip_number(&s, &p) 551 assert.Equal(t, 0x13, p) 552 assert.Equal(t, -2, q) 553 } 554 555 func TestNative_SkipOneFast(t *testing.T) { 556 p := 0 557 s := ` {"asdf": [null, true, false, 1, 2.0, -3]}, 1234.5` 558 q := skip_one_fast(&s, &p) 559 assert.Equal(t, 42, p) 560 assert.Equal(t, 1, q) 561 p = 0 562 s = `1, 2.5, -3, "asdf\nqwer", true, false, null, {}, [],` 563 q = skip_one_fast(&s, &p) 564 assert.Equal(t, 1, p) 565 assert.Equal(t, 0, q) 566 p += 1 567 q = skip_one_fast(&s, &p) 568 assert.Equal(t, 6, p) 569 assert.Equal(t, 3, q) 570 p += 1 571 q = skip_one_fast(&s, &p) 572 assert.Equal(t, 10, p) 573 assert.Equal(t, 8, q) 574 p += 1 575 q = skip_one_fast(&s, &p) 576 assert.Equal(t, 24, p) 577 assert.Equal(t, 12, q) 578 p += 1 579 q = skip_one_fast(&s, &p) 580 assert.Equal(t, 30, p) 581 assert.Equal(t, 26, q) 582 p += 1 583 q = skip_one_fast(&s, &p) 584 assert.Equal(t, 37, p) 585 assert.Equal(t, 32, q) 586 p += 1 587 q = skip_one_fast(&s, &p) 588 assert.Equal(t, 43, p) 589 assert.Equal(t, 39, q) 590 p += 1 591 q = skip_one_fast(&s, &p) 592 assert.Equal(t, 47, p) 593 assert.Equal(t, 45, q) 594 p += 1 595 q = skip_one_fast(&s, &p) 596 assert.Equal(t, 51, p) 597 assert.Equal(t, 49, q) 598 } 599 600 func TestNative_SkipOneFast_Error(t *testing.T) { 601 for _, s := range([]string{ 602 "{{", "[{", "{{}", 603 `"asdf`, `"\\\"`, 604 }) { 605 p := 0 606 q := skip_one_fast(&s, &p) 607 assert.True(t, q < 0) 608 } 609 } 610 611 func TestNative_GetByPath(t *testing.T) { 612 s := `{"asdf": [null, true, false, 1, 2.0, -3]}, 1234.5` 613 p := 0 614 path := []interface{}{"asdf", 4} 615 ret := get_by_path(&s, &p, &path, types.NewStateMachine()) 616 assert.Equal(t, strings.Index(s, "2.0"), ret) 617 } 618 619 func BenchmarkNative_SkipOneFast(b *testing.B) { 620 b.ResetTimer() 621 for i:=0; i<b.N; i++ { 622 s := `{"asdf": [null, true, false, 1, 2.0, -3]}, 1234.5` 623 p := 0 624 _ = skip_one_fast(&s, &p) 625 } 626 } 627 628 func BenchmarkNative_GetByPath(b *testing.B) { 629 b.ResetTimer() 630 for i:=0; i<b.N; i++ { 631 s := `{"asdf": [null, true, false, 1, 2.0, -3]}, 1234.5` 632 p := 0 633 path := []interface{}{"asdf", 3} 634 sm := types.NewStateMachine() 635 _ = get_by_path(&s, &p, &path, sm) 636 types.FreeStateMachine(sm) 637 } 638 }