More type parser tests

This commit is contained in:
Arne Keller 2021-02-13 10:30:34 +01:00
parent c6eb794049
commit 7aacca6941
2 changed files with 39 additions and 12 deletions

View File

@ -56,9 +56,8 @@ public class TypeAssumptionParser { // TODO: document type syntax? or refer to o
return type2; return type2;
} }
type = type2.unwrap().getLeft(); type = type2.unwrap().getLeft();
parenCount -= type2.unwrap().getRight() - 1;
removedParens += type2.unwrap().getRight() - 1; removedParens += type2.unwrap().getRight() - 1;
if (parenCount < 0) { if (parenCount - removedParens < 0) {
return new Result<>(new ImmutablePair<>(type, removedParens)); return new Result<>(new ImmutablePair<>(type, removedParens));
} }
break; break;
@ -81,9 +80,8 @@ public class TypeAssumptionParser { // TODO: document type syntax? or refer to o
} }
t = token.unwrap(); t = token.unwrap();
if (t.getType() == TokenType.RIGHT_PARENTHESIS) { if (t.getType() == TokenType.RIGHT_PARENTHESIS) {
parenCount -= 1;
removedParens += 1; removedParens += 1;
if (parenCount <= 0) { if (parenCount - removedParens <= 0) {
return new Result<>(new ImmutablePair<>(type, removedParens)); return new Result<>(new ImmutablePair<>(type, removedParens));
} }
continue; continue;
@ -100,8 +98,7 @@ public class TypeAssumptionParser { // TODO: document type syntax? or refer to o
} }
type = new FunctionType(type, nextType.unwrap().getLeft()); type = new FunctionType(type, nextType.unwrap().getLeft());
removedParens += nextType.unwrap().getRight(); removedParens += nextType.unwrap().getRight();
parenCount -= nextType.unwrap().getRight(); if (parenCount - removedParens <= 0) {
if (parenCount <= 0) {
return new Result<>(new ImmutablePair<>(type, removedParens)); return new Result<>(new ImmutablePair<>(type, removedParens));
} }
} }

View File

@ -5,6 +5,7 @@ import edu.kit.typicalc.model.type.*;
import edu.kit.typicalc.util.Result; import edu.kit.typicalc.util.Result;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -164,10 +165,10 @@ class TypeAssumptionParserTest {
assertEquals(new VarTerm("id"), assumption.getKey()); assertEquals(new VarTerm("id"), assumption.getKey());
assertEquals(new TypeAbstraction( assertEquals(new TypeAbstraction(
new FunctionType( new FunctionType(
new FunctionType( new FunctionType(
new FunctionType(INT, INT), new FunctionType(INT, INT),
new FunctionType(BOOLEAN, BOOLEAN) new FunctionType(BOOLEAN, BOOLEAN)
), ),
new FunctionType( new FunctionType(
new FunctionType(int2, boolean2), new FunctionType(int2, boolean2),
new FunctionType(boolean2, int2) new FunctionType(boolean2, int2)
@ -188,8 +189,37 @@ class TypeAssumptionParserTest {
assertEquals(new VarTerm("fun"), assumption.getKey()); assertEquals(new VarTerm("fun"), assumption.getKey());
assertEquals(new TypeAbstraction( assertEquals(new TypeAbstraction(
new FunctionType( new FunctionType(
new FunctionType(new NamedType("a"), new FunctionType(new NamedType("b"), new NamedType("c"))), new FunctionType(new NamedType("a"), new FunctionType(new NamedType("b"), new NamedType("c"))),
new NamedType("d") new NamedType("d")
)), assumption.getValue()); )), assumption.getValue());
} }
@Test
void variousTypes() {
Type x = new NamedType("x");
Type xx = new FunctionType(x, x);
Type xxx = new FunctionType(x, xx);
Type xxxx = new FunctionType(x, xxx);
Type xxxxx = new FunctionType(xx, xxx);
Type xxxxxx = new FunctionType(xxx, xxx);
Map<String, TypeAbstraction> tests = new HashMap<>();
tests.put("x", new TypeAbstraction(x));
tests.put("x -> x", new TypeAbstraction(xx));
tests.put("x -> (x)", new TypeAbstraction(xx));
tests.put("x -> ((x))", new TypeAbstraction(xx));
tests.put("x -> x -> x", new TypeAbstraction(xxx));
tests.put("x -> (x -> x)", new TypeAbstraction(xxx));
tests.put("x -> x -> x -> x", new TypeAbstraction(xxxx));
tests.put("x -> (x -> x -> x)", new TypeAbstraction(xxxx));
tests.put("x -> (x -> (x -> x))", new TypeAbstraction(xxxx));
tests.put("x -> (x -> (x -> (x)))", new TypeAbstraction(xxxx));
tests.put("(x -> x) -> (x -> (x -> (x)))", new TypeAbstraction(xxxxx));
tests.put("((x) -> ((x)) -> (x)) -> (x -> (x -> (x)))", new TypeAbstraction(xxxxxx));
for (Map.Entry<String, TypeAbstraction> entry : tests.entrySet()) {
TypeAssumptionParser parser = new TypeAssumptionParser();
Result<Map<VarTerm, TypeAbstraction>, ParseError> type = parser.parse(Map.of("type1", entry.getKey()));
assertTrue(type.isOk());
assertEquals(entry.getValue(), type.unwrap().get(new VarTerm("type1")));
}
}
} }