| 1 | ;;; -*-  Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- | 
|---|
| 2 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 
|---|
| 3 | ;;; | 
|---|
| 4 | ;;;  Copyright (C) 1999, 2002, 2009, 2015 Marek Rychlik <rychlik@u.arizona.edu> | 
|---|
| 5 | ;;; | 
|---|
| 6 | ;;;  This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | ;;;  it under the terms of the GNU General Public License as published by | 
|---|
| 8 | ;;;  the Free Software Foundation; either version 2 of the License, or | 
|---|
| 9 | ;;;  (at your option) any later version. | 
|---|
| 10 | ;;; | 
|---|
| 11 | ;;;  This program is distributed in the hope that it will be useful, | 
|---|
| 12 | ;;;  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | ;;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 14 | ;;;  GNU General Public License for more details. | 
|---|
| 15 | ;;; | 
|---|
| 16 | ;;;  You should have received a copy of the GNU General Public License | 
|---|
| 17 | ;;;  along with this program; if not, write to the Free Software | 
|---|
| 18 | ;;;  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 
|---|
| 19 | ;;; | 
|---|
| 20 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 
|---|
| 21 |  | 
|---|
| 22 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 
|---|
| 23 | ;; | 
|---|
| 24 | ;; Run tests using 5am unit testing framework | 
|---|
| 25 | ;; | 
|---|
| 26 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | 
|---|
| 27 |  | 
|---|
| 28 | ;; We assume that QuickLisp package manager is installed. | 
|---|
| 29 | ;; See : | 
|---|
| 30 | ;;      https://www.quicklisp.org/beta/ | 
|---|
| 31 | ;; | 
|---|
| 32 |  | 
|---|
| 33 | ;; The following is unnecessary after running: | 
|---|
| 34 | ;; * (ql:add-to-init-file) | 
|---|
| 35 | ;; at lisp prompt: | 
|---|
| 36 | ;;(load "~/quicklisp/setup") | 
|---|
| 37 |  | 
|---|
| 38 | (ql:quickload :fiveam) | 
|---|
| 39 |  | 
|---|
| 40 | (load "ngrobner.asd") | 
|---|
| 41 | (asdf:load-system :ngrobner) | 
|---|
| 42 |  | 
|---|
| 43 | (defpackage #:ngrobner-tests | 
|---|
| 44 | (:use :cl :it.bese.fiveam | 
|---|
| 45 | :ngrobner :priority-queue :monomial | 
|---|
| 46 | :utils :order :ring :term | 
|---|
| 47 | :termlist :polynomial | 
|---|
| 48 | :priority-queue | 
|---|
| 49 | ) | 
|---|
| 50 | ) | 
|---|
| 51 |  | 
|---|
| 52 | (in-package #:ngrobner-tests) | 
|---|
| 53 |  | 
|---|
| 54 | (def-suite ngrobner-suite | 
|---|
| 55 | :description "New Groebner Package Suite") | 
|---|
| 56 |  | 
|---|
| 57 | (in-suite ngrobner-suite) | 
|---|
| 58 |  | 
|---|
| 59 | #+nil | 
|---|
| 60 | (test dummy-test | 
|---|
| 61 | "Makelist" | 
|---|
| 62 | (is (= (+ 2 2)) "2 plus 2 wasn't equal to 4 (using #'= to test equality)") | 
|---|
| 63 | (is (= 0 (+ -1 1))) | 
|---|
| 64 | (signals | 
|---|
| 65 | (error "Trying to add 4 to FOO didn't signal an error") | 
|---|
| 66 | (+ 'foo 4)) | 
|---|
| 67 | (is (= 0 (+ 1 1)) "this should have failed")) | 
|---|
| 68 |  | 
|---|
| 69 | (test makelist-1 | 
|---|
| 70 | "makelist-1 test" | 
|---|
| 71 | (is (equal (makelist-1 (* 2 i) i 0 10) '(0 2 4 6 8 10 12 14 16 18 20))) | 
|---|
| 72 | (is (equal (makelist-1 (* 2 i) i 0 10 3) '(0 6 12 18)))) | 
|---|
| 73 |  | 
|---|
| 74 | (test makelist | 
|---|
| 75 | "makelist" | 
|---|
| 76 | (is (equal (makelist (+ (* i i) (* j j)) (i 1 4) (j 1 i)) '(2 5 8 10 13 18 17 20 25 32))) | 
|---|
| 77 | (is (equal (makelist (list i j '---> (+ (* i i) (* j j))) (i 1 4) (j 1 i)) | 
|---|
| 78 | '((1 1 ---> 2) (2 1 ---> 5) (2 2 ---> 8) (3 1 ---> 10) (3 2 ---> 13) | 
|---|
| 79 | (3 3 ---> 18) (4 1 ---> 17) (4 2 ---> 20) (4 3 ---> 25) (4 4 ---> 32))))) | 
|---|
| 80 |  | 
|---|
| 81 | (test monom | 
|---|
| 82 | "monom" | 
|---|
| 83 | (is (every #'= (make-monom :dimension 3) '(0 0 0)) "Trivial monomial is a vector of 0's") | 
|---|
| 84 | (is (every #'= (make-monom :initial-exponents '(1 2 3)) '(1 2 3)) "Monomial with powers 1,2,3") | 
|---|
| 85 | (let ((p (make-monom :initial-exponents '(1 2 3)))) | 
|---|
| 86 | (is (every #'= (monom-map (lambda (x) x) p) '(1 2 3))))) | 
|---|
| 87 |  | 
|---|
| 88 |  | 
|---|
| 89 | (test order | 
|---|
| 90 | "order" | 
|---|
| 91 | (let ((p (make-monom :initial-exponents '(1 3 2))) | 
|---|
| 92 | (q (make-monom :initial-exponents '(1 2 3)))) | 
|---|
| 93 | (is-true (lex>  p q)) | 
|---|
| 94 | (is-true (grlex>  p q)) | 
|---|
| 95 | (is-true (revlex>  p q)) | 
|---|
| 96 | (is-true (grevlex>  p q)) | 
|---|
| 97 | (is-false (invlex>  p q)))) | 
|---|
| 98 |  | 
|---|
| 99 | (test elim-order | 
|---|
| 100 | "elimination order" | 
|---|
| 101 | (let* ((p (make-monom :initial-exponents '(1 2 3))) | 
|---|
| 102 | (q (make-monom :initial-exponents '(4 5 6))) | 
|---|
| 103 | (elim-order-factory (make-elimination-order-factory)) | 
|---|
| 104 | (elim-order-1 (funcall elim-order-factory 1)) | 
|---|
| 105 | (elim-order-2 (funcall elim-order-factory 2))) | 
|---|
| 106 | (is-false (funcall elim-order-1 p q)) | 
|---|
| 107 | (is-false (funcall elim-order-2 p q)))) | 
|---|
| 108 |  | 
|---|
| 109 | (test term | 
|---|
| 110 | "term" | 
|---|
| 111 | (let* ((m1 (make-monom :initial-exponents '(1 2 3))) | 
|---|
| 112 | (m2 (make-monom :initial-exponents '(3 5 2))) | 
|---|
| 113 | (m3 (monom-mul m1 m2)) | 
|---|
| 114 | (t1 (make-term m1 7)) | 
|---|
| 115 | (t2 (make-term m2 9)) | 
|---|
| 116 | (t3 (make-term m3 (* 7 9)))) | 
|---|
| 117 | (is (equalp (term-mul *ring-of-integers* t1 t2) t3)))) | 
|---|
| 118 |  | 
|---|
| 119 | (test termlist | 
|---|
| 120 | "termlist" | 
|---|
| 121 | (let* ((m1 (make-monom :initial-exponents '(1 2 3))) | 
|---|
| 122 | (m2 (make-monom :initial-exponents '(3 5 2))) | 
|---|
| 123 | (t1 (make-term m1 7)) | 
|---|
| 124 | (t2 (make-term m2 9)) | 
|---|
| 125 | (p (make-poly-from-termlist (list t1 t2))) | 
|---|
| 126 | (ring-and-order (make-ring-and-order)) | 
|---|
| 127 | (q (termlist-expt ring-and-order p 2))) | 
|---|
| 128 | (is-true (print p)) | 
|---|
| 129 | (is-true (print q)))) | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 | (test coerce-to-infix | 
|---|
| 134 | "Conversion to infix form" | 
|---|
| 135 | (is (equal | 
|---|
| 136 | (coerce-to-infix :term (make-term-variable *ring-of-integers* 5 3) '(x y z w u v)) | 
|---|
| 137 | '(* 1 (EXPT X 0) (EXPT Y 0) (EXPT Z 0) (EXPT W 1) (EXPT U 0))))) | 
|---|
| 138 |  | 
|---|
| 139 | (test priority-queue | 
|---|
| 140 | "Priority queue" | 
|---|
| 141 | (let ((q (make-priority-queue))) | 
|---|
| 142 | (priority-queue-insert q 7) | 
|---|
| 143 | (priority-queue-insert q 8) | 
|---|
| 144 | (is (= (priority-queue-size q) 3) "Note that there is always a dummy element in the queue.") | 
|---|
| 145 | (is (equalp (priority-queue-heap q) #(0 7 8))) | 
|---|
| 146 | (is (= (priority-queue-remove q) 7)) | 
|---|
| 147 | (is (= (priority-queue-remove q) 8)) | 
|---|
| 148 | (is-true (priority-queue-empty-p q)) | 
|---|
| 149 | (signals | 
|---|
| 150 | (error "Empty queue.") | 
|---|
| 151 | (priority-queue-remove q)))) | 
|---|
| 152 |  | 
|---|
| 153 | ;; | 
|---|
| 154 | ;; Currently parser cannot be tested, as it relies on many maxima functions | 
|---|
| 155 | ;; to parse a polynomial expression. | 
|---|
| 156 | ;; | 
|---|
| 157 | #| | 
|---|
| 158 | (test parser | 
|---|
| 159 | "Parser" | 
|---|
| 160 | (let (($f '((MLIST SIMP) ((MPLUS SIMP) $X ((MTIMES SIMP) -1 $Y)) ((MPLUS SIMP) $X $Y))) | 
|---|
| 161 | ($v '((MLIST SIMP) $X $Y))) | 
|---|
| 162 | (is-true (parse-poly-list $f $v)))) | 
|---|
| 163 | |# | 
|---|
| 164 |  | 
|---|
| 165 | (test infix-print | 
|---|
| 166 | "Infix printer" | 
|---|
| 167 | (is (string= (infix-print '(+ x y) nil) "X+Y")) | 
|---|
| 168 | (is (string= (infix-print '(expt x 3) nil) "X^3")) | 
|---|
| 169 | (is (string= (infix-print '(+ 1 (expt x 3)) nil) "1+(X^3)")) | 
|---|
| 170 | (is (string= (infix-print '(* x y) nil) "X*Y")) | 
|---|
| 171 | (is (string= (infix-print '(* x (expt y 2)) nil) "X*(Y^2)"))) | 
|---|
| 172 |  | 
|---|
| 173 | (test infix | 
|---|
| 174 | "Infix parser" | 
|---|
| 175 | (is (equal '#I( x^2 + y^2 ) '(+ (expt x 2) (expt y 2)))) | 
|---|
| 176 | (is (equal '#I( [ x, y ] ) '(:[ X Y))) | 
|---|
| 177 | (is (equal '#I( x + y) '(+ x y))) | 
|---|
| 178 | (is (equal '#I( x^3 ) '(expt x 3))) | 
|---|
| 179 | (is (equal '#I( 1 + x^3) '(+ 1 (expt x 3)))) | 
|---|
| 180 | (is (equal '#I( x * y^2 ) '(* x (expt y 2))))) | 
|---|
| 181 |  | 
|---|
| 182 |  | 
|---|
| 183 | (run! 'ngrobner-suite) | 
|---|
| 184 | (format t "All tests done!~%") | 
|---|
| 185 |  | 
|---|
| 186 |  | 
|---|