Ville Saalo avatar Ville Saalo committed 310534b

Created a stub of a new class called TwoWayMap and quite extensive unit tests for it.

Comments (0)

Files changed (5)

src/com/saibotd/bitbeaker/IssuesActivity.java

 				return false;
 			}
 			/* This function is heavily linked with getpositionFromFilter() */
-			switch (pos) {
+			switch (pos) { 
+			    // These comparisons (this and the one that is repeated the other way around in the getPositionFromFilter 
+			    // method) should, in my opinion, be replaced with a TwoWayMap once it has been implemented. 
 			case 0:
 				filter = "";
 				break;

src/com/saibotd/bitbeaker/TwoWayMap.java

+package com.saibotd.bitbeaker;
+
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * This class simulates a map from which you can retrieve data
+ * both with the keys and the values.
+ * 
+ * @param <K> The type of the keys
+ * @param <V> The type of the values
+ */
+public class TwoWayMap <K,V> {
+	private Map<K,V> map = new HashMap<K,V>();
+	private Map<V,K> reversedMap = new HashMap<V,K>();
+	private final K defaultKey;
+	private final V defaultValue;
+	
+	/**
+	 * Initializes the map so that when the key or value
+	 * is not found, null is returned.
+	 */
+	public TwoWayMap() {
+		this(null, null);
+	}
+
+	/**
+	 * This initializes the map so that when the key or value
+	 * is not found, the default value for the other is returned.
+	 * @param defaultKey What getByValue should return if it doesn't find the value
+	 * @param defaultValue What getByKey should return if it doesn't find the key
+	 */
+	public TwoWayMap(K defaultKey, V defaultValue) {
+		this.defaultKey = defaultKey;
+		this.defaultValue = defaultValue;
+	}
+	
+	public V getByKey(K key) {
+		throw new UnsupportedOperationException();
+	}
+	
+	public K getByValue(V value) {
+		throw new UnsupportedOperationException();
+	}
+	
+	public void put(K key, V value) {
+		throw new UnsupportedOperationException();
+	}
+}

src/com/saibotd/bitbeaker/tests/BitbeakerInstrumentationTestRunner.java

 		suite.addTestSuite(BitbeakerTest.class);
 		suite.addTestSuite(HelperTest.class);
 		suite.addTestSuite(LauncherActivityTest.class);
+		//suite.addTestSuite(TwoWayMapTest.class);
 		
 		return suite;
 	}

src/com/saibotd/bitbeaker/tests/HelperTest.java

 package com.saibotd.bitbeaker.tests;
 
+import junit.framework.TestCase;
+
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
-import android.content.ContentResolver;
-import android.content.Context;
-import android.test.mock.MockContentResolver;
-import android.test.mock.MockContext;
-import android.util.Log;
-
 import com.saibotd.bitbeaker.Helper;
 
-import junit.framework.TestCase;
-
 public class HelperTest extends TestCase {
 	public void test_isEmpty_with_empty_results() {
 		assertTrue("Null should be evaluated as empty!", Helper.isEmpty(null));

src/com/saibotd/bitbeaker/tests/TwoWayMapTest.java

+package com.saibotd.bitbeaker.tests;
+
+import com.saibotd.bitbeaker.TwoWayMap;
+
+import junit.framework.TestCase;
+
+public class TwoWayMapTest extends TestCase {
+	public void test_put_and_get_with_String_and_Integer() {
+		TwoWayMap<String, Integer> instance = new TwoWayMap<String, Integer>();
+		instance.put("key1", 11);
+		instance.put("key2", 22);
+		
+		assertEquals(11, (int)instance.getByKey(new String("key1")));
+		assertEquals("key1", instance.getByValue(11));
+		
+		assertEquals(22, (int)instance.getByKey("key2"));
+		assertEquals(new String("key2"), instance.getByValue(22));
+	}
+	
+	public void test_default_values_with_String_and_Integer() {
+		// Test that null is the default default value:
+		TwoWayMap<String, Integer> instance = new TwoWayMap<String, Integer>();
+		assertNull(instance.getByKey("nonexistent"));
+		assertNull(instance.getByValue(1234));
+		
+		// Test default values with an empty map:
+		instance = new TwoWayMap<String, Integer>("default key", 999);
+		assertEquals(999, (int)instance.getByKey("nonexistent"));
+		assertEquals("default key", instance.getByValue(42));
+		
+		// Put some data into the map, then test the defaults again:
+		instance.put("a", 1);
+		instance.put("b", 2);
+		instance.put("c", 3);
+		assertEquals(999, (int)instance.getByKey("nonexistent"));
+		assertEquals("default key", instance.getByValue(42));
+		
+		// Test that the put commands also succeeded:
+		assertEquals(1, (int)instance.getByKey("a"));
+		assertEquals(2, (int)instance.getByKey("b"));
+		assertEquals(3, (int)instance.getByKey("c"));
+		assertEquals("a", instance.getByValue(1));
+		assertEquals("b", instance.getByValue(2));
+		assertEquals("c", instance.getByValue(3));
+	}
+	
+	public void test_overriding_values() {
+		TwoWayMap<String, String> instance = new TwoWayMap<String, String>();
+		
+		// Put something into the map and assert that it worked:
+		instance.put("key1", "old value 1");
+		assertEquals("old value 1", instance.getByKey("key1"));
+		assertEquals("key1", instance.getByValue("old value 1"));
+		
+		// Overwrite the value and assert that everything worked:
+		instance.put("key1", "new value 1");
+		assertEquals("new value 1", instance.getByKey("key1"));
+		assertEquals(null, instance.getByValue("old value 1"));
+		assertEquals("key1", instance.getByValue("new value 1"));
+	}
+	
+	public void test_with_the_default_values_also_in_the_map() {
+		TwoWayMap<String, String> instance = new TwoWayMap<String, String>("KEY", "VALUE");
+		assertEquals("VALUE", instance.getByKey("anything"));
+		assertEquals("KEY", instance.getByValue("something"));
+		
+		assertEquals("VALUE", instance.getByKey("KEY"));
+		assertEquals("KEY", instance.getByValue("VALUE"));
+		
+		instance.put("KEY", "the actual value of key");
+		assertEquals("the actual value of key", instance.getByKey("KEY"));
+		assertEquals("KEY", instance.getByValue("the actual value of key"));
+		assertEquals("KEY", instance.getByValue("anything else"));
+		assertEquals("KEY", instance.getByValue("VALUE"));
+		
+		instance.put("the actual key", "VALUE");
+		assertEquals("VALUE", instance.getByKey("the actual key"));
+		assertEquals("the actual key", instance.getByValue("VALUE"));
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.