package edu.neu.ccs.evergreen.model; import static junit.framework.Assert.assertEquals; import static org.junit.Assert.fail; import org.junit.Test; /** * Test the constraints returned by the factory are correct. */ public class ConstraintFactoryTest { private void tryRank3(int rank, int v_2, int v_1, int v_0) { assertEquals(rank, ConstraintFactory.createOr(0, new int[] { v_0, v_1, v_2 }, 1).getRelationNumber()); } /** * Test Or constraint of rank 3. */ @Test public void testCreateOrRankRelation3() { tryRank3(254, 3, 2, 1); tryRank3(253, 3, 2, -1); tryRank3(251, 3, -2, 1); tryRank3(247, 3, -2, -1); tryRank3(239, -3, 2, 1); tryRank3(223, -3, 2, -1); tryRank3(191, -3, -2, 1); tryRank3(127, -3, -2, -1); } private void tryRank2(int rank, int v_1, int v_0) { assertEquals(rank, ConstraintFactory.createOr(0, new int[] { v_0, v_1 }, 1).getRelationNumber()); } /** * Test Or constraint of rank 2. */ @Test public void testCreateOrRankRelation2() { tryRank2(238, 2, 1); tryRank2(221, 2, -1); tryRank2(187, -2, 1); tryRank2(119, -2, -1); } private void tryRank1(int rank, int v_0) { assertEquals(rank, ConstraintFactory.createOr(0, new int[] { v_0 }, 1) .getRelationNumber()); } /** * Test Or constraint of rank 1. */ @Test public void testCreateOrRankRelation1() { tryRank1(170, 1); tryRank1(85, -1); } private void tryImpliesRank3(int rank, int v_2, int v_1, int v_0) { assertEquals(rank, ConstraintFactory.createImplies(0, new int[] { v_0, v_1, v_2 }, 1).getRelationNumber()); } /** * Test implies constraint of rank 3. */ @Test public void testCreateImpliesRankRelation() { tryImpliesRank3(169, 3, 2, 1); tryImpliesRank3(166, 3, -2, 1); tryImpliesRank3(154, -3, 2, 1); tryImpliesRank3(106, -3, -2, 1); } /** * Test a bad variable in the constraint. */ @Test public void testBadVar() { try { new Constraint(0, 255, new int[] { 1, 2, 1 }, 1); fail("No error"); } catch (IllegalArgumentException illegalArgumentException) { } } /** * Test positions in constraint. */ @Test public void testPosition() { Constraint constraint = new Constraint(0, 255, new int[] { 1, 2, 3 }, 0); assertEquals(2, constraint.getPosition(3)); assertEquals(1, constraint.getPosition(2)); assertEquals(0, constraint.getPosition(1)); } }