Define iteration order of BdfDictionary.

This commit is contained in:
akwizgran
2016-08-26 10:57:57 +01:00
parent 55602ed76a
commit 625276067a
3 changed files with 79 additions and 9 deletions

View File

@@ -7,10 +7,13 @@ import org.briarproject.api.data.BdfEntry;
import org.junit.Test;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map.Entry;
import static org.briarproject.api.data.BdfDictionary.NULL_VALUE;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class BdfDictionaryTest extends BriarTestCase {
@@ -62,4 +65,69 @@ public class BdfDictionaryTest extends BriarTestCase {
assertEquals(123, bar.length);
assertArrayEquals(new byte[123], bar);
}
@Test
public void testKeySetIteratorIsOrderedByKeys() throws Exception {
BdfDictionary d = new BdfDictionary();
d.put("a", 1);
d.put("d", 4);
d.put("b", 2);
d.put("c", 3);
// Keys should be returned in their natural order
Iterator<String> it = d.keySet().iterator();
assertTrue(it.hasNext());
assertEquals("a", it.next());
assertTrue(it.hasNext());
assertEquals("b", it.next());
assertTrue(it.hasNext());
assertEquals("c", it.next());
assertTrue(it.hasNext());
assertEquals("d", it.next());
}
@Test
public void testValuesIteratorIsOrderedByKeys() throws Exception {
BdfDictionary d = new BdfDictionary();
d.put("a", 1);
d.put("d", 4);
d.put("b", 2);
d.put("c", 3);
// Values should be returned in the natural order of their keys
Iterator<Object> it = d.values().iterator();
assertTrue(it.hasNext());
assertEquals(1, it.next());
assertTrue(it.hasNext());
assertEquals(2, it.next());
assertTrue(it.hasNext());
assertEquals(3, it.next());
assertTrue(it.hasNext());
assertEquals(4, it.next());
}
@Test
public void testEntrySetIteratorIsOrderedByKeys() throws Exception {
BdfDictionary d = new BdfDictionary();
d.put("a", 1);
d.put("d", 4);
d.put("b", 2);
d.put("c", 3);
// Entries should be returned in the natural order of their keys
Iterator<Entry<String, Object>> it = d.entrySet().iterator();
assertTrue(it.hasNext());
Entry<String, Object> e = it.next();
assertEquals("a", e.getKey());
assertEquals(1, e.getValue());
assertTrue(it.hasNext());
e = it.next();
assertEquals("b", e.getKey());
assertEquals(2, e.getValue());
assertTrue(it.hasNext());
e = it.next();
assertEquals("c", e.getKey());
assertEquals(3, e.getValue());
assertTrue(it.hasNext());
e = it.next();
assertEquals("d", e.getKey());
assertEquals(4, e.getValue());
}
}