Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/SourceSectionTest.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.dsl.test/src/com/oracle/truffle/api/dsl/test/SourceSectionTest.java@08aa0372dad4 |
children | b2d1c8ff592a |
comparison
equal
deleted
inserted
replaced
21950:2a5011c7e641 | 21951:9c8c0937da41 |
---|---|
1 /* | |
2 * Copyright (c) 2014, 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.dsl.test; | |
24 | |
25 import static com.oracle.truffle.api.dsl.test.TestHelper.*; | |
26 import static org.hamcrest.CoreMatchers.*; | |
27 import static org.junit.Assert.*; | |
28 | |
29 import org.junit.*; | |
30 import org.junit.experimental.theories.*; | |
31 import org.junit.runner.*; | |
32 | |
33 import com.oracle.truffle.api.dsl.*; | |
34 import com.oracle.truffle.api.dsl.internal.*; | |
35 import com.oracle.truffle.api.dsl.test.SourceSectionTestFactory.SourceSection0Factory; | |
36 import com.oracle.truffle.api.dsl.test.SourceSectionTestFactory.SourceSection1Factory; | |
37 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ArgumentNode; | |
38 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; | |
39 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | |
40 import com.oracle.truffle.api.nodes.*; | |
41 import com.oracle.truffle.api.source.*; | |
42 | |
43 @RunWith(Theories.class) | |
44 public class SourceSectionTest { | |
45 | |
46 @DataPoints public static final int[] data = new int[]{1, 2, 3, 4}; | |
47 | |
48 @Theory | |
49 public void testSourceSections(int value0, int value1, int value2) { | |
50 TestRootNode<SourceSection0> root = createRoot(SourceSection0Factory.getInstance()); | |
51 SourceSection section = new NullSourceSection("a", "b"); | |
52 root.getNode().assignSourceSection(section); | |
53 expectSourceSection(root.getNode(), section); | |
54 assertThat((int) executeWith(root, value0), is(value0)); | |
55 expectSourceSection(root.getNode(), section); | |
56 assertThat((int) executeWith(root, value1), is(value1)); | |
57 expectSourceSection(root.getNode(), section); | |
58 assertThat((int) executeWith(root, value2), is(value2)); | |
59 expectSourceSection(root.getNode(), section); | |
60 } | |
61 | |
62 private static void expectSourceSection(Node root, SourceSection section) { | |
63 assertThat(root.getSourceSection(), is(sameInstance(section))); | |
64 for (Node child : root.getChildren()) { | |
65 if (child instanceof ArgumentNode || child instanceof SpecializationNode) { | |
66 continue; | |
67 } | |
68 if (child != null) { | |
69 expectSourceSection(child, section); | |
70 } | |
71 } | |
72 } | |
73 | |
74 @NodeChild("a") | |
75 static class SourceSection0 extends ValueNode { | |
76 | |
77 @Specialization(guards = "a == 1") | |
78 int do1(int a) { | |
79 return a; | |
80 } | |
81 | |
82 @Specialization(guards = "a == 2") | |
83 int do2(int a) { | |
84 return a; | |
85 } | |
86 | |
87 @Specialization(guards = "a == 3") | |
88 int do3(int a) { | |
89 return a; | |
90 } | |
91 | |
92 @Fallback | |
93 Object do4(Object a) { | |
94 return a; // the generic answer to all questions | |
95 } | |
96 } | |
97 | |
98 @Test | |
99 public void testCreateCast() { | |
100 SourceSection section = new NullSourceSection("a", "b"); | |
101 TestRootNode<SourceSection1> root = createRootPrefix(SourceSection1Factory.getInstance(), true, section); | |
102 expectSourceSection(root.getNode(), section); | |
103 assertThat((int) executeWith(root, 1), is(1)); | |
104 expectSourceSection(root.getNode(), section); | |
105 } | |
106 | |
107 @NodeChild("a") | |
108 static class SourceSection1 extends ValueNode { | |
109 | |
110 public SourceSection1(SourceSection section) { | |
111 super(section); | |
112 } | |
113 | |
114 @CreateCast("a") | |
115 public ValueNode cast(ValueNode node) { | |
116 assert getSourceSection() != null; | |
117 return node; | |
118 } | |
119 | |
120 @Specialization | |
121 int do0(int a) { | |
122 return a; | |
123 } | |
124 | |
125 } | |
126 } |