add tests for builtins

This commit is contained in:
Mark Thom
2018-01-25 21:01:23 -07:00
parent 700f031cd9
commit d240eaca78
3 changed files with 76 additions and 7 deletions

View File

@@ -20,7 +20,7 @@ Extend rusty-wam to include the following, among other features:
* Built-in and user-defined operators of all fixities, with custom * Built-in and user-defined operators of all fixities, with custom
associativity and precedence (_done_). associativity and precedence (_done_).
* Bignum, rational number and floating point arithmetic (_done_). * Bignum, rational number and floating point arithmetic (_done_).
* Built-in control operators (`,`, `;`, `->`, etc.) (_in progress_). * Built-in control operators (`,`, `;`, `->`, etc.) (_done_).
* Built-in predicates for list processing and top-level declarative * Built-in predicates for list processing and top-level declarative
control (`setup_call_control/3`, `call_with_inference_limit/3`, control (`setup_call_control/3`, `call_with_inference_limit/3`,
etc.) etc.)

View File

@@ -1516,7 +1516,12 @@ impl MachineState {
} }
}; };
self.heap.push(HeapCellValue::NamedStr(arity, name, None)); if arity > 0 {
self.heap.push(HeapCellValue::NamedStr(arity, name, None));
} else {
let c = Constant::Atom(name.clone());
self.heap.push(HeapCellValue::Addr(Addr::Con(c)));
}
for _ in 0 .. arity { for _ in 0 .. arity {
let h = self.heap.h; let h = self.heap.h;

View File

@@ -1166,3 +1166,67 @@ fn test_queries_on_conditionals()
assert_prolog_success!(&mut wam, "?- f(X), (g(Y), !).", [["X = a", "Y = 6"]]); assert_prolog_success!(&mut wam, "?- f(X), (g(Y), !).", [["X = a", "Y = 6"]]);
} }
#[test]
fn test_queries_on_builtins()
{
let mut wam = Machine::new();
assert_prolog_failure!(&mut wam, "?- atomic(X).");
assert_prolog_success!(&mut wam, "?- var(X), X = 3, atomic(X).", [["X = 3"]]);
assert_prolog_failure!(&mut wam, "?- var(X), X = 3, var(X).");
assert_prolog_success!(&mut wam, "?- arg(N, f(a,b,c,d), Arg).",
[["N = 1", "Arg = a"],
["N = 2", "Arg = b"],
["N = 3", "Arg = c"],
["N = 4", "Arg = d"]]);
assert_prolog_success!(&mut wam, "?- arg(1, f(a,b,c,d), Arg).", [["Arg = a"]]);
assert_prolog_success!(&mut wam, "?- arg(2, f(a,b,c,d), Arg).", [["Arg = b"]]);
assert_prolog_success!(&mut wam, "?- arg(3, f(a,b,c,d), Arg).", [["Arg = c"]]);
assert_prolog_success!(&mut wam, "?- arg(4, f(a,b,c,d), Arg).", [["Arg = d"]]);
assert_prolog_success!(&mut wam, "?- catch(arg(N, f, Arg), type_error(E), true).",
[["E = compound_expected", "Arg = _3", "N = _1"]]);
assert_prolog_success!(&mut wam, "?- catch(arg(N, _, Arg), E, true).",
[["E = instantiation_error", "Arg = _3", "N = _1"]]);
assert_prolog_success!(&mut wam, "?- arg(N, f(X, Y, Z), arg_val).",
[["X = arg_val", "Y = _3", "N = 1", "Z = _4"],
["X = _2", "Y = arg_val", "N = 2", "Z = _4"],
["X = _2", "Y = _3", "N = 3", "Z = arg_val"]]);
assert_prolog_success!(&mut wam, "?- arg(N, f(arg, not_arg, arg, X), arg).",
[["X = _5", "N = 1"],
["X = _5", "N = 3"],
["X = arg", "N = 4"]]);
assert_prolog_failure!(&mut wam, "?- arg(N, f(arg, arg, arg), not_arg).");
assert_prolog_failure!(&mut wam, "?- arg(1, f(arg, not_arg, not_arg), not_arg).");
assert_prolog_success!(&mut wam, "?- arg(2, f(arg, not_arg, not_arg), not_arg).");
assert_prolog_success!(&mut wam, "?- arg(3, f(arg, not_arg, not_arg), not_arg).");
assert_prolog_success!(&mut wam, "?- functor(f(a,b,c), F, Arity).",
[["F = f", "Arity = 3"]]);
assert_prolog_success!(&mut wam, "?- functor(f(a,b,c), F, N).",
[["F = f", "N = 3"]]);
assert_prolog_failure!(&mut wam, "?- functor(f(a,b,c), g, N).");
assert_prolog_success!(&mut wam, "?- functor(f(a,b,c), F, 3).", [["F = f"]]);
assert_prolog_failure!(&mut wam, "?- functor(f(a,b,c), F, 4).");
assert_prolog_failure!(&mut wam, "?- functor(f(a,b,c), g, 3).");
assert_prolog_success!(&mut wam, "?- functor(F, f, 0).", [["F = f"]]);
assert_prolog_success!(&mut wam, "?- catch(functor(F, \"sdf\", 3), E, true).",
[["E = instantiation_error", "F = _1"]]);
assert_prolog_success!(&mut wam, "?- catch(functor(Func, F, 3), E, true).",
[["E = instantiation_error", "Func = _1", "F = _2"]]);
assert_prolog_success!(&mut wam, "?- X is 3, call(integer, X).");
assert_prolog_failure!(&mut wam, "?- X is 3 + 3.5, call(integer, X).");
assert_prolog_success!(&mut wam, "?- X is 3 + 3.5, \\+ call(integer, X).");
assert_prolog_success!(&mut wam, "?- X is 3 + 3.5, \\+ integer(X).");
}