Advertisement
Sergiovan

Untitled

Mar 3rd, 2015
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. /**
  2. <!--//# BEGIN TODO Name, student id, and date-->
  3. <p><font color="red"><b>Sergio van Passel, s130082, 0867048, 02/03/2015</b></font></p>
  4. <!--//# END TODO-->
  5. */
  6. // -----8<----- cut line -----8<-----
  7.  
  8. import static org.junit.Assert.*;
  9. import org.junit.Test;
  10.  
  11. public abstract class IntRelationTestCases {
  12.  
  13. /** Test instance */
  14. protected IntRelation instance;
  15.  
  16. /**
  17. * Sets instance to a newly constructed relation of given extent.
  18. * (This is a kind of factory method; cf. Factory Method Design Pattern.)
  19. *
  20. * @param n the given extent
  21. * @pre {@code 0 <= n}
  22. * @modifies {@code instance}
  23. * @post {@code instance.extent() == n && AF(instance) = [ ]}
  24. */
  25. public abstract void setInstance(final int n);
  26.  
  27. /** Tests the constructor with small extent values. */
  28. @Test
  29. public void testConstructor() {
  30. System.out.println("constructor(int)");
  31. for (int n = 0; n <= 3; ++ n) {
  32. setInstance(n);
  33. assertTrue("isRepOk()", instance.isRepOk());
  34. }
  35. }
  36.  
  37. /** Tests the constructor for exceptions. */
  38. @Test()
  39. public void testConstructorFailure() {
  40. System.out.println("constructor(illegal int)");
  41. try{
  42. setInstance(-1);
  43. fail("Did not reject -1");
  44. }catch(IllegalArgumentException e){
  45. assertTrue("type: " + e.getClass().getName()
  46. + " should be instance of " + IllegalArgumentException.class,
  47. IllegalArgumentException.class.isInstance(e));
  48. }
  49. }
  50.  
  51. /** Tests the extent method with small relations. */
  52. @Test
  53. public void testExtent() {
  54. System.out.println("extent");
  55. for (int n = 0; n <= 3; ++ n) {
  56. setInstance(n);
  57. assertEquals("size", n, instance.extent());
  58. assertTrue("isRepOk()", instance.isRepOk());
  59. }
  60. }
  61.  
  62. /**
  63. * Invokes areRelated(a, b) and checks the result.
  64. *
  65. * @param a first element in pair
  66. * @param b second element in pair
  67. * @param expResult expected result
  68. */
  69. private void checkAreRelated(int a, int b, boolean expResult) {
  70. boolean result = instance.areRelated(a, b);
  71. assertEquals("areRelated(" + a + ", " + b + ")", expResult, result);
  72. assertTrue("isRepOk()", instance.isRepOk());
  73. }
  74.  
  75. /**
  76. * Invokes areRelated(a, b) and checks for an exception.
  77. *
  78. * @param a first element in pair
  79. * @param b second element in pair
  80. */
  81. private void checkAreRelatedException(int a, int b) {
  82. try{
  83. instance.areRelated(a, b);
  84. fail("Did not reject taht pair");
  85. }catch(IllegalArgumentException e){
  86. assertTrue("type: " + e.getClass().getName()
  87. + " should be instance of " + IllegalArgumentException.class,
  88. IllegalArgumentException.class.isInstance(e));
  89. }
  90. }
  91.  
  92. /** Tests the areRelated method on empty relation. */
  93. @Test
  94. public void testAreRelated() {
  95. System.out.println("areRelated");
  96. setInstance(1);
  97. checkAreRelated(0, 0, false);
  98. setInstance(2);
  99. checkAreRelated(0, 0, false);
  100. checkAreRelated(0, 1, false);
  101. checkAreRelated(1, 0, false);
  102. checkAreRelated(1, 1, false);
  103. }
  104.  
  105. /** Tests the areRelated for an exception on empty relations. */
  106. @Test
  107. public void testAreRelatedException() {
  108. System.out.println("areRelated");
  109. setInstance(2);
  110. checkAreRelatedException(0, -1);
  111. checkAreRelatedException(-1, 0);
  112. checkAreRelatedException(3, 0);
  113. checkAreRelatedException(0, 3);
  114. checkAreRelatedException(-1, -1);
  115. checkAreRelatedException(-1, 3);
  116. checkAreRelatedException(3, 3);
  117. checkAreRelatedException(-1, 3);
  118. }
  119.  
  120.  
  121. /** Tests the add method. */
  122. @Test
  123. public void testAdd() {
  124. System.out.println("add");
  125. setInstance(2);
  126. instance.add(0, 1); // N.B. not a pair of equals
  127. assertTrue("isRepOk()", instance.isRepOk());
  128. checkAreRelated(0, 1, true);
  129. checkAreRelated(0, 0, false);
  130. checkAreRelated(1, 0, false);
  131. checkAreRelated(1, 1, false);
  132. instance.add(0, 1);
  133. assertTrue("isRepOk()", instance.isRepOk());
  134. checkAreRelated(0, 1, true);
  135. checkAreRelated(0, 0, false);
  136. checkAreRelated(1, 0, false);
  137. checkAreRelated(1, 1, false);
  138. }
  139.  
  140. /** Tests the remove method. */
  141. @Test
  142. public void testRemove() {
  143. System.out.println("remove");
  144. setInstance(2);
  145. instance.remove(0, 1); // N.B. not a pair of equals
  146. assertTrue("isRepOk()", instance.isRepOk());
  147. checkAreRelated(0, 0, false);
  148. checkAreRelated(0, 1, false);
  149. checkAreRelated(1, 0, false);
  150. checkAreRelated(1, 1, false);
  151. instance.add(0, 1);
  152. assertTrue("isRepOk()", instance.isRepOk());
  153. checkAreRelated(0, 1, true);
  154. instance.remove(0, 1);
  155. assertTrue("isRepOk()", instance.isRepOk());
  156. checkAreRelated(0, 0, false);
  157. checkAreRelated(0, 1, false);
  158. checkAreRelated(1, 0, false);
  159. checkAreRelated(1, 1, false);
  160. }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement