emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/sweeprolog 4ee4291703 018/166: TEST: Add ERT based tests


From: ELPA Syncer
Subject: [nongnu] elpa/sweeprolog 4ee4291703 018/166: TEST: Add ERT based tests
Date: Fri, 30 Sep 2022 04:59:22 -0400 (EDT)

branch: elpa/sweeprolog
commit 4ee42917036e70c7e8b516a0b2a6f0b2f80d380c
Author: Eshel Yaron <me@eshelyaron.com>
Commit: Eshel Yaron <me@eshelyaron.com>

    TEST: Add ERT based tests
---
 .build.yml     |  4 +++-
 Makefile       |  5 ++++-
 sweep-tests.el | 18 ++++++++++++++++++
 sweep.c        | 22 ++++++++++++++++++----
 4 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/.build.yml b/.build.yml
index 7e2aed0690..394cd48260 100644
--- a/.build.yml
+++ b/.build.yml
@@ -26,6 +26,7 @@ packages:
   - libyaml-dev
   - zip
   - git
+  - emacs
 secrets:
   - 750079bb-9735-473b-bebf-db897c9f0c6b
   - 72d5c3dc-f83f-4cc2-96e3-b2b08f6ee8a0
@@ -38,4 +39,5 @@ tasks:
       sudo localectl set-locale LANG=en_US.UTF-8
       localectl set-locale LANG=en_US.UTF-8
       make
-      make info
+      make sweep.info
+      make check
diff --git a/Makefile b/Makefile
index e50729666a..fc31a9cc8f 100644
--- a/Makefile
+++ b/Makefile
@@ -33,7 +33,7 @@ CMAKE_OPTIONS += -DSWIPL_PACKAGES_JAVA=OFF
 CMAKE_OPTIONS += -DSWIPL_PACKAGES_X=OFF
 CMAKE_OPTIONS += -DSWIPL_INSTALL_IN_LIB=ON
 
-.PHONY: clean all swipl
+.PHONY: clean all swipl check
 
 all: $(TARGET)
 
@@ -58,3 +58,6 @@ lib/libswipl.$(SOEXT):
 
 $(BASENAME).info:: README.org
        emacs -Q --batch --eval '(require (quote ox-texinfo))' --eval 
"(with-current-buffer (find-file \"README.org\") (org-export-to-file (quote 
texinfo) \"$@\" nil nil nil nil nil (quote org-texinfo-compile)))"
+
+check: $(TARGET)
+       emacs -batch --eval '(add-to-list (quote load-path) (expand-file-name 
"."))' -l ert -l sweep -l sweep-tests.el -f ert-run-tests-batch-and-exit
diff --git a/sweep-tests.el b/sweep-tests.el
new file mode 100644
index 0000000000..c2dc790cab
--- /dev/null
+++ b/sweep-tests.el
@@ -0,0 +1,18 @@
+(ert-deftest lists:permutation/2 ()
+  "Tests calling the Prolog predicate permutation/2 from Elisp."
+  (should (equal (sweep-open-query "user" "lists" "permutation" (list 1 2 3)) 
t))
+  (should (equal (sweep-next-solution) (list t 1 2 3)))
+  (should (equal (sweep-next-solution) (list t 1 3 2)))
+  (should (equal (sweep-next-solution) (list t 2 1 3)))
+  (should (equal (sweep-next-solution) (list t 2 3 1)))
+  (should (equal (sweep-next-solution) (list t 3 1 2)))
+  (should (equal (sweep-next-solution) (list t 3 2 1)))
+  (should (equal (sweep-next-solution) nil))
+  (should (equal (sweep-cut-query) t)))
+
+(ert-deftest system:=/2 ()
+  "Tests calling the Prolog predicate permutation/2 from Elisp."
+  (should (equal (sweep-open-query "user" "system" "=" (list 1 2 3)) t))
+  (should (equal (sweep-next-solution) (list '! 1 2 3)))
+  (should (equal (sweep-next-solution) nil))
+  (should (equal (sweep-cut-query) t)))
diff --git a/sweep.c b/sweep.c
index 1d6a6af2af..ff148c71fb 100644
--- a/sweep.c
+++ b/sweep.c
@@ -154,9 +154,16 @@ term_to_value_compound(emacs_env *env, term_t t) {
   const char * chars = NULL;
   size_t len = 0;
   emacs_value * vals = NULL;
+  emacs_value res = NULL;
   size_t n = 0;
-  (void)PL_get_compound_name_arity(t, &name, &arity);
+
+  if (!PL_get_compound_name_arity(t, &name, &arity)) {
+    ethrow(env, "Not a compound");
+    goto cleanup;
+  }
+
   chars = PL_atom_nchars(name, &len);
+
   vals = (emacs_value*)malloc(sizeof(emacs_value)*arity + 1);
   if (vals == NULL) {
     ethrow(env, "malloc failed");
@@ -167,12 +174,19 @@ term_to_value_compound(emacs_env *env, term_t t) {
   vals[0] = env->make_string(env, chars, len);
 
   for(n=1; n<=arity; n++) {
-
-    (void)PL_get_arg(n, t, arg);
+    if (!PL_get_arg(n, t, arg)) {
+      ethrow(env, "get_arg falied");
+      goto cleanup;
+    }
     vals[n] = term_to_value(env, arg);
   }
 
-  return econs(env, env->intern(env, "compound"), env->funcall(env, 
env->intern(env, "list"), arity + 1, vals));
+  res = econs(env, env->intern(env, "compound"), env->funcall(env, 
env->intern(env, "list"), arity + 1, vals));
+
+ cleanup:
+  if (vals != NULL) free(vals);
+
+  return res;
 }
 
 emacs_value



reply via email to

[Prev in Thread] Current Thread [Next in Thread]