github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/experimental/wazerotest/wazerotest_test.go (about)

     1  package wazerotest
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/tetratelabs/wazero/api"
    10  )
    11  
    12  func TestNewFunction(t *testing.T) {
    13  	tests := []struct {
    14  		in  any
    15  		out *Function
    16  	}{
    17  		{
    18  			in: func(ctx context.Context, mod api.Module) {},
    19  			out: &Function{
    20  				ParamTypes:  []api.ValueType{},
    21  				ResultTypes: []api.ValueType{},
    22  			},
    23  		},
    24  
    25  		{
    26  			in: func(ctx context.Context, mod api.Module, v uint32) {},
    27  			out: &Function{
    28  				ParamTypes:  []api.ValueType{api.ValueTypeI32},
    29  				ResultTypes: []api.ValueType{},
    30  			},
    31  		},
    32  
    33  		{
    34  			in: func(ctx context.Context, mod api.Module, v uint64) {},
    35  			out: &Function{
    36  				ParamTypes:  []api.ValueType{api.ValueTypeI64},
    37  				ResultTypes: []api.ValueType{},
    38  			},
    39  		},
    40  
    41  		{
    42  			in: func(ctx context.Context, mod api.Module, v int32) {},
    43  			out: &Function{
    44  				ParamTypes:  []api.ValueType{api.ValueTypeI32},
    45  				ResultTypes: []api.ValueType{},
    46  			},
    47  		},
    48  
    49  		{
    50  			in: func(ctx context.Context, mod api.Module, v int64) {},
    51  			out: &Function{
    52  				ParamTypes:  []api.ValueType{api.ValueTypeI64},
    53  				ResultTypes: []api.ValueType{},
    54  			},
    55  		},
    56  
    57  		{
    58  			in: func(ctx context.Context, mod api.Module, v float32) {},
    59  			out: &Function{
    60  				ParamTypes:  []api.ValueType{api.ValueTypeF32},
    61  				ResultTypes: []api.ValueType{},
    62  			},
    63  		},
    64  
    65  		{
    66  			in: func(ctx context.Context, mod api.Module, v float64) {},
    67  			out: &Function{
    68  				ParamTypes:  []api.ValueType{api.ValueTypeF64},
    69  				ResultTypes: []api.ValueType{},
    70  			},
    71  		},
    72  
    73  		{
    74  			in: func(ctx context.Context, mod api.Module) uint32 { return 0 },
    75  			out: &Function{
    76  				ParamTypes:  []api.ValueType{},
    77  				ResultTypes: []api.ValueType{api.ValueTypeI32},
    78  			},
    79  		},
    80  
    81  		{
    82  			in: func(ctx context.Context, mod api.Module) uint64 { return 0 },
    83  			out: &Function{
    84  				ParamTypes:  []api.ValueType{},
    85  				ResultTypes: []api.ValueType{api.ValueTypeI64},
    86  			},
    87  		},
    88  
    89  		{
    90  			in: func(ctx context.Context, mod api.Module) int32 { return 0 },
    91  			out: &Function{
    92  				ParamTypes:  []api.ValueType{},
    93  				ResultTypes: []api.ValueType{api.ValueTypeI32},
    94  			},
    95  		},
    96  
    97  		{
    98  			in: func(ctx context.Context, mod api.Module) int64 { return 0 },
    99  			out: &Function{
   100  				ParamTypes:  []api.ValueType{},
   101  				ResultTypes: []api.ValueType{api.ValueTypeI64},
   102  			},
   103  		},
   104  
   105  		{
   106  			in: func(ctx context.Context, mod api.Module) float32 { return 0 },
   107  			out: &Function{
   108  				ParamTypes:  []api.ValueType{},
   109  				ResultTypes: []api.ValueType{api.ValueTypeF32},
   110  			},
   111  		},
   112  
   113  		{
   114  			in: func(ctx context.Context, mod api.Module) float64 { return 0 },
   115  			out: &Function{
   116  				ParamTypes:  []api.ValueType{},
   117  				ResultTypes: []api.ValueType{api.ValueTypeF64},
   118  			},
   119  		},
   120  
   121  		{
   122  			in: func(ctx context.Context, mod api.Module, _ uint32, _ uint64, _ int32, _ int64, _ float32, _ float64) (_ uint32, _ uint64, _ int32, _ int64, _ float32, _ float64) {
   123  				return
   124  			},
   125  			out: &Function{
   126  				ParamTypes: []api.ValueType{
   127  					api.ValueTypeI32, api.ValueTypeI64,
   128  					api.ValueTypeI32, api.ValueTypeI64,
   129  					api.ValueTypeF32, api.ValueTypeF64,
   130  				},
   131  				ResultTypes: []api.ValueType{
   132  					api.ValueTypeI32, api.ValueTypeI64,
   133  					api.ValueTypeI32, api.ValueTypeI64,
   134  					api.ValueTypeF32, api.ValueTypeF64,
   135  				},
   136  			},
   137  		},
   138  	}
   139  
   140  	for _, test := range tests {
   141  		t.Run(reflect.TypeOf(test.in).String(), func(t *testing.T) {
   142  			f := NewFunction(test.in)
   143  			if !bytes.Equal(test.out.ParamTypes, f.ParamTypes) {
   144  				t.Errorf("invalid parameter types: want=%v got=%v", test.out.ParamTypes, f.ParamTypes)
   145  			}
   146  			if !bytes.Equal(test.out.ResultTypes, f.ResultTypes) {
   147  				t.Errorf("invalid result types: want=%v got=%v", test.out.ResultTypes, f.ResultTypes)
   148  			}
   149  		})
   150  	}
   151  }
   152  
   153  func TestNewMemory(t *testing.T) {
   154  	if memory := NewMemory(1); len(memory.Bytes) != PageSize {
   155  		t.Error("memory buffer not aligned on page size")
   156  	}
   157  }
   158  
   159  func TestNewFixedMemory(t *testing.T) {
   160  	if memory := NewFixedMemory(1); len(memory.Bytes) != PageSize {
   161  		t.Error("memory buffer not aligned on page size")
   162  	} else if memory.Min != 1 {
   163  		t.Error("invalid min memory size:", memory.Min)
   164  	} else if memory.Max != 1 {
   165  		t.Error("invalid max memory size:", memory.Max)
   166  	}
   167  }