close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

source: branches/f4grobner/5am-monom.lisp@ 4494

Last change on this file since 4494 was 4448, checked in by Marek Rychlik, 9 years ago

Summary: Added auto-wrapping tests

File size: 9.4 KB
RevLine 
[2127]1;;; -*- Mode: Lisp -*-
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(defpackage #:5am-monom
[4355]41 (:use :cl :it.bese.fiveam :monom :copy :ring)
[2640]42 (:documentation "Monom and derived classes tests"))
[2127]43
44(in-package :5am-monom)
45
46(def-suite monom-suite
47 :description "Monom package suite")
48
49(in-suite monom-suite)
50
[2308]51(def-fixture monom-context ()
[2829]52 ;; Use SYMBOL-MACROLET not let to avoid 'unused variable' complaints
[2841]53 (symbol-macrolet
[3320]54 ((m (make-instance 'monom :exponents '(1 2 3)))
55 (n (make-instance 'monom :exponents '(4 5 6)))
56 (m*n (make-instance 'monom :exponents '(5 7 9)))
57 (n/m (make-instance 'monom :exponents '(3 3 3)))
[2841]58 (m-tensor-n (make-instance 'monom :exponents '(1 2 3 4 5 6))))
[2308]59 (&body)))
[2299]60
[2302]61(test monom-basics
[2306]62 "Monom basics"
[2308]63 (with-fixture monom-context ()
[3463]64 (is (= (monom-dimension m) 3))
65 (is (= (monom-elt m 2) 3))
[3593]66 (is (= (total-degree m) 6))
67 (is (= (sugar m) 6))
[3609]68 (is (equalp (->list (make-instance 'monom :dimension 3)) '(0 0 0)) "Trivial monomial is a vector of 0's")
[4094]69 (is (universal-equalp (multiply m n) m*n))
[3593]70 (is (universal-equalp (divide-by n m) n/m))
71 (is (universal-equalp (right-tensor-product-by m n) m-tensor-n))
[2183]72 (signals
[2400]73 (error "EXPONENTS must have length DIMENSION")
[2291]74 (make-instance 'monom :dimension 3 :exponents '(1 2 3 4 5 6)))
[3593]75 (is-true (divides-p m n))
76 (is-false (divides-p n m))
77 (is (universal-equalp (universal-gcd m n) m))
78 (is (universal-equalp (universal-lcm m n) n))
79 (is-true (depends-p m 0))
[2290]80 (signals
81 (error "Index out of bounds")
[3593]82 (depends-p m 3))
[2821]83 )
84 (with-fixture monom-context ()
[3593]85 (is (universal-equalp (multiply-by m n) m*n)))
[2821]86 (with-fixture monom-context ()
[3593]87 (is (universal-equalp (divide-by n m) n/m)))
[3467]88 (with-fixture monom-context ()
[3609]89 (is (equal (->list m) '(1 2 3)))))
[2127]90
[3479]91;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92;;
93;; Order generics (LEX>, GRLEX>,...) tests
94;;
95;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
96
97(def-fixture order-context ()
98 (symbol-macrolet
99 ((p (make-instance 'monom :exponents '(1 3 2)))
100 (q (make-instance 'monom :exponents '(1 2 3))))
101 (&body)))
102
103(test order
104 "order"
105 (with-fixture order-context ()
106 (is-true (lex> p q))
107 (is-true (grlex> p q))
108 (is-true (revlex> p q))
109 (is-true (grevlex> p q))
110 (is-false (invlex> p q))))
111
112(def-fixture elim-order-context ()
113 (let* ((p (make-instance 'monom :exponents '(1 2 3)))
114 (q (make-instance 'monom :exponents '(4 5 6)))
115 (elim-order-factory (make-elimination-order-factory))
116 (elim-order-1 (funcall elim-order-factory 1))
117 (elim-order-2 (funcall elim-order-factory 2)))
118 (&body)))
119
120
121(test elim-order
122 "Elimination order"
123 (with-fixture elim-order-context ()
124 (is-false (funcall elim-order-1 p q))
125 (is-false (funcall elim-order-2 p q))))
126
[3597]127;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
128;;
129;; TERM class tests
130;;
131;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132
133(def-fixture term-context ()
134 (symbol-macrolet
135 ((z (make-instance 'term :dimension 3 :coeff 5))
136 (m (make-instance 'term :dimension 3 :exponents '(1 2 3) :coeff 6))
137 (n (make-instance 'term :dimension 3 :exponents '(4 5 6) :coeff 12))
138 (m*n (make-instance 'term :dimension 3 :exponents '(5 7 9) :coeff 72))
139 (n/m (make-instance 'term :dimension 3 :exponents '(3 3 3) :coeff 2))
140 (m-tensor-n (make-instance 'term :exponents '(1 2 3 4 5 6) :coeff 72))
141 (m-uminus (make-instance 'term :dimension 3 :exponents '(1 2 3) :coeff -6)))
142 (&body)))
143
144(test term-basics
145 "Term basics"
146 (with-fixture term-context ()
[3598]147 (is (= (monom-dimension m) 3))
148 (is (= (monom-elt m 2) 3))
[3603]149 (is (= (total-degree m) 6))
150 (is (= (sugar m) 6))
[4047]151 (is (equalp (->list z) '((0 0 0) . 5)) "Trivial term is a vector of 0's")
[3598]152 (is (universal-equalp (multiply m n) m*n))
153 (is (universal-equalp (divide n m) n/m))
154 (is (universal-equalp (right-tensor-product-by m n) m-tensor-n))
[3597]155 (signals
156 (error "EXPONENTS must have length DIMENSION")
157 (make-instance 'term :dimension 3 :exponents '(1 2 3 4 5 6) :coeff 77))
[3598]158 (is-true (divides-p m n))
159 (is-false (divides-p n m))
[3604]160 (is (universal-equalp (universal-gcd m n) m))
161 (is (universal-equalp (universal-lcm m n) n))
[3598]162 (is-true (depends-p m 0))
[3597]163 (signals
164 (error "Index out of bounds")
[3598]165 (depends-p m 3))
[3597]166 )
167 (with-fixture term-context ()
[3598]168 (is (universal-equalp (multiply-by m n) m*n)))
[3597]169 (with-fixture term-context ()
[3598]170 (is (universal-equalp (divide-by n m) n/m)))
[3597]171 (with-fixture term-context ()
[3598]172 (is (universal-equalp (unary-minus m) m-uminus))))
[3597]173
174
175(def-fixture monom/term-conversion-context ()
176 (symbol-macrolet
177 ((term (make-instance 'term :exponents '(1 2 3) :coeff 4))
178 (monom (make-instance 'monom :exponents '(1 2 3)))
179 (promoted-monom (make-instance 'term :exponents '(1 2 3) :coeff 1)))
180 (&body)))
181
182(test monom/term-conversion
183 "Monom/term conversion"
184 (with-fixture monom/term-conversion-context ()
[3598]185 (is (universal-equalp (change-class term 'monom) monom)))
[3597]186 (with-fixture monom/term-conversion-context ()
[3598]187 (is (universal-equalp (change-class monom 'term) promoted-monom))))
[3597]188
189(test monom/term-copy
190 "Monom/term copy"
191 (with-fixture monom/term-conversion-context ()
[3598]192 (is (universal-equalp (copy-instance monom) monom))
[3601]193 (is (universal-equalp (copy-instance term) term))))
[3597]194
195(test term-tensor-product
196 "Term tensor product"
197 (let ((term1 (make-instance 'term :exponents '(1 2 3) :coeff 4))
198 (term2 (make-instance 'term :exponents '(4 5) :coeff 3))
199 (term1-left-tensor-by-term2 (make-instance 'term :exponents '(4 5 1 2 3) :coeff 12)))
[3598]200 (is (universal-equalp (left-tensor-product-by term1 term2) term1-left-tensor-by-term2))))
[3597]201
202(test term-contract
203 "Term contract"
204 (let ((term (make-instance 'term :exponents '(1 2 3) :coeff 4))
205 (term-contracted (make-instance 'term :exponents '(2 3) :coeff 4)))
[3598]206 (is (universal-equalp (left-contract term 1) term-contracted))))
[3597]207
[4167]208(test sexp-conversion
209 "Sexp conversion"
210 (is (equal (->sexp (make-instance 'term :exponents '(0 0 0 0) :coeff -5) '(x y u v)) -5))
211 (signals (error "Variables NIL and exponents #(0 0 0 0) must have the same length.")
212 (->sexp (make-instance 'monom :exponents '(0 0 0 0))))
213 (is (equal (->sexp (make-instance 'monom :exponents '(0 0 0 0)) '(x y u w)) 1))
214 (is (equal (->sexp (make-instance 'monom :exponents '(0 0 1 0)) '(x y u w)) 'u))
215 (is (equalp (->sexp (make-instance 'monom :exponents '(1 2 1 1)) '(x y u w))
216 '(* X (EXPT Y 2) U W)))
217 (is (equal (->sexp (make-instance 'term :exponents '(0 1 0 0) :coeff -5) '(x y u v))
218 '(* -5 Y)))
219 (is (equal (->sexp (make-instance 'term :exponents '(1 1 0 0) :coeff -5) '(x y u v))
220 '(* -5 X Y)))
221 (is (equal (->sexp (make-instance 'term :exponents '(1 0 0 0) :coeff -5) '(x y u v))
222 '(* -5 X)))
223 (is (equal (->sexp (make-instance 'term :exponents '(11 0 0 0) :coeff -5) '(x y u v))
224 '(* -5 (EXPT X 11))))
225 (is (equal (->sexp (make-instance 'term :exponents '(11 0 0 0) :coeff 5) '(x y u v))
226 '(* 5 (EXPT X 11))))
227 (is (equal (->sexp (make-instance 'term :exponents '(11 0 4 0) :coeff 5) '(x y u v))
228 '(* 5 (EXPT X 11) (EXPT U 4))))
229)
230
[4448]231(test term-auto-wrapping-when-created
232 "Term auto-wrapping upon creation"
233 (let ((term-with-integer-coeff (make-instance 'term :dimension 3 :coeff 7))
234 (term-with-rational-coeff (make-instance 'term :dimension 3 :coeff 3/5)))
235 (is (eq (class-of (term-coeff term-with-integer-coeff)) (find-class 'integer-ring)))
236 (is (eq (class-of (term-coeff term-with-rational-coeff)) (find-class 'rational-field)))))
237
238(test term-auto-wrapping-when-assigned
239 "Term auto-wrapping upon assignment"
240 (let ((term-with-null-coeff (make-instance 'term :dimension 3 :coeff nil)))
241 (setf (term-coeff term-with-null-coeff) 7)
242 (is (eq (class-of (term-coeff term-with-null-coeff)) (find-class 'integer-ring)))))
243
244
245
[2127]246(run! 'monom-suite)
247(format t "All tests done!~%")
248
249
Note: See TracBrowser for help on using the repository browser.