Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.junit.Assert;
- import org.junit.Test;
- import org.junit.runner.JUnitCore;
- import org.junit.runner.Result;
- import org.junit.runner.notification.Failure;
- import java.util.ArrayList;
- import java.util.List;
- import static org.hamcrest.CoreMatchers.is;
- public class SolutionTests {
- public static void main(String[] args) {
- Result result = JUnitCore.runClasses(SolutionTests.class);
- for (Failure failure : result.getFailures()) {
- System.out.println(failure.toString());
- }
- }
- @Test
- public void testThatWeCanAddToMapAndRetrieveFromMap() {
- MyMap<Integer, Integer> myMap = new MyMap<>();
- int value = 2;
- int key = 1;
- myMap.put(key, value);
- Assert.assertThat(myMap.get(key), is(value));
- }
- @Test
- public void testThatWeCanAddMultipleValuesAndRetrieveRightValueFromMap() {
- MyMap<Integer, Integer> myMap = new MyMap<>();
- int[][] setupData = {{1, 2}, {4, 5}};
- for (int[] pair : setupData) {
- Integer key = pair[0];
- Integer value = pair[1];
- myMap.put(key, value);
- }
- Assert.assertThat(myMap.get(1), is(2));
- Assert.assertThat(myMap.get(4), is(5));
- }
- @Test
- public void testThatWeUpdateAMapNewValueForExistingKey() {
- MyMap<Integer, Integer> myMap = new MyMap<>();
- int[][] setupData = {{1, 2}, {1, 5}};
- for (int[] pair : setupData) {
- Integer key = pair[0];
- Integer value = pair[1];
- myMap.put(key, value);
- }
- Assert.assertThat(myMap.get(1), is(6));
- }
- public class MyMap<K, V> {
- private List<MyEntry<K, V>> myEntryList = new ArrayList<>();
- public void put(K key, V value) {
- myEntryList.add(key.hashCode(), new MyEntry<>(key, value));
- }
- public V get(K key) {
- return myEntryList.get(key.hashCode()).getValue();
- }
- public class MyEntry<K, V> {
- private K key;
- private V value;
- public MyEntry(K key, V value) {
- this.key = key;
- this.value = value;
- }
- public K getKey() {
- return key;
- }
- public V getValue() {
- return value;
- }
- @Override
- public String toString() {
- return "MyEntry{" +
- "key=" + key +
- ", value=" + value +
- '}';
- }
- }
- }
Add Comment
Please, Sign In to add comment