using Test include("ASPLang.jl") using .ASPLang @testset "malformed" begin # # # @test_throws "" parse_term("1a") @test_throws "" parse_term("1+") @test_throws "" parse_term("+") @test_throws "" parse_term("(") @test_throws "" parse_term(")") # # Spaces # @test_throws "" parse_term("1 1") @test_throws "" parse_term("+ 1") end @testset "constants" begin # # NumberConstant # @test parse_term("1") == NumberConstant("1") @test parse_term("11") == NumberConstant("11") @test parse_term("+1") == NumberConstant("+1") @test parse_term("+++1") == NumberConstant("+++1") @test parse_term("-1") == Negated(NumberConstant("1")) @test parse_term("- 11") == Negated(NumberConstant("11")) @test parse_term("---1") == Negated(Negated(Negated(NumberConstant("1")))) # # SymbolicConstant # @test parse_term("a") == SymbolicConstant("a") @test parse_term("aA") == SymbolicConstant("aA") @test parse_term("a1") == SymbolicConstant("a1") @test parse_term("a_1") == SymbolicConstant("a_1") # # StringConstant # @test parse_term("\"a\"") == StringConstant("a") @test parse_term("\"aA\"") == StringConstant("aA") @test parse_term("\"a_1\"") == StringConstant("a_1") @test parse_term("\"a1\"") == StringConstant("a1") @test parse_term("\"a 1\"") == StringConstant("a 1") @test parse_term("\"A\"") == StringConstant("A") @test parse_term("\"AA\"") == StringConstant("AA") @test parse_term("\"A_1\"") == StringConstant("A_1") @test parse_term("\"A1\"") == StringConstant("A1") @test parse_term("\"A 1\"") == StringConstant("A 1") @test parse_term("\"0\"") == StringConstant("0") @test parse_term("\"0A\"") == StringConstant("0A") @test parse_term("\"0_1\"") == StringConstant("0_1") @test parse_term("\"01\"") == StringConstant("01") @test parse_term("\"0 1\"") == StringConstant("0 1") end @testset "variables" begin # # Variable # @test parse_term("Ax") == Variable("Ax") @test parse_term("A1") == Variable("A1") @test parse_term("A_x") == Variable("A_x") @test parse_term("_x") == Variable("_x") @test parse_term("_1") == Variable("_1") @test parse_term("__x") == Variable("__x") end @testset "basic arithmetic terms" begin # # Single Operations # @test parse_term("1+1") == Sum(Any[NumberConstant("1"), NumberConstant("1")]) @test parse_term("1-1") == Sum(Any[NumberConstant("1"), Negated(NumberConstant("1"))]) @test parse_term("1*1") == Product(Any[NumberConstant("1"), NumberConstant("1")]) @test parse_term("1/1") == Product(Any[NumberConstant("1"), Inverted(NumberConstant("1"))]) @test parse_term("1^1") == Power(Any[NumberConstant("1"), NumberConstant("1")]) # # Simple Parenthesis # @test parse_term("(1)") == NumberConstant("1") @test parse_term("(1+1)") == Sum(Any[NumberConstant("1"), NumberConstant("1")]) @test parse_term("(1-1)") == Sum(Any[NumberConstant("1"), Negated(NumberConstant("1"))]) @test parse_term("(1*1)") == Product(Any[NumberConstant("1"), NumberConstant("1")]) @test parse_term("(1/1)") == Product(Any[NumberConstant("1"), Inverted(NumberConstant("1"))]) @test parse_term("(1^1)") == Power(Any[NumberConstant("1"), NumberConstant("1")]) end @testset "arithmetic associative rules" begin @test parse_term("1 + 2 + 3") == Sum(Any[NumberConstant("1"), NumberConstant("2"), NumberConstant("3")]) @test parse_term("1 + (2 + 3)") == Sum(Any[NumberConstant("1"), Sum(Any[NumberConstant("2"), NumberConstant("3")])]) @test parse_term("(1 + 2) + 3") == Sum(Any[Sum(Any[NumberConstant("1"), NumberConstant("2")]), NumberConstant("3")]) # # Default Associative Rules # @test parse_term("2 + 3 * 4") == Sum(Any[ NumberConstant("2"), Product(Any[NumberConstant("3"), NumberConstant("4")])]) @test parse_term("2 * 3 + 4") == Sum(Any[ Product(Any[NumberConstant("2"), NumberConstant("3")]), NumberConstant("4")]) end @testset "functional terms" begin @test parse_term("1 + 2 + 3") == Sum(Any[NumberConstant("1"), NumberConstant("2"), NumberConstant("3")]) @test parse_term("1 + (2 + 3)") == Sum(Any[NumberConstant("1"), Sum(Any[NumberConstant("2"), NumberConstant("3")])]) @test parse_term("(1 + 2) + 3") == Sum(Any[Sum(Any[NumberConstant("1"), NumberConstant("2")]), NumberConstant("3")]) # # Functional Terms # @test parse_term("f()") == FunctionalTerm(Any[Functor("f")]) @test parse_term("f(1)") == FunctionalTerm(Any[Functor("f"), NumberConstant("1")]) @test parse_term("f(1,2)") == FunctionalTerm(Any[Functor("f"), NumberConstant("1"), NumberConstant("2")]) @test parse_term("f(1,g(2))") == FunctionalTerm(Any[Functor("f"), NumberConstant("1"), FunctionalTerm(Any[Functor("g"), NumberConstant("2")])]) end @testset "predicate atoms" begin # # Predicate Atoms # @test parse_atom("p") == PredicateName("p") @test parse_atom("p()") == PredicateAtom(Any[PredicateName("p")]) @test parse_atom("p(1)") == PredicateAtom(Any[PredicateName("p"), NumberConstant("1")]) @test parse_atom("p(1,2)") == PredicateAtom(Any[PredicateName("p"), NumberConstant("1"), NumberConstant("2")]) @test parse_atom("p(1,f(2))") == PredicateAtom(Any[PredicateName("p"), NumberConstant("1"), FunctionalTerm(Any[Functor("f"), NumberConstant("2")])]) end @testset "negated atoms" begin @test parse_atom("-p") == NegatedAtom(PredicateName("p")) @test parse_atom("- p") == NegatedAtom(PredicateName("p")) @test parse_atom("-- p") == NegatedAtom(NegatedAtom(PredicateName("p"))) end