1# -*- tcl -*-
2# polynomials.test --
3#    Test cases for the ::math::polynomials package
4#
5
6# -------------------------------------------------------------------------
7
8source [file join \
9	[file dirname [file dirname [file join [pwd] [info script]]]] \
10	devtools testutilities.tcl]
11
12testsNeedTcl     8.5
13testsNeedTcltest 2.1
14
15support {
16    useLocal math.tcl math
17}
18testing {
19    useLocal polynomials.tcl math::polynomials
20}
21
22# -------------------------------------------------------------------------
23
24proc matchNumbers {expected actual} {
25   set match 1
26   foreach a $actual e $expected {
27      if {abs($a-$e) > 0.1e-6} {
28         set match 0
29         break
30      }
31   }
32   return $match
33}
34
35customMatch numbers matchNumbers
36
37# -------------------------------------------------------------------------
38
39test "Polynomial-1.0" "Create polynomial (degree 3)" \
40   -match numbers -body {
41   set f1 [::math::polynomials::polynomial {1 2 3 4}]
42   set result [lindex $f1 1]
43} -result {4 3 2 1}
44
45test "Polynomials-1.1" "Create polynomial (degree 3, leading zeros)" \
46   -match numbers -body {
47   set f1 [::math::polynomials::polynomial {1 2 3 4 0 0 0}]
48   set result [lindex $f1 1]
49} -result {4 3 2 1}
50
51test "Polynomials-1.2" "Create polynomial (invalid coefficients)" \
52   -match glob -body {
53   set f1 [::math::polynomials::polynomial {A B C}]
54} -result "Coefficients *" -returnCodes 1
55
56test "Polynomials-1.3" "Create polynomial command" \
57   -match numbers -body {
58   set f1 [::math::polynomials::polynCmd {1 2 3 4 0 0 0}]
59   set result {}
60   foreach x {0 1 2 3} {
61      lappend result [$f1 $x]
62   }
63   set result
64} -result {1 10 49 142}
65
66test "Polynomials-1.4" "Evaluate polynomial" \
67   -match numbers -body {
68   set f1 [::math::polynomials::polynomial {1 2 3 4 0 0 0}]
69   set result {}
70   foreach x {0 1 2 3} {
71      lappend result [::math::polynomials::evalPolyn $f1 $x]
72   }
73   set result
74} -result {1 10 49 142}
75
76test "Polynomials-1.5" "Evaluate null polynomial" \
77   -match numbers -body {
78   set f1 [::math::polynomials::polynomial {0 0 0}]
79   set result {}
80   foreach x {0 1 2 3} {
81      lappend result [::math::polynomials::evalPolyn $f1 $x]
82   }
83   set result
84} -result {0 0 0 0}
85
86test "Polynomials-2.1" "Query polynomial properties - degree" \
87   -match exact -body {
88   set f1 [::math::polynomials::polynomial {1 2 3}]
89   set result [::math::polynomials::degreePolyn $f1]
90} -result 2
91
92test "Polynomials-2.2" "Query polynomial properties - degree (2 again)" \
93   -match exact -body {
94   set f1 [::math::polynomials::polynomial {1 2 3 0 0 0}]
95   set result [::math::polynomials::degreePolyn $f1]
96} -result 2
97
98test "Polynomials-2.3" "Query polynomial properties - degree (null)" \
99   -match exact -body {
100   set f1 [::math::polynomials::polynomial {0 0 0}]
101   set result [::math::polynomials::degreePolyn $f1]
102} -result -1
103
104test "Polynomials-2.4" "Query polynomial properties - leading coeff" \
105   -match exact -body {
106   set f1 [::math::polynomials::polynomial {1 2 3}]
107   set idx [::math::polynomials::degreePolyn $f1]
108   set coeff [::math::polynomials::coeffPolyn $f1 $idx]
109} -result 3
110
111test "Polynomials-2.5" "Query polynomial properties - all coeffs" \
112   -match numbers -body {
113   set f1 [::math::polynomials::polynomial {1 2 3}]
114   set coeffs [::math::polynomials::allCoeffsPolyn $f1]
115} -result {1 2 3}
116
117test "Polynomials-3.1" "Derivatives and primitives - derivative" \
118   -match numbers -body {
119   set f1 [::math::polynomials::polynomial {1 2 3}]
120   set f2 [::math::polynomials::derivPolyn $f1]
121   set coeffs [::math::polynomials::allCoeffsPolyn $f2]
122} -result {2 6}
123
124test "Polynomials-3.2" "Derivatives and primitives - primitive" \
125   -match numbers -body {
126   set f1 [::math::polynomials::polynomial {1 4 9}]
127   set f2 [::math::polynomials::primitivePolyn $f1]
128   set coeffs [::math::polynomials::allCoeffsPolyn $f2]
129} -result {0 1 2 3}
130
131test "Polynomials-4.1" "Arithmetical operations - add (1)" \
132   -match numbers -body {
133   set f1 [::math::polynomials::polynomial {1 2 3}]
134   set f2 [::math::polynomials::polynomial {1 2}]
135   set f3 [::math::polynomials::addPolyn $f1 $f2]
136   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
137} -result {2 4 3}
138
139test "Polynomials-4.2" "Arithmetical operations - add (2)" \
140   -match numbers -body {
141   set f1 [::math::polynomials::polynomial {1 2 3}]
142   set f2 [::math::polynomials::polynomial {1 2}]
143   set f3 [::math::polynomials::addPolyn $f2 $f1]
144   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
145} -result {2 4 3}
146
147test "Polynomials-4.3" "Arithmetical operations - subtract (1)" \
148   -match numbers -body {
149   set f1 [::math::polynomials::polynomial {1 2 3}]
150   set f2 [::math::polynomials::polynomial {1 2}]
151   set f3 [::math::polynomials::subPolyn $f1 $f2]
152   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
153} -result {0 0 3}
154
155test "Polynomials-4.4" "Arithmetical operations - subtract (2)" \
156   -match numbers -body {
157   set f1 [::math::polynomials::polynomial {1 2 3}]
158   set f2 [::math::polynomials::polynomial {1 2}]
159   set f3 [::math::polynomials::subPolyn $f2 $f1]
160   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
161} -result {0 0 -3}
162
163test "Polynomials-4.5" "Arithmetical operations - multiply (1)" \
164   -match numbers -body {
165   set f1 [::math::polynomials::polynomial {1 2 3}]
166   set f2 [::math::polynomials::polynomial {1 2}]
167   set f3 [::math::polynomials::multPolyn $f1 $f2]
168   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
169} -result {1 4 7 6}
170
171test "Polynomials-4.6" "Arithmetical operations - multiply (2)" \
172   -match numbers -body {
173   set f1 [::math::polynomials::polynomial {1 2 3}]
174   set f2 [::math::polynomials::polynomial {1 2}]
175   set f3 [::math::polynomials::multPolyn $f2 $f1]
176   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
177} -result {1 4 7 6}
178
179test "Polynomials-4.7" "Arithmetical operations - multiply (3)" \
180   -match numbers -body {
181   set f1 [::math::polynomials::polynomial {1 2 3}]
182   set f3 [::math::polynomials::multPolyn $f1 2.0]
183   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
184} -result {2 4 6}
185
186test "Polynomials-4.8" "Arithmetical operations - multiply (4)" \
187   -match numbers -body {
188   set f1 [::math::polynomials::polynomial {1 2 3}]
189   set f3 [::math::polynomials::multPolyn 2.0 $f1]
190   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
191} -result {2 4 6}
192
193test "Polynomials-4.9" "Arithmetical operations - divide (1)" \
194   -match numbers -body {
195   set f1 [::math::polynomials::polynomial {1 2 3}]
196   set f2 [::math::polynomials::polynomial {0 1}]
197   set f3 [::math::polynomials::divPolyn $f1 $f2]
198   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
199} -result {2 3}
200
201test "Polynomials-4.10" "Arithmetical operations - divide (2)" \
202   -match numbers -body {
203   set f1 [::math::polynomials::polynomial {1 2 3}]
204   set f3 [::math::polynomials::divPolyn $f1 2.0]
205   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
206} -result {0.5 1 1.5}
207
208test "Polynomials-4.11" "Arithmetical operations - divide (3)" \
209   -match numbers -body {
210   set f1 [::math::polynomials::polynomial {1 2 3}]
211   set f2 [::math::polynomials::polynomial {0 1}]
212   set f3 [::math::polynomials::divPolyn $f2 $f1]
213   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
214} -result {}
215
216test "Polynomials-4.12" "Arithmetical operations - divide (4)" \
217   -match glob -body {
218   set f1 [::math::polynomials::polynomial {1 2 3}]
219   set f2 [::math::polynomials::polynomial {0}]
220   set f3 [::math::polynomials::divPolyn $f1 $f2]
221   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
222} -result "Denominator*" -returnCodes 1
223
224test "Polynomials-4.13" "Arithmetical operations - remainder (1)" \
225   -match numbers -body {
226   set f1 [::math::polynomials::polynomial {1 2 3}]
227   set f2 [::math::polynomials::polynomial {0 1}]
228   set f3 [::math::polynomials::remainderPolyn $f1 $f2]
229   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
230} -result {1}
231
232test "Polynomials-4.14" "Arithmetical operations - remainder (2)" \
233   -match numbers -body {
234   set f1 [::math::polynomials::polynomial {1 2 3}]
235   set f3 [::math::polynomials::remainderPolyn $f1 2.0]
236   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
237} -result {}
238
239test "Polynomials-4.15" "Arithmetical operations - remainder (3)" \
240   -match numbers -body {
241   set f1 [::math::polynomials::polynomial {1 2 3}]
242   set f2 [::math::polynomials::polynomial {0 1}]
243   set f3 [::math::polynomials::remainderPolyn $f2 $f1]
244   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
245} -result {0 1}
246
247test "Polynomials-4.16" "Arithmetical operations - remainder (4)" \
248   -match glob -body {
249   set f1 [::math::polynomials::polynomial {1 2 3}]
250   set f2 [::math::polynomials::polynomial {0}]
251   set f3 [::math::polynomials::remainderPolyn $f1 $f2]
252   set coeffs [::math::polynomials::allCoeffsPolyn $f3]
253} -result "Denominator*" -returnCodes 1
254
255
256
257
258
259# End of test cases
260testsuiteCleanup
261