|
4 | 4 | import static org.junit.Assert.assertThrows;
|
5 | 5 |
|
6 | 6 | import org.junit.Test;
|
| 7 | +import org.junit.runner.RunWith; |
7 | 8 |
|
| 9 | +import com.google.testing.junit.testparameterinjector.TestParameter; |
| 10 | +import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
8 | 11 | import com.plasstech.lang.c.typecheck.Type;
|
9 | 12 |
|
| 13 | +@RunWith(TestParameterInjector.class) |
10 | 14 | public class ScannerTest {
|
11 | 15 |
|
12 | 16 | @Test
|
@@ -166,11 +170,9 @@ public void nextTokenUnsignedLongConstant() {
|
166 | 170 | }
|
167 | 171 |
|
168 | 172 | @Test
|
169 |
| - public void nextTokenBadIntConstant() { |
170 |
| - assertThrows(ScannerException.class, () -> new Scanner("0a").nextToken()); |
171 |
| - assertThrows(ScannerException.class, () -> new Scanner("23B").nextToken()); |
172 |
| - assertThrows(ScannerException.class, () -> new Scanner("234.").nextToken()); |
173 |
| - assertThrows(ScannerException.class, () -> new Scanner("234L.").nextToken()); |
| 173 | + public void nextTokenBadIntConstant( |
| 174 | + @TestParameter({"0a", "23B", "234L."}) String token) { |
| 175 | + assertThrows(ScannerException.class, () -> new Scanner(token).nextToken()); |
174 | 176 | }
|
175 | 177 |
|
176 | 178 | @Test
|
@@ -240,4 +242,69 @@ public void nextTokenBadSymbol() {
|
240 | 242 | assertThrows(ScannerException.class, () -> new Scanner("\\").nextToken());
|
241 | 243 | assertThrows(ScannerException.class, () -> new Scanner("/*").nextToken());
|
242 | 244 | }
|
| 245 | + |
| 246 | + @Test |
| 247 | + public void nextTokenDoubleConstants() { |
| 248 | + Scanner s = new Scanner("1. 1.0 0.1 .1"); |
| 249 | + Token t = s.nextToken(); |
| 250 | + assertThat(t.type()).isEqualTo(TokenType.NUMERIC_LITERAL); |
| 251 | + assertThat(t.varType()).isEqualTo(Type.DOUBLE); |
| 252 | + assertThat(t.value()).isEqualTo("1."); |
| 253 | + t = s.nextToken(); |
| 254 | + assertThat(t.type()).isEqualTo(TokenType.NUMERIC_LITERAL); |
| 255 | + assertThat(t.varType()).isEqualTo(Type.DOUBLE); |
| 256 | + assertThat(t.value()).isEqualTo("1.0"); |
| 257 | + t = s.nextToken(); |
| 258 | + assertThat(t.type()).isEqualTo(TokenType.NUMERIC_LITERAL); |
| 259 | + assertThat(t.varType()).isEqualTo(Type.DOUBLE); |
| 260 | + assertThat(t.value()).isEqualTo("0.1"); |
| 261 | + t = s.nextToken(); |
| 262 | + assertThat(t.type()).isEqualTo(TokenType.NUMERIC_LITERAL); |
| 263 | + assertThat(t.varType()).isEqualTo(Type.DOUBLE); |
| 264 | + assertThat(t.value()).isEqualTo(".1"); |
| 265 | + assertThat(s.nextToken().type()).isEqualTo(TokenType.EOF); |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + public void nextTokenDoubleConstantsWeird() { |
| 270 | + Scanner s = new Scanner(".00004 00.000005"); |
| 271 | + Token t = s.nextToken(); |
| 272 | + assertThat(t.type()).isEqualTo(TokenType.NUMERIC_LITERAL); |
| 273 | + assertThat(t.varType()).isEqualTo(Type.DOUBLE); |
| 274 | + assertThat(t.value()).isEqualTo(".00004"); |
| 275 | + t = s.nextToken(); |
| 276 | + assertThat(t.type()).isEqualTo(TokenType.NUMERIC_LITERAL); |
| 277 | + assertThat(t.varType()).isEqualTo(Type.DOUBLE); |
| 278 | + assertThat(t.value()).isEqualTo("00.000005"); |
| 279 | + t = s.nextToken(); |
| 280 | + assertThat(s.nextToken().type()).isEqualTo(TokenType.EOF); |
| 281 | + } |
| 282 | + |
| 283 | + @Test |
| 284 | + public void nextTokenDoubleConstantsWithExp() { |
| 285 | + Scanner s = new Scanner("1E0 1.0E0 0.1E+1 .1e-123"); |
| 286 | + Token t = s.nextToken(); |
| 287 | + assertThat(t.type()).isEqualTo(TokenType.NUMERIC_LITERAL); |
| 288 | + assertThat(t.varType()).isEqualTo(Type.DOUBLE); |
| 289 | + assertThat(t.value()).isEqualTo("1E0"); |
| 290 | + t = s.nextToken(); |
| 291 | + assertThat(t.type()).isEqualTo(TokenType.NUMERIC_LITERAL); |
| 292 | + assertThat(t.varType()).isEqualTo(Type.DOUBLE); |
| 293 | + assertThat(t.value()).isEqualTo("1.0E0"); |
| 294 | + t = s.nextToken(); |
| 295 | + assertThat(t.type()).isEqualTo(TokenType.NUMERIC_LITERAL); |
| 296 | + assertThat(t.varType()).isEqualTo(Type.DOUBLE); |
| 297 | + assertThat(t.value()).isEqualTo("0.1E+1"); |
| 298 | + t = s.nextToken(); |
| 299 | + assertThat(t.type()).isEqualTo(TokenType.NUMERIC_LITERAL); |
| 300 | + assertThat(t.varType()).isEqualTo(Type.DOUBLE); |
| 301 | + assertThat(t.value()).isEqualTo(".1e-123"); |
| 302 | + assertThat(s.nextToken().type()).isEqualTo(TokenType.EOF); |
| 303 | + } |
| 304 | + |
| 305 | + @Test |
| 306 | + public void nextTokenBadFloats( |
| 307 | + @TestParameter({"1.e-10x", "2._", "1E2x", "1.0e10.0"}) String token) { |
| 308 | + assertThrows(ScannerException.class, () -> new Scanner(token).nextToken()); |
| 309 | + } |
243 | 310 | }
|
0 commit comments