aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShevek <[email protected]>2017-05-02 00:26:44 -0700
committerShevek <[email protected]>2017-05-02 00:26:44 -0700
commitda16a8e409ea51b3d17edb0be6f22e8d5530219e (patch)
tree5688717ad1772684dc274dc6c06a2533ca9de94e
parent36432e516e199b5a0602aba6e1a8b97cb5d72777 (diff)
Fix #34.
-rw-r--r--src/main/java/org/anarres/cpp/Preprocessor.java12
-rw-r--r--src/test/java/org/anarres/cpp/RegressionTest.java70
-rw-r--r--src/test/resources/regression/lex-char.in4
3 files changed, 85 insertions, 1 deletions
diff --git a/src/main/java/org/anarres/cpp/Preprocessor.java b/src/main/java/org/anarres/cpp/Preprocessor.java
index 2bbea4e..5b07824 100644
--- a/src/main/java/org/anarres/cpp/Preprocessor.java
+++ b/src/main/java/org/anarres/cpp/Preprocessor.java
@@ -1559,6 +1559,16 @@ public class Preprocessor implements Closeable {
}
}
+ private int expr_char(Token token) {
+ Object value = token.getValue();
+ if (value instanceof Character)
+ return ((Character) value).charValue();
+ String text = String.valueOf(value);
+ if (text.length() == 0)
+ return 0;
+ return text.charAt(0);
+ }
+
private long expr(int priority)
throws IOException,
LexerException {
@@ -1595,7 +1605,7 @@ public class Preprocessor implements Closeable {
lhs = value.longValue();
break;
case CHARACTER:
- lhs = ((Character) tok.getValue()).charValue();
+ lhs = expr_char(tok);
break;
case IDENTIFIER:
if (warnings.contains(Warning.UNDEF))
diff --git a/src/test/java/org/anarres/cpp/RegressionTest.java b/src/test/java/org/anarres/cpp/RegressionTest.java
new file mode 100644
index 0000000..3ce3568
--- /dev/null
+++ b/src/test/java/org/anarres/cpp/RegressionTest.java
@@ -0,0 +1,70 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.anarres.cpp;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.CharStreams;
+import com.google.common.io.Files;
+import com.google.common.io.PatternFilenameFilter;
+import java.io.File;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import static org.junit.Assert.assertEquals;
+
+/**
+ *
+ * @author shevek
+ */
+@RunWith(Parameterized.class)
+public class RegressionTest {
+
+ private static final Logger LOG = LoggerFactory.getLogger(RegressionTest.class);
+
+ @Parameterized.Parameters(name = "{0}")
+ public static List<Object[]> data() throws Exception {
+ List<Object[]> out = new ArrayList<Object[]>();
+
+ File dir = new File("build/resources/test/regression");
+ for (File inFile : dir.listFiles(new PatternFilenameFilter(".*\\.in"))) {
+ String name = Files.getNameWithoutExtension(inFile.getName());
+ File outFile = new File(dir, name + ".out");
+ out.add(new Object[]{name, inFile, outFile});
+ }
+
+ return out;
+ }
+
+ private final String name;
+ private final File inFile;
+ private final File outFile;
+
+ public RegressionTest(String name, File inFile, File outFile) {
+ this.name = name;
+ this.inFile = inFile;
+ this.outFile = outFile;
+ }
+
+ @Test
+ public void testRegression() throws Exception {
+ String inText = Files.toString(inFile, Charsets.UTF_8);
+ LOG.info("Read " + name + ":\n" + inText);
+ CppReader cppReader = new CppReader(new StringReader(inText));
+ String cppText = CharStreams.toString(cppReader);
+ LOG.info("Generated " + name + ":\n" + cppText);
+ if (outFile.exists()) {
+ String outText = Files.toString(outFile, Charsets.UTF_8);
+ LOG.info("Expected " + name + ":\n" + outText);
+ assertEquals(outText, inText);
+ }
+
+ }
+}
diff --git a/src/test/resources/regression/lex-char.in b/src/test/resources/regression/lex-char.in
new file mode 100644
index 0000000..472af63
--- /dev/null
+++ b/src/test/resources/regression/lex-char.in
@@ -0,0 +1,4 @@
+#define EXAMPLE_X 'a'
+
+#if EXAMPLE == EXAMPLE_X
+#endif