/*
 * Copyright (C) 2013 Matthew Dawson <matthew@mjdsystems.ca>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ca.mcmaster.cas.matlab2smt;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.Parameter;

import java.util.Arrays;
import java.util.Collection;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

/** This test simply tests that each operator parses correctly.
 *
 * This is a simply parametrized test set that only deals with the various operators MatlabParser deals with.  It avoids
 * all the other details (it just uses literals for input).  It also avoids any dependency on generators.
 *
 * @author Matthew Dawson <matthew@mjdsystems.ca>
 */
@RunWith(Parameterized.class)
public class MatlabParserOperatorParseTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {"+", MatlabBinaryOperation.Addition, null},
                {"-", MatlabBinaryOperation.Minus, null},
                {"*", MatlabBinaryOperation.Multiplication, null},
                {"/", MatlabBinaryOperation.Division, null},
                {">", MatlabBinaryOperation.GreaterThen, null},
                {">=", MatlabBinaryOperation.GreaterThenEqual, null},
                {"<", MatlabBinaryOperation.LessThen, null},
                {"<=", MatlabBinaryOperation.LessThenEqual, null},
                {"==", MatlabBinaryOperation.Equals, null},
                {"&&", MatlabBinaryOperation.BooleanAnd, null},
                {"||", MatlabBinaryOperation.BooleanOr, null},
                {"~", null, MatlabUnaryOperation.Negation},
                {"-", null, MatlabUnaryOperation.Minus}
        });
    }
    @Parameter
    public String inputSymbol;

    @Parameter(value = 1)
    public MatlabBinaryOperation bop;

    @Parameter(value = 2)
    public MatlabUnaryOperation uop;

    @Test
    public void doTest() {
        if (bop != null) {
            // Binary op to test ...
            MatlabParser parser = new MatlabParser(null, "1" + inputSymbol + "2\n");
            MatlabExpressionBinaryOperation exp = (MatlabExpressionBinaryOperation)parser.getRootExpression();

            assertThat(exp.m_op, is(bop));
            assertThat(((MatlabLiteral)exp.m_lhs).m_value, is("1"));
            assertThat(((MatlabLiteral)exp.m_rhs).m_value, is("2"));
        } else {
            // Unary op to test ...
            MatlabParser parser = new MatlabParser(null, inputSymbol + "3\n");
            MatlabExpressionUnaryOperation exp = (MatlabExpressionUnaryOperation)parser.getRootExpression();

            assertThat(exp.m_op, is(uop));
            assertThat(((MatlabLiteral)exp.m_expr).m_value, is("3"));
        }
    }
}
