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

     1  // Code generated by Makefile, DO NOT EDIT.
     2  
     3  /*
     4   * Copyright 2023 CloudWeGo Authors.
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   *     http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package avx
    20  
    21  import (
    22  	"os"
    23  	"runtime"
    24  	"runtime/debug"
    25  	"testing"
    26  	"time"
    27  	"unsafe"
    28  
    29  	"github.com/bytedance/sonic/loader"
    30  	"github.com/cloudwego/dynamicgo/internal/caching"
    31  	"github.com/cloudwego/dynamicgo/internal/native/types"
    32  	"github.com/stretchr/testify/assert"
    33  )
    34  
    35  var (
    36  	debugAsyncGC = os.Getenv("SONIC_NO_ASYNC_GC") == ""
    37  )
    38  
    39  var stubs = []loader.GoC{
    40  	{"_tb_write_i64", nil, &__tb_write_i64},
    41  	{"_hm_get", nil, &__hm_get},
    42  	{"_trie_get", nil, &__trie_get},
    43  	{"_j2t_fsm_exec", nil, &__j2t_fsm_exec},
    44  	{"_tb_skip", nil, &__tb_skip},
    45  	{"_f64toa", nil, &__f64toa},
    46  	{"_i64toa", nil, &__i64toa},
    47      {"_quote", nil, &__quote},
    48  }
    49  
    50  func TestMain(m *testing.M) {
    51  	loader.WrapGoC(Text__native_entry__, Funcs, stubs, "avx", "avx/native.c")
    52  
    53  	go func() {
    54  		if !debugAsyncGC {
    55  			return
    56  		}
    57  		println("Begin GC looping...")
    58  		for {
    59  			runtime.GC()
    60  			debug.FreeOSMemory()
    61  		}
    62  		println("stop GC looping!")
    63  	}()
    64  	time.Sleep(time.Millisecond * 100)
    65  	m.Run()
    66  }
    67  
    68  func TestNative_tb_write_i64(t *testing.T) {
    69  	v := int64(0x0102030405060708)
    70  	buf := make([]byte, 0, 4)
    71  	ret := tb_write_i64(&buf, v)
    72  	assert.Equal(t, uint64(4<<types.ERR_WRAP_SHIFT_CODE)|uint64(types.ERR_OOM_BUF), ret)
    73  	assert.Equal(t, 0, len(buf))
    74  	buf = make([]byte, 0, 9)
    75  	ret = tb_write_i64(&buf, v)
    76  	assert.Equal(t, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, buf)
    77  }
    78  
    79  func TestNative_hm_get(t *testing.T) {
    80  	var hm = caching.NewHashMap(10, 4)
    81  	var v = int(0)
    82  	var p = unsafe.Pointer(&v)
    83  	var ex = "exist"
    84  	var ne = "not-exist"
    85  	hm.Set(ex, p)
    86  	if ret := hm_get(hm, &ex); ret != p {
    87  		t.Fatal(ret)
    88  	}
    89  	if ret := hm_get(hm, &ne); ret != nil {
    90  		t.Fatal(ret)
    91  	}
    92  }
    93  
    94  func TestNative_trie_get(t *testing.T) {
    95  	var tt = &caching.TrieTree{}
    96  	var v = int(0)
    97  	var p = unsafe.Pointer(&v)
    98  	var ex = "exist"
    99  	var ne = "not-exist"
   100  	tt.Set(ex, p)
   101  	if ret := trie_get(tt, &ex); ret != p {
   102  		t.Fatal(ret)
   103  	}
   104  	if ret := trie_get(tt, &ne); ret != nil {
   105  		t.Fatal(ret)
   106  	}
   107  }
   108  
   109  func TestRecover_j2t_fsm_exec(t *testing.T) {
   110      // var fsm = types.NewJ2TStateMachine()
   111  	var js = `{}`
   112  	var buf = make([]byte,0, 256)
   113  	type args struct {
   114  		fsm  *types.J2TStateMachine
   115  		buf  *[]byte
   116  		src  *string
   117  		flag uint64
   118  	}
   119  	tests := []struct {
   120  		name    string
   121  		args    args
   122  		wantRet uint64
   123  	}{
   124  		{"1", args{nil, &buf, &js, 0}, 0},
   125  		// {"2", args{fsm, nil, &js, 0}, 0},
   126  	}
   127  	for _, tt := range tests {
   128  		t.Run(tt.name, func(t *testing.T) {
   129              defer func() {
   130                  if r := recover(); r != nil {
   131                      t.Log("recover: ", r)
   132                  } else {
   133                      t.Fatal("no panic")
   134                  }
   135              }()
   136              _ = j2t_fsm_exec(tt.args.fsm, tt.args.buf, tt.args.src, tt.args.flag)
   137  		})
   138  	}
   139  }
   140  
   141  func TestRecover_tb_skip(t *testing.T) {
   142      // var fsm = types.NewTStateMachine()
   143  	var buf = make([]byte, 256)
   144  	type args struct {
   145  		fsm  *types.TStateMachine
   146  		buf  *byte
   147  		n int
   148          t uint8
   149  	}
   150  	tests := []struct {
   151  		name    string
   152  		args    args
   153  		wantRet uint64
   154  	}{
   155  		{"1", args{nil, &buf[0], 10, 2}, 0},
   156  	}
   157  	for _, tt := range tests {
   158  		t.Run(tt.name, 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              _ = tb_skip(tt.args.fsm, tt.args.buf, tt.args.n, tt.args.t)
   167  		})
   168  	}
   169  }