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

SimpleLinkedListTest.java 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import org.junit.Ignore;
  2. import org.junit.Test;
  3. import org.junit.Rule;
  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 thrown = ExpectedException.none();
  11. @Test
  12. public void aNewListIsEmpty() {
  13. SimpleLinkedList list = new SimpleLinkedList();
  14. assertThat(list.size(), is(0));
  15. }
  16. @Ignore
  17. @Test
  18. public void canCreateFromArray() {
  19. Integer[] values = new Integer[]{1, 2, 3};
  20. SimpleLinkedList list = new SimpleLinkedList(values);
  21. assertThat(list.size(), is(3));
  22. }
  23. @Ignore
  24. @Test
  25. public void popOnEmptyListWillThrow() {
  26. thrown.expect(NoSuchElementException.class);
  27. SimpleLinkedList list = new SimpleLinkedList();
  28. list.pop();
  29. }
  30. @Ignore
  31. @Test
  32. public void popReturnsLastAddedElement() {
  33. SimpleLinkedList list = new SimpleLinkedList();
  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
  42. @Test
  43. public void reverseReversesList() {
  44. SimpleLinkedList list = new SimpleLinkedList();
  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
  58. @Test
  59. public void canReturnListAsArray() {
  60. SimpleLinkedList list = new SimpleLinkedList();
  61. list.push(9);
  62. list.push(8);
  63. list.push(7);
  64. list.push(6);
  65. list.push(5);
  66. Integer[] expected = {5, 6, 7, 8, 9};
  67. assertEquals(list.asArray(Integer.class), expected);
  68. }
  69. @Ignore
  70. @Test
  71. public void canReturnEmptyListAsEmptyArray() {
  72. SimpleLinkedList list = new SimpleLinkedList();
  73. Object[] expected = {};
  74. assertEquals(list.asArray(Object.class), expected);
  75. }
  76. }