lots of exercises in java... from https://github.com/exercism/java

CustomSetTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import org.junit.Ignore;
  2. import org.junit.Test;
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import static org.junit.Assert.*;
  6. public class CustomSetTest {
  7. @Test
  8. public void setsWithNoElementsAreEmpty() {
  9. CustomSet<Integer> customSet = new CustomSet<>(Collections.emptyList());
  10. assertTrue(customSet.isEmpty());
  11. }
  12. @Ignore("Remove to run test")
  13. @Test
  14. public void setsWithElementsAreNotEmpty() {
  15. CustomSet<Character> customSet = new CustomSet<>(Collections.singletonList('1'));
  16. assertFalse(customSet.isEmpty());
  17. }
  18. @Ignore("Remove to run test")
  19. @Test
  20. public void nothingIsContainedInAnEmptySet() {
  21. CustomSet<String> customSet = new CustomSet<>(Collections.emptyList());
  22. assertFalse(customSet.contains("1"));
  23. }
  24. @Ignore("Remove to run test")
  25. @Test
  26. public void whenTheElementIsInTheSet() {
  27. CustomSet<Integer> customSet = new CustomSet<>(Arrays.asList(1, 2, 3));
  28. assertTrue(customSet.contains(1));
  29. }
  30. @Ignore("Remove to run test")
  31. @Test
  32. public void whenTheElementIsNotInTheSet() {
  33. CustomSet<Character> customSet = new CustomSet<>(Arrays.asList('1', '2', '3'));
  34. assertFalse(customSet.contains('4'));
  35. }
  36. @Ignore("Remove to run test")
  37. @Test
  38. public void emptySetIsASubsetOfAnotherEmptySet() {
  39. CustomSet<String> customSet = new CustomSet<>(Collections.emptyList());
  40. CustomSet<String> secondCustomSet = new CustomSet<>(Collections.emptyList());
  41. assertTrue(customSet.isSubset(secondCustomSet));
  42. }
  43. @Ignore("Remove to run test")
  44. @Test
  45. public void emptySetIsASubsetOfNonEmptySet() {
  46. CustomSet<Integer> customSet = new CustomSet<>(Collections.singletonList(1));
  47. CustomSet<Integer> secondCustomSet = new CustomSet<>(Collections.emptyList());
  48. assertTrue(customSet.isSubset(secondCustomSet));
  49. }
  50. @Ignore("Remove to run test")
  51. @Test
  52. public void nonEmptySetIsNotASubsetOfEmptySet() {
  53. CustomSet<Character> customSet = new CustomSet<>(Collections.emptyList());
  54. CustomSet<Character> secondCustomSet = new CustomSet<>(Collections.singletonList('1'));
  55. assertFalse(customSet.isSubset(secondCustomSet));
  56. }
  57. @Ignore("Remove to run test")
  58. @Test
  59. public void setIsASubsetOfSetWithExactSameElements() {
  60. CustomSet<String> customSet = new CustomSet<>(Arrays.asList("1", "2", "3"));
  61. CustomSet<String> secondCustomSet = new CustomSet<>(Arrays.asList("1", "2", "3"));
  62. assertTrue(customSet.isSubset(secondCustomSet));
  63. }
  64. @Ignore("Remove to run test")
  65. @Test
  66. public void setIsASubsetOfLargerSetWithSameElements() {
  67. CustomSet<Integer> customSet = new CustomSet<>(Arrays.asList(4, 1, 2, 3));
  68. CustomSet<Integer> secondCustomSet = new CustomSet<>(Arrays.asList(1, 2, 3));
  69. assertTrue(customSet.isSubset(secondCustomSet));
  70. }
  71. @Ignore("Remove to run test")
  72. @Test
  73. public void setIsNotASubsetOfSetThatDoesNotContainItsElements() {
  74. CustomSet<Character> customSet = new CustomSet<>(Arrays.asList('4', '1', '3'));
  75. CustomSet<Character> secondCustomSet = new CustomSet<>(Arrays.asList('1', '2', '3'));
  76. assertFalse(customSet.isSubset(secondCustomSet));
  77. }
  78. @Ignore("Remove to run test")
  79. @Test
  80. public void theEmptySetIsDisjointWithItself() {
  81. CustomSet<String> customSet = new CustomSet<>(Collections.emptyList());
  82. CustomSet<String> secondCustomSet = new CustomSet<>(Collections.emptyList());
  83. assertTrue(customSet.isDisjoint(secondCustomSet));
  84. }
  85. @Ignore("Remove to run test")
  86. @Test
  87. public void emptySetIsDisjointWithNonEmptySet() {
  88. CustomSet<Integer> customSet = new CustomSet<>(Collections.emptyList());
  89. CustomSet<Integer> secondCustomSet = new CustomSet<>(Collections.singletonList(1));
  90. assertTrue(customSet.isDisjoint(secondCustomSet));
  91. }
  92. @Ignore("Remove to run test")
  93. @Test
  94. public void nonEmptySetIsDisjointWithEmptySet() {
  95. CustomSet<Character> customSet = new CustomSet<>(Collections.singletonList('1'));
  96. CustomSet<Character> secondCustomSet = new CustomSet<>(Collections.emptyList());
  97. assertTrue(customSet.isDisjoint(secondCustomSet));
  98. }
  99. @Ignore("Remove to run test")
  100. @Test
  101. public void setsAreNotDisjointIfTheyShareAnElement() {
  102. CustomSet<String> customSet = new CustomSet<>(Arrays.asList("1", "2"));
  103. CustomSet<String> secondCustomSet = new CustomSet<>(Arrays.asList("2", "3"));
  104. assertFalse(customSet.isDisjoint(secondCustomSet));
  105. }
  106. @Ignore("Remove to run test")
  107. @Test
  108. public void setsAreDisjointIfTheyShareNoElements() {
  109. CustomSet<Integer> customSet = new CustomSet<>(Arrays.asList(1, 2));
  110. CustomSet<Integer> secondCustomSet = new CustomSet<>(Arrays.asList(3, 4));
  111. assertTrue(customSet.isDisjoint(secondCustomSet));
  112. }
  113. @Ignore("Remove to run test")
  114. @Test
  115. public void emptySetsAreEqual() {
  116. CustomSet<Character> customSet = new CustomSet<>(Collections.emptyList());
  117. CustomSet<Character> secondCustomSet = new CustomSet<>(Collections.emptyList());
  118. assertTrue(customSet.equals(secondCustomSet));
  119. }
  120. @Ignore("Remove to run test")
  121. @Test
  122. public void emptySetIsNotEqualToNonEmptySet() {
  123. CustomSet<String> customSet = new CustomSet<>(Collections.emptyList());
  124. CustomSet<String> secondCustomSet = new CustomSet<>(Arrays.asList("1", "2", "3"));
  125. assertFalse(customSet.equals(secondCustomSet));
  126. }
  127. @Ignore("Remove to run test")
  128. @Test
  129. public void nonEmptySetIsNotEqualToEmptySet() {
  130. CustomSet<Integer> customSet = new CustomSet<>(Arrays.asList(1, 2, 3));
  131. CustomSet<Integer> secondCustomSet = new CustomSet<>(Collections.emptyList());
  132. assertFalse(customSet.equals(secondCustomSet));
  133. }
  134. @Ignore("Remove to run test")
  135. @Test
  136. public void setsWithTheSameElementsAreEqual() {
  137. CustomSet<Character> customSet = new CustomSet<>(Arrays.asList('1', '2'));
  138. CustomSet<Character> secondCustomSet = new CustomSet<>(Arrays.asList('2', '1'));
  139. assertTrue(customSet.equals(secondCustomSet));
  140. }
  141. @Ignore("Remove to run test")
  142. @Test
  143. public void setsWithDifferentElementsAreNotEqual() {
  144. CustomSet<String> customSet = new CustomSet<>(Arrays.asList("1", "2", "3"));
  145. CustomSet<String> secondCustomSet = new CustomSet<>(Arrays.asList("1", "2", "4"));
  146. assertFalse(customSet.equals(secondCustomSet));
  147. }
  148. @Ignore("Remove to run test")
  149. @Test
  150. public void setIsNotEqualToLargerSetWithSameElements() {
  151. CustomSet<String> customSet = new CustomSet<>(Arrays.asList("1", "2", "3"));
  152. CustomSet<String> secondCustomSet = new CustomSet<>(Arrays.asList("1", "2", "3", "4"));
  153. assertFalse(customSet.equals(secondCustomSet));
  154. }
  155. @Ignore("Remove to run test")
  156. @Test
  157. public void addToEmptySet() {
  158. int element = 3;
  159. CustomSet<Integer> expected = new CustomSet<>(Collections.unmodifiableList(Collections.singletonList(element)));
  160. CustomSet<Integer> actual = new CustomSet<>(Collections.emptyList());
  161. actual.add(element);
  162. assertNotNull(actual);
  163. assertFalse(actual.isEmpty());
  164. assertTrue(expected.equals(actual));
  165. }
  166. @Ignore("Remove to run test")
  167. @Test
  168. public void addToNonEmptySet() {
  169. char element = '3';
  170. CustomSet<Character> expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList('1', '2', '3', '4')));
  171. CustomSet<Character> actual = new CustomSet<>(Arrays.asList('1', '2', '4'));
  172. actual.add(element);
  173. assertNotNull(actual);
  174. assertFalse(actual.isEmpty());
  175. assertTrue(expected.equals(actual));
  176. }
  177. @Ignore("Remove to run test")
  178. @Test
  179. public void addingAnExistingElementDoesNotChangeTheSet() {
  180. String element = "3";
  181. CustomSet<String> expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList("1", "2", "3")));
  182. CustomSet<String> actual = new CustomSet<>(Arrays.asList("1", "2", "3"));
  183. actual.add(element);
  184. assertNotNull(actual);
  185. assertTrue(expected.equals(actual));
  186. }
  187. @Ignore("Remove to run test")
  188. @Test
  189. public void intersectionOfTwoEmptySetsIsAnEmptySet() {
  190. CustomSet<Integer> actual = new CustomSet<Integer>(Collections.emptyList())
  191. .getIntersection(new CustomSet<>(Collections.emptyList()));
  192. assertNotNull(actual);
  193. assertTrue(actual.isEmpty());
  194. }
  195. @Ignore("Remove to run test")
  196. @Test
  197. public void intersectionOfAnEmptySetAndNonEmptySetIsAnEmptySet() {
  198. CustomSet<Character> actual = new CustomSet<Character>(Collections.emptyList())
  199. .getIntersection(new CustomSet<>(Arrays.asList('3', '2', '5')));
  200. assertNotNull(actual);
  201. assertTrue(actual.isEmpty());
  202. }
  203. @Ignore("Remove to run test")
  204. @Test
  205. public void intersectionOfANonEmptySetAndAnEmptySetIsAnEmptySet() {
  206. CustomSet<String> actual = new CustomSet<>(Arrays.asList("1", "2", "3", "4"))
  207. .getIntersection(new CustomSet<>(Collections.emptyList()));
  208. assertNotNull(actual);
  209. assertTrue(actual.isEmpty());
  210. }
  211. @Ignore("Remove to run test")
  212. @Test
  213. public void intersectionOfTwoSetsWithNoSharedElementsIsAnEmptySet() {
  214. CustomSet<Integer> actual = new CustomSet<>(Arrays.asList(1, 2, 3))
  215. .getIntersection(new CustomSet<>(Arrays.asList(4, 5, 6)));
  216. assertNotNull(actual);
  217. assertTrue(actual.isEmpty());
  218. }
  219. @Ignore("Remove to run test")
  220. @Test
  221. public void intersectionOfTwoSetsWithSharedElementsIsASetOfTheSharedElements() {
  222. CustomSet<Character> expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList('2', '3')));
  223. CustomSet<Character> actual = new CustomSet<>(Arrays.asList('1', '2', '3', '4'))
  224. .getIntersection(new CustomSet<>(Arrays.asList('3', '2', '5')));
  225. assertNotNull(actual);
  226. assertFalse(actual.isEmpty());
  227. assertTrue(expected.equals(actual));
  228. }
  229. @Ignore("Remove to run test")
  230. @Test
  231. public void differenceOfTwoEmptySetsIsAnEmptySet() {
  232. CustomSet<String> actual = new CustomSet<String>(Collections.emptyList())
  233. .getDifference(new CustomSet<>(Collections.emptyList()));
  234. assertNotNull(actual);
  235. assertTrue(actual.isEmpty());
  236. }
  237. @Ignore("Remove to run test")
  238. @Test
  239. public void differenceOfAnEmptySetAndNonEmptySetIsAnEmptySet() {
  240. CustomSet<Integer> actual = new CustomSet<Integer>(Collections.emptyList())
  241. .getDifference(new CustomSet<>(Arrays.asList(3, 2, 5)));
  242. assertNotNull(actual);
  243. assertTrue(actual.isEmpty());
  244. }
  245. @Ignore("Remove to run test")
  246. @Test
  247. public void differenceOfANonEmptySetAndAnEmptySetIsTheNonEmptySet() {
  248. CustomSet<Character> expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList('1', '2', '3', '4')));
  249. CustomSet<Character> actual = new CustomSet<>(Arrays.asList('1', '2', '3', '4'))
  250. .getDifference(new CustomSet<>(Collections.emptyList()));
  251. assertNotNull(actual);
  252. assertFalse(actual.isEmpty());
  253. assertTrue(expected.equals(actual));
  254. }
  255. @Ignore("Remove to run test")
  256. @Test
  257. public void differenceOfTwoNonEmptySetsIsASetOfElementsThatAreOnlyInTheFirstSet() {
  258. CustomSet<String> expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList("1", "3")));
  259. CustomSet<String> actual = new CustomSet<>(Arrays.asList("3", "2", "1"))
  260. .getDifference(new CustomSet<>(Arrays.asList("2", "4")));
  261. assertNotNull(actual);
  262. assertFalse(actual.isEmpty());
  263. assertTrue(expected.equals(actual));
  264. }
  265. @Ignore("Remove to run test")
  266. @Test
  267. public void unionOfTwoEmptySetsIsAnEmptySet() {
  268. CustomSet<Integer> actual = new CustomSet<Integer>(Collections.emptyList())
  269. .getUnion(new CustomSet<>(Collections.emptyList()));
  270. assertNotNull(actual);
  271. assertTrue(actual.isEmpty());
  272. }
  273. @Ignore("Remove to run test")
  274. @Test
  275. public void unionOfAnEmptySetAndNonEmptySetIsTheNonEmptySet() {
  276. CustomSet<Character> expected = new CustomSet<>(Collections.unmodifiableList(Collections.singletonList('2')));
  277. CustomSet<Character> actual = new CustomSet<Character>(Collections.emptyList())
  278. .getUnion(new CustomSet<>(Collections.singletonList('2')));
  279. assertNotNull(actual);
  280. assertFalse(actual.isEmpty());
  281. assertTrue(expected.equals(actual));
  282. }
  283. @Ignore("Remove to run test")
  284. @Test
  285. public void unionOfANonEmptySetAndAnEmptySetIsTheNonEmptySet() {
  286. CustomSet<String> expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList("1", "3")));
  287. CustomSet<String> actual = new CustomSet<>(Arrays.asList("1", "3"))
  288. .getUnion(new CustomSet<>(Collections.emptyList()));
  289. assertNotNull(actual);
  290. assertFalse(actual.isEmpty());
  291. assertTrue(expected.equals(actual));
  292. }
  293. @Ignore("Remove to run test")
  294. @Test
  295. public void unionOfTwoNonEmptySetsContainsAllUniqueElements() {
  296. CustomSet<Integer> expected = new CustomSet<>(Collections.unmodifiableList(Arrays.asList(3, 2, 1)));
  297. CustomSet<Integer> actual = new CustomSet<>(Arrays.asList(1, 3))
  298. .getUnion(new CustomSet<>(Arrays.asList(2, 3)));
  299. assertNotNull(actual);
  300. assertFalse(actual.isEmpty());
  301. assertTrue(expected.equals(actual));
  302. }
  303. }