Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/VariableLengthIntBufferTest.java @ 21951:9c8c0937da41
Moving all sources into truffle subdirectory
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 17 Jun 2015 10:58:08 +0200 |
parents | graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/VariableLengthIntBufferTest.java@96c1d057a5ed |
children | dc83cc1f94f2 |
comparison
equal
deleted
inserted
replaced
21950:2a5011c7e641 | 21951:9c8c0937da41 |
---|---|
1 /* | |
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.api.test.nodes.serial; | |
24 | |
25 import java.nio.*; | |
26 | |
27 import org.junit.*; | |
28 | |
29 import com.oracle.truffle.api.nodes.serial.*; | |
30 | |
31 public class VariableLengthIntBufferTest { | |
32 | |
33 private VariableLengthIntBuffer buf; | |
34 | |
35 @Before | |
36 public void setUp() { | |
37 buf = new VariableLengthIntBuffer(ByteBuffer.allocate(512)); | |
38 } | |
39 | |
40 @After | |
41 public void tearDown() { | |
42 buf = null; | |
43 } | |
44 | |
45 @Test | |
46 public void testPutNull() { | |
47 buf.put(VariableLengthIntBuffer.NULL); | |
48 assertBytes(0xFF); | |
49 } | |
50 | |
51 @Test | |
52 public void testPutByteCornerCase0() { | |
53 buf.put(0x00); // 0 | |
54 assertBytes(0x00); | |
55 } | |
56 | |
57 @Test | |
58 public void testPutByteCornerCase1() { | |
59 buf.put(0x7F); // 127 | |
60 assertBytes(0x7F); | |
61 } | |
62 | |
63 @Test | |
64 public void testPutByteCornerCase2() { | |
65 buf.put(0x3FFF_FFFF); | |
66 assertBytes(0xBF, 0xFF, 0xFF, 0xFF); | |
67 } | |
68 | |
69 @Test(expected = IllegalArgumentException.class) | |
70 public void testPutByteCornerCase3() { | |
71 buf.put(0x4000_0000); // out of encodeable | |
72 } | |
73 | |
74 @Test | |
75 public void testGetNull() { | |
76 create(0xFF); | |
77 assertGet(VariableLengthIntBuffer.NULL); | |
78 } | |
79 | |
80 @Test | |
81 public void testGetCornerCase0() { | |
82 create(0x00); | |
83 assertGet(0x00); | |
84 } | |
85 | |
86 @Test | |
87 public void testGetCornerCase1() { | |
88 create(0x7F); | |
89 assertGet(0x7F); | |
90 } | |
91 | |
92 @Test | |
93 public void testGetCornerCase2() { | |
94 create(0xBF, 0xFF, 0xFF, 0xFF); | |
95 assertGet(0x3FFF_FFFF); | |
96 } | |
97 | |
98 @Test(expected = AssertionError.class) | |
99 public void testGetCornerCase3() { | |
100 create(0xFF, 0xFF, 0xFF, 0xFF); | |
101 assertGet(0x0); | |
102 } | |
103 | |
104 private void create(int... bytes) { | |
105 byte[] convBytes = new byte[bytes.length]; | |
106 for (int i = 0; i < bytes.length; i++) { | |
107 convBytes[i] = (byte) bytes[i]; | |
108 } | |
109 buf = new VariableLengthIntBuffer(convBytes); | |
110 } | |
111 | |
112 private void assertGet(int expected) { | |
113 Assert.assertEquals(expected, buf.get()); | |
114 } | |
115 | |
116 private void assertBytes(int... expectedBytes) { | |
117 byte[] actualBytes = buf.getBytes(); | |
118 Assert.assertEquals(expectedBytes.length, actualBytes.length); | |
119 for (int i = 0; i < expectedBytes.length; i++) { | |
120 Assert.assertTrue(actualBytes[i] == (byte) expectedBytes[i]); | |
121 } | |
122 } | |
123 | |
124 } |