Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- <!--//# BEGIN TODO Name, student id, and date-->
- <p><font color="red"><b>Sergio van Passel, s130082, 0867048, 02/03/2015</b></font></p>
- <!--//# END TODO-->
- */
- // -----8<----- cut line -----8<-----
- import static org.junit.Assert.*;
- import org.junit.Test;
- public abstract class IntRelationTestCases {
- /** Test instance */
- protected IntRelation instance;
- /**
- * Sets instance to a newly constructed relation of given extent.
- * (This is a kind of factory method; cf. Factory Method Design Pattern.)
- *
- * @param n the given extent
- * @pre {@code 0 <= n}
- * @modifies {@code instance}
- * @post {@code instance.extent() == n && AF(instance) = [ ]}
- */
- public abstract void setInstance(final int n);
- /** Tests the constructor with small extent values. */
- @Test
- public void testConstructor() {
- System.out.println("constructor(int)");
- for (int n = 0; n <= 3; ++ n) {
- setInstance(n);
- assertTrue("isRepOk()", instance.isRepOk());
- }
- }
- /** Tests the constructor for exceptions. */
- @Test()
- public void testConstructorFailure() {
- System.out.println("constructor(illegal int)");
- try{
- setInstance(-1);
- fail("Did not reject -1");
- }catch(IllegalArgumentException e){
- assertTrue("type: " + e.getClass().getName()
- + " should be instance of " + IllegalArgumentException.class,
- IllegalArgumentException.class.isInstance(e));
- }
- }
- /** Tests the extent method with small relations. */
- @Test
- public void testExtent() {
- System.out.println("extent");
- for (int n = 0; n <= 3; ++ n) {
- setInstance(n);
- assertEquals("size", n, instance.extent());
- assertTrue("isRepOk()", instance.isRepOk());
- }
- }
- /**
- * Invokes areRelated(a, b) and checks the result.
- *
- * @param a first element in pair
- * @param b second element in pair
- * @param expResult expected result
- */
- private void checkAreRelated(int a, int b, boolean expResult) {
- boolean result = instance.areRelated(a, b);
- assertEquals("areRelated(" + a + ", " + b + ")", expResult, result);
- assertTrue("isRepOk()", instance.isRepOk());
- }
- /**
- * Invokes areRelated(a, b) and checks for an exception.
- *
- * @param a first element in pair
- * @param b second element in pair
- */
- private void checkAreRelatedException(int a, int b) {
- try{
- instance.areRelated(a, b);
- fail("Did not reject taht pair");
- }catch(IllegalArgumentException e){
- assertTrue("type: " + e.getClass().getName()
- + " should be instance of " + IllegalArgumentException.class,
- IllegalArgumentException.class.isInstance(e));
- }
- }
- /** Tests the areRelated method on empty relation. */
- @Test
- public void testAreRelated() {
- System.out.println("areRelated");
- setInstance(1);
- checkAreRelated(0, 0, false);
- setInstance(2);
- checkAreRelated(0, 0, false);
- checkAreRelated(0, 1, false);
- checkAreRelated(1, 0, false);
- checkAreRelated(1, 1, false);
- }
- /** Tests the areRelated for an exception on empty relations. */
- @Test
- public void testAreRelatedException() {
- System.out.println("areRelated");
- setInstance(2);
- checkAreRelatedException(0, -1);
- checkAreRelatedException(-1, 0);
- checkAreRelatedException(3, 0);
- checkAreRelatedException(0, 3);
- checkAreRelatedException(-1, -1);
- checkAreRelatedException(-1, 3);
- checkAreRelatedException(3, 3);
- checkAreRelatedException(-1, 3);
- }
- /** Tests the add method. */
- @Test
- public void testAdd() {
- System.out.println("add");
- setInstance(2);
- instance.add(0, 1); // N.B. not a pair of equals
- assertTrue("isRepOk()", instance.isRepOk());
- checkAreRelated(0, 1, true);
- checkAreRelated(0, 0, false);
- checkAreRelated(1, 0, false);
- checkAreRelated(1, 1, false);
- instance.add(0, 1);
- assertTrue("isRepOk()", instance.isRepOk());
- checkAreRelated(0, 1, true);
- checkAreRelated(0, 0, false);
- checkAreRelated(1, 0, false);
- checkAreRelated(1, 1, false);
- }
- /** Tests the remove method. */
- @Test
- public void testRemove() {
- System.out.println("remove");
- setInstance(2);
- instance.remove(0, 1); // N.B. not a pair of equals
- assertTrue("isRepOk()", instance.isRepOk());
- checkAreRelated(0, 0, false);
- checkAreRelated(0, 1, false);
- checkAreRelated(1, 0, false);
- checkAreRelated(1, 1, false);
- instance.add(0, 1);
- assertTrue("isRepOk()", instance.isRepOk());
- checkAreRelated(0, 1, true);
- instance.remove(0, 1);
- assertTrue("isRepOk()", instance.isRepOk());
- checkAreRelated(0, 0, false);
- checkAreRelated(0, 1, false);
- checkAreRelated(1, 0, false);
- checkAreRelated(1, 1, false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement