Skip to content

Commit

Permalink
Improved some test coverage. (#192)
Browse files Browse the repository at this point in the history
Fixed one small issue in the process:
 - Modified `wasmtime_engine_increment_epoch` to return `void` instead of `IntPtr`. Based on signature from these docs: https://docs.wasmtime.dev/c-api/engine_8h.html
  • Loading branch information
martindevans committed Dec 6, 2022
1 parent 992efb0 commit 55b687b
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static class Native
public static extern void wasm_engine_delete(IntPtr engine);

[DllImport(LibraryName)]
public static extern IntPtr wasmtime_engine_increment_epoch(IntPtr engine);
public static extern void wasmtime_engine_increment_epoch(IntPtr engine);
}

private readonly Handle handle;
Expand Down
94 changes: 94 additions & 0 deletions tests/ConfigTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using FluentAssertions;
using Xunit;

Expand All @@ -16,6 +17,15 @@ public void ItSetsCompilerStrategy()
using var engine = new Engine(config);
}

[Fact]
public void ItFailsSettingNonexistantCompilerStrategy()
{
var config = new Config();

var act = () => { config.WithCompilerStrategy((CompilerStrategy)123); };
act.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void ItSetsProfilingStrategy()
{
Expand All @@ -26,6 +36,15 @@ public void ItSetsProfilingStrategy()
using var engine = new Engine(config);
}

[Fact]
public void ItFailsSettingNonexistantProfilingStrategy()
{
var config = new Config();

var act = () => { config.WithProfilingStrategy((ProfilingStrategy)123); };
act.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void ItSetsOptimizationLevel()
{
Expand All @@ -36,6 +55,15 @@ public void ItSetsOptimizationLevel()
using var engine = new Engine(config);
}

[Fact]
public void ItFailsSettingNonexistantOptimizationLevel()
{
var config = new Config();

var act = () => { config.WithOptimizationLevel((OptimizationLevel)123); };
act.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void ItSetsNanCanonicalization()
{
Expand All @@ -55,5 +83,71 @@ public void ItSetsEpochInterruption()

using var engine = new Engine(config);
}

[Fact]
public void ItSetsDebugInfo()
{
var config = new Config();

config.WithDebugInfo(true);

using var engine = new Engine(config);
}

[Fact]
public void ItSetsThreads()
{
var config = new Config();
config.WithWasmThreads(true);

using var engine = new Engine(config);
using var module = Module.FromTextFile(engine, Path.Combine("Modules", "SharedMemory.wat"));
}

[Fact]
public void ItSetsSIMD()
{
var config = new Config();
config.WithSIMD(false);

Action act = () =>
{
using var engine = new Engine(config);
using var module = Module.FromTextFile(engine, Path.Combine("Modules", "SIMD.wat"));
};

act.Should().Throw<WasmtimeException>();
}

[Fact]
public void ItSetsBulkMemory()
{
var config = new Config();
config.WithBulkMemory(false);
config.WithReferenceTypes(false);

Action act = () =>
{
using var engine = new Engine(config);
using var module = Module.FromTextFile(engine, Path.Combine("Modules", "BulkMemory.wat"));
};

act.Should().Throw<WasmtimeException>();
}

[Fact]
public void ItSetsMultiValue()
{
var config = new Config();
config.WithMultiValue(false);

Action act = () =>
{
using var engine = new Engine(config);
using var module = Module.FromTextFile(engine, Path.Combine("Modules", "MultiValue.wat"));
};

act.Should().Throw<WasmtimeException>();
}
}
}
18 changes: 18 additions & 0 deletions tests/GlobalImportBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ public void ItFailsToInstantiateWhenGlobalIsMut()
.WithMessage("incompatible import type for `::global_i32`*");
}

[Fact]
public void ItFailsBindingGlobalsWithWrongType()
{
var global_i32_mut = new Global(Store, ValueKind.Int32, 0, Mutability.Mutable);

global_i32_mut.Wrap<float>().Should().BeNull();
global_i32_mut.Wrap<double>().Should().BeNull();
global_i32_mut.Wrap<uint>().Should().BeNull();
}

[Fact]
public void ItFailsMutatingImmutableGlobal()
{
var global_i32_mut = new Global(Store, ValueKind.Int32, 0, Mutability.Immutable);
Action action = () => global_i32_mut.Wrap<int>()!.SetValue(3);
action.Should().Throw<InvalidOperationException>();
}

[Fact]
public void ItBindsTheGlobalsCorrectly()
{
Expand Down
9 changes: 9 additions & 0 deletions tests/Modules/BulkMemory.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(module
(func (param $dst i32) (param $src i32) (param $size i32) (result i32)
local.get $dst
local.get $src
local.get $size
memory.copy
local.get $dst
)
)
4 changes: 4 additions & 0 deletions tests/Modules/MultiValue.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(module
(func $echo_tuple2 (result i32 i32) i32.const 1 i32.const 2)
(export "$echo_tuple2" (func $echo_tuple2))
)
4 changes: 4 additions & 0 deletions tests/Modules/SIMD.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(module
(func $echo_v128 (param v128) (result v128) local.get 0)
(export "$echo_v128" (func $echo_v128))
)
3 changes: 3 additions & 0 deletions tests/Modules/SharedMemory.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(module
(memory 1 1 shared)
)

0 comments on commit 55b687b

Please sign in to comment.