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

SimpleLinkedListTest.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import org.junit.Ignore;
  2. import org.junit.Rule;
  3. import org.junit.Test;
  4. import org.junit.rules.ExpectedException;
  5. import java.util.NoSuchElementException;
  6. import static org.hamcrest.CoreMatchers.*;
  7. import static org.junit.Assert.*;
  8. public class SimpleLinkedListTest {
  9. @Rule
  10. public ExpectedException expectedException = ExpectedException.none();
  11. @Test
  12. public void aNewListIsEmpty() {
  13. SimpleLinkedList<Integer> list = new SimpleLinkedList<>();
  14. assertThat(list.size(), is(0));
  15. }
  16. @Ignore("Remove to run test")
  17. @Test
  18. public void canCreateFromArray() {
  19. Character[] values = new Character[]{'1', '2', '3'};
  20. SimpleLinkedList<Character> list = new SimpleLinkedList<Character>(values);
  21. assertThat(list.size(), is(3));
  22. }
  23. @Ignore("Remove to run test")
  24. @Test
  25. public void popOnEmptyListWillThrow() {
  26. expectedException.expect(NoSuchElementException.class);
  27. SimpleLinkedList<String> list = new SimpleLinkedList<String>();
  28. list.pop();
  29. }
  30. @Ignore("Remove to run test")
  31. @Test
  32. public void popReturnsLastAddedElement() {
  33. SimpleLinkedList<Integer> list = new SimpleLinkedList<Integer>();
  34. list.push(9);
  35. list.push(8);
  36. assertThat(list.size(), is(2));
  37. assertThat(list.pop(), is(8));
  38. assertThat(list.pop(), is(9));
  39. assertThat(list.size(), is(0));
  40. }
  41. @Ignore("Remove to run test")
  42. @Test
  43. public void reverseReversesList() {
  44. SimpleLinkedList<String> list = new SimpleLinkedList<String>();
  45. list.push("9");
  46. list.push("8");
  47. list.push("7");
  48. list.push("6");
  49. list.push("5");
  50. list.reverse();
  51. assertThat(list.pop(), is("9"));
  52. assertThat(list.pop(), is("8"));
  53. assertThat(list.pop(), is("7"));
  54. assertThat(list.pop(), is("6"));
  55. assertThat(list.pop(), is("5"));
  56. }
  57. @Ignore("Remove to run test")
  58. @Test
  59. public void canReturnListAsArray() {
  60. SimpleLinkedList<Character> list = new SimpleLinkedList<Character>();
  61. list.push('9');
  62. list.push('8');
  63. list.push('7');
  64. list.push('6');
  65. list.push('5');
  66. Character[] expected = {'5', '6', '7', '8', '9'};
  67. assertArrayEquals(expected, list.asArray(Character.class));
  68. }
  69. @Ignore("Remove to run test")
  70. @Test
  71. public void canReturnEmptyListAsEmptyArray() {
  72. SimpleLinkedList<Object> list = new SimpleLinkedList<Object>();
  73. Object[] expected = {};
  74. assertArrayEquals(expected, list.asArray(Object.class));
  75. }
  76. }