Commit a2169b28 authored by Robert Chatley's avatar Robert Chatley
Browse files

solution

parent 54887781
package ic.doc;
import java.util.ArrayList;
import java.util.List;
public class RecentlyUsedList {
private List<String> content = new ArrayList<>();
public boolean isEmpty() {
return content.isEmpty();
}
public void add(String element) {
content.remove(element);
content.add(0, element);
}
public int size() {
return content.size();
}
public String get(int i) {
return content.get(i);
}
}
package ic.doc;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class RecentlyUsedListTest {
RecentlyUsedList list = new RecentlyUsedList();
@Test
public void isInitialisedEmpty() {
assertTrue(list.isEmpty());
assertThat(list.size(), is(0));
}
@Test
public void canHaveElementsAdded() {
list.add("hello");
assertFalse(list.isEmpty());
assertThat(list.size(), is(1));
list.add("goodbye");
assertThat(list.size(), is(2));
}
@Test
public void hasMostRecentItemFirst() {
list.add("cat");
list.add("dog");
assertThat(list.get(0), is("dog"));
assertThat(list.get(1), is("cat"));
}
@Test
public void movesDuplicatesToTheHeadOfTheList() {
list.add("cat");
list.add("dog");
list.add("cat");
assertThat(list.size(), is(2));
assertThat(list.get(0), is("cat"));
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment