Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/serial/PostOrderDeserializerTest.java @ 12388:96c1d057a5ed
Truffle: Added experimental serialization API.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Wed, 02 Oct 2013 15:33:08 +0200 |
parents | |
children | 97321295c974 |
comparison
equal
deleted
inserted
replaced
12387:aff825fef0fd | 12388:96c1d057a5ed |
---|---|
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 import java.util.*; | |
27 | |
28 import org.junit.*; | |
29 | |
30 import com.oracle.truffle.api.nodes.*; | |
31 import com.oracle.truffle.api.nodes.serial.*; | |
32 import com.oracle.truffle.api.test.nodes.serial.TestNodes.EmptyNode; | |
33 import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithArray; | |
34 import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithFields; | |
35 import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithOneChild; | |
36 import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithThreeChilds; | |
37 import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithTwoArray; | |
38 import com.oracle.truffle.api.test.nodes.serial.TestNodes.NodeWithTwoChilds; | |
39 import com.oracle.truffle.api.test.nodes.serial.TestNodes.StringNode; | |
40 | |
41 public class PostOrderDeserializerTest { | |
42 | |
43 private PostOrderDeserializer d; | |
44 private TestSerializerConstantPool cp; | |
45 | |
46 @Before | |
47 public void setUp() { | |
48 cp = new TestSerializerConstantPool(); | |
49 d = new PostOrderDeserializer(cp); | |
50 } | |
51 | |
52 @After | |
53 public void tearDown() { | |
54 d = null; | |
55 cp = null; | |
56 } | |
57 | |
58 private Node deserialize(byte[] bytes) { | |
59 return d.deserialize(bytes, Node.class); | |
60 } | |
61 | |
62 @Test | |
63 public void testNull() { | |
64 createCP(); | |
65 Node ast = deserialize(createBytes(VariableLengthIntBuffer.NULL)); | |
66 Assert.assertNull(ast); | |
67 } | |
68 | |
69 @Test | |
70 public void testSingleNode() { | |
71 createCP(EmptyNode.class); | |
72 Node expectedAst = new EmptyNode(); | |
73 Node ast = deserialize(createBytes(0)); | |
74 assertAST(expectedAst, ast); | |
75 } | |
76 | |
77 @Test | |
78 public void testThreeChilds() { | |
79 createCP(EmptyNode.class, NodeWithThreeChilds.class); | |
80 Node expectedAst = new NodeWithThreeChilds(new EmptyNode(), null, new EmptyNode()); | |
81 Node ast = deserialize(createBytes(0, VariableLengthIntBuffer.NULL, 0, 1)); | |
82 assertAST(expectedAst, ast); | |
83 } | |
84 | |
85 @Test | |
86 public void testFields() { | |
87 createCP(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, | |
88 (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); | |
89 NodeWithFields expectedAst = new NodeWithFields("test", Integer.MIN_VALUE, Integer.MAX_VALUE, Long.MIN_VALUE, Long.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE, Double.MIN_VALUE, | |
90 Double.MAX_VALUE, Character.MIN_VALUE, Character.MAX_VALUE, Short.MIN_VALUE, Short.MAX_VALUE, Byte.MIN_VALUE, Byte.MAX_VALUE, Boolean.TRUE, Boolean.FALSE); | |
91 Node ast = deserialize(createBytes(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 10)); | |
92 assertAST(expectedAst, ast); | |
93 } | |
94 | |
95 @Test | |
96 public void testFieldsNull() { | |
97 createCP(NodeWithFields.class, "test", 0, 0L, 0.0F, 0.0D); | |
98 NodeWithFields expectedAst = new NodeWithFields("test", 0, null, 0L, null, 0f, null, 0d, null, (char) 0, null, (short) 0, null, (byte) 0, null, false, null); | |
99 int nil = VariableLengthIntBuffer.NULL; | |
100 Node ast = deserialize(createBytes(0, 1, 2, nil, 3, nil, 4, nil, 5, nil, 2, nil, 2, nil, 2, nil, 2, nil)); | |
101 assertAST(expectedAst, ast); | |
102 } | |
103 | |
104 @Test | |
105 public void testNullChilds() { | |
106 createCP(Node[].class, NodeWithArray.class); | |
107 Node expectedAst = new NodeWithArray(null); | |
108 Node ast = deserialize(createBytes(0, VariableLengthIntBuffer.NULL, 1)); | |
109 assertAST(expectedAst, ast); | |
110 } | |
111 | |
112 @Test | |
113 public void testNChilds() { | |
114 Node expectedAst = new NodeWithArray(new Node[]{new EmptyNode(), new NodeWithArray(new Node[]{new EmptyNode(), new EmptyNode(), new EmptyNode()}), new EmptyNode(), new EmptyNode()}); | |
115 createCP(Node[].class, 4, EmptyNode.class, 3, NodeWithArray.class); | |
116 Node ast = deserialize(createBytes(0, 1, 2, 0, 3, 2, 2, 2, 4, 2, 2, 4)); | |
117 | |
118 assertAST(expectedAst, ast); | |
119 } | |
120 | |
121 @Test | |
122 public void test2xNChilds() { | |
123 Node expectedAst = new NodeWithTwoArray(new Node[]{new StringNode("a0"), new StringNode("a1")}, new Node[]{new StringNode("b0"), new StringNode("b1"), new StringNode("b2")}); | |
124 createCP(Node[].class, 2, StringNode.class, "a0", "a1", 3, "b0", "b1", "b2", NodeWithTwoArray.class); | |
125 Node ast = deserialize(createBytes(0, 1, 2, 3, 2, 4, 0, 5, 2, 6, 2, 7, 2, 8, 9)); | |
126 | |
127 assertAST(expectedAst, ast); | |
128 } | |
129 | |
130 @Test | |
131 public void testBug0() { | |
132 Node expectedAst = new NodeWithArray(new Node[]{new NodeWithOneChild(new EmptyNode())}); | |
133 | |
134 createCP(Node[].class, 1, EmptyNode.class, NodeWithOneChild.class, NodeWithArray.class); | |
135 Node ast = deserialize(createBytes(0, 1, 2, 3, 4)); | |
136 assertAST(expectedAst, ast); | |
137 } | |
138 | |
139 @Test | |
140 public void testBug1() { | |
141 Node expectedAst = new NodeWithArray(new Node[]{new NodeWithTwoChilds(new EmptyNode(), new EmptyNode())}); | |
142 | |
143 createCP(Node[].class, 1, EmptyNode.class, NodeWithTwoChilds.class, NodeWithArray.class); | |
144 Node ast = deserialize(createBytes(0, 1, 2, 2, 3, 4)); | |
145 assertAST(expectedAst, ast); | |
146 } | |
147 | |
148 private static void assertAST(Node expectedAst, Node actualAst) { | |
149 if (expectedAst == null) { | |
150 Assert.assertNull(actualAst); | |
151 return; | |
152 } | |
153 | |
154 Assert.assertNotNull(actualAst); | |
155 // fields are asserted using the corresponding equals implementation | |
156 Assert.assertEquals(expectedAst, actualAst); | |
157 | |
158 Iterable<Node> expectedChildIterator = expectedAst.getChildren(); | |
159 Iterator<Node> actualChildIterator = actualAst.getChildren().iterator(); | |
160 for (Node node : expectedChildIterator) { | |
161 Assert.assertTrue(actualChildIterator.hasNext()); | |
162 assertAST(node, actualChildIterator.next()); | |
163 } | |
164 Assert.assertFalse(actualChildIterator.hasNext()); | |
165 } | |
166 | |
167 private static byte[] createBytes(int... refs) { | |
168 VariableLengthIntBuffer buf = new VariableLengthIntBuffer(ByteBuffer.allocate(512)); | |
169 for (int i = 0; i < refs.length; i++) { | |
170 buf.put(refs[i]); | |
171 } | |
172 return buf.getBytes(); | |
173 } | |
174 | |
175 private void createCP(Object... cpData) { | |
176 for (int i = 0; i < cpData.length; i++) { | |
177 Object object = cpData[i]; | |
178 | |
179 cp.putObject(object.getClass(), object); | |
180 | |
181 } | |
182 } | |
183 | |
184 } |