| 71 | } |
| 72 | |
| 73 | func TestReduce(t *testing.T) { |
| 74 | tests := []testCase{ |
| 75 | { |
| 76 | initialFacts: []ast.Atom{atom("bar(0)"), atom("bar(1)")}, |
| 77 | clause: clause("foo(X) :- bar(Y) |> do fn:group_by(), let X = fn:sum(Y)."), |
| 78 | expectedFacts: []ast.Atom{atom("foo(1)")}, |
| 79 | }, |
| 80 | { |
| 81 | initialFacts: []ast.Atom{atom("bar(0)"), atom("bar(1)")}, |
| 82 | clause: clause("foo(X) :- bar(Y) |> do fn:group_by(), let X = fn:count()."), |
| 83 | expectedFacts: []ast.Atom{atom("foo(2)")}, |
| 84 | }, |
| 85 | { |
| 86 | initialFacts: []ast.Atom{atom("bar(0)"), atom("bar(1)")}, |
| 87 | clause: clause("foo(X) :- bar(Y) |> do fn:group_by(), let X = fn:min(Y)."), |
| 88 | expectedFacts: []ast.Atom{atom("foo(0)")}, |
| 89 | }, |
| 90 | { |
| 91 | initialFacts: []ast.Atom{atom("bar(0)"), atom("bar(1)")}, |
| 92 | clause: clause("foo(Min, Max) :- bar(Y) |> do fn:group_by(), let Min = fn:min(Y), let Max = fn:max(Y)."), |
| 93 | expectedFacts: []ast.Atom{atom("foo(0, 1)")}, |
| 94 | }, |
| 95 | // An alternative way to fn:count() |
| 96 | { |
| 97 | initialFacts: []ast.Atom{atom("bar(0)"), atom("bar(1)")}, |
| 98 | clause: clause("foo(Num) :- bar(Y) |> do fn:group_by(), let L = fn:collect(Y), let Num = fn:list:len(L)."), |
| 99 | expectedFacts: []ast.Atom{atom("foo(2)")}, |
| 100 | }, |
| 101 | } |
| 102 | |
| 103 | for _, test := range tests { |
| 104 | runEval(test, t) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestGroupBy(t *testing.T) { |
| 109 | tests := []testCase{ |