Skip to content

Commit b1483e8

Browse files
committed
Fix WrongDeallocationResolutionTest with latest valgrind
Adjust to changes in valgrind output and thus markers generated by the plugin. In order to make understanding the test simpler - different cases are splitted to be handled separately instead of all in one source file.
1 parent d83cffa commit b1483e8

File tree

2 files changed

+42
-49
lines changed

2 files changed

+42
-49
lines changed

valgrind/org.eclipse.linuxtools.valgrind.ui.tests/resources/wrongDeallocTest/wrongDealloc.cpp

+1-14
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,7 @@ using namespace std;
77

88
int main()
99
{
10-
char *p1 = (char *)malloc(sizeof(char) * SIZE);
11-
delete(p1);
12-
13-
char* p2 = (char *)malloc(5 * sizeof(char) * SIZE);
14-
delete[] p2;
15-
16-
char *p3 = new char;
17-
free(p3);
18-
19-
char *p4 = new char[5];
20-
free(p4);
21-
22-
char* p5 = new char[5];
23-
delete p5;
10+
__VALGRIND__
2411

2512
return 0;
2613
}

valgrind/org.eclipse.linuxtools.valgrind.ui.tests/src/org/eclipse/linuxtools/internal/valgrind/ui/quickfixes/tests/WrongDeallocationResolutionTest.java

+41-35
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
import static org.junit.Assert.assertNotNull;
1919
import static org.junit.Assert.assertTrue;
2020

21+
import java.io.ByteArrayInputStream;
2122
import java.io.InputStream;
2223
import java.util.Arrays;
23-
import java.util.Scanner;
2424

25+
import org.eclipse.core.resources.IFile;
2526
import org.eclipse.core.resources.IMarker;
2627
import org.eclipse.core.runtime.CoreException;
2728
import org.eclipse.debug.core.ILaunchConfiguration;
@@ -30,12 +31,10 @@
3031
import org.eclipse.linuxtools.internal.valgrind.tests.AbstractValgrindTest;
3132
import org.eclipse.linuxtools.internal.valgrind.ui.quickfixes.WrongDeallocationResolution;
3233
import org.junit.After;
33-
import org.junit.Before;
3434
import org.junit.Test;
3535

