summaryrefslogtreecommitdiffstats
blob: 183ae769a06184689bec42c7bb760854c238df43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
    }
}