Skip to content

Commit

Permalink
Added basic tests for PrimitiveLoader
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <git@stefan-marr.de>
  • Loading branch information
smarr committed Aug 17, 2017
1 parent b685e5c commit 98b3076
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
41 changes: 41 additions & 0 deletions tests/bd/primitives/PrimitiveTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package bd.primitives;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.Test;

import bd.testsetup.AddNodeFactory;
import bd.testsetup.ExprNode;


public class PrimitiveTests {

private final Primitives ps = new Primitives(null);

@Test
public void testPrimitiveAnnotation() {
Primitive[] annotations = ps.getPrimitiveAnnotation(AddNodeFactory.getInstance());
Primitive p = annotations[0];

assertEquals("Int", p.className());
assertEquals("+", p.primitive());
assertEquals(1, annotations.length);
}

@Test
public void testEagerSpecializer() {
Specializer<Void, ExprNode, String> s = ps.getEagerSpecializer("+", null, null);
assertNotNull(s);

assertEquals("AddNodeFactory", s.getName());
}

@Test
public void testParserSpecializer() {
Specializer<Void, ExprNode, String> s = ps.getParserSpecializer("+", null);
assertNotNull(s);

assertEquals("AddNodeFactory", s.getName());
}
}
33 changes: 33 additions & 0 deletions tests/bd/primitives/Primitives.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package bd.primitives;

import java.util.ArrayList;
import java.util.List;

import com.oracle.truffle.api.dsl.NodeFactory;

import bd.testsetup.AddNodeFactory;
import bd.testsetup.ExprNode;


public class Primitives extends PrimitiveLoader<Void, ExprNode, String> {
protected Primitives(final Void context) {
super(context);
initialize();
}

@Override
protected List<NodeFactory<? extends ExprNode>> getFactories() {
List<NodeFactory<? extends ExprNode>> allFactories = new ArrayList<>();
allFactories.add(AddNodeFactory.getInstance());
return allFactories;
}

@Override
protected void registerPrimitive(final Primitive prim,
final Specializer<Void, ExprNode, String> specializer) {}

@Override
protected String getId(final String id) {
return id.intern();
}
}
5 changes: 5 additions & 0 deletions tests/bd/testsetup/AddNode.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package bd.testsetup;

import com.oracle.truffle.api.dsl.GenerateNodeFactory;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.Specialization;

import bd.primitives.Primitive;


@NodeChild(value = "left", type = ExprNode.class)
@NodeChild(value = "right", type = ExprNode.class)
@Primitive(className = "Int", primitive = "+", selector = "+")
@GenerateNodeFactory
public abstract class AddNode extends ExprNode {

@Specialization
Expand Down
2 changes: 1 addition & 1 deletion tests/bd/testsetup/TestAdd.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TestAdd {

@Test
public void testLiteralIntegerAddition() throws UnexpectedResultException {
AddNode node = AddNodeGen.create(new IntLiteral(2), new IntLiteral(40));
AddNode node = AddNodeFactory.create(new IntLiteral(2), new IntLiteral(40));
assertEquals(42, node.executeInt(null));
}
}

0 comments on commit 98b3076

Please sign in to comment.