Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9aa129a878a9405234d262eaad56ab808f4f4008 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*******************************************************************************
 * Copyright (c) 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.debug.tests.core;

import org.eclipse.debug.core.model.ILineBreakpoint;
import org.eclipse.debug.core.model.IValue;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.debug.core.IJavaThread;
import org.eclipse.jdt.debug.eval.IEvaluationResult;
import org.eclipse.jdt.debug.tests.AbstractDebugTest;
import org.eclipse.jdt.internal.debug.core.model.JDIPrimitiveValue;

/**
 * Tests that the new forms allowed in literals in Java 7 work as expected
 * 
 * @since 3.1.200
 */
public class LiteralTests17 extends AbstractDebugTest {

	public static final String LITERAL_TYPE_NAME = "Literals17";
	
	/**
	 * Constructor
	 */
	public LiteralTests17() {
		super("Tests for Java 1.7 literal support in evaluations");
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jdt.debug.tests.AbstractDebugTest#getProjectContext()
	 */
	@Override
	protected IJavaProject getProjectContext() {
		return get17Project();
	}
	
	/**
	 * Perform the evaluation on the given snippet and return the value
	 * @param snippet
	 * @return returns the evaluation value or <code>null</code>
	 * @throws Exception
	 */
	IValue doEval(String snippet) throws Exception {
		ILineBreakpoint bp = createLineBreakpoint(25, LITERAL_TYPE_NAME);
		IJavaThread thread = null;
		try {
			thread = launchToLineBreakpoint(LITERAL_TYPE_NAME, bp);
			IEvaluationResult result = evaluate(snippet, thread);
			assertNotNull("There must be an evaluation result", result);
			assertTrue("There must be no errors in the result", !result.hasErrors());
			return result.getValue(); 
		}
		finally {
			terminateAndRemove(thread);
			removeAllBreakpoints();
		}
	}
	
	/**
	 * Runs evaluations on the two given snippets returning the value from the second snippet. This method allows us to 
	 * run two snippets on the same thread where the second snippet may or may not depend on the state change from the
	 * first snippet
	 * 
	 * @param snippet
	 * @param snippet2
	 * @return the {@link IEvaluationResult}
	 * @throws Exception
	 */
	IValue doEval(String snippet, String snippet2) throws Exception {
		ILineBreakpoint bp = createLineBreakpoint(25, LITERAL_TYPE_NAME);
		IJavaThread thread = null;
		try {
			thread = launchToLineBreakpoint(LITERAL_TYPE_NAME, bp);
			IEvaluationResult result = evaluate(snippet, thread);
			assertNotNull("There must be an evaluation result", result);
			assertTrue("There must be no errors in the result", !result.hasErrors());
			result = evaluate(snippet2, thread);
			assertNotNull("There must be an evaluation result", result);
			assertTrue("There must be no errors in the result", !result.hasErrors());
			return result.getValue(); 
		}
		finally {
			terminateAndRemove(thread);
			removeAllBreakpoints();
		}
	}
	
	/**
	 * Tests that an addition evaluation with an int with underscores in it works
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreIntEval() throws Exception {
		IValue value = doEval("literals.x1 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new integer value should be 11", val.getIntValue() == 11);
	}
	
	/**
	 * Tests that we can assign a variable value to an int with underscores
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreIntVarAssignment() throws Exception {
		IValue value = doEval("literals.x1 = 1_______1;", "literals.x1 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new integer value should be 12", val.getIntValue() == 12);
	}
	
	/**
	 * Tests that an addition evaluation with a short with underscores in it works
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreShortEval() throws Exception {
		IValue value = doEval("literals.x9 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new short value should be 11", val.getShortValue() == 11);
	}
	
	/**
	 * Tests that we can assign a variable value to a short with underscores
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreShortVarAssignment() throws Exception {
		IValue value = doEval("literals.x9 = 1_______1;", "literals.x9 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new short value should be 12", val.getShortValue() == 12);
	}
	
	/**
	 * Tests that an addition evaluation with a byte with underscores in it works
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreByteEval() throws Exception {
		IValue value = doEval("literals.x10 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new byte value should be 9", val.getByteValue() == 9);
	}
	
	/**
	 * Tests that we can assign a variable value to a short with underscores
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreByteVarAssignment() throws Exception {
		IValue value = doEval("literals.x10 = 1_______1;", "literals.x10 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new byte value should be 12", val.getByteValue() == 12);
	}
	
	/**
	 * Tests that an addition evaluation with a long with underscores in it works
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreLongEval() throws Exception {
		IValue value = doEval("literals.x8 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new long value should be 11", val.getLongValue() == 11);
	}
	
	/**
	 * Tests that we can assign a variable value to a long with underscores
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreLongVarAssignment() throws Exception {
		IValue value = doEval("literals.x8 = 1_______1L;", "literals.x8 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new long value should be 12", val.getLongValue() == 12);
	}
	
	/**
	 * Tests that an addition evaluation with a float with underscores in it works
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreFloatEval() throws Exception {
		IValue value = doEval("literals.x6 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new float value should be 4.1415", val.getFloatValue() == 4.1415F);
	}
	
	/**
	 * Tests that we can assign a variable value to a float with underscores
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreFloatVarAssignment() throws Exception {
		IValue value = doEval("literals.x6 = 6.1_4_1_5F;", "literals.x6 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new float value should be 7.1415", val.getFloatValue() == 7.1415F);
	}
	
	/**
	 * Tests that an addition evaluation with a double with underscores in it works
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreDoubleEval() throws Exception {
		IValue value = doEval("literals.x5 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new double value should be 11.556", val.getDoubleValue() == 11.556D);
	}
	
	/**
	 * Tests that we can assign a variable value to a double with underscores
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreDoubleVarAssignment() throws Exception {
		IValue value = doEval("literals.x5 = 1_5.5_5_6D;", "literals.x5 + 1.000D");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new double value should be 16.556", val.getDoubleValue() == 16.555999999999997D);
	}
	
	/**
	 * Tests that an addition evaluation with a binary literal with underscores in it works
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreBinaryEval() throws Exception {
		IValue value = doEval("literals.x4 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new integer value should be 9", val.getIntValue() == 9);
	}
	
	/**
	 * Tests that we can assign a variable value to a binary literal with underscores
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreBinaryVarAssignment() throws Exception {
		IValue value = doEval("literals.x4 = 0b1_0_0_0_0;", "literals.x4 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new integer value should be 17", val.getIntValue() == 17);
	}
	
	/**
	 * Tests that an addition evaluation with a hex with underscores in it works
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreHexEval() throws Exception {
		IValue value = doEval("literals.x2 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new integer value should be 17", val.getIntValue() == 17);
	}
	
	/**
	 * Tests that we can assign a variable value to a hex with underscores
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreHexVarAssignment() throws Exception {
		IValue value = doEval("literals.x2 = 0x1_0_0;", "literals.x2 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new integer value should be 257", val.getIntValue() == 257);
	}
	
	/**
	 * Tests that an addition evaluation with an octal with underscores in it works
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreOctEval() throws Exception {
		IValue value = doEval("literals.x3 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new integer value should be 9", val.getIntValue() == 9);
	}
	
	/**
	 * Tests that we can assign a variable value to an octal with underscores
	 * 
	 * @throws Exception
	 */
	public void testUnderscoreOctVarAssignment() throws Exception {
		IValue value = doEval("literals.x3 = 0_100;", "literals.x3 + 1");
		assertNotNull("The value should not be null", value);
		assertTrue("The underlying value must be a primitive value", value instanceof JDIPrimitiveValue);
		JDIPrimitiveValue val = (JDIPrimitiveValue) value;
		assertTrue("The new integer value should be 65", val.getIntValue() == 65);
	}
}

Back to the top