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

CustomSetTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertNotNull;
  5. import static org.junit.Assert.assertTrue;
  6. import org.junit.Test;
  7. import org.junit.Ignore;
  8. public class CustomSetTest {
  9. @Test
  10. public void setsWithNoElementsAreEmpty() {
  11. final boolean actual
  12. = new CustomSet<>(Collections.EMPTY_LIST)
  13. .isEmpty();
  14. assertTrue(actual);
  15. }
  16. @Test
  17. @Ignore
  18. public void setsWithElementsAreNotEmpty() {
  19. final boolean actual
  20. = new CustomSet<>(Arrays.asList(1))
  21. .isEmpty();
  22. assertFalse(actual);
  23. }
  24. @Test
  25. @Ignore
  26. public void nothingIsContainedInAnEmptySet() {
  27. final boolean actual
  28. = new CustomSet<>(Collections.EMPTY_LIST)
  29. .contains(1);
  30. assertFalse(actual);
  31. }
  32. @Test
  33. @Ignore
  34. public void whenTheElementIsInTheSet() {
  35. final boolean actual
  36. = new CustomSet<>(Arrays.asList(1, 2, 3))
  37. .contains(1);
  38. assertTrue(actual);
  39. }
  40. @Test
  41. @Ignore
  42. public void whenTheElementIsNotInTheSet() {
  43. final boolean actual
  44. = new CustomSet<>(Arrays.asList(1, 2, 3))
  45. .contains(4);
  46. assertFalse(actual);
  47. }
  48. @Test
  49. @Ignore
  50. public void emptySetIsASubsetOfAnotherEmptySet() {
  51. final boolean actual
  52. = new CustomSet<>(Collections.EMPTY_LIST)
  53. .isSubset(
  54. new CustomSet<>(Collections.EMPTY_LIST)
  55. );
  56. assertTrue(actual);
  57. }
  58. @Test
  59. @Ignore
  60. public void emptySetIsASubsetOfNonEemptySet() {
  61. final boolean actual
  62. = new CustomSet<>(Arrays.asList(1))
  63. .isSubset(
  64. new CustomSet<>(Collections.EMPTY_LIST)
  65. );
  66. assertTrue(actual);
  67. }
  68. @Test
  69. @Ignore
  70. public void nonEmptySetIsNotASubsetOfEmptySet() {
  71. final boolean actual
  72. = new CustomSet<>(Collections.EMPTY_LIST)
  73. .isSubset(
  74. new CustomSet<>(Arrays.asList(1))
  75. );
  76. assertFalse(actual);
  77. }
  78. @Test
  79. @Ignore
  80. public void setIsASubsetOfSetWithExactSameElements() {
  81. final boolean actual
  82. = new CustomSet<>(Arrays.asList(1, 2, 3))
  83. .isSubset(
  84. new CustomSet<>(Arrays.asList(1, 2, 3))
  85. );
  86. assertTrue(actual);
  87. }
  88. @Test
  89. @Ignore
  90. public void setIsASubsetOfLargerSetWithSameElements() {
  91. final boolean actual
  92. = new CustomSet<>(Arrays.asList(4, 1, 2, 3))
  93. .isSubset(
  94. new CustomSet<>(Arrays.asList(1, 2, 3))
  95. );
  96. assertTrue(actual);
  97. }
  98. @Test
  99. @Ignore
  100. public void setIsNotASubsetOfSetThatDoesNotContainItsElements() {
  101. final boolean actual
  102. = new CustomSet<>(Arrays.asList(4, 1, 3))
  103. .isSubset(
  104. new CustomSet<>(Arrays.asList(1, 2, 3))
  105. );
  106. assertFalse(actual);
  107. }
  108. @Test
  109. @Ignore
  110. public void theEmptySetIsDisjointWithItself() {
  111. final boolean actual
  112. = new CustomSet<>(Collections.EMPTY_LIST)
  113. .isDisjoint(
  114. new CustomSet<>(Collections.EMPTY_LIST)
  115. );
  116. assertTrue(actual);
  117. }
  118. @Test
  119. @Ignore
  120. public void emptySetIsDisjointWithNonEmptySet() {
  121. final boolean actual
  122. = new CustomSet<>(Collections.EMPTY_LIST)
  123. .isDisjoint(
  124. new CustomSet<>(Arrays.asList(1))
  125. );
  126. assertTrue(actual);
  127. }
  128. @Test
  129. @Ignore
  130. public void nonEmptySetIsDisjointWithEmptySet() {
  131. final boolean actual
  132. = new CustomSet<>(Arrays.asList(1))
  133. .isDisjoint(
  134. new CustomSet<>(Collections.EMPTY_LIST)
  135. );
  136. assertTrue(actual);
  137. }
  138. @Test
  139. @Ignore
  140. public void setsAreNotDisjointIfTheyShareAnElement() {
  141. final boolean actual
  142. = new CustomSet<>(Arrays.asList(1, 2))
  143. .isDisjoint(
  144. new CustomSet<>(Arrays.asList(2, 3))
  145. );
  146. assertFalse(actual);
  147. }
  148. @Test
  149. @Ignore
  150. public void setsAreDisjointIfTheyShareNoElements() {
  151. final boolean actual
  152. = new CustomSet<>(Arrays.asList(1, 2))
  153. .isDisjoint(
  154. new CustomSet<>(Arrays.asList(3, 4))
  155. );
  156. assertTrue(actual);
  157. }
  158. @Test
  159. @Ignore
  160. public void emptySetsAreEqual() {
  161. final boolean actual
  162. = new CustomSet<>(Collections.EMPTY_LIST)
  163. .equals(
  164. new CustomSet<>(Collections.EMPTY_LIST)
  165. );
  166. assertTrue(actual);
  167. }
  168. @Test
  169. @Ignore
  170. public void emptySetIsNotEqualToNonEmptySet() {
  171. final boolean actual
  172. = new CustomSet<>(Collections.EMPTY_LIST)
  173. .equals(
  174. new CustomSet<>(Arrays.asList(1, 2, 3))
  175. );
  176. assertFalse(actual);
  177. }
  178. @Test
  179. @Ignore
  180. public void nonEmptySetIsNotEqualToEmptySet() {
  181. final boolean actual
  182. = new CustomSet<>(Arrays.asList(1, 2, 3))
  183. .equals(
  184. new CustomSet<>(Collections.EMPTY_LIST)
  185. );
  186. assertFalse(actual);
  187. }
  188. @Test
  189. @Ignore
  190. public void setsWithTheSameElementsAreEqual() {
  191. final boolean actual
  192. = new CustomSet<>(Arrays.asList(1, 2))
  193. .equals(
  194. new CustomSet<>(Arrays.asList(2, 1))
  195. );
  196. assertTrue(actual);
  197. }
  198. @Test
  199. @Ignore
  200. public void setsWithDifferentElementsAreNotEqual() {
  201. final boolean actual
  202. = new CustomSet<>(Arrays.asList(1, 2, 3))
  203. .equals(
  204. new CustomSet<>(Arrays.asList(1, 2, 4))
  205. );
  206. assertFalse(actual);
  207. }
  208. @Test
  209. @Ignore
  210. public void addToEmptySet() {
  211. final int element = 3;
  212. final CustomSet<Integer> expected
  213. = new CustomSet<>(
  214. Collections.unmodifiableList(Arrays.asList(element))
  215. );
  216. final CustomSet<Integer> actual
  217. = new CustomSet<>(Collections.EMPTY_LIST);
  218. actual.add(element);
  219. assertNotNull(actual);
  220. assertFalse(actual.isEmpty());
  221. assertTrue(expected.equals(actual));
  222. }
  223. @Test
  224. @Ignore
  225. public void addToNonEmptySet() {
  226. final int element = 3;
  227. final CustomSet<Integer> expected
  228. = new CustomSet<>(
  229. Collections.unmodifiableList(Arrays.asList(1, 2, 3, 4))
  230. );
  231. final CustomSet<Integer> actual
  232. = new CustomSet<>(Arrays.asList(1, 2, 4));
  233. actual.add(element);
  234. assertNotNull(actual);
  235. assertFalse(actual.isEmpty());
  236. assertTrue(expected.equals(actual));
  237. }
  238. @Test
  239. @Ignore
  240. public void addingAnExistingElementDoesNotChangeTheSet() {
  241. final int element = 3;
  242. final CustomSet<Integer> expected
  243. = new CustomSet<>(
  244. Collections.unmodifiableList(Arrays.asList(1, 2, 3))
  245. );
  246. final CustomSet<Integer> actual
  247. = new CustomSet<>(Arrays.asList(1, 2, 3));
  248. actual.add(element);
  249. assertNotNull(actual);
  250. assertTrue(expected.equals(actual));
  251. }
  252. @Test
  253. @Ignore
  254. public void intersectionOfTwoEmptySetsIsAnEmptySet() {
  255. final CustomSet<Integer> actual
  256. = new CustomSet<>(Collections.EMPTY_LIST)
  257. .getIntersection(
  258. new CustomSet<>(Collections.EMPTY_LIST)
  259. );
  260. assertNotNull(actual);
  261. assertTrue(actual.isEmpty());
  262. }
  263. @Test
  264. @Ignore
  265. public void intersectionOfAnEmptySetAndNonEmptySetIsAnEmptySet() {
  266. final CustomSet<Integer> actual
  267. = new CustomSet<>(Collections.EMPTY_LIST)
  268. .getIntersection(
  269. new CustomSet<>(Arrays.asList(3, 2, 5))
  270. );
  271. assertNotNull(actual);
  272. assertTrue(actual.isEmpty());
  273. }
  274. @Test
  275. @Ignore
  276. public void intersectionOfANonEmptySetAndAnEmptySetIsAnEmptySet() {
  277. final CustomSet<Integer> actual
  278. = new CustomSet<>(Arrays.asList(1, 2, 3, 4))
  279. .getIntersection(
  280. new CustomSet<>(Collections.EMPTY_LIST)
  281. );
  282. assertNotNull(actual);
  283. assertTrue(actual.isEmpty());
  284. }
  285. @Test
  286. @Ignore
  287. public void intersectionOfTwoSetsWithNoSharedElementsIsAnEmptySet() {
  288. final CustomSet<Integer> actual
  289. = new CustomSet<>(Arrays.asList(1, 2, 3))
  290. .getIntersection(
  291. new CustomSet<>(Arrays.asList(4, 5, 6))
  292. );
  293. assertNotNull(actual);
  294. assertTrue(actual.isEmpty());
  295. }
  296. @Test
  297. @Ignore
  298. public void intersectionOfTwoSetsWithSharedElementsIsASetOfTheSharedElements() {
  299. final CustomSet<Integer> expected
  300. = new CustomSet<>(
  301. Collections.unmodifiableList(Arrays.asList(2, 3))
  302. );
  303. final CustomSet<Integer> actual
  304. = new CustomSet<>(Arrays.asList(1, 2, 3, 4))
  305. .getIntersection(
  306. new CustomSet<>(Arrays.asList(3, 2, 5))
  307. );
  308. assertNotNull(actual);
  309. assertFalse(actual.isEmpty());
  310. assertTrue(expected.equals(actual));
  311. }
  312. @Test
  313. @Ignore
  314. public void differenceOfTwoEmptySetsIsAnEmptySet() {
  315. final CustomSet<Integer> actual
  316. = new CustomSet<>(Collections.EMPTY_LIST)
  317. .getDifference(
  318. new CustomSet<>(Collections.EMPTY_LIST)
  319. );
  320. assertNotNull(actual);
  321. assertTrue(actual.isEmpty());
  322. }
  323. @Test
  324. @Ignore
  325. public void differenceOfAnEmptySetAndNonEmptySetIsAnEmptySet() {
  326. final CustomSet<Integer> actual
  327. = new CustomSet<>(Collections.EMPTY_LIST)
  328. .getDifference(
  329. new CustomSet<>(Arrays.asList(3, 2, 5))
  330. );
  331. assertNotNull(actual);
  332. assertTrue(actual.isEmpty());
  333. }
  334. @Test
  335. @Ignore
  336. public void differenceOfANonEmptySetAndAnEmptySetIsTheNonEmptySet() {
  337. final CustomSet<Integer> expected
  338. = new CustomSet<>(
  339. Collections.unmodifiableList(Arrays.asList(1, 2, 3, 4))
  340. );
  341. final CustomSet<Integer> actual
  342. = new CustomSet<>(Arrays.asList(1, 2, 3, 4))
  343. .getDifference(
  344. new CustomSet<>(Collections.EMPTY_LIST)
  345. );
  346. assertNotNull(actual);
  347. assertFalse(actual.isEmpty());
  348. assertTrue(expected.equals(actual));
  349. }
  350. @Test
  351. @Ignore
  352. public void differenceOfTwoNonEmptySetsIsASetOfElementsThatAreOnlyInTheFirstSet() {
  353. final CustomSet<Integer> expected
  354. = new CustomSet<>(
  355. Collections.unmodifiableList(Arrays.asList(1, 3))
  356. );
  357. final CustomSet<Integer> actual
  358. = new CustomSet<>(Arrays.asList(3, 2, 1))
  359. .getDifference(
  360. new CustomSet<>(Arrays.asList(2, 4))
  361. );
  362. assertNotNull(actual);
  363. assertFalse(actual.isEmpty());
  364. assertTrue(expected.equals(actual));
  365. }
  366. @Test
  367. @Ignore
  368. public void unionOfTwoEmptySetsIsAnEmptySet() {
  369. final CustomSet<Integer> actual
  370. = new CustomSet<>(Collections.EMPTY_LIST)
  371. .getUnion(
  372. new CustomSet<>(Collections.EMPTY_LIST)
  373. );
  374. assertNotNull(actual);
  375. assertTrue(actual.isEmpty());
  376. }
  377. @Test
  378. @Ignore
  379. public void unionOfAnEmptySetAndNonEmptySetIsTheNonEmptySet() {
  380. final CustomSet<Integer> expected
  381. = new CustomSet<>(
  382. Collections.unmodifiableList(Arrays.asList(2))
  383. );
  384. final CustomSet<Integer> actual
  385. = new CustomSet<>(Collections.EMPTY_LIST)
  386. .getUnion(
  387. new CustomSet<>(Arrays.asList(2))
  388. );
  389. assertNotNull(actual);
  390. assertFalse(actual.isEmpty());
  391. assertTrue(expected.equals(actual));
  392. }
  393. @Test
  394. @Ignore
  395. public void unionOfANonEmptySetAndAnEmptySetIsTheNonEmptySet() {
  396. final CustomSet<Integer> expected
  397. = new CustomSet<>(
  398. Collections.unmodifiableList(Arrays.asList(1, 3))
  399. );
  400. final CustomSet<Integer> actual
  401. = new CustomSet<>(Arrays.asList(1, 3))
  402. .getUnion(
  403. new CustomSet<>(Collections.EMPTY_LIST)
  404. );
  405. assertNotNull(actual);
  406. assertFalse(actual.isEmpty());
  407. assertTrue(expected.equals(actual));
  408. }
  409. @Test
  410. @Ignore
  411. public void unionOfTwoNonEmptySetsContainsAllUniqueElements() {
  412. final CustomSet<Integer> expected
  413. = new CustomSet<>(
  414. Collections.unmodifiableList(Arrays.asList(3, 2, 1))
  415. );
  416. final CustomSet<Integer> actual
  417. = new CustomSet<>(Arrays.asList(1, 3))
  418. .getUnion(
  419. new CustomSet<>(Arrays.asList(2, 3))
  420. );
  421. assertNotNull(actual);
  422. assertFalse(actual.isEmpty());
  423. assertTrue(expected.equals(actual));
  424. }
  425. }