Helper methods and unit tests for BdfList/Dictionary.

This commit is contained in:
akwizgran
2016-02-26 16:27:24 +00:00
parent 76e6b7cfa9
commit e28dc17881
5 changed files with 199 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
package org.briarproject.data;
import org.briarproject.BriarTestCase;
import org.briarproject.api.Bytes;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfEntry;
import org.junit.Test;
import java.util.Collections;
import static org.briarproject.api.data.BdfDictionary.NULL_VALUE;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class BdfDictionaryTest extends BriarTestCase {
@Test
public void testConstructors() {
assertEquals(Collections.emptyMap(), new BdfDictionary());
assertEquals(Collections.singletonMap("foo", NULL_VALUE),
new BdfDictionary(Collections.singletonMap("foo", NULL_VALUE)));
}
@Test
public void testFactoryMethod() {
assertEquals(Collections.emptyMap(), BdfDictionary.of());
assertEquals(Collections.singletonMap("foo", NULL_VALUE),
BdfDictionary.of(new BdfEntry("foo", NULL_VALUE)));
}
@Test
public void testIntegerPromotion() throws Exception {
BdfDictionary d = new BdfDictionary();
d.put("foo", (byte) 1);
d.put("bar", (short) 2);
d.put("baz", 3);
d.put("bam", 4L);
assertEquals(Long.valueOf(1), d.getInteger("foo"));
assertEquals(Long.valueOf(2), d.getInteger("bar"));
assertEquals(Long.valueOf(3), d.getInteger("baz"));
assertEquals(Long.valueOf(4), d.getInteger("bam"));
}
@Test
public void testFloatPromotion() throws Exception {
BdfDictionary d = new BdfDictionary();
d.put("foo", 1F);
d.put("bar", 2D);
assertEquals(Double.valueOf(1), d.getFloat("foo"));
assertEquals(Double.valueOf(2), d.getFloat("bar"));
}
@Test
public void testByteArrayUnwrapping() throws Exception {
BdfDictionary d = new BdfDictionary();
d.put("foo", new byte[123]);
d.put("bar", new Bytes(new byte[123]));
assertArrayEquals(new byte[123], d.getRaw("foo"));
assertArrayEquals(new byte[123], d.getRaw("bar"));
}
}

View File

@@ -0,0 +1,61 @@
package org.briarproject.data;
import org.briarproject.BriarTestCase;
import org.briarproject.api.Bytes;
import org.briarproject.api.data.BdfList;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import static org.briarproject.api.data.BdfDictionary.NULL_VALUE;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class BdfListTest extends BriarTestCase {
@Test
public void testConstructors() {
assertEquals(Collections.emptyList(), new BdfList());
assertEquals(Arrays.asList(1, 2, NULL_VALUE),
new BdfList(Arrays.asList(1, 2, NULL_VALUE)));
}
@Test
public void testFactoryMethod() {
assertEquals(Collections.emptyList(), BdfList.of());
assertEquals(Arrays.asList(1, 2, NULL_VALUE),
BdfList.of(1, 2, NULL_VALUE));
}
@Test
public void testIntegerPromotion() throws Exception {
BdfList list = new BdfList();
list.add((byte) 1);
list.add((short) 2);
list.add(3);
list.add(4L);
assertEquals(Long.valueOf(1), list.getInteger(0));
assertEquals(Long.valueOf(2), list.getInteger(1));
assertEquals(Long.valueOf(3), list.getInteger(2));
assertEquals(Long.valueOf(4), list.getInteger(3));
}
@Test
public void testFloatPromotion() throws Exception {
BdfList list = new BdfList();
list.add(1F);
list.add(2D);
assertEquals(Double.valueOf(1), list.getFloat(0));
assertEquals(Double.valueOf(2), list.getFloat(1));
}
@Test
public void testByteArrayUnwrapping() throws Exception {
BdfList list = new BdfList();
list.add(new byte[123]);
list.add(new Bytes(new byte[123]));
assertArrayEquals(new byte[123], list.getRaw(0));
assertArrayEquals(new byte[123], list.getRaw(1));
}
}