diff options
Diffstat (limited to 'src/test/java/org/anarres/cpp')
-rw-r--r-- | src/test/java/org/anarres/cpp/BuildMetadataTest.java | 33 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/CppReaderTest.java | 61 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/ErrorTest.java | 65 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/IncludeAbsoluteTest.java | 35 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/JavaFileSystemTest.java | 39 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/JoinReaderTest.java | 41 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/LexerSourceTest.java | 140 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/MainTest.java | 11 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/NumericValueTest.java | 95 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/PreprocessorTest.java | 237 | ||||
-rw-r--r-- | src/test/java/org/anarres/cpp/TokenPastingWhitespaceTest.java | 43 |
11 files changed, 0 insertions, 800 deletions
diff --git a/src/test/java/org/anarres/cpp/BuildMetadataTest.java b/src/test/java/org/anarres/cpp/BuildMetadataTest.java deleted file mode 100644 index 42dc071..0000000 --- a/src/test/java/org/anarres/cpp/BuildMetadataTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.anarres.cpp; - -import com.google.common.base.Charsets; -import com.google.common.io.Resources; -import java.net.URL; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - * @author shevek - */ -public class BuildMetadataTest { - - private static final Logger LOG = LoggerFactory.getLogger(BuildMetadataTest.class); - - @Test - public void testProperties() throws Exception { - URL url = Resources.getResource("META-INF/jcpp.properties"); - String text = Resources.asCharSource(url, Charsets.ISO_8859_1).read(); - LOG.info("Metadata is " + text); - } - - @Test - public void testMetadata() throws Exception { - BuildMetadata metadata = BuildMetadata.getInstance(); - LOG.info("Version is " + metadata.getVersion()); - LOG.info("BuildDate is " + metadata.getBuildDate()); - LOG.info("ChangeId is " + metadata.getChangeId()); - } - -} diff --git a/src/test/java/org/anarres/cpp/CppReaderTest.java b/src/test/java/org/anarres/cpp/CppReaderTest.java deleted file mode 100644 index c901722..0000000 --- a/src/test/java/org/anarres/cpp/CppReaderTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.anarres.cpp; - -import java.io.BufferedReader; -import java.io.StringReader; -import java.util.Collections; -import javax.annotation.Nonnull; -import org.junit.Test; -import static org.junit.Assert.assertEquals; - -public class CppReaderTest { - - public static String testCppReader(@Nonnull String in, Feature... f) - throws Exception { - System.out.println("Testing " + in); - StringReader r = new StringReader(in); - CppReader p = new CppReader(r); - p.getPreprocessor().setSystemIncludePath( - Collections.singletonList("src/test/resources") - ); - p.getPreprocessor().addFeatures(f); - BufferedReader b = new BufferedReader(p); - - StringBuilder out = new StringBuilder(); - String line; - while ((line = b.readLine()) != null) { - System.out.println(" >> " + line); - out.append(line).append("\n"); - } - - return out.toString(); - } - - @Test - public void testCppReader() - throws Exception { - testCppReader("#include <test0.h>\n", Feature.LINEMARKERS); - } - - @Test - public void testVarargs() - throws Exception { - // The newlines are irrelevant, We want exactly one "foo" - testCppReader("#include <varargs.c>\n"); - } - - @Test - public void testPragmaOnce() - throws Exception { - // The newlines are irrelevant, We want exactly one "foo" - String out = testCppReader("#include <once.c>\n", Feature.PRAGMA_ONCE); - assertEquals("foo", out.trim()); - } - - @Test - public void testPragmaOnceWithMarkers() - throws Exception { - // The newlines are irrelevant, We want exactly one "foo" - testCppReader("#include <once.c>\n", Feature.PRAGMA_ONCE, Feature.LINEMARKERS); - } - -} diff --git a/src/test/java/org/anarres/cpp/ErrorTest.java b/src/test/java/org/anarres/cpp/ErrorTest.java deleted file mode 100644 index 42240d4..0000000 --- a/src/test/java/org/anarres/cpp/ErrorTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package org.anarres.cpp; - -import java.io.IOException; -import org.junit.Test; -import static org.anarres.cpp.Token.*; -import static org.junit.Assert.*; - -public class ErrorTest { - - private boolean testError(Preprocessor p) - throws LexerException, - IOException { - for (;;) { - Token tok = p.token(); - if (tok.getType() == EOF) - break; - if (tok.getType() == INVALID) - return true; - } - return false; - } - - private void testError(String input) throws Exception { - StringLexerSource sl; - DefaultPreprocessorListener pl; - Preprocessor p; - - /* Without a PreprocessorListener, throws an exception. */ - sl = new StringLexerSource(input, true); - p = new Preprocessor(); - p.addFeature(Feature.CSYNTAX); - p.addInput(sl); - try { - assertTrue(testError(p)); - fail("Lexing unexpectedly succeeded without listener."); - } catch (LexerException e) { - /* required */ - } - - /* With a PreprocessorListener, records the error. */ - sl = new StringLexerSource(input, true); - p = new Preprocessor(); - p.addFeature(Feature.CSYNTAX); - p.addInput(sl); - pl = new DefaultPreprocessorListener(); - p.setListener(pl); - assertNotNull("CPP has listener", p.getListener()); - assertTrue(testError(p)); - assertTrue("Listener has errors", pl.getErrors() > 0); - - /* Without CSYNTAX, works happily. */ - sl = new StringLexerSource(input, true); - p = new Preprocessor(); - p.addInput(sl); - assertTrue(testError(p)); - } - - @Test - public void testErrors() throws Exception { - testError("\""); - testError("'"); - // testError("''"); - } - -} diff --git a/src/test/java/org/anarres/cpp/IncludeAbsoluteTest.java b/src/test/java/org/anarres/cpp/IncludeAbsoluteTest.java deleted file mode 100644 index df7ffb7..0000000 --- a/src/test/java/org/anarres/cpp/IncludeAbsoluteTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.anarres.cpp; - -import com.google.common.io.CharStreams; -import java.io.BufferedReader; -import java.io.File; -import java.io.Reader; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import static org.junit.Assert.*; - -/** - * - * @author shevek - */ -public class IncludeAbsoluteTest { - - private static final Logger LOG = LoggerFactory.getLogger(IncludeAbsoluteTest.class); - - @Test - public void testAbsoluteInclude() throws Exception { - File file = new File("build/resources/test/absolute.h"); - assertTrue(file.exists()); - - String input = "#include <" + file.getAbsolutePath() + ">\n"; - LOG.info("Input: " + input); - Preprocessor pp = new Preprocessor(); - pp.addInput(new StringLexerSource(input, true)); - Reader r = new CppReader(pp); - String output = CharStreams.toString(r); - r.close(); - LOG.info("Output: " + output); - assertTrue(output.contains("absolute-result")); - } -} diff --git a/src/test/java/org/anarres/cpp/JavaFileSystemTest.java b/src/test/java/org/anarres/cpp/JavaFileSystemTest.java deleted file mode 100644 index 6e6f834..0000000 --- a/src/test/java/org/anarres/cpp/JavaFileSystemTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.anarres.cpp; - -import java.io.FileNotFoundException; -import org.junit.Test; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class JavaFileSystemTest { - - @Test - public void testJavaFileSystem() throws Exception { - JavaFileSystem fs = new JavaFileSystem(); - VirtualFile f; - - /* Anyone who has this file on their Unix box is messed up. */ - f = fs.getFile("/foo/bar baz"); - try { - f.getSource(); /* drop on floor */ - - assertTrue("Got a source for a non-file", f.isFile()); - } catch (FileNotFoundException e) { - assertFalse("Got no source for a file", f.isFile()); - } - - /* We hope we have this. */ - f = fs.getFile("/usr/include/stdio.h"); - try { - f.getSource(); /* drop on floor */ - - System.out.println("Opened stdio.h"); - assertTrue("Got a source for a non-file", f.isFile()); - } catch (FileNotFoundException e) { - System.out.println("Failed to open stdio.h"); - assertFalse("Got no source for a file", f.isFile()); - } - - } - -} diff --git a/src/test/java/org/anarres/cpp/JoinReaderTest.java b/src/test/java/org/anarres/cpp/JoinReaderTest.java deleted file mode 100644 index bb9cc2d..0000000 --- a/src/test/java/org/anarres/cpp/JoinReaderTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.anarres.cpp; - -import java.io.StringReader; -import org.junit.Test; -import static org.junit.Assert.assertEquals; - -public class JoinReaderTest { - - private void testJoinReader(String in, String out, boolean tg) - throws Exception { - System.out.println("Testing " + in + " => " + out); - StringReader r = new StringReader(in); - JoinReader j = new JoinReader(r, tg); - - for (int i = 0; i < out.length(); i++) { - int c = j.read(); - System.out.println("At offset " + i + ": " + (char) c); - assertEquals(out.charAt(i), c); - } - assertEquals(-1, j.read()); - assertEquals(-1, j.read()); - } - - private void testJoinReader(String in, String out) - throws Exception { - testJoinReader(in, out, true); - testJoinReader(in, out, false); - } - - @Test - public void testJoinReader() - throws Exception { - testJoinReader("ab", "ab"); - testJoinReader("a\\b", "a\\b"); - testJoinReader("a\nb", "a\nb"); - testJoinReader("a\\\nb", "ab\n"); - testJoinReader("foo??(bar", "foo[bar", true); - testJoinReader("foo??/\nbar", "foobar\n", true); - } - -} diff --git a/src/test/java/org/anarres/cpp/LexerSourceTest.java b/src/test/java/org/anarres/cpp/LexerSourceTest.java deleted file mode 100644 index 96ec4a3..0000000 --- a/src/test/java/org/anarres/cpp/LexerSourceTest.java +++ /dev/null @@ -1,140 +0,0 @@ -package org.anarres.cpp; - -import java.util.Arrays; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import static org.anarres.cpp.PreprocessorTest.assertType; -import static org.anarres.cpp.Token.*; -import static org.junit.Assert.*; - -public class LexerSourceTest { - - private static final Logger LOG = LoggerFactory.getLogger(LexerSourceTest.class); - - public static void testLexerSource(String in, boolean textmatch, int... out) - throws Exception { - LOG.info("Testing '" + in + "' => " - + Arrays.toString(out)); - StringLexerSource s = new StringLexerSource(in); - - StringBuilder buf = new StringBuilder(); - for (int i = 0; i < out.length; i++) { - Token tok = s.token(); - LOG.info("Token is " + tok); - assertType(out[i], tok); - // assertEquals(col, tok.getColumn()); - buf.append(tok.getText()); - } - - Token tok = s.token(); - LOG.info("Token is " + tok); - assertType(EOF, tok); - - if (textmatch) - assertEquals(in, buf.toString()); - } - - @Test - public void testLexerSource() - throws Exception { - - testLexerSource("int a = 5;", true, - IDENTIFIER, WHITESPACE, IDENTIFIER, WHITESPACE, - '=', WHITESPACE, NUMBER, ';' - ); - - // \n is WHITESPACE because ppvalid = false - testLexerSource("# # \r\n\n\r \rfoo", true, - HASH, WHITESPACE, '#', WHITESPACE, IDENTIFIER - ); - - // No match - trigraphs - testLexerSource("%:%:", false, PASTE); - testLexerSource("%:?", false, '#', '?'); - testLexerSource("%:%=", false, '#', MOD_EQ); - - testLexerSource("0x1234ffdUL 0765I", true, - NUMBER, WHITESPACE, NUMBER); - - testLexerSource("+= -= *= /= %= <= >= >>= <<= &= |= ^= x", true, - PLUS_EQ, WHITESPACE, - SUB_EQ, WHITESPACE, - MULT_EQ, WHITESPACE, - DIV_EQ, WHITESPACE, - MOD_EQ, WHITESPACE, - LE, WHITESPACE, - GE, WHITESPACE, - RSH_EQ, WHITESPACE, - LSH_EQ, WHITESPACE, - AND_EQ, WHITESPACE, - OR_EQ, WHITESPACE, - XOR_EQ, WHITESPACE, - IDENTIFIER); - - testLexerSource("/**/", true, CCOMMENT); - testLexerSource("/* /**/ */", true, CCOMMENT, WHITESPACE, '*', '/'); - testLexerSource("/** ** **/", true, CCOMMENT); - testLexerSource("//* ** **/", true, CPPCOMMENT); - testLexerSource("'\\r' '\\xf' '\\xff' 'x' 'aa' ''", true, - CHARACTER, WHITESPACE, - CHARACTER, WHITESPACE, - CHARACTER, WHITESPACE, - CHARACTER, WHITESPACE, - SQSTRING, WHITESPACE, - SQSTRING); - - if (false) // Actually, I think this is illegal. - testLexerSource("1i1I1l1L1ui1ul", true, - NUMBER, NUMBER, - NUMBER, NUMBER, - NUMBER, NUMBER); - - testLexerSource("'' 'x' 'xx'", true, - SQSTRING, WHITESPACE, CHARACTER, WHITESPACE, SQSTRING); - } - - @Test - public void testNumbers() throws Exception { - testLexerSource("0", true, NUMBER); - testLexerSource("045", true, NUMBER); - testLexerSource("45", true, NUMBER); - testLexerSource("0.45", true, NUMBER); - testLexerSource("1.45", true, NUMBER); - testLexerSource("1e6", true, NUMBER); - testLexerSource("1.45e6", true, NUMBER); - testLexerSource(".45e6", true, NUMBER); - testLexerSource("-6", true, '-', NUMBER); - } - - @Test - public void testNumbersSuffix() throws Exception { - testLexerSource("6f", true, NUMBER); - testLexerSource("6d", true, NUMBER); - testLexerSource("6l", true, NUMBER); - testLexerSource("6ll", true, NUMBER); - testLexerSource("6ul", true, NUMBER); - testLexerSource("6ull", true, NUMBER); - testLexerSource("6e3f", true, NUMBER); - testLexerSource("6e3d", true, NUMBER); - testLexerSource("6e3l", true, NUMBER); - testLexerSource("6e3ll", true, NUMBER); - testLexerSource("6e3ul", true, NUMBER); - testLexerSource("6e3ull", true, NUMBER); - } - - @Test - public void testNumbersInvalid() throws Exception { - // testLexerSource("0x foo", true, INVALID, WHITESPACE, IDENTIFIER); // FAIL - testLexerSource("6x foo", true, INVALID, WHITESPACE, IDENTIFIER); - testLexerSource("6g foo", true, INVALID, WHITESPACE, IDENTIFIER); - testLexerSource("6xsd foo", true, INVALID, WHITESPACE, IDENTIFIER); - testLexerSource("6gsd foo", true, INVALID, WHITESPACE, IDENTIFIER); - } - - @Test - public void testUnterminatedComment() throws Exception { - testLexerSource("5 /*", false, NUMBER, WHITESPACE, INVALID); // Bug #15 - testLexerSource("5 //", false, NUMBER, WHITESPACE, CPPCOMMENT); - } -} diff --git a/src/test/java/org/anarres/cpp/MainTest.java b/src/test/java/org/anarres/cpp/MainTest.java deleted file mode 100644 index 5ff7350..0000000 --- a/src/test/java/org/anarres/cpp/MainTest.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.anarres.cpp; - -import org.junit.Test; - -public class MainTest { - - @Test - public void testMain() throws Exception { - Main.main(new String[]{"--version"}); - } -} diff --git a/src/test/java/org/anarres/cpp/NumericValueTest.java b/src/test/java/org/anarres/cpp/NumericValueTest.java deleted file mode 100644 index d4ea432..0000000 --- a/src/test/java/org/anarres/cpp/NumericValueTest.java +++ /dev/null @@ -1,95 +0,0 @@ -package org.anarres.cpp; - -import java.io.IOException; -import org.junit.Test; -import static org.anarres.cpp.Token.*; -import static org.junit.Assert.*; - -/** - * - * @author shevek - */ -public class NumericValueTest { - - private Token testNumericValue(String in) throws IOException, LexerException { - StringLexerSource s = new StringLexerSource(in); - - Token tok = s.token(); - System.out.println("Token is " + tok); - assertEquals(NUMBER, tok.getType()); - - Token eof = s.token(); - assertEquals("Didn't get EOF, but " + tok, EOF, eof.getType()); - - return tok; - } - - private void testNumericValue(String in, double out) throws IOException, LexerException { - System.out.println("Testing '" + in + "' -> " + out); - Token tok = testNumericValue(in); - assertEquals(in, tok.getText()); - NumericValue value = (NumericValue) tok.getValue(); - assertEquals("Double mismatch", out, value.doubleValue(), 0.01d); - assertEquals("Float mismatch", (float) out, value.floatValue(), 0.01f); - assertEquals("Long mismatch", (long) out, value.longValue()); - assertEquals("Integer mismatch", (int) out, value.intValue()); - } - - @Test - public void testNumericValue() throws Exception { - - // Zero - testNumericValue("0", 0); - - // Decimal - testNumericValue("1", 1); - testNumericValue("1L", 1); - testNumericValue("12", 12); - testNumericValue("12L", 12); - - // Hex - testNumericValue("0xf", 0xf); - testNumericValue("0xfL", 0xf); - testNumericValue("0x12", 0x12); - testNumericValue("0x12L", 0x12); - - // Negative - // testNumericValue("-0", 0); - // testNumericValue("-1", -1); - // Negative hex - // testNumericValue("-0x56", -0x56); - // testNumericValue("-0x102", -0x102); - // Octal and negative octal - testNumericValue("0673", Integer.parseInt("673", 8)); - // testNumericValue("-0673", Integer.parseInt("-673", 8)); - - // Floating point - testNumericValue(".0", 0); - testNumericValue(".00", 0); - testNumericValue("0.", 0); - testNumericValue("0.0", 0); - testNumericValue("00.0", 0); - testNumericValue("00.", 0); - - // Sign on exponents - testNumericValue("1e1", 1e1); - // testNumericValue("-1e1", -1e1); - testNumericValue("1e-1", 1e-1); - - // Hex numbers with decimal exponents - testNumericValue("0x12e3", 0x12e3); - testNumericValue("0x12p3", 0x12p3); - - // Octal numbers with decimal exponents - testNumericValue("012e3", 012e3); // Fails - testNumericValue("067e4", 067e4); // Fails - - // Issues a warning. - try { - testNumericValue("097", 97); - fail("No warning."); - } catch (LexerException e) { - } - - } -} diff --git a/src/test/java/org/anarres/cpp/PreprocessorTest.java b/src/test/java/org/anarres/cpp/PreprocessorTest.java deleted file mode 100644 index 13bd944..0000000 --- a/src/test/java/org/anarres/cpp/PreprocessorTest.java +++ /dev/null @@ -1,237 +0,0 @@ -package org.anarres.cpp; - -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.PipedInputStream; -import java.io.PipedOutputStream; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import static org.anarres.cpp.Token.*; -import static org.junit.Assert.*; - -public class PreprocessorTest { - - private static final Logger LOG = LoggerFactory.getLogger(PreprocessorTest.class); - - private OutputStreamWriter writer; - private Preprocessor p; - - @Before - public void setUp() throws Exception { - final PipedOutputStream po = new PipedOutputStream(); - writer = new OutputStreamWriter(po); - - p = new Preprocessor(); - p.addInput( - new LexerSource( - new InputStreamReader( - new PipedInputStream(po) - ), - true - ) - ); - } - - private static class I { - - private final String t; - - public I(String t) { - this.t = t; - } - - public String getText() { - return t; - } - - @Override - public String toString() { - return getText(); - } - } - - private static I I(String t) { - return new I(t); - } - - /* - * When writing tests in this file, remember the preprocessor - * stashes NLs, so you won't see an immediate NL at the end of any - * input line. You will see it right before the next nonblank on - * the following input line. - */ - @Test - public void testPreprocessor() throws Exception { - /* Magic macros */ - testInput("line = __LINE__\n", - I("line"), WHITESPACE, '=', WHITESPACE, NUMBER - /*, NL - all nls deferred so as not to block the reader */ - ); - testInput("file = __FILE__\n", NL, /* from before, etc */ - I("file"), WHITESPACE, '=', WHITESPACE, STRING - ); - - /* Simple definitions */ - testInput("#define A a /* a defined */\n", NL); - testInput("#define B b /* b defined */\n", NL); - testInput("#define C c /* c defined */\n", NL); - - /* Expansion of arguments */ - testInput("#define EXPAND(x) x\n", NL); - testInput("EXPAND(a)\n", NL, I("a")); - testInput("EXPAND(A)\n", NL, I("a")); - - /* Stringification */ - testInput("#define _STRINGIFY(x) #x\n", NL); - testInput("_STRINGIFY(A)\n", NL, "A"); - testInput("#define STRINGIFY(x) _STRINGIFY(x)\n", NL); - testInput("STRINGIFY(b)\n", NL, "b"); - testInput("STRINGIFY(A)\n", NL, "a"); - - /* Concatenation */ - testInput("#define _CONCAT(x, y) x ## y\n", NL); - testInput("_CONCAT(A, B)\n", NL, I("AB")); - testInput("#define A_CONCAT done_a_concat\n", NL); - testInput("_CONCAT(A, _CONCAT(B, C))\n", NL, - I("done_a_concat"), '(', I("b"), ',', WHITESPACE, I("c"), ')' - ); - testInput("#define CONCAT(x, y) _CONCAT(x, y)\n", NL); - testInput("CONCAT(A, CONCAT(B, C))\n", NL, I("abc")); - testInput("#define _CONCAT3(x, y, z) x ## y ## z\n", NL); - testInput("_CONCAT3(a, b, c)\n", NL, I("abc")); - testInput("_CONCAT3(A, B, C)\n", NL, I("ABC")); - testInput("_CONCAT(test_, inline)\n", NL, I("test_inline")); - testInput("_CONCAT(test_, \nnewline)\n", NL, I("test_newline")); - - /* Redefinitions, undefinitions. */ - testInput("#define two three\n", NL); - testInput("one /* one */\n", NL, I("one"), WHITESPACE, CCOMMENT); - testInput("#define one two\n", NL); - testInput("one /* three */\n", NL, I("three"), WHITESPACE, CCOMMENT); - testInput("#undef two\n", NL); - testInput("#define two five\n", NL); - testInput("one /* five */\n", NL, I("five"), WHITESPACE, CCOMMENT); - testInput("#undef two\n", NL); - testInput("one /* two */\n", NL, I("two"), WHITESPACE, CCOMMENT); - testInput("#undef one\n", NL); - testInput("#define one four\n", NL); - testInput("one /* four */\n", NL, I("four"), WHITESPACE, CCOMMENT); - testInput("#undef one\n", NL); - testInput("#define one one\n", NL); - testInput("one /* one */\n", NL, I("one"), WHITESPACE, CCOMMENT); - - /* Variadic macros. */ - testInput("#define var(x...) a x __VA_ARGS__ b\n", NL); - testInput("var(e, f, g)\n", NL, - I("a"), WHITESPACE, - I("e"), ',', WHITESPACE, - I("f"), ',', WHITESPACE, - I("g"), WHITESPACE, - I("__VA_ARGS__"), WHITESPACE, // __VA_ARGS__ is not expanded in this case. - I("b") - ); - /* Missing arguments are fine. */ - testInput("var()\n", NL, - I("a"), WHITESPACE, - /* No expansion for 'x'. */ WHITESPACE, - I("__VA_ARGS__"), WHITESPACE, - I("b") - ); - - /* Variadic macros with anonymous args. */ - testInput("#define var2(x, ...) a x __VA_ARGS__ e\n", NL); - testInput("var2(b, c, d)\n", NL, - I("a"), WHITESPACE, - I("b"), WHITESPACE, - I("c"), ',', WHITESPACE, - I("d"), WHITESPACE, - I("e") - ); - /* Missing arguments are fine. */ - testInput("var2(b)\n", NL, - I("a"), WHITESPACE, - I("b"), WHITESPACE, - /* No expansion for '__VA_ARGS__'. */ WHITESPACE, - I("e") - ); - - testInput("#define var3(...) a __VA_ARGS__ d\n", NL); - testInput("var3(b, c)\n", NL, - I("a"), WHITESPACE, - I("b"), ',', WHITESPACE, - I("c"), WHITESPACE, - I("d") - ); - testInput("var3()\n", NL, - I("a"), WHITESPACE, - /* No expansion for '__VA_ARGS__'. */ WHITESPACE, - I("d") - ); - - testInput("#define _Widen(x) L ## x\n", NL); - testInput("#define Widen(x) _Widen(x)\n", NL); - testInput("#define LStr(x) _Widen(#x)\n", NL); - testInput("LStr(x);\n", NL, I("L"), "x", ';'); - - testInput("'foo'\n", NL, SQSTRING); - testInput("#if 1 ? 2 : 0\nTEXT\n#endif\n", NL, NL, I("TEXT"), NL); - testInput("#if 1 ? 0 : 2\nTEXT\n#endif\n", NL, NL, NL); - testInput("#if 0 ? 0 : 2\nTEXT\n#endif\n", NL, NL, I("TEXT"), NL); - testInput("#if 0 ? 2 : 0\nTEXT\n#endif\n", NL, NL, NL); - - writer.close(); - - Token t; - do { - t = p.token(); - LOG.warn("Remaining token " + t); - } while (t.getType() != EOF); - } - - @Test - public void testPreprocessorUnterminated() throws Exception { - testInput("#ifndef X\na\n#else\nb\n"); // Bug #16 - - writer.close(); - - Token t; - do { - t = p.token(); - LOG.warn("Remaining token " + t); - } while (t.getType() != EOF); - } - - public static void assertType(int type, Token t) { - String typeExpect = TokenType.getTokenName(type); - String typeActual = TokenType.getTokenName(t.getType()); - assertEquals("Expected " + typeExpect + " but got " + typeActual, type, t.getType()); - } - - private void testInput(String in, Object... out) - throws Exception { - LOG.info("Input: " + in); - writer.write(in); - writer.flush(); - for (Object v : out) { - Token t = p.token(); - LOG.info(String.valueOf(t)); - if (v instanceof String) { - if (t.getType() != STRING) - fail("Expected STRING, but got " + t); - assertEquals(v, t.getValue()); - } else if (v instanceof I) { - if (t.getType() != IDENTIFIER) - fail("Expected IDENTIFIER " + v + ", but got " + t); - assertEquals(((I) v).getText(), t.getText()); - } else if (v instanceof Character) { - assertType(((Character) v).charValue(), t); - } else if (v instanceof Integer) { - assertType(((Number) v).intValue(), t); - } else { - fail("Bad object " + v.getClass()); - } - } - } -} diff --git a/src/test/java/org/anarres/cpp/TokenPastingWhitespaceTest.java b/src/test/java/org/anarres/cpp/TokenPastingWhitespaceTest.java deleted file mode 100644 index 2e6a2b5..0000000 --- a/src/test/java/org/anarres/cpp/TokenPastingWhitespaceTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.anarres.cpp; - -import com.google.common.io.CharStreams; -import java.io.IOException; -import java.io.Reader; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import static org.junit.Assert.*; - -/** - * https://github.com/shevek/jcpp/issues/25 - * - * @author shevek - */ -public class TokenPastingWhitespaceTest { - - private static final Logger LOG = LoggerFactory.getLogger(TokenPastingWhitespaceTest.class); - - @Test - public void testWhitespacePasting() throws IOException { - Preprocessor pp = new Preprocessor(); - pp.addInput(new StringLexerSource( - "#define ONE(arg) one_##arg\n" - + "#define TWO(arg) ONE(two_##arg)\n" - + "\n" - + "TWO(good)\n" - + "TWO( /* evil newline */\n" - + " bad)\n" - + "\n" - + "ONE(good)\n" - + "ONE( /* evil newline */\n" - + " bad)\n", true)); - Reader r = new CppReader(pp); - String text = CharStreams.toString(r).trim(); - LOG.info("Output is:\n" + text); - assertEquals("one_two_good\n" - + "one_two_bad\n" - + "\n" - + "one_good\n" - + "one_bad", text); - } -} |