Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint committed Oct 13, 2024
1 parent f8989f1 commit 496cfc7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
29 changes: 29 additions & 0 deletions runtime/languages/assemblyscript/testdata/assembly/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,32 @@ export function testClassContainingMapOutput_string_string(): TestClassWithMap {
c.m.set("c", "3");
return c;
}

export function testMapInput_string_f64(m: Map<string, f64>): void {
assert(m.size == 4, "expected size 4");

let n: f64 = 0.5;
assert(m.has("a"), "expected key 'a' to be present");
assert(m.get("a") == n, `expected value for key 'a' to be ${n}`);

n = f64.EPSILON;
assert(m.has("b"), "expected key 'b' to be present");
assert(m.get("b") == n, `expected value for key 'b' to be ${n}`);

n = f64.MIN_VALUE;
assert(m.has("c"), "expected key 'c' to be present");
assert(m.get("c") == n, `expected value for key 'c' to be ${n}`);

n = f64.MAX_VALUE;
assert(m.has("d"), "expected key 'd' to be present");
assert(m.get("d") == n, `expected value for key 'd' to be ${n}`);
}

export function testMapOutput_string_f64(): Map<string, f64> {
const m = new Map<string, f64>();
m.set("a", 0.5);
m.set("b", f64.EPSILON);
m.set("c", f64.MIN_VALUE);
m.set("d", f64.MAX_VALUE);
return m;
}
Binary file modified runtime/languages/assemblyscript/testdata/build/testdata.wasm
Binary file not shown.
45 changes: 45 additions & 0 deletions runtime/languages/assemblyscript/tests/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package assemblyscript_test
import (
"fmt"
"maps"
"math"
"reflect"
"testing"

Expand Down Expand Up @@ -232,3 +233,47 @@ func makeTestMap(size int) map[string]string {
}
return m
}

var epsilon = math.Nextafter(1, 2) - 1

func TestMapInput_string_f64(t *testing.T) {
fnName := "testMapInput_string_f64"
m := map[string]float64{
"a": 0.5,
"b": epsilon,
"c": math.SmallestNonzeroFloat64,
"d": math.MaxFloat64,
}

if _, err := fixture.CallFunction(t, fnName, m); err != nil {
t.Error(err)
}
if m, err := utils.ConvertToMap(m); err != nil {
t.Error(fmt.Errorf("failed conversion to interface map: %w", err))
} else if _, err := fixture.CallFunction(t, fnName, m); err != nil {
t.Error(err)
}
}

func TestMapOutput_string_f64(t *testing.T) {
fnName := "testMapOutput_string_f64"
result, err := fixture.CallFunction(t, fnName)
if err != nil {
t.Fatal(err)
}

expected := map[string]float64{
"a": 0.5,
"b": epsilon,
"c": math.SmallestNonzeroFloat64,
"d": math.MaxFloat64,
}

if result == nil {
t.Error("expected a result")
} else if r, ok := result.(map[string]float64); !ok {
t.Errorf("expected %T, got %T", expected, result)
} else if !maps.Equal(expected, r) {
t.Errorf("expected %v, got %v", expected, r)
}
}

0 comments on commit 496cfc7

Please sign in to comment.