Some of you who are still working on Project 1 may want to find a way to test your code. One of the joys of Lisp is that it is very easy to set up a testing system. In Java this requires a module such as JUnit. In Lisp you can roll your own in a few lines.

The following code is a testing system for Project 1. Load the code into your version of Lisp, and then call (project1). This module will then test your code against all the examples given in the homework, as follows:

CL-USER 40 > (project1)

CORRECT: The result of (MY-MEMBER (QUOTE A) (QUOTE (B C J A))) is (A)

CORRECT: The result of (MY-MEMBER (QUOTE (C D)) (QUOTE (A B C (A B) (C D) J K))) is ((C D) J K)

CORRECT: The result of (MY-MEMBER (QUOTE A) (QUOTE (B C (A D) E))) is NIL

CORRECT: The result of (MY-INSERT (QUOTE A) (QUOTE (B C D E)) 0) is (A B C D E)

CORRECT: The result of (MY-INSERT (QUOTE A) (QUOTE (B C D E)) 3) is (B C D A E)

CORRECT: The result of (MY-INSERT (QUOTE A) (QUOTE (B C D E)) 14) is (B C D E A)

CORRECT: The result of (MY-REMOVE-DUPS (QUOTE (A B A C A D))) is (D C B A)

CORRECT: The result of (MY-REMOVE-DUPS (QUOTE (A B (A C) A D))) is (D (A C) B A)

CORRECT: The result of (MY-REMOVE-DUPS (QUOTE (A B (B C) A (B C) D))) is (D (B C) B A)

CORRECT: The result of (MY-INTERSECTION (QUOTE (A B C)) (QUOTE (C A E))) is (C A)

CORRECT: The result of (MY-INTERSECTION (QUOTE (A B (A C) C)) (QUOTE (A C B))) is (C B A)

CORRECT: The result of (MY-INTERSECTION (QUOTE (A B (A C) C)) (QUOTE ((A C) B))) is ((A C) B)

CORRECT: The result of (DYADIC-APPLY (FUNCTION +) (QUOTE (1 2 3)) (QUOTE (6 5 4))) is (7 7 7)

CORRECT: The result of (DYADIC-APPLY (FUNCTION LIST) (QUOTE (A B C)) (QUOTE (1 2 3 4))) is ((A 1) (B 2) (C 3))

CORRECT: The result of (TREE-SUM (QUOTE (1 2 3 4 5))) is 15

CORRECT: The result of (TREE-SUM (QUOTE (A 1 B 2))) is 3

CORRECT: The result of (TREE-SUM (QUOTE (A (1 (2 (3 B) C) D E) F 4))) is 10

CORRECT: The result of (MY-FLATTEN (QUOTE (A B (C D)))) is (A B C D)

CORRECT: The result of (MY-FLATTEN (QUOTE (A (B (C (D (E F))) G (H I (J K)) L (M)) (((N)))))) is (A B C D E F G H I J K L M N)

CORRECT: The result of (FULL-REVERSE (QUOTE (A B C D))) is (D C B A)

CORRECT: The result of (FULL-REVERSE (QUOTE (A (B C) D))) is (D (C B) A)

CORRECT: The result of (FULL-REVERSE (QUOTE ((A B) (C (D E) F G (H I) J K) L M))) is (M L (K J (I H) G F (E D) C) (B A))

CORRECT: The result of (MY-SUBST (QUOTE A) (QUOTE B) (QUOTE (T H B D S T B R N E R))) is (T H A D S T A R N E R)

CORRECT: The result of (MY-SUBST (QUOTE A) (QUOTE B) (QUOTE (A B (A B (A B)) (A B)))) is (A A (A A (A A)) (A A))

NIL

Here’s the code (after the jump):

;;; ;;; TESTING FUNCTION FOR PROJECT 1

;;;

;;; Ron Ferguson, August 2007

;;;

;;; To run the tests for Project 1, load this file

;;; and use the function (PROJECT1).

;;;

;;;

(defun test-form (form test expected-result)

"Test a particular form and print out the result."

(let ((result (eval form)))

(if (funcall test result expected-result)

(format t "~%CORRECT: The result of ~A is ~A" form result)

(format t "~%WRONG: The result of ~A is ~A, expected ~A" form result expected-result))))

(defun test-forms (forms)

"Pop each set of three items and perform the test."

(do* ((form (first forms) (first forms))

(test (second forms) (second forms))

(expected-result (third forms) (third forms))

(forms (cdddr forms) (cdddr forms)))

((null forms) nil)

(test-form form test expected-result)))

(defun same-set (list1 list2)

"Do list1 and list2 contain the same set of elements?"

(and (null (set-difference list1 list2 :test 'equal))

(null (set-difference list2 list1 :test 'equal))))

(defun project1 ()

"Test all the needed forms."

(test-forms

'(

(my-member 'a '(b c j a)) equal (A)

(my-member '(c d) '(a b c (a b) (c d) j k)) equal ((c d) j k)

(my-member 'a '(b c (a d) e)) equal nil

(my-insert 'a '(b c d e) 0) equal (A B C D E)

(my-insert 'a '(b c d e) 3) equal (B C D A E)

(my-insert 'a '(b c d e) 14) equal (B C D E A)

(my-remove-dups '(a b a c a d)) same-set (B C A D)

(my-remove-dups '(a b (a c) a d)) same-set (B (A C) A D)

(my-remove-dups '(a b (b c) a (b c) d)) same-set (B A (B C) D)

(my-intersection '(a b c) '(c a e)) same-set (A C)

(my-intersection '(a b (a c) c) '(a c b)) same-set (A B C)

(my-intersection '(a b (a c) c) '((a c) b)) same-set (B (A C))

(dyadic-apply #'+ '(1 2 3) '(6 5 4)) equal (7 7 7)

(dyadic-apply #'list '(a b c) '(1 2 3 4)) equal ((A 1) (B 2) (C 3))

(tree-sum '(1 2 3 4 5)) = 15

(tree-sum '(a 1 b 2)) = 3

(tree-sum '(a (1 (2 (3 b) c) d e) f 4)) = 10

(my-flatten '(a b (c d))) equal (A B C D)

(my-flatten '(a (b (c (d (e f))) g (h i (j k)) l (m)) (((n))))) equal

(A B C D E F G H I J K L M N)

(full-reverse '(a b c d)) equal (D C B A)

(full-reverse '(a (b c) d)) equal (D (C B) A)

(full-reverse '((a b) (c (d e) f g (h i) j k) l m)) equal

(M L (K J (I H) G F (E D) C) (B A))

(my-subst 'a 'b '(t h b d s t b r n e r)) equal (T H A D S T A R N E R)

(my-subst 'a 'b '(a b (a b (a b)) (a b))) equal (A A (A A (A A)) (A A))

(my-subst 'a '(p q) '(p q (p q) (((p q) a b (p q a))))) equal

(P Q A ((A A B (P Q A))))

)))

Advertisements

## Leave a Reply