Skip to content

Commit

Permalink
Add test for function comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
ngallagher committed Aug 5, 2017
1 parent df35b15 commit f985277
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.snapscript.compile;

public class ArrayElementCoercionTest extends ScriptTestCase {

private static final String SOURCE =
"var s : String[][] = [['a','b'],[]];\n"+
"var i : Integer[][] = [['1','2', 3.0d, 11L],[2]];\n"+
"var b : Byte[] = ['99','100', 111.0d, 120L];\n"+
"println(s);\n"+
"println(i);\n"+
"println(b);\n"+
"var buffer = new ByteArrayOutputStream();\n"+
"buffer.write(b);\n"+
"println(buffer);\n";

public void testElementConversion() throws Exception {
execute(SOURCE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package org.snapscript.compile;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import junit.framework.TestCase;

import org.snapscript.common.store.ClassPathStore;
import org.snapscript.core.convert.ConstraintMatcher;
import org.snapscript.core.convert.FunctionComparator;
import org.snapscript.core.convert.Score;
import org.snapscript.core.function.Function;

public class FunctionComparatorTest extends TestCase {

private static final String SOURCE_1 =
"import org.snapscript.compile.FunctionComparatorTest.X;\n"+
"class ExtendX extends X {\n"+
" override func(array: Byte[], off: Integer, length: Integer){}\n"+
"}\n"+
"println('type='+ExtendX.class);";

private static final String SOURCE_2 =
"import org.snapscript.compile.FunctionComparatorTest.Y;\n"+
"import java.awt.Graphics;\n"+
"class ExtendY extends Y {\n"+
" override func(g){}\n"+
"}\n"+
"println('type='+ExtendY.class);";

private static final String SOURCE_3 =
"import org.snapscript.compile.FunctionComparatorTest.Z;\n"+
"class ExtendZ extends Z {\n"+
" override func(s, i: Integer){}\n"+
"}\n"+
"println('type='+ExtendZ.class);";

public static class X {
public void func(byte[] array, int off, int length) {}
}

public static class Y {
public void func(Graphics g) {}
}

public static class Z {
public void func(String string, Integer value) {}
}

public void testPrimitiveTypeFunctions() throws Exception {
ClassPathStore store = new ClassPathStore();
StoreContext context = new StoreContext(store);
ConstraintMatcher matcher = context.getMatcher();
FunctionComparator comparator = new FunctionComparator(matcher);
Compiler compiler = new StringCompiler(context);

compiler.compile(SOURCE_1).execute();

Function extendX = context.getLoader().resolveType("default.ExtendX").getFunctions().get(0);
Function x = context.getLoader().resolveType("org.snapscript.compile.FunctionComparatorTest$X").getFunctions().get(0);

Score score = comparator.compare(extendX, x);
Score scoreExtendX = extendX.getSignature().getConverter().score(new byte[10], 1, 1);
Score scoreX = extendX.getSignature().getConverter().score(new byte[10], 1, 1);

assertEquals(scoreExtendX.getScore(), scoreX.getScore());

System.err.println(extendX);
System.err.println(x);
System.err.println(score);
System.err.println(scoreExtendX);
System.err.println(scoreX);
}


public void testSingleArgument() throws Exception {
ClassPathStore store = new ClassPathStore();
StoreContext context = new StoreContext(store);
ConstraintMatcher matcher = context.getMatcher();
FunctionComparator comparator = new FunctionComparator(matcher);
Compiler compiler = new StringCompiler(context);

compiler.compile(SOURCE_2).execute();

Function extendY = context.getLoader().resolveType("default.ExtendY").getFunctions().get(0);
Function y = context.getLoader().resolveType("org.snapscript.compile.FunctionComparatorTest$Y").getFunctions().get(0);

BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();

Score score = comparator.compare(extendY, y);
Score scoreExtendY = extendY.getSignature().getConverter().score(graphics);
Score scoreY = extendY.getSignature().getConverter().score(graphics);

assertEquals(scoreExtendY.getScore(), scoreY.getScore());

System.err.println(extendY);
System.err.println(y);
System.err.println(score);
System.err.println(scoreExtendY);
System.err.println(scoreY);
}

public void testMultipleArgument() throws Exception {
ClassPathStore store = new ClassPathStore();
StoreContext context = new StoreContext(store);
ConstraintMatcher matcher = context.getMatcher();
FunctionComparator comparator = new FunctionComparator(matcher);
Compiler compiler = new StringCompiler(context);

compiler.compile(SOURCE_3).execute();

Function extendZ = context.getLoader().resolveType("default.ExtendZ").getFunctions().get(0);
Function z = context.getLoader().resolveType("org.snapscript.compile.FunctionComparatorTest$Z").getFunctions().get(0);

Score score = comparator.compare(extendZ, z);
Score scoreExtendZ = extendZ.getSignature().getConverter().score("text", 1);
Score scoreZ = extendZ.getSignature().getConverter().score("text", 1);

assertEquals(scoreExtendZ.getScore(), scoreZ.getScore());

System.err.println(extendZ);
System.err.println(z);
System.err.println(score);
System.err.println(scoreExtendZ);
System.err.println(scoreZ);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.snapscript.compile;

import junit.framework.TestCase;

public abstract class ScriptTestCase extends TestCase {

protected void execute(String source) throws Exception {
System.err.println(source);
Compiler compiler = ClassPathCompilerBuilder.createCompiler();
Executable executable = compiler.compile(source);
executable.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ protected Object convert(Class type, String text) throws Exception {
Class actual = promoter.promote(type);

try {
if(actual == type) {
return text;
}
if(actual == String.class) {
return text;
}
Expand Down Expand Up @@ -74,9 +71,10 @@ protected Object convert(Class type, String text) throws Exception {

protected Object convert(Class type, Number number) {
Class actual = promoter.promote(type);
Class real = number.getClass();

try {
if(actual == type) {
if(actual == real) {
return number;
}
if(actual == Number.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public Score(double score, boolean cache) {
this.cache = cache;
}

public double getScore() {
return score;
}

public boolean isFinal() {
return cache;
}
Expand Down

0 comments on commit f985277

Please sign in to comment.