Add compileToBinary() for samples

This commit is contained in:
Volker Berlin 2018-03-24 12:49:23 +01:00
parent 5c7b7256c7
commit cf71920447
3 changed files with 93 additions and 79 deletions

View File

@ -1,79 +1,93 @@
/* /*
* Copyright 2017 Volker Berlin (i-net software) * Copyright 2017 Volker Berlin (i-net software)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.inetsoftware.jwebassembly; package de.inetsoftware.jwebassembly;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.Test;
import org.junit.runners.Parameterized; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters; import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/**
* Test to compile a list of single sample files. /**
* * Test to compile a list of single sample files.
* @author Volker Berlin *
* * @author Volker Berlin
*/ *
@RunWith( Parameterized.class ) */
public class SampleCompileTest { @RunWith( Parameterized.class )
public class SampleCompileTest {
private final String testName;
private final File classFile; private final String testName;
private final File classFile;
public SampleCompileTest( String testName, File classFile ) {
this.testName = testName; public SampleCompileTest( String testName, File classFile ) {
this.classFile = classFile; this.testName = testName;
} this.classFile = classFile;
}
@Parameters( name = "{0}" )
public static List<Object[]> params() throws Exception { @Parameters( name = "{0}" )
ArrayList<Object[]> params = new ArrayList<>(); public static List<Object[]> params() throws Exception {
URL url = SampleCompileTest.class.getResource( "samples" ); ArrayList<Object[]> params = new ArrayList<>();
File dir = new File( url.toURI() ); URL url = SampleCompileTest.class.getResource( "samples" );
int baseLength = dir.getPath().length() + 1; File dir = new File( url.toURI() );
params( params, dir, baseLength ); int baseLength = dir.getPath().length() + 1;
return params; params( params, dir, baseLength );
} return params;
}
private static void params( ArrayList<Object[]> params, File dir, int baseLength ) {
for( File file : dir.listFiles() ) { private static void params( ArrayList<Object[]> params, File dir, int baseLength ) {
if( file.getName().endsWith( ".class" ) ) { for( File file : dir.listFiles() ) {
String path = file.getPath(); if( file.getName().endsWith( ".class" ) ) {
params.add( new Object[] { path.substring( baseLength, path.length() - 6 ), file } ); String path = file.getPath();
} else if( file.isDirectory() ) { params.add( new Object[] { path.substring( baseLength, path.length() - 6 ), file } );
params( params, file, baseLength ); } else if( file.isDirectory() ) {
} params( params, file, baseLength );
} }
} }
}
@Test
public void compileToText() throws Exception { @Test
URL url = SampleCompileTest.class.getResource( "samples/" + testName + ".wat" ); public void compileToText() throws Exception {
File watFile = new File( url.toURI() ); URL url = SampleCompileTest.class.getResource( "samples/" + testName + ".wat" );
String expected = new String( Files.readAllBytes( watFile.toPath() ) ); File watFile = new File( url.toURI() );
JWebAssembly webAsm = new JWebAssembly(); String expected = new String( Files.readAllBytes( watFile.toPath() ) );
webAsm.addFile( classFile ); JWebAssembly webAsm = new JWebAssembly();
String text = webAsm.compileToText(); webAsm.addFile( classFile );
assertEquals( expected, text ); String text = webAsm.compileToText();
} assertEquals( expected, text );
} }
@Test
public void compileToBinary() throws Exception {
URL url = SampleCompileTest.class.getResource( "samples/" + testName + ".wasm" );
File wasmFile = new File( url.toURI() );
byte[] expected = Files.readAllBytes( wasmFile.toPath() );
JWebAssembly webAsm = new JWebAssembly();
webAsm.addFile( classFile );
byte[] actual = webAsm.compileToBinary();
System.err.println(Arrays.toString( expected ));
System.err.println(Arrays.toString( actual ));
assertArrayEquals( expected, actual );
}
}