Convert CPP tests

This commit is contained in:
Clément Fournier
2020-06-13 20:08:51 +02:00
parent 0a1e82efad
commit 551ab453fd
34 changed files with 877 additions and 328 deletions

View File

@ -80,10 +80,21 @@
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>net.sourceforge.pmd</groupId> <groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-test</artifactId> <artifactId>pmd-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-lang-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,128 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.cpd.SourceCode.StringCodeLoader;
import net.sourceforge.pmd.lang.cpp.CppTokenManager;
import net.sourceforge.pmd.lang.cpp.ast.Token;
public class CPPTokenizerContinuationTest {
@Test
public void parseWithContinuation() throws Exception {
String code = load("cpp_with_continuation.cpp");
Tokens tokens = parse(code);
if (tokens.size() < 52) {
printTokens(tokens);
fail("Not enough tokens - probably parsing error. Tokens: " + tokens.size());
}
assertEquals("static", findByLine(8, tokens).get(0).toString());
assertEquals("int", findByLine(8, tokens).get(1).toString());
// special case, if the continuation is *within* a token
// see also test #testContinuationIntraToken
TokenEntry tokenEntry = findByLine(8, tokens).get(2);
assertEquals("ab", tokenEntry.toString());
assertEquals("int", findByLine(12, tokens).get(0).toString());
assertEquals("main", findByLine(12, tokens).get(1).toString());
assertEquals("(", findByLine(12, tokens).get(2).toString());
assertEquals(")", findByLine(12, tokens).get(3).toString());
assertEquals("{", findByLine(13, tokens).get(0).toString());
assertEquals("\"world!\\n\"", findByLine(16, tokens).get(0).toString());
assertEquals("\"3 Hello, \\world!\\n\"", findByLine(22, tokens).get(4).toString());
assertEquals("}", findByLine(29, tokens).get(0).toString());
}
/**
* Verifies the begin/end of a token. Uses the underlaying JavaCC Token and
* not TokenEntry.
*/
@Test
public void parseWithContinuationCppTokenManager() throws Exception {
String code = load("cpp_with_continuation.cpp");
CppTokenManager tokenManager = new CppTokenManager(new StringReader(code));
List<Token> tokens = new ArrayList<>();
Token token = (Token) tokenManager.getNextToken();
while (!token.image.isEmpty()) {
tokens.add(token);
token = (Token) tokenManager.getNextToken();
}
assertEquals(51, tokens.size());
assertToken(tokens.get(2), "ab", 8, 12, 9, 1);
assertToken(tokens.get(22), "\"2 Hello, world!\\n\"", 18, 16, 19, 9);
}
private void assertToken(Token token, String image, int beginLine, int beginColumn, int endLine, int endColumn) {
assertEquals(image, token.image);
assertEquals(beginLine, token.beginLine);
assertEquals(beginColumn, token.beginColumn);
assertEquals(endLine, token.endLine);
assertEquals(endColumn, token.endColumn);
}
@Test
public void testContinuationIntraToken() throws Exception {
Tokens tokens = parse(load("cpp_continuation_intra_token.cpp"));
assertEquals(7, tokens.size());
}
@Test
public void testContinuationInterToken() throws Exception {
Tokens tokens = parse(load("cpp_continuation_inter_token.cpp"));
assertEquals(17, tokens.size());
}
private void printTokens(Tokens tokens) {
for (TokenEntry entry : tokens.getTokens()) {
System.out.printf("%02d: %s%s", entry.getBeginLine(), entry.toString(), PMD.EOL);
}
}
private List<TokenEntry> findByLine(int line, Tokens tokens) {
List<TokenEntry> result = new ArrayList<>();
for (TokenEntry entry : tokens.getTokens()) {
if (entry.getBeginLine() == line) {
result.add(entry);
}
}
if (result.isEmpty()) {
fail("No tokens at line " + line + " found");
}
return result;
}
private String load(String name) throws Exception {
return IOUtils.toString(CPPTokenizerContinuationTest.class
.getResourceAsStream("cpp/" + name), StandardCharsets.UTF_8);
}
private Tokens parse(String code) throws IOException {
CPPTokenizer tokenizer = new CPPTokenizer();
tokenizer.setProperties(new Properties());
Tokens tokens = new Tokens();
tokenizer.tokenize(new SourceCode(new StringCodeLoader(code)), tokens);
return tokens;
}
}

