diff options
Diffstat (limited to 'test/com/isode/stroke/base')
-rw-r--r-- | test/com/isode/stroke/base/ByteArrayTest.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/test/com/isode/stroke/base/ByteArrayTest.java b/test/com/isode/stroke/base/ByteArrayTest.java new file mode 100644 index 0000000..183ae76 --- /dev/null +++ b/test/com/isode/stroke/base/ByteArrayTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2010-2011, Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.base; + +import java.io.UnsupportedEncodingException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ByteArrayTest { + @Test + public void testASCIIString_fromString() { + String testling = "Wheeblahpling"; + assertEquals(testling, new ByteArray(testling).toString()); + } + + @Test + public void testASCIIString_fromBytesSimple() throws UnsupportedEncodingException { + String testling = "WheeBlahBlahPling"; + byte[] bytes = testling.getBytes("UTF-8"); + assertEquals(testling, new ByteArray(bytes).toString()); + } + + @Test + public void testASCIIString_fromBytes() throws UnsupportedEncodingException { + String target = "ABCZ"; + byte[] bytes = {65, 66, 67, 90}; + assertEquals(target, new ByteArray(bytes).toString()); + } + + @Test + public void testExtendedString_fromString() { + String testling = "Wheeblahpling\u0041\u00DF\u00F7"; + assertEquals(testling, new ByteArray(testling).toString()); + } + + @Test + public void testExtendedString_fromBytes() { + String target = "ABCZ\u0041\u00DF\u00F7"; + byte[] bytes = byteify(new int[]{65, 66, 67, 90, 0x41, 0xc3, 0x9f, 0xc3, 0xb7}); + assertEquals(target, new ByteArray(bytes).toString()); + } + + @Test + public void testExtendedString_fromBytesSegmented() { + String target = "ABCZ\u0041\u00DF\u00F7"; + byte[] bytes = byteify(new int[]{65, 66, 67, 90, 0x41, 0xc3, 0x9f}); + ByteArray testling = new ByteArray(bytes); + testling.append((byte)0xc3); + testling.append((byte)0xb7); + assertEquals(target, testling.toString()); + } + + @Test + public void testExtendedBytes_fromString() { + String string = "ABCZ\u0041\u00DF\u00F7"; + byte[] target = byteify(new int[]{65, 66, 67, 90, 0x41, 0xc3, 0x9f, 0xc3, 0xb7}); + assertArrayEquals(target, new ByteArray(string).getData()); + } + + private byte[] byteify(int[] ints) { + byte[] bytes = new byte[ints.length]; + for (int i = 0; i < ints.length; i++) { + int j = ints[i]; + bytes[i] = (byte)j; + } + return bytes; + } +} |