Files
wazero/api/wasm_test.go
Crypt Keeper f5598c9a8e Fixes global numeric types to have max of signed encoding (#442)
This adjusts towards the exiting code which used int32/64 instead of
uint32/64. The reason is that the spec indicates intepretation as signed
numbers, which affects the maximum value.

See https://www.w3.org/TR/wasm-core-1/#value-types%E2%91%A2

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-06 06:35:31 +08:00

76 lines
1.7 KiB
Go

package api
import (
"fmt"
"math"
"testing"
"github.com/stretchr/testify/require"
)
func TestValueTypeName(t *testing.T) {
tests := []struct {
name string
input ValueType
expected string
}{
{"i32", ValueTypeI32, "i32"},
{"i64", ValueTypeI64, "i64"},
{"f32", ValueTypeF32, "f32"},
{"f64", ValueTypeF64, "f64"},
{"unknown", 100, "unknown"},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expected, ValueTypeName(tc.input))
})
}
}
func TestEncodeDecodeF32(t *testing.T) {
for _, v := range []float32{
0, 100, -100, 1, -1,
100.01234124, -100.01234124, 200.12315,
math.MaxFloat32,
math.SmallestNonzeroFloat32,
float32(math.Inf(1)), float32(math.Inf(-1)), float32(math.NaN()),
} {
t.Run(fmt.Sprintf("%f", v), func(t *testing.T) {
encoded := EncodeF32(v)
binary := DecodeF32(encoded)
if math.IsNaN(float64(binary)) { // NaN cannot be compared with themselves, so we have to use IsNaN
require.True(t, math.IsNaN(float64(binary)))
} else {
require.Equal(t, v, binary)
}
})
}
}
func TestEncodeDecodeF64(t *testing.T) {
for _, v := range []float64{
0, 100, -100, 1, -1,
100.01234124, -100.01234124, 200.12315,
math.MaxFloat32,
math.SmallestNonzeroFloat32,
math.MaxFloat64,
math.SmallestNonzeroFloat64,
6.8719476736e+10, /* = 1 << 36 */
1.37438953472e+11, /* = 1 << 37 */
math.Inf(1), math.Inf(-1), math.NaN(),
} {
t.Run(fmt.Sprintf("%f", v), func(t *testing.T) {
encoded := EncodeF64(v)
val := DecodeF64(encoded)
if math.IsNaN(val) { // cannot use require.Equal as NaN by definition doesn't equal itself
require.True(t, math.IsNaN(val))
} else {
require.Equal(t, v, val)
}
})
}
}