fork download
  1. import org.junit.Before;
  2. import org.junit.Test;
  3. import static org.junit.Assert.*;
  4.  
  5. import org.junit.runner.JUnitCore;
  6. import org.junit.runner.Result;
  7. import org.junit.runner.notification.Failure;
  8.  
  9.  
  10. class Node {
  11. int value;
  12. Node next;
  13.  
  14. Node(int value) {
  15. this.value = value;
  16. this.next = null;
  17. }
  18.  
  19.  
  20. }
  21.  
  22. class CustomLinkedList {
  23. Node head;
  24.  
  25. CustomLinkedList(){
  26. head = null;
  27. }
  28.  
  29. public void add(int value) {
  30. if (head == null) {
  31. head = new Node(value);
  32. return;
  33. }
  34.  
  35. Node current = head;
  36. while (current.next != null) {
  37. current = current.next;
  38. }
  39. current.next = new Node(value);
  40. }
  41.  
  42. public boolean remove(int value) {
  43. if (head == null) return false;
  44.  
  45. if (head.value == value) {
  46. head = head.next;
  47. return true;
  48. }
  49.  
  50. Node current = head;
  51. while (current.next != null) {
  52. if (current.next.value == value) {
  53. current.next = current.next.next;
  54. return true;
  55. }
  56. current = current.next;
  57. }
  58.  
  59. return false;
  60. }
  61.  
  62. public boolean find(int value) {
  63. Node current = head;
  64. while (current != null) {
  65. if (current.value == value) {
  66. return true;
  67. }
  68. current = current.next;
  69. }
  70. return false;
  71. }
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. class CustomLinkedListTest {
  80.  
  81. @Test
  82. void testAdd() {
  83. CustomLinkedList list = new CustomLinkedList();
  84. list.add(1);
  85. assertTrue(list.find(1));
  86.  
  87. list.add(2);
  88. assertTrue(list.find(2));
  89.  
  90. list.add(3);
  91. assertTrue(list.find(3));
  92. }
  93.  
  94. @Test
  95. void testRemoveExisting() {
  96. CustomLinkedList list = new CustomLinkedList();
  97. list.add(1);
  98. list.add(2);
  99. list.add(3);
  100.  
  101. assertTrue(list.remove(2));
  102. assertFalse(list.find(2));
  103.  
  104. assertTrue(list.remove(1));
  105. assertFalse(list.find(1));
  106.  
  107. assertTrue(list.remove(3));
  108. assertFalse(list.find(3));
  109. }
  110.  
  111. @Test
  112. void testRemoveNonExisting() {
  113. CustomLinkedList list = new CustomLinkedList();
  114. list.add(1);
  115. list.add(2);
  116. list.add(3);
  117.  
  118. assertFalse(list.remove(4));
  119. assertTrue(list.find(1));
  120. assertTrue(list.find(2));
  121. assertTrue(list.find(3));
  122. }
  123.  
  124. @Test
  125. void testFind() {
  126. CustomLinkedList list = new CustomLinkedList();
  127. list.add(1);
  128. list.add(2);
  129. list.add(3);
  130.  
  131. assertTrue(list.find(1));
  132. assertTrue(list.find(2));
  133. assertTrue(list.find(3));
  134. assertFalse(list.find(4));
  135. }
  136.  
  137. @Test
  138. void testEmptyList() {
  139. CustomLinkedList list = new CustomLinkedList();
  140. assertFalse(list.find(1));
  141. assertFalse(list.remove(1));
  142. }
  143. }
  144.  
  145.  
  146.  
  147.  
  148. class TestRunner {
  149. public static void main(String[] args) {
  150. Result result = JUnitCore.runClasses(CustomLinkedListTest.class);
  151.  
  152. for (Failure failure : result.getFailures()) {
  153. System.out.println(failure.toString());
  154. }
  155.  
  156. System.out.println(result.wasSuccessful());
  157. }
  158. }
  159.  
Success #stdin #stdout 0.2s 57776KB
stdin
Standard input is empty
stdout
initializationError(CustomLinkedListTest): Test class should have exactly one public constructor
initializationError(CustomLinkedListTest): Class CustomLinkedListTest should be public
initializationError(CustomLinkedListTest): Method testAdd() should be public
initializationError(CustomLinkedListTest): Class CustomLinkedListTest should be public
initializationError(CustomLinkedListTest): Method testFind() should be public
initializationError(CustomLinkedListTest): Class CustomLinkedListTest should be public
initializationError(CustomLinkedListTest): Method testEmptyList() should be public
initializationError(CustomLinkedListTest): Class CustomLinkedListTest should be public
initializationError(CustomLinkedListTest): Method testRemoveExisting() should be public
initializationError(CustomLinkedListTest): Class CustomLinkedListTest should be public
initializationError(CustomLinkedListTest): Method testRemoveNonExisting() should be public
false