Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/PostOrderSerializerTest.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/PostOrderSerializerTest.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 org.junit.*; | |
26 | |
27 import com.oracle.truffle.api.nodes.*; | |
28 import com.oracle.truffle.api.nodes.serial.*; | |
29 import com.oracle.truffle.api.test.nodes.serial.TestNodes.EmptyNode; | |
30 import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithArray; | |
31 import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithFields; | |
32 import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithThreeChilds; | |
33 import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithTwoArray; | |
34 import com.oracle.truffle.api.test.nodes.serial.TestNodes.StringNode; | |
35 | |
36 public class PostOrderSerializerTest { | |
37 | |
38 private PostOrderSerializer s; | |
39 private TestSerializerConstantPool cp; | |
40 | |
41 @Before | |
42 public void setUp() { | |
43 cp = new TestSerializerConstantPool(); | |
44 s = new PostOrderSerializer(cp); | |
45 } | |
46 | |
47 @After | |
48 public void tearDown() { | |
49 cp = null; | |
50 s = null; | |
51 } | |
52 | |
53 @Test | |
54 public void testNull() { | |
55 Node ast = null; | |
56 assertBytes(s.serialize(ast), VariableLengthIntBuffer.NULL); | |
57 assertCP(); | |
58 } | |
59 | |
60 @Test | |
61 public void testSingleEmptyNode() { | |
62 Node ast = new EmptyNode(); | |
63 assertBytes(s.serialize(ast), 0); | |
64 assertCP(EmptyNode.class); | |
65 } | |
66 | |
67 @Test | |
68 public void testThreeChilds() { | |
69 Node ast = new NodeWithThreeChilds(new EmptyNode(), null, new EmptyNode()); | |
70 assertBytes(s.serialize(ast), 0, VariableLengthIntBuffer.NULL, 0, 1); | |
71 assertCP(EmptyNode.class, NodeWithThreeChilds.class); | |
72 } | |
73 | |
74 @Test | |
75 public void testFields() { | |
76 NodeWithFields ast = new NodeWithFields("test", Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE, Double.MIN_VALUE, Double.MAX_VALUE, | |
77 Character.MIN_VALUE, Character.MAX_VALUE, Short.MIN_VALUE, Short.MAX_VALUE, Byte.MIN_VALUE, Byte.MAX_VALUE, Boolean.TRUE, Boolean.FALSE); | |
78 assertBytes(s.serialize(ast), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 10); | |
79 assertCP(NodeWithFields.class, "test", Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE, Double.MIN_VALUE, Double.MAX_VALUE, | |
80 (int) Character.MIN_VALUE, (int) Character.MAX_VALUE, (int) Short.MIN_VALUE, (int) Short.MAX_VALUE, (int) Byte.MIN_VALUE, (int) Byte.MAX_VALUE, 1); | |
81 } | |
82 | |
83 @Test | |
84 public void testFieldsNull() { | |
85 NodeWithFields ast = new NodeWithFields("test", 0, null, 0L, null, 0f, null, 0d, null, (char) 0, null, (short) 0, null, (byte) 0, null, false, null); | |
86 int nil = VariableLengthIntBuffer.NULL; | |
87 assertBytes(s.serialize(ast), 0, 1, 2, nil, 3, nil, 4, nil, 5, nil, 2, nil, 2, nil, 2, nil, 2, nil); | |
88 assertCP(NodeWithFields.class, "test", 0, 0L, 0.0F, 0.0D); | |
89 } | |
90 | |
91 @Test | |
92 public void testNChilds() { | |
93 Node ast = new NodeWithArray(new Node[]{new EmptyNode(), new NodeWithArray(new Node[]{new EmptyNode(), new EmptyNode(), new EmptyNode()}), new EmptyNode(), new EmptyNode()}); | |
94 assertBytes(s.serialize(ast), 0, 1, 2, 0, 3, 2, 2, 2, 4, 2, 2, 4); | |
95 assertCP(Node[].class, 4, EmptyNode.class, 3, NodeWithArray.class); | |
96 } | |
97 | |
98 @Test | |
99 public void testNullChilds() { | |
100 Node ast = new NodeWithArray(null); | |
101 assertBytes(s.serialize(ast), 0, VariableLengthIntBuffer.NULL, 1); | |
102 assertCP(Node[].class, NodeWithArray.class); | |
103 } | |
104 | |
105 @Test | |
106 public void test2xNChilds() { | |
107 Node ast = new NodeWithTwoArray(new Node[]{new StringNode("a0"), new StringNode("a1")}, new Node[]{new StringNode("b0"), new StringNode("b1"), new StringNode("b2")}); | |
108 assertBytes(s.serialize(ast), 0, 1, 2, 3, 2, 4, 0, 5, 2, 6, 2, 7, 2, 8, 9); | |
109 assertCP(Node[].class, 2, StringNode.class, "a0", "a1", 3, "b0", "b1", "b2", NodeWithTwoArray.class); | |
110 } | |
111 | |
112 private static void assertBytes(byte[] actualBytes, int... expectedIndexes) { | |
113 VariableLengthIntBuffer buf = new VariableLengthIntBuffer(actualBytes); | |
114 for (int i = 0; i < expectedIndexes.length; i++) { | |
115 Assert.assertTrue("Unexpected EOF " + i, buf.hasRemaining()); | |
116 Assert.assertEquals("Index at pos " + i + ".", expectedIndexes[i], buf.get()); | |
117 } | |
118 Assert.assertFalse(buf.hasRemaining()); | |
119 } | |
120 | |
121 private void assertCP(Object... object) { | |
122 for (int i = 0; i < object.length; i++) { | |
123 Object cpvalue = object[i]; | |
124 Assert.assertNotNull("CP at index " + i, cpvalue); | |
125 Assert.assertEquals("CP at index " + i, cpvalue, cp.getObject(cpvalue.getClass(), i)); | |
126 } | |
127 Assert.assertEquals(object.length, cp.getIndex()); | |
128 } | |
129 | |
130 } |