3636
public class WrongDeallocationResolutionTest extends AbstractValgrindTest {
3737

38-
private final String EMPTY_STRING = ""; //$NON-NLS-1$
3938
private final String VALGRIND_MARKER_TYPE = "org.eclipse.linuxtools.valgrind.launch.marker"; //$NON-NLS-1$
4039
private Document document;
4140
private IMarker[] markers;
@@ -45,24 +44,20 @@ protected String getToolID() {
4544
return "org.eclipse.linuxtools.valgrind.launch.memcheck"; //$NON-NLS-1$
4645
}
4746

48-
@Before
49-
public void prep() throws Exception {
50-
proj = createProjectAndBuild("wrongDeallocTest"); //$NON-NLS-1$
47+
private void prep(String statements) throws Exception {
48+
proj = createProject(getBundle(), "wrongDeallocTest");
49+
IFile cppFile = proj.getProject().getFile("wrongDealloc.cpp");
50+
InputStream preContentStream = cppFile.getContents();
51+
String content = new String(preContentStream.readAllBytes());
52+
content = content.replace("__VALGRIND__", statements);
53+
cppFile.setContents(new ByteArrayInputStream(content.getBytes()) , 0, null);
54+
buildProject(proj);
55+
5156
ILaunchConfiguration config = createConfiguration(proj.getProject());
5257
doLaunch(config, "wrongDeallocTest"); //$NON-NLS-1$
5358

5459
document = new Document();
55-
InputStream fileInputStream = proj.getProject().getFile("wrongDealloc.cpp").getContents(); //$NON-NLS-1$
56-
try (Scanner scanner = new Scanner(fileInputStream)) {
57-
scanner.useDelimiter("\\A"); //$NON-NLS-1$
58-
String content;
59-
if (scanner.hasNext()) {
60-
content = scanner.next();
61-
} else {
62-
content = EMPTY_STRING;
63-
}
64-
document.set(content);
65-
}
60+
document.set(content);
6661
markers = proj.getProject().findMarkers(VALGRIND_MARKER_TYPE, true, 1);
6762
Arrays.sort(markers, (marker1, marker2) -> {
6863
int line1 = marker1.getAttribute(IMarker.LINE_NUMBER, -1);
@@ -93,23 +88,27 @@ public void tearDown() throws CoreException {
9388
}
9489

9590
@Test
96-
public void testMallocDeleteQuickFix() throws CoreException {
97-
IMarker mallocDeleteMarker = markers[1];
91+
public void testMallocDeleteQuickFix() throws Exception {
92+
prep("char *p1 = (char *)malloc(sizeof(char) * SIZE);\n"
93+
+ " delete(p1);");
94+
IMarker mallocDeleteMarker = markers[2];
9895
int markerLine = mallocDeleteMarker.getAttribute(IMarker.LINE_NUMBER, -1);
9996

100-
createResolutionAndApply(mallocDeleteMarker);
97+
createResolutionAndApply(mallocDeleteMarker, 3);
10198

10299
String newContent = getLineContent(document, markerLine);
103100
assertTrue(newContent.contains("free")); //$NON-NLS-1$
104101
assertFalse(newContent.contains("delete")); //$NON-NLS-1$
105102
}
106103

107104
@Test
108-
public void testMallocDeleteArrayQuickFix() throws CoreException {
109-
IMarker mallocDeleteArrayMarker = markers[3];
105+
public void testMallocDeleteArrayQuickFix() throws Exception {
106+
prep("char* p2 = (char *)malloc(5 * sizeof(char) * SIZE);\n"
107+
+ " delete[] p2;");
108+
IMarker mallocDeleteArrayMarker = markers[1];
110109
int markerLine = mallocDeleteArrayMarker.getAttribute(IMarker.LINE_NUMBER, -1);
111110

112-
createResolutionAndApply(mallocDeleteArrayMarker);
111+
createResolutionAndApply(mallocDeleteArrayMarker, 2);
113112

114113
String newContent = getLineContent(document, markerLine);
115114
assertTrue(newContent.contains("free")); //$NON-NLS-1$
@@ -118,49 +117,56 @@ public void testMallocDeleteArrayQuickFix() throws CoreException {
118117
}
119118

120119
@Test
121-
public void testNewFreeQuickFix() throws CoreException {
122-
IMarker newFreeMarker = markers[5];
120+
public void testNewFreeQuickFix() throws Exception {
121+
prep("char *p3 = new char;\n"
122+
+ " free(p3);");
123+
IMarker newFreeMarker = markers[1];
123124
int markerLine = newFreeMarker.getAttribute(IMarker.LINE_NUMBER, -1);
124125

125-
createResolutionAndApply(newFreeMarker);
126+
createResolutionAndApply(newFreeMarker, 2);
126127

127128
String newContent = getLineContent(document, markerLine);
128129
assertTrue(newContent.contains("delete")); //$NON-NLS-1$
129130
assertFalse(newContent.contains("free")); //$NON-NLS-1$
130131
}
131132

132133
@Test
133-
public void testNewArrayFreeQuickFix() throws CoreException {
134-
IMarker newArrayFreeMarker = markers[7];
134+
public void testNewArrayFreeQuickFix() throws Exception {
135+
prep("char *p4 = new char[5];\n"
136+
+ " free(p4);");
137+
IMarker newArrayFreeMarker = markers[1];
135138
int markerLine = newArrayFreeMarker.getAttribute(IMarker.LINE_NUMBER, -1);
136139

137-
createResolutionAndApply(newArrayFreeMarker);
140+
createResolutionAndApply(newArrayFreeMarker, 2);
138141

139142
String newContent = getLineContent(document, markerLine);
140143
assertTrue(newContent.contains("delete[]")); //$NON-NLS-1$
141144
assertFalse(newContent.contains("free")); //$NON-NLS-1$
142145
}
143146

144147
@Test
145-
public void testNewArrayDeleteQuickFix() throws CoreException {
146-
IMarker newArrayDeleteMarker = markers[9];
148+
public void testNewArrayDeleteQuickFix() throws Exception {
149+
prep("char* p5 = new char[5];\n"
150+
+ " delete p5;");
151+
IMarker newArrayDeleteMarker = markers[2];
147152
int markerLine = newArrayDeleteMarker.getAttribute(IMarker.LINE_NUMBER, -1);
148153

149-
createResolutionAndApply(newArrayDeleteMarker);
154+
createResolutionAndApply(newArrayDeleteMarker, 3);
150155

151156
String newContent = getLineContent(document, markerLine);
152157
assertTrue(newContent.contains("delete[]")); //$NON-NLS-1$
153158
}
154159

155-
private void createResolutionAndApply(IMarker marker) throws CoreException {
160+
private void createResolutionAndApply(IMarker marker, int fixedMarkers) throws CoreException {
156161
WrongDeallocationResolution resolution = new WrongDeallocationResolution();
157162
assertNotNull(resolution);
158163

159164
int numMarkersBefore = proj.getProject().findMarkers(VALGRIND_MARKER_TYPE, true, 1).length;
160165
resolution.apply(marker, document);
161-
int numMarkersAfter = proj.getProject().findMarkers(VALGRIND_MARKER_TYPE, true, 1).length;
166+
IMarker[] markers2 = proj.getProject().findMarkers(VALGRIND_MARKER_TYPE, true, 1);
167+
int numMarkersAfter = markers2.length;
162168

163-
assertEquals(numMarkersAfter, numMarkersBefore - 2);
169+
assertEquals(numMarkersAfter, numMarkersBefore - fixedMarkers);
164170
}
165171

166172
private String getLineContent(Document document, int line) {

0 commit comments

Comments
 (0)