]> Repositorios git - scryer-prolog.git/commitdiff
use eager_stackful_preorder_iter in variable_set, add term_variables/1 tests
authorMark <[email protected]>
Wed, 4 Oct 2023 21:12:25 +0000 (15:12 -0600)
committerMark <[email protected]>
Wed, 4 Oct 2023 21:12:25 +0000 (15:12 -0600)
src/heap_iter.rs
src/machine/machine_state_impl.rs
src/machine/system_calls.rs
src/tests/term_variables.pl [new file with mode: 0644]
tests/scryer/src_tests.rs

index d3b62527ac2ae6714d005fdba07f7657f3dc686f..bd5836323ec649c57e0f68f93e77e0ab88fda572 100644 (file)
@@ -12,6 +12,14 @@ use modular_bitfield::prelude::*;
 use std::ops::Deref;
 use std::vec::Vec;
 
+#[inline(always)]
+pub fn eager_stackful_preorder_iter(
+    heap: &mut Heap,
+    value: HeapCellValue,
+) -> EagerStackfulPreOrderHeapIter {
+    EagerStackfulPreOrderHeapIter::new(heap, value)
+}
+
 /*
  * Unlike StackfulPreOrderHeapIter, this iterator not only marks
  * cyclic terms for the sake of skipping them at the second visit but
index 4510fa5d23274fd87781276a9b14f34ba40a6111..4ed05354814a5a2b60e47f2d803777baac512413 100644 (file)
@@ -1621,7 +1621,7 @@ impl MachineState {
 
     // returns true on failure.
     pub fn ground_test(&mut self) -> bool {
-        let iter = EagerStackfulPreOrderHeapIter::new(&mut self.heap, self.registers[1]);
+        let iter = eager_stackful_preorder_iter(&mut self.heap, self.registers[1]);
 
         for term in iter {
             if term.is_var() {
index 40881c54df02105bed72384d3fa35a0705dc9996..ba21168385881b718a19e227c3949bddb212b0d6 100644 (file)
@@ -583,20 +583,11 @@ impl MachineState {
         seen_set: &mut IndexSet<HeapCellValue, S>,
         value: HeapCellValue,
     ) {
-        let mut iter = stackful_preorder_iter::<NonListElider>(&mut self.heap, &mut self.stack, value);
+        let iter = eager_stackful_preorder_iter(&mut self.heap, value);
 
-        while let Some(value) = iter.next() {
-            let value = unmark_cell_bits!(value);
-
-            if value.is_var() {
-                let value = unmark_cell_bits!(heap_bound_store(
-                    iter.heap,
-                    heap_bound_deref(iter.heap, value)
-                ));
-
-                if value.is_var() {
-                    seen_set.insert(value);
-                }
+        for term in iter {
+            if term.is_var() {
+                seen_set.insert(term);
             }
         }
     }
diff --git a/src/tests/term_variables.pl b/src/tests/term_variables.pl
new file mode 100644 (file)
index 0000000..57c55ed
--- /dev/null
@@ -0,0 +1,84 @@
+/**/
+
+:- use_module(library(format)).
+:- use_module(library(dcgs)).
+:- use_module(library(lists)).
+:- use_module(library(debug)).
+
+test("term_variables#1400", (
+    term_variables(A+B*C/B-D, Vars),
+    term_variables(t(A,B,C,D), Vars),
+    Vars = [A,B,C,D]
+)).
+
+test("term_variables#1405", (
+    \+ (B=[C|D],C=[_|D],C=[B|B], term_variables(B,_), false)
+)).
+
+test("term_variables#1409", (
+    G_0 = (A=[B|B],A=[C|C]), G_0, term_variables(G_0, Vars), Vars = [B]
+)).
+
+test("term_variables#1410", (
+    \+ \+ (G_0 = ( A=s(A) ), G_0, term_variables(G_0, Vars), Vars = []),
+    E_0 = (_=[B|B]), G_0 = (E_0,\_=B), G_0, term_variables(G_0, Vars)
+)).
+
+test("term_variables#1412", (
+    G_0 = =([A|B],[A|B]), G_0, term_variables(G_0, Vars),
+    Vars = [A,B]
+)).
+
+test("term_variables#1414", (
+    \+ (\B=A,C=[A|D],B=[a,b|E],C=[D|E], term_variables(\E,_), false)
+)).
+
+test("term_variables#2063", (
+    A=[B|C], B=[A], term_variables([B], Vars),
+    Vars = [C]
+)).
+
+main :-
+    findall(test(Name, Goal), test(Name, Goal), Tests),
+    run_tests(Tests, Failed),
+    show_failed(Failed),
+    halt.
+
+main_quiet :-
+    findall(test(Name, Goal), test(Name, Goal), Tests),
+    run_tests_quiet(Tests, Failed),
+    (   Failed = [] ->
+        format("All tests passed", [])
+    ;   format("Some tests failed", [])
+    ),
+    halt.
+
+run_tests([], []).
+run_tests([test(Name, Goal)|Tests], Failed) :-
+    format("Running test \"~s\"~n", [Name]),
+    (   call(Goal) ->
+        Failed = Failed1
+    ;   format("Failed test \"~s\"~n", [Name]),
+        Failed = [Name|Failed1]
+    ),
+    run_tests(Tests, Failed1).
+
+run_tests_quiet([], []).
+run_tests_quiet([test(Name, Goal)|Tests], Failed) :-
+    (   call(Goal) ->
+        Failed = Failed1
+    ;   Failed = [Name|Failed1]
+    ),
+    run_tests_quiet(Tests, Failed1).
+
+portray_failed_([]) --> [].
+portray_failed_([F|Fs]) -->
+    "\"", F, "\"",  "\n", portray_failed_(Fs).
+
+portray_failed([]) --> [].
+portray_failed([F|Fs]) -->
+    "\n", "Failed tests:", "\n", portray_failed_([F|Fs]).
+
+show_failed(Failed) :-
+    phrase(portray_failed(Failed), F),
+    format("~s", [F]).
index 8a04abbe2aea38b51d556299c404ad24e86dcf3e..edc43d32503b6538070103ab977f0b296ac0d899 100644 (file)
@@ -93,3 +93,12 @@ fn ground_tests() {
         "All tests passed",
     );
 }
+
+#[test]
+fn term_variables_tests() {
+    run_top_level_test_with_args(
+        &["src/tests/term_variables.pl", "-f", "-g", "main_quiet"],
+        "",
+        "All tests passed",
+    );
+}