View File

@ -0,0 +1,36 @@
asm void eSPI_boot()
{
// setup stack pointer
lis r1, _stack_addr@h
ori r1, r1, _stack_addr@l
}
int main() {
}
#if DEBUG
int foobar() {
}
#endif
#if 0
static void my_memset(void *dest,int fill_value,int count)
{
__asm __volatile__(
"cld\n"
"mov %ecx, %ebx\n"
"shr 2,%ecx\n"
"rep "
"stosl\n"
"mov %ebx,%ecx\n"
" // line 157 mentioned above"
:
: "c" (count), "a" (fill_value), "D" (dest)
: "cc","%ebx" );
}
#endif
int otherMethod() {
}

View File

@ -0,0 +1,114 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[asm] 1 3
[void] 5 8
[eSPI_boot] 10 18
[(] 19 19
[)] 20 20
L2
[{] 1 1
L5
[lis] 3 5
[r1] 7 8
[,] 9 9
[_stack_addr] 11 21
[@] 22 22
[h] 23 23
L6
[ori] 3 5
[r1] 7 8
[,] 9 9
[r1] 11 12
[,] 13 13
[_stack_addr] 15 25
[@] 26 26
[l] 27 27
L7
[}] 1 1
L9
[int] 1 3
[main] 5 8
[(] 9 9
[)] 10 10
[{] 12 12
L10
[}] 1 1
L13
[int] 1 3
[foobar] 5 10
[(] 11 11
[)] 12 12
[{] 14 14
L14
[}] 1 1
L18
[static] 1 6
[void] 8 11
[my_memset] 13 21
[(] 22 22
[void] 23 26
[*] 28 28
[dest] 29 32
[,] 33 33
[int] 34 36
[fill_value] 38 47
[,] 48 48
[int] 49 51
[count] 53 57
[)] 58 58
L19
[{] 1 1
L20
[__asm] 5 9
[__volatile__] 11 22
[(] 23 23
L21
["cld\\n"] 10 16
L22
["mov %ecx, %ebx\\n"] 10 27
L23
["shr 2,%ecx\\n"] 10 23
L24
["rep "] 10 15
L25
["stosl\\n"] 10 18
L26
["mov %ebx,%ecx\\n"] 10 26
L27
[" // line 157 mentioned above"] 10 40
L28
[:] 10 10
L29
[:] 10 10
["c"] 12 14
[(] 16 16
[count] 17 21
[)] 22 22
[,] 23 23
["a"] 25 27
[(] 29 29
[fill_value] 30 39
[)] 40 40
[,] 41 41
["D"] 43 45
[(] 47 47
[dest] 48 51
[)] 52 52
L30
[:] 10 10
["cc"] 12 15
[,] 16 16
["%ebx"] 17 22
[)] 24 24
[;] 25 25
L31
[}] 1 1
L35
[int] 1 3
[otherMethod] 5 15
[(] 16 16
[)] 17 17
[{] 19 19
L36
[}] 1 1
EOF

View File

