comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/TextMapTest.java @ 13474:108ba3e82d3a

Truffle: add TextMap tests
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Sun, 22 Dec 2013 15:12:40 -0800
parents
children
comparison
equal deleted inserted replaced
13473:69f3251332c9 13474:108ba3e82d3a
1 /*
2 * Copyright (c) 2013, 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.utilities;
24
25 import static org.junit.Assert.*;
26
27 import org.junit.*;
28
29 import com.oracle.truffle.api.source.*;
30
31 public class TextMapTest {
32
33 final TextMap emptyTextMap = new TextMap("");
34
35 final TextMap emptyLineMap = new TextMap("\n");
36
37 private final TextMap shortMap = new TextMap("01");
38
39 private final TextMap longMap = new TextMap("01234\n67\n9\n");
40
41 @Test
42 public void emptyTextTest0() {
43 assertEquals(emptyTextMap.lineCount(), 0);
44 }
45
46 @Test(expected = IllegalArgumentException.class)
47 public void emptyTextTest1() {
48 emptyTextMap.offsetToLine(0);
49 }
50
51 @Test(expected = IllegalArgumentException.class)
52 public void emptyTextTest2() {
53 emptyTextMap.offsetToCol(0);
54 }
55
56 @Test(expected = IllegalArgumentException.class)
57 public void emptyTextTest3() {
58 emptyTextMap.lineStartOffset(-1);
59 }
60
61 @Test(expected = IllegalArgumentException.class)
62 public void emptyTextTest4() {
63 emptyTextMap.lineStartOffset(0);
64 }
65
66 @Test(expected = IllegalArgumentException.class)
67 public void emptyTextTest5() {
68 emptyTextMap.lineStartOffset(1);
69 }
70
71 @Test(expected = IllegalArgumentException.class)
72 public void emptyTextTest6() {
73 emptyTextMap.lineLength(1);
74 }
75
76 @Test
77 public void emptyLineTest0() {
78 assertEquals(emptyLineMap.lineCount(), 1);
79 assertEquals(emptyLineMap.offsetToLine(0), 1);
80 assertEquals(emptyLineMap.lineStartOffset(1), 0);
81 assertEquals(emptyLineMap.offsetToCol(0), 1);
82 assertEquals(emptyLineMap.lineLength(1), 0);
83 }
84
85 @Test(expected = IllegalArgumentException.class)
86 public void emptyLineTest1() {
87 emptyLineMap.offsetToLine(1);
88 }
89
90 @Test(expected = IllegalArgumentException.class)
91 public void emptyLineTest2() {
92 emptyLineMap.lineStartOffset(2);
93 }
94
95 @Test(expected = IllegalArgumentException.class)
96 public void emptyLineTest3() {
97 emptyLineMap.offsetToCol(1);
98 }
99
100 @Test(expected = IllegalArgumentException.class)
101 public void emptyLineTest4() {
102 emptyLineMap.lineLength(2);
103 }
104
105 @Test
106 public void shortTextTest0() {
107
108 assertEquals(shortMap.lineCount(), 1);
109
110 assertEquals(shortMap.offsetToLine(0), 1);
111 assertEquals(shortMap.lineStartOffset(1), 0);
112 assertEquals(shortMap.offsetToCol(0), 1);
113
114 assertEquals(shortMap.offsetToLine(1), 1);
115 assertEquals(shortMap.offsetToCol(1), 2);
116
117 assertEquals(shortMap.lineLength(1), 2);
118 }
119
120 @Test(expected = IllegalArgumentException.class)
121 public void shortTextTest1() {
122 shortMap.offsetToLine(-1);
123 }
124
125 @Test(expected = IllegalArgumentException.class)
126 public void shortTextTest2() {
127 shortMap.offsetToCol(-1);
128 }
129
130 @Test(expected = IllegalArgumentException.class)
131 public void shortTextTest3() {
132 shortMap.offsetToLine(2);
133 }
134
135 @Test(expected = IllegalArgumentException.class)
136 public void shortTextTest4() {
137 shortMap.offsetToCol(2);
138 }
139
140 @Test(expected = IllegalArgumentException.class)
141 public void shortTextTest5() {
142 shortMap.lineStartOffset(2);
143 }
144
145 @Test(expected = IllegalArgumentException.class)
146 public void shortTextTest6() {
147 shortMap.lineLength(2);
148 }
149
150 @Test
151 public void longTextTest0() {
152
153 assertEquals(longMap.lineCount(), 3);
154
155 assertEquals(longMap.offsetToLine(0), 1);
156 assertEquals(longMap.lineStartOffset(1), 0);
157 assertEquals(longMap.offsetToCol(0), 1);
158
159 assertEquals(longMap.offsetToLine(4), 1);
160 assertEquals(longMap.offsetToCol(4), 5);
161
162 assertEquals(longMap.offsetToLine(5), 1); // newline
163 assertEquals(longMap.offsetToCol(5), 6); // newline
164 assertEquals(longMap.lineLength(1), 5);
165
166 assertEquals(longMap.offsetToLine(6), 2);
167 assertEquals(longMap.lineStartOffset(2), 6);
168 assertEquals(longMap.offsetToCol(6), 1);
169
170 assertEquals(longMap.offsetToLine(7), 2);
171 assertEquals(longMap.offsetToCol(7), 2);
172
173 assertEquals(longMap.offsetToLine(8), 2); // newline
174 assertEquals(longMap.offsetToLine(8), 2); // newline
175 assertEquals(longMap.lineLength(2), 2);
176
177 assertEquals(longMap.offsetToLine(9), 3);
178 assertEquals(longMap.lineStartOffset(3), 9);
179 assertEquals(longMap.offsetToCol(9), 1);
180
181 assertEquals(longMap.offsetToLine(10), 3); // newline
182 assertEquals(longMap.offsetToCol(10), 2); // newline
183 assertEquals(longMap.lineLength(3), 1);
184
185 }
186
187 @Test(expected = IllegalArgumentException.class)
188 public void longTextTest1() {
189 longMap.offsetToLine(11);
190 }
191
192 @Test(expected = IllegalArgumentException.class)
193 public void longTextTest2() {
194 longMap.offsetToCol(11);
195 }
196
197 @Test(expected = IllegalArgumentException.class)
198 public void longTextTest3() {
199 longMap.lineStartOffset(4);
200 }
201
202 }