18
18
import static org .junit .Assert .assertNotNull ;
19
19
import static org .junit .Assert .assertTrue ;
20
20
21
+ import java .io .ByteArrayInputStream ;
21
22
import java .io .InputStream ;
22
23
import java .util .Arrays ;
23
- import java .util .Scanner ;
24
24
25
+ import org .eclipse .core .resources .IFile ;
25
26
import org .eclipse .core .resources .IMarker ;
26
27
import org .eclipse .core .runtime .CoreException ;
27
28
import org .eclipse .debug .core .ILaunchConfiguration ;
30
31
import org .eclipse .linuxtools .internal .valgrind .tests .AbstractValgrindTest ;
31
32
import org .eclipse .linuxtools .internal .valgrind .ui .quickfixes .WrongDeallocationResolution ;
32
33
import org .junit .After ;
33
- import org .junit .Before ;
34
34
import org .junit .Test ;
35
35
36
36
public class WrongDeallocationResolutionTest extends AbstractValgrindTest {
37
37
38
- private final String EMPTY_STRING = "" ; //$NON-NLS-1$
39
38
private final String VALGRIND_MARKER_TYPE = "org.eclipse.linuxtools.valgrind.launch.marker" ; //$NON-NLS-1$
40
39
private Document document ;
41
40
private IMarker [] markers ;
@@ -45,24 +44,20 @@ protected String getToolID() {
45
44
return "org.eclipse.linuxtools.valgrind.launch.memcheck" ; //$NON-NLS-1$
46
45
}
47
46
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
+
51
56
ILaunchConfiguration config = createConfiguration (proj .getProject ());
52
57
doLaunch (config , "wrongDeallocTest" ); //$NON-NLS-1$
53
58
54
59
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 );
66
61
markers = proj .getProject ().findMarkers (VALGRIND_MARKER_TYPE , true , 1 );
67
62
Arrays .sort (markers , (marker1 , marker2 ) -> {
68
63
int line1 = marker1 .getAttribute (IMarker .LINE_NUMBER , -1 );
@@ -93,23 +88,27 @@ public void tearDown() throws CoreException {
93
88
}
94
89
95
90
@ 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 ];
98
95
int markerLine = mallocDeleteMarker .getAttribute (IMarker .LINE_NUMBER , -1 );
99
96
100
- createResolutionAndApply (mallocDeleteMarker );
97
+ createResolutionAndApply (mallocDeleteMarker , 3 );
101
98
102
99
String newContent = getLineContent (document , markerLine );
103
100
assertTrue (newContent .contains ("free" )); //$NON-NLS-1$
104
101
assertFalse (newContent .contains ("delete" )); //$NON-NLS-1$
105
102
}
106
103
107
104
@ 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 ];
110
109
int markerLine = mallocDeleteArrayMarker .getAttribute (IMarker .LINE_NUMBER , -1 );
111
110
112
- createResolutionAndApply (mallocDeleteArrayMarker );
111
+ createResolutionAndApply (mallocDeleteArrayMarker , 2 );
113
112
114
113
String newContent = getLineContent (document , markerLine );
115
114
assertTrue (newContent .contains ("free" )); //$NON-NLS-1$
@@ -118,49 +117,56 @@ public void testMallocDeleteArrayQuickFix() throws CoreException {
118
117
}
119
118
120
119
@ 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 ];
123
124
int markerLine = newFreeMarker .getAttribute (IMarker .LINE_NUMBER , -1 );
124
125
125
- createResolutionAndApply (newFreeMarker );
126
+ createResolutionAndApply (newFreeMarker , 2 );
126
127
127
128
String newContent = getLineContent (document , markerLine );
128
129
assertTrue (newContent .contains ("delete" )); //$NON-NLS-1$
129
130
assertFalse (newContent .contains ("free" )); //$NON-NLS-1$
130
131
}
131
132
132
133
@ 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 ];
135
138
int markerLine = newArrayFreeMarker .getAttribute (IMarker .LINE_NUMBER , -1 );
136
139
137
- createResolutionAndApply (newArrayFreeMarker );
140
+ createResolutionAndApply (newArrayFreeMarker , 2 );
138
141
139
142
String newContent = getLineContent (document , markerLine );
140
143
assertTrue (newContent .contains ("delete[]" )); //$NON-NLS-1$
141
144
assertFalse (newContent .contains ("free" )); //$NON-NLS-1$
142
145
}
143
146
144
147
@ 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 ];
147
152
int markerLine = newArrayDeleteMarker .getAttribute (IMarker .LINE_NUMBER , -1 );
148
153
149
- createResolutionAndApply (newArrayDeleteMarker );
154
+ createResolutionAndApply (newArrayDeleteMarker , 3 );
150
155
151
156
String newContent = getLineContent (document , markerLine );
152
157
assertTrue (newContent .contains ("delete[]" )); //$NON-NLS-1$
153
158
}
154
159
155
- private void createResolutionAndApply (IMarker marker ) throws CoreException {
160
+ private void createResolutionAndApply (IMarker marker , int fixedMarkers ) throws CoreException {
156
161
WrongDeallocationResolution resolution = new WrongDeallocationResolution ();
157
162
assertNotNull (resolution );
158
163
159
164
int numMarkersBefore = proj .getProject ().findMarkers (VALGRIND_MARKER_TYPE , true , 1 ).length ;
160
165
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 ;
162
168
163
- assertEquals (numMarkersAfter , numMarkersBefore - 2 );
169
+ assertEquals (numMarkersAfter , numMarkersBefore - fixedMarkers );
164
170
}
165
171
166
172
private String getLineContent (Document document , int line ) {
0 commit comments