Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Unit Tests #335

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import at.ac.tuwien.kr.alpha.app.mappers.AnswerSetToWorkbookMapperTest;
import at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder;

// TODO this is an integration test
public class AnswerSetToXlsxWriterTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

// TODO this is an end-to-end test
public class MainTest {

private static final String INPUT = "p(a). " + System.lineSeparator() + " b :- p(X)." + System.lineSeparator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import at.ac.tuwien.kr.alpha.api.impl.AlphaImpl;
import at.ac.tuwien.kr.alpha.api.programs.analysis.ComponentGraph;

// TODO This is a functional test and should not be run with standard unit tests
public class ComponentGraphWriterTest {
private static final String LS = System.lineSeparator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import at.ac.tuwien.kr.alpha.api.impl.AlphaImpl;
import at.ac.tuwien.kr.alpha.api.programs.analysis.DependencyGraph;

// TODO This is a functional test and should not be run with standard unit tests
public class DependencyGraphWriterTest {
private static final String LS = System.lineSeparator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import at.ac.tuwien.kr.alpha.api.terms.Term;
import at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder;

// TODO This is a functional test and should not be run with standard unit tests
public class AnswerSetToWorkbookMapperTest {

private AnswerSetToWorkbookMapper mapper = new AnswerSetToWorkbookMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package at.ac.tuwien.kr.alpha.core.externals;
package at.ac.tuwien.kr.alpha.commons.externals;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package at.ac.tuwien.kr.alpha.core.common.fixedinterpretations;
package at.ac.tuwien.kr.alpha.commons.externals;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package at.ac.tuwien.kr.alpha.core.common.fixedinterpretations;
package at.ac.tuwien.kr.alpha.commons.externals;

import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package at.ac.tuwien.kr.alpha.core.externals;
package at.ac.tuwien.kr.alpha.commons.externals;

import java.lang.reflect.Method;
import java.util.ArrayList;
Expand All @@ -44,13 +44,6 @@
import at.ac.tuwien.kr.alpha.commons.Predicates;
import at.ac.tuwien.kr.alpha.commons.atoms.Atoms;
import at.ac.tuwien.kr.alpha.commons.terms.Terms;
import at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.BinaryPredicateInterpretation;
import at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.BindingMethodPredicateInterpretation;
import at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.IntPredicateInterpretation;
import at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.LongPredicateInterpretation;
import at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.MethodPredicateInterpretation;
import at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.SuppliedPredicateInterpretation;
import at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.UnaryPredicateInterpretation;

public final class Externals {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package at.ac.tuwien.kr.alpha.core.common.fixedinterpretations;
package at.ac.tuwien.kr.alpha.commons.externals;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package at.ac.tuwien.kr.alpha.core.common.fixedinterpretations;
package at.ac.tuwien.kr.alpha.commons.externals;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package at.ac.tuwien.kr.alpha.core.common.fixedinterpretations;
package at.ac.tuwien.kr.alpha.commons.externals;

import org.apache.commons.lang3.ClassUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package at.ac.tuwien.kr.alpha.core.common.fixedinterpretations;
package at.ac.tuwien.kr.alpha.commons.externals;

import at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation;
import at.ac.tuwien.kr.alpha.api.terms.ConstantTerm;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package at.ac.tuwien.kr.alpha.commons.externals;

import java.util.List;
import java.util.Set;

import at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation;
import at.ac.tuwien.kr.alpha.api.terms.ConstantTerm;
import at.ac.tuwien.kr.alpha.api.terms.Term;

// TODO this looks like a duplicate
public interface PredicateInterpretationImpl extends PredicateInterpretation {
@Override
Set<List<ConstantTerm<?>>> evaluate(List<Term> terms);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package at.ac.tuwien.kr.alpha.core.common.fixedinterpretations;
package at.ac.tuwien.kr.alpha.commons.externals;

import at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.BindingPredicateInterpretation;
import at.ac.tuwien.kr.alpha.api.terms.ConstantTerm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package at.ac.tuwien.kr.alpha.core.common.fixedinterpretations;
package at.ac.tuwien.kr.alpha.commons.externals;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package at.ac.tuwien.kr.alpha.commons.atoms;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import at.ac.tuwien.kr.alpha.api.programs.atoms.Atom;
import at.ac.tuwien.kr.alpha.commons.Predicates;
import at.ac.tuwien.kr.alpha.commons.terms.Terms;

/**
* Test for basic functionality of various implementations of {@link Atom}.
*
* Copyright (c) 2019-2021, the Alpha Team.
*/
public class BasicAtomImplTest {

@Test
public void testIsBasicAtomGround() {
BasicAtomImpl a = new BasicAtomImpl(Predicates.getPredicate("bla", 2), Terms.newSymbolicConstant("blubb"),
Terms.newFunctionTerm("foo", Terms.newSymbolicConstant("bar")));
assertTrue(a.isGround());
BasicAtomImpl a1 = new BasicAtomImpl(Predicates.getPredicate("foo", 4), Terms.newConstant(1), Terms.newConstant(2), Terms.newConstant(3),
Terms.newConstant("bar"));
assertTrue(a1.isGround());
BasicAtomImpl a2 = new BasicAtomImpl(Predicates.getPredicate("foo", 1), Terms.newVariable("BAR"));
assertFalse(a2.isGround());
BasicAtomImpl a3 = new BasicAtomImpl(Predicates.getPredicate("foo", 3), Terms.newSymbolicConstant("b"), Terms.newSymbolicConstant("a"),
Terms.newFunctionTerm("r", Terms.newConstant("bla"), Terms.newVariable("BLUBB")));
assertFalse(a3.isGround());
}

@Test
public void testAreBasicAtomsEqual() {
BasicAtomImpl a1 = new BasicAtomImpl(Predicates.getPredicate("bla", 2), Terms.newSymbolicConstant("blubb"),
Terms.newFunctionTerm("foo", Terms.newSymbolicConstant("bar")));
BasicAtomImpl a2 = new BasicAtomImpl(Predicates.getPredicate("bla", 2), Terms.newSymbolicConstant("blubb"),
Terms.newFunctionTerm("foo", Terms.newSymbolicConstant("bar")));
assertEquals(a1, a2);

BasicAtomImpl a3 = new BasicAtomImpl(Predicates.getPredicate("foo", 4), Terms.newConstant(1), Terms.newConstant(2), Terms.newConstant(3),
Terms.newConstant("bar"));
BasicAtomImpl a4 = new BasicAtomImpl(Predicates.getPredicate("foo", 4), Terms.newConstant(1), Terms.newConstant(2), Terms.newConstant(3),
Terms.newConstant("bar"));
assertEquals(a3, a4);

BasicAtomImpl a5 = new BasicAtomImpl(Predicates.getPredicate("foo", 1), Terms.newVariable("BAR"));
BasicAtomImpl a6 = new BasicAtomImpl(Predicates.getPredicate("foo", 1), Terms.newVariable("BAR"));
assertEquals(a5, a6);

BasicAtomImpl a7 = new BasicAtomImpl(Predicates.getPredicate("foo", 3), Terms.newSymbolicConstant("b"), Terms.newSymbolicConstant("a"),
Terms.newFunctionTerm("r", Terms.newConstant("bla"), Terms.newVariable("BLUBB")));
BasicAtomImpl a8 = new BasicAtomImpl(Predicates.getPredicate("foo", 3), Terms.newSymbolicConstant("b"), Terms.newSymbolicConstant("a"),
Terms.newFunctionTerm("r", Terms.newConstant("bla"), Terms.newVariable("BLUBB")));
assertEquals(a7, a8);

assertFalse(a1.equals(a3));
assertFalse(a3.equals(a1));
assertFalse(a1.equals(a5));
assertFalse(a5.equals(a1));
assertFalse(a1.equals(a7));
assertFalse(a7.equals(a1));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package at.ac.tuwien.kr.alpha.commons.atoms;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.Test;

import at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation;
import at.ac.tuwien.kr.alpha.api.externals.Predicate;
import at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom;
import at.ac.tuwien.kr.alpha.api.terms.ConstantTerm;
import at.ac.tuwien.kr.alpha.api.terms.Term;
import at.ac.tuwien.kr.alpha.commons.Predicates;
import at.ac.tuwien.kr.alpha.commons.externals.Externals;
import at.ac.tuwien.kr.alpha.commons.terms.Terms;

public class ExternalAtomImplTest {

private Map<String, PredicateInterpretation> externals;

public ExternalAtomImplTest() throws NoSuchMethodException, SecurityException {
externals = new HashMap<>();
externals.put("isFoo", Externals.processPredicateMethod(ExternalAtomImplTest.class.getMethod("isFoo", int.class)));
externals.put("extWithOutput", Externals.processPredicateMethod(ExternalAtomImplTest.class.getMethod("extWithOutput", int.class)));
}

@Predicate
public static final boolean isFoo(int bar) {
return 0xF00 == bar;
}

@Predicate
public static final Set<List<ConstantTerm<Integer>>> extWithOutput(int in) {
Set<List<ConstantTerm<Integer>>> retVal = new HashSet<>();
List<ConstantTerm<Integer>> lst = new ArrayList<>();
lst.add(Terms.newConstant(in));
retVal.add(lst);
return retVal;
}

@Test
public void testIsExternalAtomGround() {
List<Term> ext1Input = new ArrayList<>();
ext1Input.add(Terms.newConstant(1));
// ext1 := &isFoo[1]
ExternalAtom ext1 = Atoms.newExternalAtom(Predicates.getPredicate("isFoo", 1), externals.get("isFoo"), ext1Input, Collections.emptyList());
assertTrue(ext1.isGround());

// ext2 := &isFoo[bar(1)]
List<Term> ext2Input = new ArrayList<>();
ext2Input.add(Terms.newFunctionTerm("bar", Terms.newConstant(1)));
ExternalAtom ext2 = Atoms.newExternalAtom(Predicates.getPredicate("isFoo", 1), externals.get("isFoo"), ext2Input, Collections.emptyList());
assertTrue(ext2.isGround());

// ext3 := &isFoo[BLA]
List<Term> ext3Input = new ArrayList<>();
ext3Input.add(Terms.newVariable("BLA"));
ExternalAtom ext3 = Atoms.newExternalAtom(Predicates.getPredicate("isFoo", 1), externals.get("isFoo"), ext3Input, Collections.emptyList());
assertFalse(ext3.isGround());
}

@Test
@SuppressWarnings("unlikely-arg-type")
public void testAreExternalAtomsEqual() {
// ext1 := &isFoo[1]
List<Term> ext1Input = new ArrayList<>();
ext1Input.add(Terms.newConstant(1));
ExternalAtom ext1 = Atoms.newExternalAtom(Predicates.getPredicate("isFoo", 1), externals.get("isFoo"), ext1Input, Collections.emptyList());
// ext2 := &isFoo[1]
List<Term> ext2Input = new ArrayList<>();
ext2Input.add(Terms.newConstant(1));
ExternalAtom ext2 = Atoms.newExternalAtom(Predicates.getPredicate("isFoo", 1), externals.get("isFoo"), ext2Input, Collections.emptyList());

assertEquals(ext1, ext2);
assertEquals(ext2, ext1);

assertFalse(ext1.equals(null));
assertFalse(ext1.equals("bla"));
assertTrue(ext1.hashCode() == ext2.hashCode());
}

@Test
public void testExternalHasOutput() {
// ext := &extWithOutput[1](OUT)
List<Term> input = new ArrayList<>();
List<Term> output = new ArrayList<>();
input.add(Terms.newConstant(1));
output.add(Terms.newVariable("OUT"));
ExternalAtom ext = Atoms.newExternalAtom(Predicates.getPredicate("extWithOutput", 2), externals.get("extWithOutput"), input, output);

assertFalse(ext.isGround());
assertTrue(ext.hasOutput());
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package at.ac.tuwien.kr.alpha.core.externals;
package at.ac.tuwien.kr.alpha.commons.externals;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down
Loading