269 lines
6.8 KiB
Java
Raw Normal View History

2018-11-11 11:00:52 +01:00
/*
* Copyright 2018 - 2019 Volker Berlin (i-net software)
2018-11-11 11:00:52 +01:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.inetsoftware.jwebassembly.module;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.HashMap;
import javax.annotation.Nullable;
import org.junit.Test;
import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.binary.BinaryModuleWriter;
2018-11-11 11:00:52 +01:00
import de.inetsoftware.jwebassembly.text.TextModuleWriter;
import de.inetsoftware.jwebassembly.watparser.WatParser;
/**
* @author Volker Berlin
*/
public class WatParserTest {
private void test( String wat ) throws IOException {
WatParser parser = new WatParser();
parser.parse( wat, 100 );
WasmCodeBuilder codeBuilder = parser;
StringBuilder builder = new StringBuilder();
2019-06-30 14:43:45 +02:00
ModuleWriter writer = new TextModuleWriter( new WasmTarget( builder ), new HashMap<>() );
2019-06-09 23:25:28 +02:00
writer.writeMethodStart( new FunctionName( "A.a()V" ), null );
2018-11-11 11:00:52 +01:00
for( WasmInstruction instruction : codeBuilder.getInstructions() ) {
instruction.writeTo( writer );
}
writer.writeMethodFinish();
writer.close();
2019-06-09 23:25:28 +02:00
String expected = normalize( "(module (func $A.a " + wat + " ) )" );
2018-11-11 11:00:52 +01:00
String actual = normalize( builder );
assertEquals( expected, actual );
// smoke test of the binary writer
writer = new BinaryModuleWriter( new WasmTarget( builder ), new HashMap<>() );
writer.writeMethodStart( new FunctionName( "A.a()V" ), null );
for( WasmInstruction instruction : codeBuilder.getInstructions() ) {
instruction.writeTo( writer );
}
2018-11-11 11:00:52 +01:00
}
private String normalize( @Nullable CharSequence str ) {
boolean wasSpace = false;
StringBuilder builder = new StringBuilder();
for( int i = 0; i < str.length(); i++ ) {
char ch = str.charAt( i );
switch( ch ) {
case ' ':
case '\n':
case '\r':
case '\t':
if( !wasSpace ) {
builder.append( ' ' );
wasSpace = true;
}
break;
default:
builder.append( ch );
wasSpace = false;
}
}
return builder.toString();
}
private void testError( String wat, String errorMessage ) throws IOException {
try {
test( wat );
fail( "Exception expected with message: " + errorMessage );
} catch( WasmException ex ) {
String error = ex.getMessage();
int newlineIdx = error.indexOf( '\n' );
if( newlineIdx > 0 ) {
error = error.substring( 0, newlineIdx );
}
assertEquals( errorMessage, error );
}
}
@Test
public void Local_get() throws IOException {
test( "local.get 1" );
2018-11-11 11:00:52 +01:00
}
@Test
public void Local_set() throws IOException {
test( "local.set 2" );
2018-11-11 11:00:52 +01:00
}
@Test
public void i32_add() throws IOException {
test( "i32.add" );
}
@Test
public void i32_const() throws IOException {
test( " i32.const -7 " );
}
@Test
public void i32_reinterpret_f32() throws IOException {
test( "i32.reinterpret_f32" );
}
@Test
public void i32_trunc_sat_f32_s() throws IOException {
test( "i32.trunc_sat_f32_s" );
}
@Test
public void i64_extend_i32_s() throws IOException {
test( "i64.extend_i32_s" );
}
@Test
public void i64_reinterpret_f64() throws IOException {
test( "i64.reinterpret_f64" );
}
@Test
public void i64_trunc_sat_f64_s() throws IOException {
test( "i64.trunc_sat_f64_s" );
}
@Test
public void f32_ceil() throws IOException {
test( "f32.ceil" );
}
@Test
public void f32_convert_i32_s() throws IOException {
test( "f32.convert_i32_s" );
}
@Test
public void f32_div() throws IOException {
test( "f32.div" );
}
@Test
public void f32_floor() throws IOException {
test( "f32.floor" );
}
2018-11-16 20:27:19 +01:00
@Test
public void f32_max() throws IOException {
test( "f32.max" );
}
@Test
public void f32_mul() throws IOException {
test( "f32.mul" );
}
@Test
public void f32_nearest() throws IOException {
test( "f32.nearest" );
}
@Test
public void f32_reinterpret_i32() throws IOException {
test( "f32.reinterpret_i32" );
}
@Test
public void f32_sqrt() throws IOException {
test( "f32.sqrt" );
}
@Test
public void f32_sub() throws IOException {
test( "f32.sub" );
}
@Test
public void f32_trunc() throws IOException {
test( "f32.trunc" );
}
@Test
public void f64_ceil() throws IOException {
test( "f64.ceil" );
}
@Test
public void f64_convert_i64_s() throws IOException {
test( "f64.convert_i64_s" );
}
@Test
public void f64_div() throws IOException {
test( "f64.div" );
}
@Test
public void f64_floor() throws IOException {
test( "f64.floor" );
}
@Test
public void f64_nearest() throws IOException {
test( "f64.nearest" );
}
2018-11-16 20:27:19 +01:00
@Test
public void f64_max() throws IOException {
test( "f64.max" );
}
2018-11-11 11:00:52 +01:00
@Test
public void f64_mul() throws IOException {
test( "f64.mul" );
2018-11-11 11:00:52 +01:00
}
@Test
public void f64_reinterpret_i64() throws IOException {
test( "f64.reinterpret_i64" );
}
@Test
public void f64_sqrt() throws IOException {
test( "f64.sqrt" );
}
2018-11-11 11:00:52 +01:00
@Test
public void f64_sub() throws IOException {
test( "f64.sub" );
2018-11-11 11:00:52 +01:00
}
@Test
public void f64_trunc() throws IOException {
test( "f64.trunc" );
}
2018-11-11 11:00:52 +01:00
@Test
public void return_() throws IOException {
test( "return\n" );
}
@Test
public void errorMissingToken() throws IOException {
testError( "i32.const", "Missing Token in wasm text format after token: i32.const" );
}
@Test
public void errorUnknownToken() throws IOException {
testError( "foobar", "Unknown WASM token: foobar" );
}
2018-11-11 11:00:52 +01:00
}