aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/text/Utf8ArrayTestCase.java
blob: 4b16695bf9f09defa59ce97c2e6b5095fc2b78fe (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;

import static org.junit.Assert.*;

import java.nio.ByteBuffer;

import org.junit.Before;
import org.junit.Test;

/**
 * Check the Utf8Array API behaves as expected.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class Utf8ArrayTestCase {
    private String raw;
    private byte[] rawBytes;
    private Utf8Array toCheck;

    @Before
    public void setUp() {
        raw = "0123456789";
        rawBytes = Utf8.toBytes(raw);
        toCheck = new Utf8Array(rawBytes);
    }

    @Test
    public final void testGetByteLength() {
        assertEquals(rawBytes.length, toCheck.getByteLength());
    }

    @Test
    public final void testGetBytes() {
        assertSame(rawBytes, toCheck.getBytes());
    }

    @Test
    public final void testGetByteOffset() {
        assertEquals(0, toCheck.getByteOffset());
    }

    @Test
    public final void testUtf8ArrayByteArrayIntInt() {
        Utf8Array otherConstructed = new Utf8Array(rawBytes, 0, rawBytes.length);
        assertNotSame(rawBytes, otherConstructed.getBytes());
        assertArrayEquals(rawBytes, otherConstructed.getBytes());
    }

    @Test
    public final void testUtf8ArrayByteBufferInt() {
        final ByteBuffer wrapper = ByteBuffer.wrap(rawBytes);
        Utf8Array otherConstructed = new Utf8Array(wrapper, wrapper.remaining());
        assertNotSame(rawBytes, otherConstructed.getBytes());
        assertArrayEquals(rawBytes, otherConstructed.getBytes());
    }

    @Test
    public final void testHashCode() {
        Utf8Array other = new Utf8Array(Utf8.toBytes(" a23456789"));
        assertFalse(other.hashCode() == toCheck.hashCode());
    }

    @Test
    public final void testWriteTo() {
        ByteBuffer b = ByteBuffer.allocate(rawBytes.length * 2);
        byte[] copy = new byte[rawBytes.length];
        toCheck.writeTo(b);
        assertEquals(rawBytes.length, b.position());
        b.position(0);
        b.limit(rawBytes.length);
        b.get(copy);
        assertArrayEquals(rawBytes, copy);
    }

    @Test
    public final void testGetByte() {
        assertEquals('8', toCheck.getByte(8));
    }

    @Test
    public final void testWrap() {
        ByteBuffer b1 = toCheck.wrap();
        ByteBuffer b2 = ByteBuffer.wrap(rawBytes);
        byte[] c1 = new byte[b1.remaining()];
        byte[] c2 = new byte[b2.remaining()];
        b1.get(c1);
        b2.get(c2);
        assertArrayEquals(c2, c1);
    }

    @Test
    public final void testIsEmpty() {
        assertFalse(toCheck.isEmpty());
        assertTrue(new Utf8Array(new byte[] {}).isEmpty());
    }

    @Test
    public final void testEqualsObject() {
        assertTrue(toCheck.equals(raw));
        assertFalse(toCheck.equals(new Utf8Array(new byte[] {})));
        assertFalse(toCheck.equals(new Utf8Array(Utf8.toBytes(" " + raw.substring(1)))));
        assertTrue(toCheck.equals(toCheck));
        assertTrue(toCheck.equals(new Utf8Array(rawBytes)));
    }

    @Test
    public final void testToString() {
        assertEquals(raw, toCheck.toString());
    }

    @Test
    public final void testCompareTo() {
        assertTrue(toCheck.compareTo(new Utf8Array(new byte[] {})) > 0);
        assertTrue(toCheck.compareTo(new Utf8Array(Utf8.toBytes(raw + raw))) < 0);
        assertTrue(toCheck.compareTo(new Utf8Array(Utf8.toBytes(" " + raw.substring(1)))) > 0);
        assertTrue(toCheck.compareTo(new Utf8Array(Utf8.toBytes("a" + raw.substring(1)))) < 0);
        assertTrue(toCheck.compareTo(new Utf8Array(rawBytes)) == 0);
    }

    @Test
    public final void testPartial() {
        final int length = 3;
        final int offset = 1;
        Utf8PartialArray partial = new Utf8PartialArray(rawBytes, offset, length);
        assertEquals(length, partial.getByteLength());
        assertEquals(offset, partial.getByteOffset());
        byte[] expected = new byte[length];
        ByteBuffer intermediate = ByteBuffer.allocate(rawBytes.length * 2);
        System.arraycopy(rawBytes, offset, expected, 0, length);
        partial.writeTo(intermediate);
        intermediate.flip();
        byte written[] = new byte[intermediate.remaining()];
        intermediate.get(written);
        assertArrayEquals(expected, written);
    }

    @Test
    public final void testUtf8Strings() {
        String nalle = "nalle";
        Utf8String utf = new Utf8String(new Utf8Array(Utf8.toBytes(nalle)));
        assertEquals('n', utf.charAt(0));
        assertEquals(nalle.length(), utf.length());
        assertEquals("alle", utf.subSequence(1, 5));
        assertTrue(utf.equals(new Utf8String(new Utf8Array(Utf8.toBytes(nalle)))));
        assertTrue(utf.equals(nalle));
    }

    @Test
    public final void testAscii7bitLowercase() {
        final byte [] expected = {
                0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 ,0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
                0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 ,0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
                0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 ,0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
                0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 ,0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
                0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 ,0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
                0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76 ,0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
                0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 ,0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
                0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76 ,0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
        };
        byte [] org = new byte[128];
        for (byte b = 0; b >= 0; b++) {
            org[b] = b;
        }
        assertArrayEquals(expected, new Utf8Array(org).ascii7BitLowerCase().getBytes());
    }
}