All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.evosuite.assertion.ComparisonTraceEntry Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2010-2018 Gordon Fraser, Andrea Arcuri and EvoSuite
 * contributors
 *
 * This file is part of EvoSuite.
 *
 * EvoSuite is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3.0 of the License, or
 * (at your option) any later version.
 *
 * EvoSuite is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with EvoSuite. If not, see .
 */
/**
 *
 */
package org.evosuite.assertion;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.evosuite.Properties;
import org.evosuite.testcase.variable.VariableReference;

/**
 * 

ComparisonTraceEntry class.

* * @author Gordon Fraser */ public class ComparisonTraceEntry implements OutputTraceEntry { private final VariableReference var; private final Map equalityMap = new HashMap(); private final Map equalityMapIntVar = new HashMap(); /** *

Constructor for ComparisonTraceEntry.

* * @param var a {@link org.evosuite.testcase.variable.VariableReference} object. */ public ComparisonTraceEntry(VariableReference var) { this.var = var; } public static boolean equals(Object a, Object b) { if (a == null) { return b == null; } else if (b == null) { return false; } if (a.getClass().equals(Double.class) || a.getClass().equals(double.class)) { if (Double.compare((Double) a, (Double) b) == 0) { return true; } if ((Math.abs((Double) a - (Double) b) <= Properties.DOUBLE_PRECISION)) { return true; } return false; } else if (a.getClass().equals(Float.class) || a.getClass().equals(float.class)) { if (Float.compare((Float) a, (Float) b) == 0) { return true; } if ((Math.abs((Float) a - (Float) b) <= Properties.FLOAT_PRECISION)) { return true; } return false; } else { return a.equals(b); } } /* (non-Javadoc) * @see org.evosuite.assertion.OutputTraceEntry#differs(org.evosuite.assertion.OutputTraceEntry) */ /** *

addEntry

* * @param other a {@link org.evosuite.testcase.variable.VariableReference} object. * @param value a boolean. */ public void addEntry(VariableReference other, boolean value) { equalityMap.put(other, value); equalityMapIntVar.put(other.getStPosition(), other); } /* (non-Javadoc) * @see org.evosuite.assertion.OutputTraceEntry#getAssertions(org.evosuite.assertion.OutputTraceEntry) */ /** * {@inheritDoc} */ @Override public boolean differs(OutputTraceEntry other) { if (other instanceof ComparisonTraceEntry) { if (!((ComparisonTraceEntry) other).var.equals(var)) { return false; } ComparisonTraceEntry otherEntry = (ComparisonTraceEntry) other; for (VariableReference otherVar : equalityMap.keySet()) { if (!otherEntry.equalityMap.containsKey(otherVar)) { continue; } if (!equals(otherEntry.equalityMap.get(otherVar), equalityMap.get(otherVar))) { return true; } } } return false; } /* (non-Javadoc) * @see org.evosuite.assertion.OutputTraceEntry#getAssertions() */ /** * {@inheritDoc} */ @Override public Set getAssertions(OutputTraceEntry other) { Set assertions = new HashSet(); if (other instanceof ComparisonTraceEntry) { ComparisonTraceEntry otherEntry = (ComparisonTraceEntry) other; for (Integer otherVar : equalityMapIntVar.keySet()) { if (!otherEntry.equalityMapIntVar.containsKey(otherVar)) { continue; } if (otherVar == null) { continue; } if (!equals(otherEntry.equalityMap.get(otherEntry.equalityMapIntVar.get(otherVar)), equalityMap.get(equalityMapIntVar.get(otherVar)))) { EqualsAssertion assertion = new EqualsAssertion(); assertion.source = var; assertion.dest = equalityMapIntVar.get(otherVar); assertion.value = equalityMap.get(equalityMapIntVar.get(otherVar)); if (Properties.isRegression()) { assertion.setComment( "// (Comp) Original Value: " + equalityMap.get(equalityMapIntVar.get(otherVar)) + " | Regression Value: " + otherEntry.equalityMap .get(otherEntry.equalityMapIntVar.get(otherVar))); } assertions.add(assertion); assert (assertion.isValid()); } } } return assertions; } /* (non-Javadoc) * @see org.evosuite.assertion.OutputTraceEntry#isDetectedBy(org.evosuite.assertion.Assertion) */ /** * {@inheritDoc} */ @Override public Set getAssertions() { Set assertions = new HashSet(); for (VariableReference otherVar : equalityMap.keySet()) { if (otherVar == null) { continue; } EqualsAssertion assertion = new EqualsAssertion(); assertion.source = var; assertion.dest = otherVar; assertion.value = equalityMap.get(otherVar); assertions.add(assertion); assert (assertion.isValid()); } return assertions; } /* (non-Javadoc) * @see org.evosuite.assertion.OutputTraceEntry#cloneEntry() */ /** * {@inheritDoc} */ @Override public boolean isDetectedBy(Assertion assertion) { if (assertion instanceof EqualsAssertion) { EqualsAssertion ass = (EqualsAssertion) assertion; if (ass.source.equals(var) && equalityMap.containsKey(ass.dest)) { return !equals(equalityMap.get(ass.dest), ass.value); } } return false; } /** * {@inheritDoc} */ @Override public OutputTraceEntry cloneEntry() { ComparisonTraceEntry copy = new ComparisonTraceEntry(var); copy.equalityMap.putAll(equalityMap); copy.equalityMapIntVar.putAll(equalityMapIntVar); return copy; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy