From b6b0b8dd82031eb55189ad3bea5eaacd8444b9d5 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 1 Nov 2014 13:29:10 -0400 Subject: More work on data filter; add tests --- test/tests.el | 33 ++++++++++++++++++++++++--------- test/util.el | 12 ------------ 2 files changed, 24 insertions(+), 21 deletions(-) delete mode 100644 test/util.el (limited to 'test') diff --git a/test/tests.el b/test/tests.el index e8452af..cbeb80d 100644 --- a/test/tests.el +++ b/test/tests.el @@ -5,6 +5,19 @@ (if (string-prefix-p "stack-" (symbol-name symbol)) (unintern symbol))))) +(defmacro stack-test-sample-data (method &optional directory) + (with-current-buffer + (find-file-noselect + (concat "data-samples/" + (when directory (concat directory "/")) + method ".el")) + (eval (read (buffer-string))))) + +(setq stack-test-data-questions + (stack-test-sample-data "questions") + stack-test-data-sites + (stack-test-sample-data "sites")) + ;;; Tests (setq stack-core-remaining-api-requests-message-threshold 50000) @@ -44,12 +57,14 @@ "Test the meta-convenience function -- complex structure" (should (equal - '([()]) - (stack-core-filter-data '((0 . 3) - (1 . t) - (a . five) - (2 . [1 2]) - ("5" . bop) - (3) - (p . 4)) - '(1 2 3))))) + '((1 . [a b c]) (2 . [(a . 1)]) (3 . peach)) + (stack-core-filter-data '((1 . [a b c]) + (2 . [(a . 1) + (b . 2)]) + (3 . peach) + (4 . banana)) + '(1 (2 a) 3))))) + +(ert-deftest test-data-filter-3 () + "Test the meta-convenience function -- vector structure" + (equal)) diff --git a/test/util.el b/test/util.el deleted file mode 100644 index 7d5937f..0000000 --- a/test/util.el +++ /dev/null @@ -1,12 +0,0 @@ -(defmacro stack-test-get-sample-data (method &optional directory) - (with-current-buffer - (find-file-noselect - (concat "data-samples/" - (when directory (concat directory "/")) - method ".el")) - (eval (read (buffer-string))))) - -(setq stack-test-data-questions - (stack-test-get-sample-data "questions") - stack-test-data-sites - (stack-test-get-sample-data "sites")) -- cgit v1.2.3 From 7ba0f27980a48da21db71c05be179be126c875bc Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 1 Nov 2014 13:36:23 -0400 Subject: Fix test --- test/tests.el | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/tests.el b/test/tests.el index cbeb80d..e3675de 100644 --- a/test/tests.el +++ b/test/tests.el @@ -67,4 +67,10 @@ (ert-deftest test-data-filter-3 () "Test the meta-convenience function -- vector structure" - (equal)) + (equal + '(((1 . 2) (2 . 3) (3 . 4)) ((1 . a) (2 . b) (3 . c)) nil ((1 . alpha) (2 . beta))) + (stack-core-filter-data [((1 . 2) (2 . 3) (3 . 4)) + ((1 . a) (2 . b) (3 . c) (5 . seven)) + ((should-not-go)) + ((1 . alpha) (2 . beta))] + '(1 2 3)))) -- cgit v1.2.3 From 4b91b49fc84bc1721bc1d97a8519d725e4e3a11e Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 1 Nov 2014 13:49:33 -0400 Subject: Make the sample data loader a function --- test/tests.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/tests.el b/test/tests.el index e3675de..0787705 100644 --- a/test/tests.el +++ b/test/tests.el @@ -5,7 +5,7 @@ (if (string-prefix-p "stack-" (symbol-name symbol)) (unintern symbol))))) -(defmacro stack-test-sample-data (method &optional directory) +(defun stack-test-sample-data (method &optional directory) (with-current-buffer (find-file-noselect (concat "data-samples/" -- cgit v1.2.3 From 8831556b94b9d2f5f80b14103947add2b562f5f9 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 1 Nov 2014 14:06:57 -0400 Subject: Fix syntax issue --- stack-core.el | 7 +++++-- test/tests.el | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/stack-core.el b/stack-core.el index be385c3..ef0dc9f 100644 --- a/stack-core.el +++ b/stack-core.el @@ -220,8 +220,11 @@ entire response as a complex alist." nil (mapcar (lambda (cons-cell) (when (member (car cons-cell) desired-tree) - (if (sequencep (cdr cons-cell)) - (stack-core-filter-data )) + (if (and (sequencep (cdr cons-cell)) + (sequencep (elt (cdr cons-cell) 0))) + (stack-core-filter-data + (cdr cons-cell) + (cdr (assoc (car cons-cell) desired-tree)))) cons-cell)) data)))) diff --git a/test/tests.el b/test/tests.el index 0787705..d8a653b 100644 --- a/test/tests.el +++ b/test/tests.el @@ -68,7 +68,9 @@ (ert-deftest test-data-filter-3 () "Test the meta-convenience function -- vector structure" (equal - '(((1 . 2) (2 . 3) (3 . 4)) ((1 . a) (2 . b) (3 . c)) nil ((1 . alpha) (2 . beta))) + '(((1 . 2) (2 . 3) (3 . 4)) + ((1 . a) (2 . b) (3 . c)) + nil ((1 . alpha) (2 . beta))) (stack-core-filter-data [((1 . 2) (2 . 3) (3 . 4)) ((1 . a) (2 . b) (3 . c) (5 . seven)) ((should-not-go)) -- cgit v1.2.3 From 9078fe4e6bb9d74e8fcd6020c1364526b41f35e5 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 1 Nov 2014 14:07:08 -0400 Subject: Return -no-value if it cannot find sample Allows build to continue if it cannot find the file. This is for reading in sample data. I'm not really sure why the local build cannot find the file, but I will figure it out later today. --- test/tests.el | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/tests.el b/test/tests.el index d8a653b..59c3413 100644 --- a/test/tests.el +++ b/test/tests.el @@ -11,7 +11,9 @@ (concat "data-samples/" (when directory (concat directory "/")) method ".el")) - (eval (read (buffer-string))))) + (eval (read (if (string-equal "" (buffer-string)) + "'no-value" + (buffer-string)))))) (setq stack-test-data-questions (stack-test-sample-data "questions") -- cgit v1.2.3 From 0943b14b1991b42ce838c5f2189af92bb62f6d96 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 1 Nov 2014 18:58:34 -0400 Subject: Return the correct type from filter Vectors should not be turning into lists. See test cases. --- stack-core.el | 42 ++++++++++++++++++++++++--------- test/tests.el | 75 +++++++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 86 insertions(+), 31 deletions(-) (limited to 'test') diff --git a/stack-core.el b/stack-core.el index ef0dc9f..37ae2af 100644 --- a/stack-core.el +++ b/stack-core.el @@ -212,21 +212,41 @@ entire response as a complex alist." (defun stack-core-filter-data (data desired-tree) "Filters DATA and returns the DESIRED-TREE" (if (vectorp data) - (mapcar (lambda (entry) - (stack-core-filter-data - entry desired-tree)) - data) + (apply #'vector + (mapcar (lambda (entry) + (stack-core-filter-data + entry desired-tree)) + data)) + (mapcar #'identity '(1 2 3)) (delq nil (mapcar (lambda (cons-cell) - (when (member (car cons-cell) desired-tree) - (if (and (sequencep (cdr cons-cell)) - (sequencep (elt (cdr cons-cell) 0))) - (stack-core-filter-data - (cdr cons-cell) - (cdr (assoc (car cons-cell) desired-tree)))) - cons-cell)) + (let ((f (stack-core-filter-data--item-in-tree + (car cons-cell) desired-tree))) + (when f + (if (and (sequencep (cdr cons-cell)) + (sequencep (elt (cdr cons-cell) 0))) + (cons (car cons-cell) + (stack-core-filter-data + (cdr cons-cell) (cdr f))) + cons-cell)))) data)))) +(defun stack-core-filter-data--item-in-tree (item tree) + "Check if ITEM is in the direct leaves of TREE + +For example, when called with (f 'item '(some item here)), the +return would be `t'. When called as (f 'item '(some (item here) +in a (deep structure))), `(item here)' would be returned. +" + (when tree + (if (equal item (car tree)) tree + (if (and (listp (car tree)) + (equal item (caar tree))) + (car tree) + (stack-core-filter-data--item-in-tree item (cdr tree)))))) + +(stack-core-filter-data--item-in-tree 'a '(b (a 1 b c) c)) + (provide 'stack-core) ;;; stack-core.el ends here diff --git a/test/tests.el b/test/tests.el index 59c3413..01d501c 100644 --- a/test/tests.el +++ b/test/tests.el @@ -41,8 +41,9 @@ (should-error (stack-core-make-request "questions" '(())))) -(ert-deftest test-data-filter-1 () - "Test the meta-convenience function -- flat structure" +(ert-deftest test-tree-filter () + "`stack-core-filter-data'" + ;; flat (should (equal '((1 . t) (2 . [1 2]) (3)) @@ -53,28 +54,62 @@ ("5" . bop) (3) (p . 4)) - '(1 2 3))))) - -(ert-deftest test-data-filter-2 () - "Test the meta-convenience function -- complex structure" + '(1 2 3)))) + ;; complex (should (equal - '((1 . [a b c]) (2 . [(a . 1)]) (3 . peach)) + '((1 . [a b c]) (2 . [((a . 1) (c . 3)) ((a . 4) (c . 6))]) (3 . peach)) (stack-core-filter-data '((1 . [a b c]) - (2 . [(a . 1) - (b . 2)]) + (2 . [((a . 1) + (b . 2) + (c . 3)) + ((a . 4) + (b . 5) + (c . 6))]) (3 . peach) (4 . banana)) - '(1 (2 a) 3))))) + '(1 (2 a c) 3)))) -(ert-deftest test-data-filter-3 () - "Test the meta-convenience function -- vector structure" - (equal - '(((1 . 2) (2 . 3) (3 . 4)) + ;; vector + (should + (equal + [((1 . 2) (2 . 3) (3 . 4)) ((1 . a) (2 . b) (3 . c)) - nil ((1 . alpha) (2 . beta))) - (stack-core-filter-data [((1 . 2) (2 . 3) (3 . 4)) - ((1 . a) (2 . b) (3 . c) (5 . seven)) - ((should-not-go)) - ((1 . alpha) (2 . beta))] - '(1 2 3)))) + nil ((1 . alpha) (2 . beta))] + (stack-core-filter-data [((1 . 2) (2 . 3) (3 . 4)) + ((1 . a) (2 . b) (3 . c) (5 . seven)) + ((should-not-go)) + ((1 . alpha) (2 . beta))] + '(1 2 3))))) + +(ert-deftest test-tree-member () + "`stack-core-filter-data--item-in-tree'" + (should + (equal + '(a b c) + (stack-core-filter-data--item-in-tree 'a '(a b c)))) + + (should + (equal + '(a b c) + (stack-core-filter-data--item-in-tree 'a '(b a b c)))) + + (should + (equal + '(a b c) + (stack-core-filter-data--item-in-tree 'a '((a b c) 1 2)))) + + (should + (equal + '(a b c) + (stack-core-filter-data--item-in-tree 'a '(1 (a b c) 2)))) + + (should + (equal + '(a b c) + (stack-core-filter-data--item-in-tree 'a '(1 2 (a b c))))) + + (should + (equal + '(a a b c) + (stack-core-filter-data--item-in-tree 'a '(1 (a a b c) 2))))) -- cgit v1.2.3 From 58ac7ce7c587a5dcb876bffde0aca72b6469cd86 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 1 Nov 2014 19:25:18 -0400 Subject: More readable test cases; whitespace --- test/tests.el | 46 ++++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 26 deletions(-) (limited to 'test') diff --git a/test/tests.el b/test/tests.el index 01d501c..ac40cea 100644 --- a/test/tests.el +++ b/test/tests.el @@ -3,17 +3,17 @@ (mapatoms (lambda (symbol) (if (string-prefix-p "stack-" (symbol-name symbol)) - (unintern symbol))))) + (unintern symbol))))) (defun stack-test-sample-data (method &optional directory) (with-current-buffer (find-file-noselect (concat "data-samples/" - (when directory (concat directory "/")) - method ".el")) + (when directory (concat directory "/")) + method ".el")) (eval (read (if (string-equal "" (buffer-string)) - "'no-value" - (buffer-string)))))) + "'no-value" + (buffer-string)))))) (setq stack-test-data-questions (stack-test-sample-data "questions") @@ -47,28 +47,22 @@ (should (equal '((1 . t) (2 . [1 2]) (3)) - (stack-core-filter-data '((0 . 3) - (1 . t) - (a . five) - (2 . [1 2]) - ("5" . bop) - (3) - (p . 4)) + (stack-core-filter-data '((0 . 3) (1 . t) (a . five) (2 . [1 2]) + ("5" . bop) (3) (p . 4)) '(1 2 3)))) ;; complex (should (equal - '((1 . [a b c]) (2 . [((a . 1) (c . 3)) ((a . 4) (c . 6))]) (3 . peach)) + '((1 . [a b c]) + (2 . [((a . 1) (c . 3)) + ((a . 4) (c . 6))]) + (3 . peach)) (stack-core-filter-data '((1 . [a b c]) - (2 . [((a . 1) - (b . 2) - (c . 3)) - ((a . 4) - (b . 5) - (c . 6))]) - (3 . peach) - (4 . banana)) - '(1 (2 a c) 3)))) + (2 . [((a . 1) (b . 2) (c . 3)) + ((a . 4) (b . 5) (c . 6))]) + (3 . peach) + (4 . banana)) + '(1 (2 a c) 3)))) ;; vector (should @@ -77,10 +71,10 @@ ((1 . a) (2 . b) (3 . c)) nil ((1 . alpha) (2 . beta))] (stack-core-filter-data [((1 . 2) (2 . 3) (3 . 4)) - ((1 . a) (2 . b) (3 . c) (5 . seven)) - ((should-not-go)) - ((1 . alpha) (2 . beta))] - '(1 2 3))))) + ((1 . a) (2 . b) (3 . c) (5 . seven)) + ((should-not-go)) + ((1 . alpha) (2 . beta))] + '(1 2 3))))) (ert-deftest test-tree-member () "`stack-core-filter-data--item-in-tree'" -- cgit v1.2.3 From 39ec0ba8a34b3764fcf46f0e5deb1133b0e70e2c Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sun, 2 Nov 2014 08:55:53 -0500 Subject: Use a simpler construct to test filter inclusion Also removed tests for `stack-core-filter-data--item-in-tree' since it no longer exists. --- stack-core.el | 23 +++++++---------------- test/tests.el | 32 -------------------------------- 2 files changed, 7 insertions(+), 48 deletions(-) (limited to 'test') diff --git a/stack-core.el b/stack-core.el index 45d7cb3..9a535cd 100644 --- a/stack-core.el +++ b/stack-core.el @@ -225,8 +225,13 @@ entire response as a complex alist." (delq nil (mapcar (lambda (cons-cell) - (let ((f (stack-core-filter-data--item-in-tree - (car cons-cell) desired-tree))) + ;; TODO the resolution of `f' is O(2n) in the worst + ;; case. It may be faster to implement the same + ;; functionality as a `while' loop to stop looking the + ;; list once it has found a match. Do speed tests. + ;; See edfab4443ec3d376c31a38bef12d305838d3fa2e. + (let ((f (or (memq (car cons-cell) desired-tree) + (assoc (car cons-cell) desired-tree)))) (when f (if (and (sequencep (cdr cons-cell)) (sequencep (elt (cdr cons-cell) 0))) @@ -236,19 +241,5 @@ entire response as a complex alist." cons-cell)))) data)))) -(defun stack-core-filter-data--item-in-tree (item tree) - "Check if ITEM is in the direct leaves of TREE - -For example, when called with (f 'item '(some item here)), the -return would be `t'. When called as (f 'item '(some (item here) -in a (deep structure))), `(item here)' would be returned. -" - (when tree - (if (equal item (car tree)) tree - (if (and (listp (car tree)) - (equal item (caar tree))) - (car tree) - (stack-core-filter-data--item-in-tree item (cdr tree)))))) - (provide 'stack-core) ;;; stack-core.el ends here diff --git a/test/tests.el b/test/tests.el index ac40cea..44b0de0 100644 --- a/test/tests.el +++ b/test/tests.el @@ -75,35 +75,3 @@ ((should-not-go)) ((1 . alpha) (2 . beta))] '(1 2 3))))) - -(ert-deftest test-tree-member () - "`stack-core-filter-data--item-in-tree'" - (should - (equal - '(a b c) - (stack-core-filter-data--item-in-tree 'a '(a b c)))) - - (should - (equal - '(a b c) - (stack-core-filter-data--item-in-tree 'a '(b a b c)))) - - (should - (equal - '(a b c) - (stack-core-filter-data--item-in-tree 'a '((a b c) 1 2)))) - - (should - (equal - '(a b c) - (stack-core-filter-data--item-in-tree 'a '(1 (a b c) 2)))) - - (should - (equal - '(a b c) - (stack-core-filter-data--item-in-tree 'a '(1 2 (a b c))))) - - (should - (equal - '(a a b c) - (stack-core-filter-data--item-in-tree 'a '(1 (a a b c) 2))))) -- cgit v1.2.3