github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/internal/native/native_amd64_test.tmpl (about)

     1  /*
     2   * Copyright 2023 CloudWeGo Authors.
     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/bytedance/sonic/loader"
    28  	"github.com/cloudwego/dynamicgo/internal/caching"
    29  	"github.com/cloudwego/dynamicgo/internal/native/types"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  var (
    34  	debugAsyncGC = os.Getenv("SONIC_NO_ASYNC_GC") == ""
    35  )
    36  
    37  var stubs = []loader.GoC{
    38  	{"_tb_write_i64", nil, &__tb_write_i64},
    39  	{"_hm_get", nil, &__hm_get},
    40  	{"_trie_get", nil, &__trie_get},
    41  	{"_j2t_fsm_exec", nil, &__j2t_fsm_exec},
    42  	{"_tb_skip", nil, &__tb_skip},
    43  	{"_f64toa", nil, &__f64toa},
    44  	{"_i64toa", nil, &__i64toa},
    45      {"_quote", nil, &__quote},
    46  }
    47  
    48  func TestMain(m *testing.M) {
    49  	loader.WrapGoC(Text__native_entry__, Funcs, stubs, "{{PACKAGE}}", "{{PACKAGE}}/native.c")
    50  
    51  	go func() {
    52  		if !debugAsyncGC {
    53  			return
    54  		}
    55  		println("Begin GC looping...")
    56  		for {
    57  			runtime.GC()
    58  			debug.FreeOSMemory()
    59  		}
    60  		println("stop GC looping!")
    61  	}()
    62  	time.Sleep(time.Millisecond * 100)
    63  	m.Run()
    64  }
    65  
    66  func TestNative_tb_write_i64(t *testing.T) {
    67  	v := int64(0x0102030405060708)
    68  	buf := make([]byte, 0, 4)
    69  	ret := tb_write_i64(&buf, v)
    70  	assert.Equal(t, uint64(4<<types.ERR_WRAP_SHIFT_CODE)|uint64(types.ERR_OOM_BUF), ret)
    71  	assert.Equal(t, 0, len(buf))
    72  	buf = make([]byte, 0, 9)
    73  	ret = tb_write_i64(&buf, v)
    74  	assert.Equal(t, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, buf)
    75  }
    76  
    77  func TestNative_hm_get(t *testing.T) {
    78  	var hm = caching.NewHashMap(10, 4)
    79  	var v = int(0)
    80  	var p = unsafe.Pointer(&v)
    81  	var ex = "exist"
    82  	var ne = "not-exist"
    83  	hm.Set(ex, p)
    84  	if ret := hm_get(hm, &ex); ret != p {
    85  		t.Fatal(ret)
    86  	}
    87  	if ret := hm_get(hm, &ne); ret != nil {
    88  		t.Fatal(ret)
    89  	}
    90  }
    91  
    92  func TestNative_trie_get(t *testing.T) {
    93  	var tt = &caching.TrieTree{}
    94  	var v = int(0)
    95  	var p = unsafe.Pointer(&v)
    96  	var ex = "exist"
    97  	var ne = "not-exist"
    98  	tt.Set(ex, p)
    99  	if ret := trie_get(tt, &ex); ret != p {
   100  		t.Fatal(ret)
   101  	}
   102  	if ret := trie_get(tt, &ne); ret != nil {
   103  		t.Fatal(ret)
   104  	}
   105  }
   106  
   107  func TestRecover_j2t_fsm_exec(t *testing.T) {
   108      // var fsm = types.NewJ2TStateMachine()
   109  	var js = `{}`
   110  	var buf = make([]byte,0, 256)
   111  	type args struct {
   112  		fsm  *types.J2TStateMachine
   113  		buf  *[]byte
   114  		src  *string
   115  		flag uint64
   116  	}
   117  	tests := []struct {
   118  		name    string
   119  		args    args
   120  		wantRet uint64
   121  	}{
   122  		{"1", args{nil, &buf, &js, 0}, 0},
   123  		// {"2", args{fsm, nil, &js, 0}, 0},
   124  	}
   125  	for _, tt := range tests {
   126  		t.Run(tt.name, func(t *testing.T) {
   127              defer func() {
   128                  if r := recover(); r != nil {
   129                      t.Log("recover: ", r)
   130                  } else {
   131                      t.Fatal("no panic")
   132                  }
   133              }()
   134              _ = j2t_fsm_exec(tt.args.fsm, tt.args.buf, tt.args.src, tt.args.flag)
   135  		})
   136  	}
   137  }
   138  
   139  func TestRecover_tb_skip(t *testing.T) {
   140      // var fsm = types.NewTStateMachine()
   141  	var buf = make([]byte, 256)
   142  	type args struct {
   143  		fsm  *types.TStateMachine
   144  		buf  *byte
   145  		n int
   146          t uint8
   147  	}
   148  	tests := []struct {
   149  		name    string
   150  		args    args
   151  		wantRet uint64
   152  	}{
   153  		{"1", args{nil, &buf[0], 10, 2}, 0},
   154  	}
   155  	for _, tt := range tests {
   156  		t.Run(tt.name, func(t *testing.T) {
   157              defer func() {
   158                  if r := recover(); r != nil {
   159                      t.Log("recover: ", r)
   160                  } else {
   161                      t.Fatal("no panic")
   162                  }
   163              }()
   164              _ = tb_skip(tt.args.fsm, tt.args.buf, tt.args.n, tt.args.t)
   165  		})
   166  	}
   167  }