test(singleton_fragment, [true]) :-
code2vec:reverse_fragment(a, a).
test(long_fragment, [true]) :-
code2vec:reverse_fragment(a-b-c-d-e-f, f-e-d-c-b-a).
test(joining_two, [true(Result == [(1-2-3)-root(root)-(c-b-a)])]) :-
Root = root,
LeftPaths = [a-b-c],
RightPaths = [1-2-3],
code2vec:joined_paths(Root, LeftPaths, RightPaths, Result).
test(two_left_one_right, [true(Result == [(1-2-3)-root(root)-(c-b-a), (1-2-3)-root(root)-(f-e-d)])]) :-
Root = root,
LeftPaths = [a-b-c, d-e-f],
RightPaths = [1-2-3],
code2vec:joined_paths(Root, LeftPaths, RightPaths, Result).
test(one_left_two_right, [true(Result == [(1-2-3)-root(root)-(c-b-a), (4-5-6)-root(root)-(c-b-a)])]) :-
Root = root,
LeftPaths = [a-b-c],
RightPaths = [1-2-3, 4-5-6],
code2vec:joined_paths(Root, LeftPaths, RightPaths, Result).
test(lots, [true(Result == [
(1-2-3)-root(root)-(c-b-a),
(1-2-3)-root(root)-(f-e-d),
(1-2-3)-root(root)-(i-h-g),
(4-5-6)-root(root)-(c-b-a),
(4-5-6)-root(root)-(f-e-d),
(4-5-6)-root(root)-(i-h-g),
(7-8-9)-root(root)-(c-b-a),
(7-8-9)-root(root)-(f-e-d),
(7-8-9)-root(root)-(i-h-g)
])]) :-
Root = root,
LeftPaths = [a-b-c, d-e-f, g-h-i],
RightPaths = [1-2-3, 4-5-6, 7-8-9],
code2vec:joined_paths(Root, LeftPaths, RightPaths, Result).
test(int_member, [true(Result == [x-identifier-root(member)-(integer_set-'INT')]), nondet]) :-
B = b(member(b(identifier(x),integer,[]),b(integer_set('INT'),set(integer),[])),pred,[]),
code2vec:leaf_paths(B, Result).
test(conjunct, [true(Result == Expected), nondet]) :-
% x : INTEGER & y : INTEGER
B = b(conjunct(b(member(b(identifier(x),integer,[]),b(integer_set('INTEGER'),set(integer),[])),pred,[]),b(member(b(identifier(y),integer,[]),b(integer_set('INTEGER'),set(integer),[])),pred,[])),pred,[]),
code2vec:leaf_paths(B, ResultUnsorted),
ExpectedUnsorted = [
x-identifier-member-root(conjunct)-(member-identifier-y),
x-identifier-member-root(conjunct)-(member-integer_set-'INTEGER'),
'INTEGER'-integer_set-member-root(conjunct)-(member-identifier-y),
'INTEGER'-integer_set-member-root(conjunct)-(member-integer_set-'INTEGER'),
x-identifier-root(member)-(integer_set-'INTEGER'),
y-identifier-root(member)-(integer_set-'INTEGER')],
sort(ResultUnsorted, Result),
sort(ExpectedUnsorted, Expected).
test(integer_addition, [true(Result == Expected), nondet]) :-
% x : INTEGER & y : INTEGER & y = x + 1
B = b(conjunct(b(conjunct(b(member(b(identifier(x),integer,[]),b(integer_set('INTEGER'),set(integer),[])),pred,[]),b(member(b(identifier(y),integer,[]),b(integer_set('INTEGER'),set(integer),[])),pred,[])),pred,[]),b(equal(b(identifier(y),integer,[]),b(add(b(identifier(x),integer,[]),b(integer(1),integer,[])),integer,[])),pred,[])),pred,[]),
code2vec:leaf_paths(B, UnsortedResult),
sort(UnsortedResult, Result),
UnsortedExpected = [
'INTEGER'-integer_set-member-conjunct-root(conjunct)-(equal-add-identifier-x),
'INTEGER'-integer_set-member-conjunct-root(conjunct)-(equal-add-identifier-x),
'INTEGER'-integer_set-member-conjunct-root(conjunct)-(equal-add-integer-1),
'INTEGER'-integer_set-member-conjunct-root(conjunct)-(equal-add-integer-1),
'INTEGER'-integer_set-member-conjunct-root(conjunct)-(equal-identifier-y),
'INTEGER'-integer_set-member-conjunct-root(conjunct)-(equal-identifier-y),
'INTEGER'-integer_set-member-root(conjunct)-(member-identifier-y),
'INTEGER'-integer_set-member-root(conjunct)-(member-integer_set-'INTEGER'),
x-identifier-member-conjunct-root(conjunct)-(equal-add-identifier-x),
x-identifier-member-conjunct-root(conjunct)-(equal-add-integer-1),
x-identifier-member-conjunct-root(conjunct)-(equal-identifier-y),
x-identifier-member-root(conjunct)-(member-identifier-y),
x-identifier-member-root(conjunct)-(member-integer_set-'INTEGER'),
x-identifier-root(add)-(integer-1),
x-identifier-root(member)-(integer_set-'INTEGER'),
y-identifier-member-conjunct-root(conjunct)-(equal-add-identifier-x),
y-identifier-member-conjunct-root(conjunct)-(equal-add-integer-1),
y-identifier-member-conjunct-root(conjunct)-(equal-identifier-y),
y-identifier-root(equal)-(add-identifier-x),
y-identifier-root(equal)-(add-integer-1),
y-identifier-root(member)-(integer_set-'INTEGER')],
sort(UnsortedExpected, Expected).
test(exists_extraction, [true(Result == Expected), nondet]) :-
% #a,b . (c > d & e > 1)
% Note: quantifiers have the list of variables as the last child node.
B = b(exists([b(identifier(a),integer,[]),b(identifier(b),integer,[])],
b(conjunct(
b(greater(b(identifier(c),integer,[]),b(identifier(d),integer,[])),pred,[]),
b(greater(b(identifier(e),integer,[]),b(integer(1),integer,[])),pred,[])),
pred,[])),pred,[]),
code2vec:leaf_paths(B, UnsortedResult),
sort(UnsortedResult, Result),
UnsortedExpected = [
c-identifier-root(greater)-(identifier-d),
c-identifier-greater-root(conjunct)-(greater-identifier-e),
c-identifier-greater-root(conjunct)-(greater-integer-1),
d-identifier-greater-root(conjunct)-(greater-identifier-e),
d-identifier-greater-root(conjunct)-(greater-integer-1),
c-identifier-greater-conjunct-root(exists)-(identifier-a),
c-identifier-greater-conjunct-root(exists)-(identifier-b),
d-identifier-greater-conjunct-root(exists)-(identifier-a),
d-identifier-greater-conjunct-root(exists)-(identifier-b),
e-identifier-greater-conjunct-root(exists)-(identifier-a),
e-identifier-greater-conjunct-root(exists)-(identifier-b),
e-identifier-root(greater)-(integer-1),
1-integer-greater-conjunct-root(exists)-(identifier-a),
1-integer-greater-conjunct-root(exists)-(identifier-b),
a-identifier-root(exists)-(identifier-b)
],
sort(UnsortedExpected, Expected).
test(normal_constant, [true(Result == [constant-expr])]) :-
code2vec:paths_and_fragments(expr, [], constant, [], Result).
test(wrapped_constant, [true(Result == [constant-expr])]) :-
code2vec:paths_and_fragments(expr, [], [constant], [], Result).
test(list_of_constants, [true(Result == [a-expr, b-expr, c-expr])]) :-
code2vec:paths_and_fragments(expr, [], [a, b, c], [], Result).
test(join_two, [true(Result == Expected)]) :-
Fragments = [[a-1-root, b-1-root], [c-2-root, d-2-root]],
Expected = [
a-1-root(root)-(2-c),
a-1-root(root)-(2-d),
b-1-root(root)-(2-c),
b-1-root(root)-(2-d)
],
code2vec:joined_sub_paths(root, Fragments, Result).
test(join_three, [true(Result == Expected)]) :-
Fragments = [[a-1-root, b-1-root], [c-2-root, d-2-root], [e-3-root]],
Expected = [
a-1-root(root)-(2-c),
a-1-root(root)-(2-d),
b-1-root(root)-(2-c),
b-1-root(root)-(2-d),
c-2-root(root)-(3-e),
d-2-root(root)-(3-e),
a-1-root(root)-(3-e),
b-1-root(root)-(3-e)
],
code2vec:joined_sub_paths(root, Fragments, Result).