package de.bmoth.typechecker;

import org.junit.Test;

import java.util.Map;

import static de.bmoth.typechecker.TestTypechecker.getFormulaTypes;
import static org.junit.Assert.assertEquals;

public class RelationsTest {
    private static final String INTEGER = "INTEGER";
    private static final String POW_INTEGER = "POW(INTEGER)";
    private static final String POW_INTEGER_X_INTEGER = "POW(INTEGER*INTEGER)";

    @Test
    public void testFunctionCall() {
        String formula = "[4,5,6](2) = a";
        Map<String, String> formulaTypes = getFormulaTypes(formula);
        assertEquals("INTEGER", formulaTypes.get("a"));
    }

    @Test
    public void testDirectProduct() {
        String formula = "a = {1 |-> TRUE} >< {b |-> 1}";
        Map<String, String> formulaTypes = getFormulaTypes(formula);
        assertEquals("POW(INTEGER*(BOOL*INTEGER))", formulaTypes.get("a"));
        assertEquals(INTEGER, formulaTypes.get("b"));
    }

    @Test
    public void testOverwriteRelation() {
        String formula = "k = {x|->2} <+ {1|->y}";
        Map<String, String> formulaTypes = getFormulaTypes(formula);
        assertEquals(POW_INTEGER_X_INTEGER, formulaTypes.get("k"));
        assertEquals(INTEGER, formulaTypes.get("x"));
        assertEquals(INTEGER, formulaTypes.get("y"));
    }

    @Test
    public void testDomainRestriction() {
        String formula = "k = x <| {1|->1}";
        Map<String, String> formulaTypes = getFormulaTypes(formula);
        assertEquals(POW_INTEGER_X_INTEGER, formulaTypes.get("k"));
        assertEquals(POW_INTEGER, formulaTypes.get("x"));
    }

    @Test
    public void testDomainSubstraction() {
        String formula = "k = x <<| {1|->1}";
        Map<String, String> formulaTypes = getFormulaTypes(formula);
        assertEquals(POW_INTEGER_X_INTEGER, formulaTypes.get("k"));
        assertEquals(POW_INTEGER, formulaTypes.get("x"));
    }

    @Test
    public void testRangeRestriction() {
        String formula = "k = {1|->1} |> x";
        Map<String, String> formulaTypes = getFormulaTypes(formula);
        assertEquals(POW_INTEGER_X_INTEGER, formulaTypes.get("k"));
        assertEquals(POW_INTEGER, formulaTypes.get("x"));
    }

    @Test
    public void testRangeSubstraction() {
        String formula = "k = {1|->1} |>> x";
        Map<String, String> formulaTypes = getFormulaTypes(formula);
        assertEquals(POW_INTEGER_X_INTEGER, formulaTypes.get("k"));
        assertEquals(POW_INTEGER, formulaTypes.get("x"));
    }

    @Test
    public void testInverseRelation() {
        String formula = "k = {1|->TRUE}~";
        Map<String, String> formulaTypes = getFormulaTypes(formula);
        assertEquals("POW(BOOL*INTEGER)", formulaTypes.get("k"));
    }

}

@@ -139,16 +139,103 @@
 aTypes;%0A
+import static de.bmoth.typechecker.TestTypechecker.typeCheckFormulaAndGetErrorMessage;%0A
 import s
@@ -2820,10 +2820,742 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testMultOrCart() %7B%0A        String formula = %22a * b = c & c = d * e & e = 1%22;%0A        Map%3CString, String%3E formulaTypes = getFormulaTypes(formula);%0A        assertEquals(%22INTEGER%22, formulaTypes.get(%22a%22));%0A    %7D%0A%0A    @Test%0A    public void testMultOrCartAndMinus() %7B%0A        String formula = %22a * b = c & c = d - e & e = 1%22;%0A        Map%3CString, String%3E formulaTypes = getFormulaTypes(formula);%0A        assertEquals(%22INTEGER%22, formulaTypes.get(%22a%22));%0A    %7D%0A%0A    @Test%0A    public void testMultOrCartAndSet() %7B%0A        String formula = %22a * b = c & %7B%7D /= c  & c = %7B1%7C-%3ETRUE%7D%22;%0A        Map%3CString, String%3E formulaTypes = getFormulaTypes(formula);%0A        assertEquals(%22POW(INTEGER)%22, formulaTypes.get(%22a%22));%0A    %7D%0A%0A
 %7D%0A

increase code coverage