@ -0,0 +1,68 @@
[Image] or [Truncated image[ Bcol Ecol
L8
[static] 1 6
[int] 8 10
[ab] 12 1
L9
[=] 3 3
[5] 5 5
[;] 6 6
L12
[int] 1 3
[main] 5 8
[(] 9 9
[)] 10 10
L13
[{] 1 1
L15
[std] 3 5
[::] 6 7
[cout] 8 11
[<<] 13 14
["1 Hello, "] 16 26
L16
["world!\\n"] 16 25
[;] 26 26
L18
[std] 3 5
[::] 6 7
[cout] 8 11
[<<] 13 14
["2 Hello, world!\\n"] 16 9
L19
[;] 10 10
L22
[std] 3 5
[::] 6 7
[cout] 8 11
[<<] 13 14
["3 Hello, \\world!\\n"] 16 9
L23
[;] 10 10
L25
[std] 3 5
[::] 6 7
[cout] 8 11
[<<] 13 14
["4 Hello, "] 16 26
L26
["world!\\n"] 15 24
[;] 25 25
L27
[std] 3 5
[::] 6 7
[cout] 8 11
[<<] 13 14
["ab="] 16 20
[<<] 22 23
[ab] 25 26
[<<] 28 29
["\\n"] 31 34
[;] 35 35
L28
[return] 3 8
[0] 10 10
[;] 11 11
L29
[}] 1 1
EOF

View File

@ -0,0 +1,24 @@
[Image] or [Truncated image[ Bcol Ecol
L3
[int] 1 3
[main] 5 8
[(] 9 9
[)] 10 10
L4
[{] 1 1
L5
[std] 4 6
[::] 7 8
[cout] 9 12
[<<] 14 15
["Hello, "] 17 25
L6
["world!\\n"] 17 26
[;] 27 27
L7
[return] 4 9
[0] 11 11
[;] 12 12
L8
[}] 1 1
EOF

View File

@ -0,0 +1,14 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[void] 1 3
L6
[main] 1 3
L10
[(] 1 1
L11
[)] 1 1
L13
[{] 1 1
L15
[}] 1 1
EOF

View File

@ -0,0 +1,5 @@
void main() {
int x$y = 42;
int $yx = 42;
}

View File

@ -0,0 +1,22 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[void] 2 5
[main] 7 10
[(] 11 11
[)] 12 12
[{] 14 14
L2
[int] 5 7
[x$y] 9 11
[=] 13 13
[42] 15 16
[;] 17 17
L3
[int] 5 7
[$yx] 9 11
[=] 13 13
[42] 15 16
[;] 17 17
L5
[}] 2 2
EOF

View File

@ -0,0 +1,25 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[namespace] 1 9
[ABC] 11 13
L2
[{] 1 1
L3
[namespace] 3 11
[DEF] 13 15
L4
[{] 3 3
L7
[const] 5 9
[char] 11 14
[*] 15 15
[perPixelQml] 17 27
[=] 29 29
[R"QML(\n )NOTTHEND";\n)QML"] 31 5
L9
[;] 6 6
L10
[}] 3 3
L11
[}] 1 1
EOF

View File

@ -0,0 +1,40 @@
void main() {
char x = L'a'; // wide chars
x = '\0x05'; // hex
x = L''; // empty
print("\ oMedia"); // whitespace escape
// char prefixes
char16_t c = u'\u00F6';
wchar_t b = L'\xFFEF';
char a = '\x30';
char32_t d = U'\U0010FFFF';
// string prefixes
char A[] = "Hello\x0A";
wchar_t B[] = L"Hell\xF6\x0A";
char16_t C[] = u"Hell\u00F6";
char32_t D[] = U"Hell\U000000F6\U0010FFFF";
auto E[] = u8"\u00F6\U0010FFFF";
char* rawString = R"(
[Sinks.1]
Destination=Console
AutoFlush=true
Format="[%TimeStamp%] %ThreadId% %QueryIdHigh% %QueryIdLow% %LoggerFile%:%Line% (%Severity%) - %Message%"
Filter="%Severity% >= WRN"
)";
// digit separators
auto integer_literal = 1'000'000;
auto floating_point_literal = 0.000'015'3;
auto hex_literal = 0x0F00'abcd'6f3d;
auto silly_example = 1'0'0'000'00;
}

View File

@ -0,0 +1,128 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[void] 2 5
[main] 7 10
[(] 11 11
[)] 12 12
[{] 14 14
L2
[char] 5 8
[x] 10 10
[=] 12 12
[L'a'] 14 17
[;] 18 18
L3
[x] 5 5
[=] 7 7
['\\0x05'] 9 15
[;] 16 16
L4
[x] 5 5
[=] 7 7
[L''] 9 11
[;] 12 12
L6
[print] 5 9
[(] 10 10
["\\ oMedia"] 11 23
[)] 24 24
[;] 25 25
L10
[char16_t] 5 12
[c] 14 14
[=] 16 16
[u'\\u00F6'] 18 26
[;] 27 27
L11
[wchar_t] 5 11
[b] 13 13
[=] 15 15
[L'\\xFFEF'] 17 25
[;] 26 26
L12
[char] 5 8
[a] 10 10
[=] 12 12
['\\x30'] 15 20
[;] 21 21
L13
[char32_t] 5 12
[d] 14 14
[=] 16 16
[U'\\U0010FFFF'] 18 30
[;] 31 31
L16
[char] 5 8
[A] 10 10
[\[] 11 11
[\]] 12 12
[=] 14 14
["Hello\\x0A"] 16 26
[;] 27 27
L17
[wchar_t] 5 11
[B] 13 13
[\[] 14 14
[\]] 15 15
[=] 17 17
[L"Hell\\xF6\\x0A"] 19 33
[;] 34 34
L18
[char16_t] 5 12
[C] 14 14
[\[] 15 15
[\]] 16 16
[=] 18 18
[u"Hell\\u00F6"] 20 32
[;] 33 33
L19
[char32_t] 5 12
[D] 14 14
[\[] 15 15
[\]] 16 16
[=] 18 18
[U"Hell\\U000000F6\\U0010FFFF"] 20 46
[;] 47 47
L20
[auto] 5 8
[E] 10 10
[\[] 11 11
[\]] 12 12
[=] 14 14
[u8"\\u00F6\\U0010FFFF"] 16 35
[;] 36 36
L24
[char] 5 8
[*] 9 9
[rawString] 11 19
[=] 21 21
[R"(\n \[Sinks.1\]\n [ 23 6
L30
[;] 7 7
L35
[auto] 5 8
[integer_literal] 10 24
[=] 26 26
[1'000'000] 28 36
[;] 37 37
L36
[auto] 5 8
[floating_point_literal] 10 31
[=] 33 33
[0.000'015'3] 35 45
[;] 46 46
L37
[auto] 5 8
[hex_literal] 10 20
[=] 22 22
[0x0F00'abcd'6f3d] 24 39
[;] 40 40
L38
[auto] 5 8
[silly_example] 10 22
[=] 24 24
[1'0'0'000'00] 26 37
[;] 38 38
L40
[}] 1 1
EOF

View File

@ -0,0 +1,8 @@
#define FOO a +\
b +\
c +\
d +\
e +\
f +\
g
void main() {}

View File

@ -0,0 +1,9 @@
[Image] or [Truncated image[ Bcol Ecol
L8
[void] 2 5
[main] 7 10
[(] 11 11
[)] 12 12
[{] 14 14
[}] 15 15
EOF

View File

@ -16,8 +16,8 @@ static void my_memset(void *dest,int fill_value,int count)
"rep " "rep "
"stosl\n" "stosl\n"
"mov %ebx,%ecx\n" "mov %ebx,%ecx\n"
" // line 157 mentioned above " // line 157 mentioned above"
: :
: "c" (count), "a" (fill_value), "D" (dest) : "c" (count), "a" (fill_value), "D" (dest)
: "cc","%ebx" ); : "cc","%ebx" );
} }

View File

@ -0,0 +1,88 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[int] 1 3
[main] 5 8
[(] 9 9
[)] 10 10
[{] 12 12
L2
[}] 1 1
L5
[int] 1 3
[foobar] 5 10
[(] 11 11
[)] 12 12
[{] 14 14
L6
[}] 1 1
L10
[static] 1 6
[void] 8 11
[my_memset] 13 21
[(] 22 22
[void] 23 26
[*] 28 28
[dest] 29 32
[,] 33 33
[int] 34 36
[fill_value] 38 47
[,] 48 48
[int] 49 51
[count] 53 57
[)] 58 58
L11
[{] 1 1
L12
[__asm] 5 9
[__volatile__] 11 22
[(] 23 23
L13
["cld\\n"] 10 16
L14
["mov %ecx, %ebx\\n"] 10 27
L15
["shr 2,%ecx\\n"] 10 23
L16
["rep "] 10 15
L17
["stosl\\n"] 10 18
L18
["mov %ebx,%ecx\\n"] 10 26
L19
[" // line 157 mentioned above"] 10 40
L20
[:] 10 10
L21
[:] 10 10
["c"] 12 14
[(] 16 16
[count] 17 21
[)] 22 22
[,] 23 23
["a"] 25 27
[(] 29 29
[fill_value] 30 39
[)] 40 40
[,] 41 41
["D"] 43 45
[(] 47 47
[dest] 48 51
[)] 52 52
L22
[:] 10 10
["cc"] 12 15
[,] 16 16
["%ebx"] 17 22
[)] 24 24
[;] 25 25
L23
[}] 1 1
L27
[int] 1 3
[otherMethod] 5 15
[(] 16 16
[)] 17 17
[{] 19 19
L28
[}] 1 1
EOF

View File

@ -0,0 +1,80 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[int] 1 3
[main] 5 8
[(] 9 9
[)] 10 10
[{] 12 12
L2
[}] 1 1
L10
[static] 1 6
[void] 8 11
[my_memset] 13 21
[(] 22 22
[void] 23 26
[*] 28 28
[dest] 29 32
[,] 33 33
[int] 34 36
[fill_value] 38 47
[,] 48 48
[int] 49 51
[count] 53 57
[)] 58 58
L11
[{] 1 1
L12
[__asm] 5 9
[__volatile__] 11 22
[(] 23 23
L13
["cld\\n"] 10 16
L14
["mov %ecx, %ebx\\n"] 10 27
L15
["shr 2,%ecx\\n"] 10 23
L16
["rep "] 10 15
L17
["stosl\\n"] 10 18
L18
["mov %ebx,%ecx\\n"] 10 26
L19
[" // line 157 mentioned above"] 10 40
L20
[:] 10 10
L21
[:] 10 10
["c"] 12 14
[(] 16 16
[count] 17 21
[)] 22 22
[,] 23 23
["a"] 25 27
[(] 29 29
[fill_value] 30 39
[)] 40 40
[,] 41 41
["D"] 43 45
[(] 47 47
[dest] 48 51
[)] 52 52
L22
[:] 10 10
["cc"] 12 15
[,] 16 16
["%ebx"] 17 22
[)] 24 24
[;] 25 25
L23
[}] 1 1
L27
[int] 1 3
[otherMethod] 5 15
[(] 16 16
[)] 17 17
[{] 19 19
L28
[}] 1 1
EOF

View File

@ -0,0 +1,26 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[int] 1 3
[main] 5 8
[(] 9 9
[)] 10 10
[{] 12 12
L2
[}] 1 1
L5
[int] 1 3
[foobar] 5 10
[(] 11 11
[)] 12 12
[{] 14 14
L6
[}] 1 1
L27
[int] 1 3
[otherMethod] 5 15
[(] 16 16
[)] 17 17
[{] 19 19
L28
[}] 1 1
EOF

View File

@ -0,0 +1,11 @@
#include <iostream>
#include <string>
// CPD-OFF
int main()
{
std::string text("ąęćśźńó");
std::cout << text;
return 0;
// CPD-ON
}

View File

@ -0,0 +1,4 @@
[Image] or [Truncated image[ Bcol Ecol
L11
[}] 1 1
EOF

View File

@ -0,0 +1,11 @@
#include <iostream>
#include <string>
// example
int main()
{
std::string text("ąęćśźńó");
std::cout << text;
return 0;
}

View File

@ -0,0 +1,31 @@
[Image] or [Truncated image[ Bcol Ecol
L6
[int] 1 3
[main] 5 8
[(] 9 9
[)] 10 10
L7
[{] 1 1
L8
[std] 5 7
[::] 8 9
[string] 10 15
[text] 17 20
[(] 21 21
["ąęćśźńó"] 22 30
[)] 31 31
[;] 32 32
L9
[std] 5 7
[::] 8 9
[cout] 10 13
[<<] 15 16
[text] 18 21
[;] 22 22
L10
[return] 5 10
[0] 12 12
[;] 13 13
L11
[}] 1 1
EOF

View File

@ -4,6 +4,8 @@
package net.sourceforge.pmd.cpd; package net.sourceforge.pmd.cpd;
import java.util.Properties;
import org.junit.Test; import org.junit.Test;
import net.sourceforge.pmd.cpd.test.CpdTextComparisonTest; import net.sourceforge.pmd.cpd.test.CpdTextComparisonTest;
@ -15,7 +17,7 @@ public class DartTokenizerTest extends CpdTextComparisonTest {
} }
@Override @Override
public Tokenizer newTokenizer() { public Tokenizer newTokenizer(Properties properties) {
return new DartTokenizer(); return new DartTokenizer();
} }

View File

@ -4,6 +4,8 @@
package net.sourceforge.pmd.cpd; package net.sourceforge.pmd.cpd;
import java.util.Properties;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.Test; import org.junit.Test;
@ -17,7 +19,7 @@ public class EcmascriptTokenizerTest extends CpdTextComparisonTest {
@NotNull @NotNull
@Override @Override
public Tokenizer newTokenizer() { public Tokenizer newTokenizer(Properties properties) {
return new EcmascriptTokenizer(); return new EcmascriptTokenizer();
} }

Some files were not shown because too many files have changed in this diff Show More