github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/internal/native/recover_amd64_test.tmpl (about)

     1  /**
     2   * Copyright 2023 ByteDance Inc.
     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   *     http://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  package {{PACKAGE}}
    18  
    19  import (
    20  	`os`
    21  	`runtime`
    22  	`runtime/debug`
    23  	`testing`
    24  	`time`
    25  	`unsafe`
    26  
    27  	`github.com/goshafaq/sonic/internal/native/types`
    28  	`github.com/goshafaq/sonic/loader`
    29  )
    30  
    31  var (
    32  	debugAsyncGC = os.Getenv("SONIC_NO_ASYNC_GC") == ""
    33  )
    34  
    35  var stubs = []loader.GoC{
    36      {"_f32toa", nil, &__f32toa},
    37      {"_f64toa", nil, &__f64toa},
    38      {"_fsm_exec", nil, &__fsm_exec},
    39      {"_get_by_path", nil, &__get_by_path},
    40      {"_html_escape", nil, &__html_escape},
    41      {"_i64toa", nil, &__i64toa},
    42      {"_lspace", nil, &__lspace},
    43      {"_quote", nil, &__quote},
    44      {"_skip_array", nil, &__skip_array},
    45      {"_skip_number", nil, &__skip_number},
    46      {"_skip_object", nil, &__skip_object},
    47      {"_skip_one", nil, &__skip_one},
    48      {"_skip_one_fast", nil, &__skip_one_fast},
    49      {"_u64toa", nil, &__u64toa},
    50      {"_unquote", nil, &__unquote},
    51      {"_validate_one", nil, &__validate_one},
    52      {"_validate_utf8", nil, &__validate_utf8},
    53      {"_validate_utf8_fast", nil, &__validate_utf8_fast},
    54      {"_value", nil,  &__value},
    55      {"_vnumber", nil, &__vnumber},
    56      {"_vsigned", nil, &__vsigned},
    57      {"_vstring", nil, &__vstring},
    58      {"_vunsigned", nil, &__vunsigned},
    59  }
    60  
    61  func TestMain(m *testing.M) {
    62  	loader.WrapGoC(Text__native_entry__, Funcs, stubs, "{{PACKAGE}}", "{{PACKAGE}}/native.c")
    63  	
    64  	go func ()  {
    65  		if !debugAsyncGC {
    66  			return
    67  		}
    68  		println("Begin GC looping...")
    69  		for {
    70  		runtime.GC()
    71  		debug.FreeOSMemory() 
    72  		}
    73  		println("stop GC looping!")
    74  	}()
    75  	time.Sleep(time.Millisecond*100)
    76  	m.Run()
    77  }
    78  
    79  func TestRecover_f64toa(t *testing.T) {
    80  	defer func() {
    81  		if r := recover(); r!= nil {
    82  			t.Log("recover: ", r)
    83  		} else {
    84  			t.Fatal("no panic")
    85  		}
    86  	}()
    87  	_ = f64toa(nil, 123)
    88  }
    89  
    90  func TestRecover_f32toa(t *testing.T) {
    91  	defer func() {
    92  		if r := recover(); r!= nil {
    93  			t.Log("recover: ", r)
    94  		} else {
    95  			t.Fatal("no panic")
    96  		}
    97  	}()
    98  	_ = f32toa(nil, 123)
    99  }
   100  
   101  func TestRecover_i64toa(t *testing.T) {
   102  	defer func() {
   103  		if r := recover(); r!= nil {
   104  			t.Log("recover: ", r)
   105  		} else {
   106  			t.Fatal("no panic")
   107  		}
   108  	}()
   109  	_ = i64toa(nil, 123)
   110  }
   111  
   112  func TestRecover_u64toa(t *testing.T) {
   113  	defer func() {
   114  		if r := recover(); r!= nil {
   115  			t.Log("recover: ", r)
   116  		} else {
   117  			t.Fatal("no panic")
   118  		}
   119  	}()
   120  	_ = u64toa(nil, 123)
   121  }
   122  
   123  func TestRecover_lspace(t *testing.T) {
   124  	defer func() {
   125  		if r := recover(); r!= nil {
   126  			t.Log("recover: ", r)
   127  		} else {
   128  			t.Fatal("no panic")
   129  		}
   130  	}()
   131  	_ = lspace(nil, 2, 0)
   132  }
   133  
   134  func TestRecover_quote(t *testing.T) {
   135  	var dn = 10
   136  	var dp = make([]byte, dn)
   137  	var sp = []byte("123")
   138  	t.Run("sp", func(t *testing.T) {
   139  		defer func() {
   140  			if r := recover(); r!= nil {
   141  				t.Log("recover: ", r)
   142  			} else {
   143  				t.Fatal("no panic")
   144  			}
   145  		}()
   146  		_ = quote(nil, 3, unsafe.Pointer(&dp[0]), &dn, 0)
   147  	})
   148  	t.Run("dp", func(t *testing.T) {
   149  		defer func() {
   150  			if r := recover(); r!= nil {
   151  				t.Log("recover: ", r)
   152  			} else {
   153  				t.Fatal("no panic")
   154  			}
   155  		}()
   156  		_ = quote(unsafe.Pointer(&sp[0]), 3, nil, &dn, 0)
   157  	})
   158  	t.Run("dn", func(t *testing.T) {
   159  		defer func() {
   160  			if r := recover(); r!= nil {
   161  				t.Log("recover: ", r)
   162  			} else {
   163  				t.Fatal("no panic")
   164  			}
   165  		}()
   166  		_ = quote(unsafe.Pointer(&sp[0]), 3, unsafe.Pointer(&dp[0]), nil, 0)
   167  	})
   168  }
   169  
   170  func TestRecover_html_escape(t *testing.T) {
   171  	var dn = 10
   172  	var dp = make([]byte, dn)
   173  	var sp = []byte("123")
   174  	t.Run("sp", func(t *testing.T) {
   175  		defer func() {
   176  			if r := recover(); r!= nil {
   177  				t.Log("recover: ", r)
   178  			} else {
   179  				t.Fatal("no panic")
   180  			}
   181  		}()
   182  		_ = html_escape(nil, 3, unsafe.Pointer(&dp[0]), &dn)
   183  	})
   184  	t.Run("dp", func(t *testing.T) {
   185  		defer func() {
   186  			if r := recover(); r!= nil {
   187  				t.Log("recover: ", r)
   188  			} else {
   189  				t.Fatal("no panic")
   190  			}
   191  		}()
   192  		_ = html_escape(unsafe.Pointer(&sp[0]), 3, nil, &dn)
   193  	})
   194  	t.Run("dn", func(t *testing.T) {
   195  		defer func() {
   196  			if r := recover(); r!= nil {
   197  				t.Log("recover: ", r)
   198  			} else {
   199  				t.Fatal("no panic")
   200  			}
   201  		}()
   202  		_ = html_escape(unsafe.Pointer(&sp[0]), 3, unsafe.Pointer(&dp[0]), nil)
   203  	})
   204  }
   205  
   206  func TestRecover_unquote(t *testing.T) {
   207      var ep = 0
   208      var dp = make([]byte, 10)
   209      var sp = []byte("12\\x\"3\"4")
   210      t.Run("sp", func(t *testing.T) {
   211          defer func() {
   212              if r := recover(); r!= nil {
   213                  t.Log("recover: ", r)
   214              } else {
   215                  t.Fatal("no panic")
   216              }
   217          }()
   218          _ = unquote(nil, len(sp), unsafe.Pointer(&dp[0]), &ep, 0)
   219      })
   220      t.Run("dp", func(t *testing.T) {
   221          defer func() {
   222              if r := recover(); r!= nil {
   223                  t.Log("recover: ", r)
   224              } else {
   225                  t.Fatal("no panic")
   226              }
   227          }()
   228          _ = unquote(unsafe.Pointer(&sp[0]), len(sp), nil, &ep, 0)
   229      })
   230      t.Run("ep", func(t *testing.T) {
   231          defer func() {
   232              if r := recover(); r!= nil {
   233                  t.Log("recover: ", r)
   234              } else {
   235                  t.Fatal("no panic")
   236              }
   237          }()
   238          _ = unquote(unsafe.Pointer(&sp[0]), len(sp), unsafe.Pointer(&dp[0]), nil, 0)
   239      })
   240  }
   241  
   242  func TestRecover_value(t *testing.T) {
   243  	var v = new(types.JsonState)
   244  	var sp = []byte("123")
   245  	t.Run("sp", func(t *testing.T) {
   246  		defer func() {
   247  			if r := recover(); r!= nil {
   248  				t.Log("recover: ", r)
   249  			} else {
   250  				t.Fatal("no panic")
   251  			}
   252  		}()
   253  		_ = value(nil, 3, 0, v, 0)
   254  	})
   255  	t.Run("v", func(t *testing.T) {
   256  		defer func() {
   257  			if r := recover(); r!= nil {
   258  				t.Log("recover: ", r)
   259  			} else {
   260  				t.Fatal("no panic")
   261  			}
   262  		}()
   263  		_ = value(unsafe.Pointer(&sp[0]), 3, 0, nil, 0)
   264  	})
   265  }
   266  
   267  func TestRecover_vstring(t *testing.T) {
   268  	var v = new(types.JsonState)
   269  	var sp = "123"
   270  	var p = 0
   271  	t.Run("sp", func(t *testing.T) {
   272  		defer func() {
   273  			if r := recover(); r!= nil {
   274  				t.Log("recover: ", r)
   275  			} else {
   276  				t.Fatal("no panic")
   277  			}
   278  		}()
   279  		vstring(nil, &p, v, 0)
   280  	})
   281  	t.Run("p", func(t *testing.T) {
   282  		defer func() {
   283  			if r := recover(); r!= nil {
   284  				t.Log("recover: ", r)
   285  			} else {
   286  				t.Fatal("no panic")
   287  			}
   288  		}()
   289  		vstring(&sp, nil, v, 0)
   290  	})
   291  	t.Run("v", func(t *testing.T) {
   292  		defer func() {
   293  			if r := recover(); r!= nil {
   294  				t.Log("recover: ", r)
   295  			} else {
   296  				t.Fatal("no panic")
   297  			}
   298  		}()
   299  		vstring(&sp, &p, nil, 0)
   300  	})
   301  }
   302  
   303  func TestRecover_vnumber(t *testing.T) {
   304  	var v = new(types.JsonState)
   305  	var sp = "123"
   306  	var p = 0
   307  	t.Run("sp", func(t *testing.T) {
   308  		defer func() {
   309  			if r := recover(); r!= nil {
   310  				t.Log("recover: ", r)
   311  			} else {
   312  				t.Fatal("no panic")
   313  			}
   314  		}()
   315  		vnumber(nil, &p, v)
   316  	})
   317  	t.Run("p", func(t *testing.T) {
   318  		defer func() {
   319  			if r := recover(); r!= nil {
   320  				t.Log("recover: ", r)
   321  			} else {
   322  				t.Fatal("no panic")
   323  			}
   324  		}()
   325  		vnumber(&sp, nil, v)
   326  	})
   327  	t.Run("v", func(t *testing.T) {
   328  		defer func() {
   329  			if r := recover(); r!= nil {
   330  				t.Log("recover: ", r)
   331  			} else {
   332  				t.Fatal("no panic")
   333  			}
   334  		}()
   335  		vnumber(&sp, &p, nil)
   336  	})
   337  }
   338  
   339  func TestRecover_vsigned(t *testing.T) {
   340  	var v = new(types.JsonState)
   341  	var sp = "123"
   342  	var p = 0
   343  	t.Run("sp", func(t *testing.T) {
   344  		defer func() {
   345  			if r := recover(); r!= nil {
   346  				t.Log("recover: ", r)
   347  			} else {
   348  				t.Fatal("no panic")
   349  			}
   350  		}()
   351  		vsigned(nil, &p, v)
   352  	})
   353  	t.Run("p", func(t *testing.T) {
   354  		defer func() {
   355  			if r := recover(); r!= nil {
   356  				t.Log("recover: ", r)
   357  			} else {
   358  				t.Fatal("no panic")
   359  			}
   360  		}()
   361  		vsigned(&sp, nil, v)
   362  	})
   363  	t.Run("v", func(t *testing.T) {
   364  		defer func() {
   365  			if r := recover(); r!= nil {
   366  				t.Log("recover: ", r)
   367  			} else {
   368  				t.Fatal("no panic")
   369  			}
   370  		}()
   371  		vsigned(&sp, &p, nil)
   372  	})
   373  }
   374  
   375  func TestRecover_vunsigned(t *testing.T) {
   376  	var v = new(types.JsonState)
   377  	var sp = "123"
   378  	var p = 0
   379  	t.Run("sp", func(t *testing.T) {
   380  		defer func() {
   381  			if r := recover(); r!= nil {
   382  				t.Log("recover: ", r)
   383  			} else {
   384  				t.Fatal("no panic")
   385  			}
   386  		}()
   387  		vunsigned(nil, &p, v)
   388  	})
   389  	t.Run("p", func(t *testing.T) {
   390  		defer func() {
   391  			if r := recover(); r!= nil {
   392  				t.Log("recover: ", r)
   393  			} else {
   394  				t.Fatal("no panic")
   395  			}
   396  		}()
   397  		vunsigned(&sp, nil, v)
   398  	})
   399  	t.Run("v", func(t *testing.T) {
   400  		defer func() {
   401  			if r := recover(); r!= nil {
   402  				t.Log("recover: ", r)
   403  			} else {
   404  				t.Fatal("no panic")
   405  			}
   406  		}()
   407  		vunsigned(&sp, &p, nil)
   408  	})
   409  }
   410  
   411  func TestRecover_skip_one(t *testing.T) {
   412  	var v = types.NewStateMachine()
   413  	var sp = "123"
   414  	var p = 0
   415  	t.Run("sp", func(t *testing.T) {
   416  		defer func() {
   417  			if r := recover(); r!= nil {
   418  				t.Log("recover: ", r)
   419  			} else {
   420  				t.Fatal("no panic")
   421  			}
   422  		}()
   423  		_ = skip_one(nil, &p, v, 0)
   424  	})
   425  	t.Run("p", func(t *testing.T) {
   426  		defer func() {
   427  			if r := recover(); r!= nil {
   428  				t.Log("recover: ", r)
   429  			} else {
   430  				t.Fatal("no panic")
   431  			}
   432  		}()
   433  		_ = skip_one(&sp, nil, v, 0)
   434  	})
   435  	t.Run("v", func(t *testing.T) {
   436  		defer func() {
   437  			if r := recover(); r!= nil {
   438  				t.Log("recover: ", r)
   439  			} else {
   440  				t.Fatal("no panic")
   441  			}
   442  		}()
   443  		_ = skip_one(&sp, &p, nil, 0)
   444  	})
   445  }
   446  
   447  func TestRecover_skip_one_fast(t *testing.T) {
   448  	var sp = "123"
   449  	var p = 0
   450  	t.Run("sp", func(t *testing.T) {
   451  		defer func() {
   452  			if r := recover(); r!= nil {
   453  				t.Log("recover: ", r)
   454  			} else {
   455  				t.Fatal("no panic")
   456  			}
   457  		}()
   458  		_ = skip_one_fast(nil, &p)
   459  	})
   460  	t.Run("p", func(t *testing.T) {
   461  		defer func() {
   462  			if r := recover(); r!= nil {
   463  				t.Log("recover: ", r)
   464  			} else {
   465  				t.Fatal("no panic")
   466  			}
   467  		}()
   468  		_ = skip_one_fast(&sp, nil)
   469  	})
   470  }
   471  
   472  func TestRecover_skip_array(t *testing.T) {
   473  	var v = types.NewStateMachine()
   474  	var sp = "123"
   475  	var p = 0
   476  	t.Run("sp", func(t *testing.T) {
   477  		defer func() {
   478  			if r := recover(); r!= nil {
   479  				t.Log("recover: ", r)
   480  			} else {
   481  				t.Fatal("no panic")
   482  			}
   483  		}()
   484  		_ = skip_array(nil, &p, v, 0)
   485  	})
   486  	t.Run("p", func(t *testing.T) {
   487  		defer func() {
   488  			if r := recover(); r!= nil {
   489  				t.Log("recover: ", r)
   490  			} else {
   491  				t.Fatal("no panic")
   492  			}
   493  		}()
   494  		_ = skip_array(&sp, nil, v, 0)
   495  	})
   496  	t.Run("v", func(t *testing.T) {
   497  		defer func() {
   498  			if r := recover(); r!= nil {
   499  				t.Log("recover: ", r)
   500  			} else {
   501  				t.Fatal("no panic")
   502  			}
   503  		}()
   504  		_ = skip_array(&sp, &p, nil, 0)
   505  	})
   506  }
   507  
   508  func TestRecover_skip_object(t *testing.T) {
   509  	var v = types.NewStateMachine()
   510  	var sp = "123"
   511  	var p = 0
   512  	t.Run("sp", func(t *testing.T) {
   513  		defer func() {
   514  			if r := recover(); r!= nil {
   515  				t.Log("recover: ", r)
   516  			} else {
   517  				t.Fatal("no panic")
   518  			}
   519  		}()
   520  		_ = skip_object(nil, &p, v, 0)
   521  	})
   522  	t.Run("p", func(t *testing.T) {
   523  		defer func() {
   524  			if r := recover(); r!= nil {
   525  				t.Log("recover: ", r)
   526  			} else {
   527  				t.Fatal("no panic")
   528  			}
   529  		}()
   530  		_ = skip_object(&sp, nil, v, 0)
   531  	})
   532  	t.Run("v", func(t *testing.T) {
   533  		defer func() {
   534  			if r := recover(); r!= nil {
   535  				t.Log("recover: ", r)
   536  			} else {
   537  				t.Fatal("no panic")
   538  			}
   539  		}()
   540  		_ = skip_object(&sp, &p, nil, 0)
   541  	})
   542  }
   543  
   544  func TestRecover_skip_number(t *testing.T) {
   545  	var sp = "123"
   546  	var p = 0
   547  	t.Run("sp", func(t *testing.T) {
   548  		defer func() {
   549  			if r := recover(); r!= nil {
   550  				t.Log("recover: ", r)
   551  			} else {
   552  				t.Fatal("no panic")
   553  			}
   554  		}()
   555  		_ = skip_number(nil, &p)
   556  	})
   557  	t.Run("p", func(t *testing.T) {
   558  		defer func() {
   559  			if r := recover(); r!= nil {
   560  				t.Log("recover: ", r)
   561  			} else {
   562  				t.Fatal("no panic")
   563  			}
   564  		}()
   565  		_ = skip_number(&sp, nil)
   566  	})
   567  }
   568  
   569  func TestRecover_get_by_path(t *testing.T) {
   570  	var v = []interface{}{}
   571  	var sp = "123"
   572  	var p = 0
   573  	var m = types.NewStateMachine()
   574  	t.Run("sp", func(t *testing.T) {
   575  		defer func() {
   576  			if r := recover(); r!= nil {
   577  				t.Log("recover: ", r)
   578  			} else {
   579  				t.Fatal("no panic")
   580  			}
   581  		}()
   582  		_ = get_by_path(nil, &p, &v, m)
   583  	})
   584  	t.Run("p", func(t *testing.T) {
   585  		defer func() {
   586  			if r := recover(); r!= nil {
   587  				t.Log("recover: ", r)
   588  			} else {
   589  				t.Fatal("no panic")
   590  			}
   591  		}()
   592  		_ = get_by_path(&sp, nil, &v, m)
   593  	})
   594  	t.Run("path", func(t *testing.T) {
   595  		defer func() {
   596  			if r := recover(); r!= nil {
   597  				t.Log("recover: ", r)
   598  			} else {
   599  				t.Fatal("no panic")
   600  			}
   601  		}()
   602  		_ = get_by_path(&sp, &p, nil, m)
   603  	})
   604  	t.Run("m", func(t *testing.T) {
   605  		defer func() {
   606  			if r := recover(); r!= nil {
   607  				t.Log("recover: ", r)
   608  			} else {
   609  				t.Fatal("no panic")
   610  			}
   611  		}()
   612  		_ = get_by_path(&sp, &p, &v, nil)
   613  	})
   614  }
   615  
   616  func TestRecover_validate_one(t *testing.T) {
   617  	var v = types.NewStateMachine()
   618  	var sp = "123"
   619  	var p = 0
   620  	t.Run("sp", func(t *testing.T) {
   621  		defer func() {
   622  			if r := recover(); r!= nil {
   623  				t.Log("recover: ", r)
   624  			} else {
   625  				t.Fatal("no panic")
   626  			}
   627  		}()
   628  		_ = validate_one(nil, &p, v)
   629  	})
   630  	t.Run("p", func(t *testing.T) {
   631  		defer func() {
   632  			if r := recover(); r!= nil {
   633  				t.Log("recover: ", r)
   634  			} else {
   635  				t.Fatal("no panic")
   636  			}
   637  		}()
   638  		_ = validate_one(&sp, nil, v)
   639  	})
   640  	t.Run("v", func(t *testing.T) {
   641  		defer func() {
   642  			if r := recover(); r!= nil {
   643  				t.Log("recover: ", r)
   644  			} else {
   645  				t.Fatal("no panic")
   646  			}
   647  		}()
   648  		_ = validate_one(&sp, &p, nil)
   649  	})
   650  }
   651  
   652  func TestRecover_validate_utf8(t *testing.T) {
   653  	var v = types.NewStateMachine()
   654  	var sp = string([]byte{0xff, 0xff, 0xff})
   655  	var p = 0
   656  	t.Run("sp", func(t *testing.T) {
   657  		defer func() {
   658  			if r := recover(); r!= nil {
   659  				t.Log("recover: ", r)
   660  			} else {
   661  				t.Fatal("no panic")
   662  			}
   663  		}()
   664  		_ = validate_utf8(nil, &p, v)
   665  	})
   666  	t.Run("p", func(t *testing.T) {
   667  		defer func() {
   668  			if r := recover(); r!= nil {
   669  				t.Log("recover: ", r)
   670  			} else {
   671  				t.Fatal("no panic")
   672  			}
   673  		}()
   674  		_ = validate_utf8(&sp, nil, v)
   675  	})
   676  	t.Run("v", func(t *testing.T) {
   677  		defer func() {
   678  			if r := recover(); r!= nil {
   679  				t.Log("recover: ", r)
   680  			} else {
   681  				t.Fatal("no panic")
   682  			}
   683  		}()
   684  		_ = validate_utf8(&sp, &p, nil)
   685  	})
   686  }
   687  
   688  func TestRecover_validate_utf8_fast(t *testing.T) {
   689  	defer func() {
   690  		if r := recover(); r!= nil {
   691  			t.Log("recover: ", r)
   692  		} else {
   693  			t.Fatal("no panic")
   694  		}
   695  	}()
   696  	_ = validate_utf8_fast(nil)
   697  }