1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.mojo.bindings; 6 7 import android.test.suitebuilder.annotation.SmallTest; 8 9 import junit.framework.TestCase; 10 11 import java.nio.charset.Charset; 12 13 /** 14 * Testing {@link BindingsHelper}. 15 */ 16 public class BindingsHelperTest extends TestCase { 17 18 /** 19 * Testing {@link BindingsHelper#utf8StringSizeInBytes(String)}. 20 */ 21 @SmallTest testUTF8StringLength()22 public void testUTF8StringLength() { 23 String[] stringsToTest = { 24 "", 25 "a", 26 "hello world", 27 "éléphant", 28 "", 29 "你午饭想吃什么", 30 "你午饭想吃什么\0éléphant", 31 }; 32 for (String s : stringsToTest) { 33 assertEquals(s.getBytes(Charset.forName("utf8")).length, 34 BindingsHelper.utf8StringSizeInBytes(s)); 35 } 36 assertEquals(1, BindingsHelper.utf8StringSizeInBytes("\0")); 37 String s = new StringBuilder().appendCodePoint(0x0).appendCodePoint(0x80). 38 appendCodePoint(0x800).appendCodePoint(0x10000).toString(); 39 assertEquals(10, BindingsHelper.utf8StringSizeInBytes(s)); 40 assertEquals(10, s.getBytes(Charset.forName("utf8")).length); 41 } 42 43 /** 44 * Testing {@link BindingsHelper#align(int)}. 45 */ 46 @SmallTest testAlign()47 public void testAlign() { 48 for (int i = 0; i < 3 * BindingsHelper.ALIGNMENT; ++i) { 49 int j = BindingsHelper.align(i); 50 assertTrue(j >= i); 51 assertTrue(j % BindingsHelper.ALIGNMENT == 0); 52 assertTrue(j - i < BindingsHelper.ALIGNMENT); 53 } 54 } 55 } 56