Blame view

code/asplang/tests_ASPlang.jl 5.58 KB
3e0f9b8a   Francisco Coelho   back to work?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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