summaryrefslogtreecommitdiff
path: root/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsHelperTest.java
blob: 38bd3482e439709e492dfb41cf0cb7001b8d4665 (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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.mojo.bindings;

import android.support.test.filters.SmallTest;

import junit.framework.TestCase;

import java.nio.charset.Charset;

/**
 * Testing {@link BindingsHelper}.
 */
public class BindingsHelperTest extends TestCase {

    /**
     * Testing {@link BindingsHelper#utf8StringSizeInBytes(String)}.
     */
    @SmallTest
    public void testUTF8StringLength() {
        String[] stringsToTest = {
            "",
            "a",
            "hello world",
            "éléphant",
            "𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕",
            "你午饭想吃什么",
            "你午饭想吃什么\0éléphant",
        };
        for (String s : stringsToTest) {
            assertEquals(s.getBytes(Charset.forName("utf8")).length,
                    BindingsHelper.utf8StringSizeInBytes(s));
        }
        assertEquals(1, BindingsHelper.utf8StringSizeInBytes("\0"));
        String s = new StringBuilder().appendCodePoint(0x0).appendCodePoint(0x80)
                .appendCodePoint(0x800).appendCodePoint(0x10000).toString();
        assertEquals(10, BindingsHelper.utf8StringSizeInBytes(s));
        assertEquals(10, s.getBytes(Charset.forName("utf8")).length);
    }

    /**
     * Testing {@link BindingsHelper#align(int)}.
     */
    @SmallTest
    public void testAlign() {
        for (int i = 0; i < 3 * BindingsHelper.ALIGNMENT; ++i) {
            int j = BindingsHelper.align(i);
            assertTrue(j >= i);
            assertTrue(j % BindingsHelper.ALIGNMENT == 0);
            assertTrue(j - i < BindingsHelper.ALIGNMENT);
        }
    }
}