blob: 1fe975120fd428afffa66b7e389eb691d788d676 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2011 Nokia 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:
* Nokia - Initial API and implementation. June, 2011
*******************************************************************************/
package org.eclipse.cdt.debug.edc.debugger.tests;
import java.math.BigInteger;
import org.eclipse.cdt.core.IAddress;
import org.eclipse.cdt.debug.edc.internal.formatter.FormatExtensionManager;
import org.eclipse.cdt.debug.edc.symbols.IInvalidVariableLocation;
import org.eclipse.cdt.debug.edc.symbols.IRegisterOffsetVariableLocation;
import org.eclipse.cdt.debug.edc.symbols.IRegisterVariableLocation;
import org.eclipse.cdt.debug.edc.symbols.IValueVariableLocation;
import org.eclipse.cdt.debug.edc.symbols.IVariableLocation;
import org.eclipse.cdt.debug.edc.symbols.VariableLocationFactory;
import org.eclipse.core.runtime.CoreException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Test various VariableLocations.
*/
public class VariableLocationTest extends SimpleDebuggerTest {
boolean formatterSetting;
@Before
public void turnOnFormatter() {
formatterSetting = FormatExtensionManager.instance().isEnabled();
FormatExtensionManager.instance().setEnabled(true);
}
@After
public void restoreFormatter() {
FormatExtensionManager.instance().setEnabled(formatterSetting);
}
/*
* Control should stop at a location in the snapshot launch.
*/
@Test
public void testRegisterVariableLocation() throws Exception {
// create an artificial RegisterVariableLocation.
IRegisterVariableLocation loc = VariableLocationFactory.createRegisterVariableLocation(getEdcServiceTracker(), frame, 0);
IAddress addr = loc.getAddress();
Assert.assertNull(addr); // no addr for register location
String str = loc.getRegisterName();
Assert.assertEquals("EAX", str);
// This is the register value of "EAX"
BigInteger val = loc.readValue(4);
Assert.assertEquals(0x425e3333, val.intValue());
/* Hmm, this is asynchronous write. */
loc.writeValue(4, BigInteger.valueOf(0x10));
Thread.sleep(1000);
val = loc.readValue(4);
Assert.assertEquals(0x10, val.intValue());
IVariableLocation loc2 = loc.addOffset(0x10);
Assert.assertTrue(loc2 instanceof IRegisterOffsetVariableLocation);
IRegisterOffsetVariableLocation rovLoc = (IRegisterOffsetVariableLocation)loc2;
addr = rovLoc.getAddress();
Assert.assertNull(addr);
val = rovLoc.readValue(4);
Assert.assertEquals(0x20, val.intValue());
Assert.assertEquals("0x20", rovLoc.getLocationName());
Assert.assertEquals("EAX + 16", rovLoc.toString());
Assert.assertEquals(0x10, rovLoc.getOffset());
// Add more "offset"
//
loc2 = rovLoc.addOffset(0x10);
Assert.assertTrue(loc2 instanceof IRegisterOffsetVariableLocation);
rovLoc = (IRegisterOffsetVariableLocation)loc2;
Assert.assertEquals(0x30, rovLoc.readValue(4).intValue());
Assert.assertEquals("EAX + 32", rovLoc.toString());
}
@Test
public void testValueVariableLocation() {
// create an artificial RegisterVariableLocation.
IValueVariableLocation loc = VariableLocationFactory.createValueVariableLocation(BigInteger.valueOf(0x1000));
String str = loc.toString();
Assert.assertEquals("0x1000", str);
BigInteger val;
try {
val = loc.readValue(4);
} catch (CoreException e) {
Assert.fail("readValue() failed.");
return;
}
Assert.assertEquals(0x1000, val.intValue());
IAddress addr = loc.getAddress();
Assert.assertNull(addr); // no addr
Assert.assertNull(loc.getServicesTracker());
Assert.assertEquals("", loc.getLocationName());
Assert.assertNull(loc.getContext());
/* this just throws exception. */
try {
loc.writeValue(4, BigInteger.valueOf(0x10));
Assert.fail("writeValue fails to throw exception.");
} catch (CoreException e) {
}
// Now addOffset to create a new location.
//
IVariableLocation loc2 = loc.addOffset(0x10);
Assert.assertTrue(loc2 instanceof IValueVariableLocation);
IValueVariableLocation vvLoc = (IValueVariableLocation)loc2;
addr = vvLoc.getAddress();
Assert.assertNull(addr);
try {
val = vvLoc.readValue(4);
} catch (CoreException e) {
Assert.fail("readValue() failed.");
return;
}
Assert.assertEquals(0x1010, val.intValue());
}
@Test
public void testInvalidVariableLocation() {
String msg = "testTrash";
// create an artificial RegisterVariableLocation.
IInvalidVariableLocation loc = VariableLocationFactory.createInvalidVariableLocation(msg);
String str = loc.toString();
Assert.assertTrue(str.contains(msg));
Assert.assertEquals(msg, loc.getMessage());
try {
loc.readValue(4);
Assert.fail("readValue() should fail but didn't.");
} catch (CoreException e) {
Assert.assertTrue(e.getMessage().contains(msg));
}
Assert.assertEquals("", loc.getLocationName());
Assert.assertNull(loc.getAddress()); // no addr
Assert.assertNull(loc.getContext());
Assert.assertNull(loc.getServicesTracker());
/* this just throws exception. */
try {
loc.writeValue(4, BigInteger.valueOf(0x10));
Assert.fail("writeValue() should fail but didn't.");
} catch (CoreException e) {
}
loc.setMessage("msg2");
loc.getMessage().equals("msg2");
// Now addOffset to create a new location.
//
IVariableLocation loc2 = loc.addOffset(0x10);
Assert.assertEquals(loc, loc2);
}
@Override
public String getAlbumName() {
return "ExpressionsBasic.dsa";
}
}