blob: 2576fd43031bdcefa3a1114ea2946ff42da898f0 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2011 Broadcom 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:
* Broadcom - Initial API and implementation
* Nokia - move to package org.eclipse.cdt.debug.edc.tests
*******************************************************************************/
package org.eclipse.cdt.debug.edc.tests;
import org.eclipse.cdt.core.IAddressFactory;
import org.eclipse.cdt.debug.edc.internal.symbols.FunctionScope;
import org.eclipse.cdt.debug.edc.symbols.IScope;
import org.eclipse.cdt.utils.Addr64Factory;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class ScopeTest {
protected static IScope scopeA;
protected static IScope scopeA_;
protected static IScope scopeB;
protected static IScope scopeC;
protected static IScope scopeD;
protected static IScope scopeE;
@BeforeClass
public static void setUp(){
IAddressFactory addrFactory = new Addr64Factory();
// |0 |50 |100 |150 |200 |250
// < scopeA >< scopeB >
// < scope C >
// < scopeD >
// < scopeE >
scopeA = new FunctionScope("scopeA", null, addrFactory.createAddress("0x0"), addrFactory.createAddress("0x100"), null);
scopeA_ = new FunctionScope("scopeA", null, addrFactory.createAddress("0x0"), addrFactory.createAddress("0x100"), null);
scopeB = new FunctionScope("scopeB", null, addrFactory.createAddress("0x100"), addrFactory.createAddress("0x200"), null);
scopeC = new FunctionScope("scopeC", null, addrFactory.createAddress("0x50"), addrFactory.createAddress("0x150"), null);
scopeD = new FunctionScope("scopeD", null, addrFactory.createAddress("0x100"), addrFactory.createAddress("0x250"), null);
scopeE = new FunctionScope("scopeE", null, addrFactory.createAddress("0x0"), addrFactory.createAddress("0x100"), null);
}
@Test
public void testEquals() {
// the following two assertions w.r.t. Scope equality are not really
// valid based upon the fact that one cannot compute a hashCode() for
// an object based upon mutable values, and lowAddress and highAddress
// in Scope are mutable. and since two objects that return true for
// equals() must return the same hashCode(), these members of Scope
// cannot be used for equals(), either.
// Assert.assertTrue("Identically valued scopes are equal", scopeA.equals(scopeA_));
//
// Assert.assertEquals("Identically valued scopes have same hashCode", scopeA.hashCode(), scopeA_.hashCode());
Assert.assertTrue("Different scope objects with equal contents return true for contentsEquals()",
((FunctionScope)scopeA).contentsEquals(scopeA_));
((FunctionScope)scopeA).addChild(scopeE);
Assert.assertFalse("Scopes with same location but different children return false for contentsEquals()",
((FunctionScope)scopeA).contentsEquals(scopeA_));
((FunctionScope)scopeA_).addChild(scopeE);
Assert.assertTrue("Scopes with same location and same children return true for contentsEquals()",
((FunctionScope)scopeA).contentsEquals(scopeA_));
Assert.assertFalse("Different valued scopes are not equal", scopeA.equals(scopeE));
Assert.assertFalse("Different valued scopes are not equal", scopeA.equals(scopeD));
Assert.assertFalse("Different valued scopes are not equal", scopeA.equals(scopeB));
Assert.assertFalse("Different valued scopes have different hashCodes", scopeA.hashCode() == scopeB.hashCode());
Assert.assertFalse("Different valued scopes have different hashCodes", scopeA.hashCode() == scopeE.hashCode());
}
@Test
public void testScopeComparison(){// Fix 1
Assert.assertTrue(scopeA.compareTo(scopeB)<0);
Assert.assertTrue(scopeA.compareTo(scopeC)<0);
Assert.assertTrue(scopeA.compareTo(scopeD)<0);
Assert.assertTrue(scopeB.compareTo(scopeD)<0); // original bug being tested
Assert.assertTrue(scopeE.compareTo(scopeA)==0);
}
}