Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. package oo.hide;
  2.  
  3. import org.junit.Test;
  4.  
  5. import java.lang.reflect.Field;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. import java.util.stream.Collectors;
  9.  
  10. import static junit.framework.TestCase.assertTrue;
  11. import static org.hamcrest.MatcherAssert.assertThat;
  12. import static org.hamcrest.Matchers.is;
  13. import static org.hamcrest.Matchers.not;
  14. import static org.junit.Assert.assertFalse;
  15. import static org.junit.Assert.fail;
  16.  
  17. public class PointSetTests {
  18.  
  19. @Test
  20. public void pointSetKeepsTrackOfPoints() {
  21. PointSet set = new PointSet();
  22.  
  23. set.add(new Point(1, 1));
  24. set.add(new Point(2, 1));
  25. set.add(new Point(1, 2));
  26.  
  27. assertThat(set.size(), is(3));
  28.  
  29. set.add(new Point(2, 1));
  30.  
  31. assertThat(set.size(), is(3));
  32.  
  33. assertTrue(set.contains(new Point(1, 1)));
  34. assertTrue(set.contains(new Point(1, 2)));
  35. assertFalse(set.contains(new Point(1, 3)));
  36.  
  37. assertThat(set.toString(), is("(1, 1), (2, 1), (1, 2)"));
  38. }
  39.  
  40. @Test
  41. public void pointSetSupportsEqualityTesting() {
  42. assertThat(getSet(), is(getSet()));
  43.  
  44. assertThat(getSet(new Point(1, 1)), is(not(getSet())));
  45.  
  46. assertThat(getSet(new Point(1, 1)),
  47. is(not(getSet(new Point(1, 2)))));
  48.  
  49. assertThat(getSet(new Point(1, 1), new Point(1, 2)),
  50. is(getSet(new Point(1, 2), new Point(1, 1))));
  51. }
  52.  
  53. @Test
  54. public void pointSetSupportsSubtractingAnotherSet() {
  55. PointSet a = getSet(new Point(1, 1), new Point(1, 2));
  56. PointSet b = getSet(new Point(1, 1), new Point(1, 3));
  57.  
  58. PointSet remainder = a.subtract(b);
  59.  
  60. assertThat(a, is(getSet(new Point(1, 1), new Point(1, 2))));
  61.  
  62. assertThat(remainder, is(getSet(new Point(1, 2))));
  63. }
  64.  
  65. @Test
  66. public void pointSetSupportsIntersectionOperation() {
  67. PointSet a = getSet(new Point(1, 1), new Point(1, 2));
  68. PointSet b = getSet(new Point(1, 1), new Point(1, 3));
  69.  
  70. PointSet intersection = a.intersect(b);
  71.  
  72. assertThat(a, is(getSet(new Point(1, 1), new Point(1, 2))));
  73.  
  74. assertThat(intersection, is(getSet(new Point(1, 1))));
  75. }
  76.  
  77. @Test
  78. public void setGrowsWhenThereIsNoMoreRoom() {
  79. PointSet set = new PointSet(2);
  80.  
  81. set.add(new Point(1, 1));
  82. set.add(new Point(2, 1));
  83.  
  84. assertThat(getInternalArray(set).length, is(2));
  85.  
  86. set.add(new Point(3, 1));
  87.  
  88. assertThat(getInternalArray(set).length, is(4));
  89. }
  90.  
  91. private PointSet getSet(Point... points) {
  92. PointSet set = new PointSet();
  93. for (Point point : points) {
  94. set.add(point);
  95. }
  96.  
  97. return set;
  98. }
  99.  
  100. private Point[] getInternalArray(PointSet set) {
  101.  
  102. Field[] fields = set.getClass().getDeclaredFields();
  103.  
  104. List<Field> integerArrayFields = Arrays.asList(fields)
  105. .stream()
  106. .filter(field -> field.getType().equals(Point[].class))
  107. .collect(Collectors.toList());
  108.  
  109. if (integerArrayFields.isEmpty()) {
  110. fail("PointSet should have a field of type Point[]");
  111. }
  112.  
  113. if (integerArrayFields.size() > 1) {
  114. fail("PointSet should have just one field of type Point[]");
  115. }
  116.  
  117. integerArrayFields.stream()
  118. .forEach(field -> field.setAccessible(true));
  119.  
  120. try {
  121. return (Point[]) integerArrayFields.get(0).get(set);
  122. } catch (IllegalAccessException e) {
  123. throw new RuntimeException(e);
  124. }
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement