1 | // $Id: tst.lib,v 1.12 1998-09-01 12:03:13 obachman Exp $ |
---|
2 | //(obachman, last modified 6/30/98) |
---|
3 | ///////////////////////////////////////////////////////////////////////////// |
---|
4 | |
---|
5 | version="$Id: tst.lib,v 1.12 1998-09-01 12:03:13 obachman Exp $"; |
---|
6 | info=" |
---|
7 | LIBRARY: tst.lib PROCEDURES FOR RUNNING AUTOMATIC TST TESTS |
---|
8 | |
---|
9 | |
---|
10 | tst_system(s) returns string which is stdout of system(\"sh\", s) |
---|
11 | tst_ignore(any,[keyword], [link]) writes string(any) to link (or stdout), |
---|
12 | prepending prefix \"// tst_ignore:\" |
---|
13 | tst_init() writes some identification data to GetTstStatusFile() |
---|
14 | tst_status([any]) writes status info to to GetTstStatusFile() |
---|
15 | tst_groebnerTest(ideal i) |
---|
16 | tests groebner command |
---|
17 | tst_stdEqual(ideal i1, ideal i2) |
---|
18 | test whether two std's are \"equal\" |
---|
19 | "; |
---|
20 | |
---|
21 | ///////////////////////////////////////////////////////////////////////////// |
---|
22 | |
---|
23 | proc tst_system(string s, list #) |
---|
24 | "USAGE: tst_system(s); s string |
---|
25 | RETURN: string which is stdout and stderr of system(\"sh\", s) |
---|
26 | EXAMPLE: example tst_system; shows examples" |
---|
27 | { |
---|
28 | string tmpfile = "/tmp/tst_" + string(system("pid")); |
---|
29 | int errno; |
---|
30 | |
---|
31 | s = s + " 1>" + tmpfile + " 2>&1"; |
---|
32 | errno = system("sh", s); |
---|
33 | s = read(tmpfile); |
---|
34 | errno = system("sh", "rm -f " + tmpfile); |
---|
35 | if (size(#) > 0) |
---|
36 | { |
---|
37 | s = s[1, size(s) -1]; |
---|
38 | } |
---|
39 | return (s); |
---|
40 | } |
---|
41 | example |
---|
42 | { |
---|
43 | "EXAMPLE"; echo = 2; |
---|
44 | string s = tst_system("echo This is is an example of tst_system"); |
---|
45 | "The following is what the system call wrote to stdout: " + s; |
---|
46 | } |
---|
47 | |
---|
48 | proc tst_ignore(list #) |
---|
49 | "USAGE: tst_ignore(any,[keyword], [link]) |
---|
50 | any -- valid argument to string() |
---|
51 | keyword -- an arbitrary string |
---|
52 | link -- a link which can be written to |
---|
53 | RETURN: none; writes string(any) to link (or stdout, if no link given), |
---|
54 | prepending prefix \"// tst_ignore:\", or |
---|
55 | \"// tst_ignore:keyword hostname:\", |
---|
56 | if keyword was given. |
---|
57 | Should be used in tst files to output system dependent data |
---|
58 | (like date, pathnames). |
---|
59 | EXAMPLE: example tst_ignore; shows examples |
---|
60 | " |
---|
61 | { |
---|
62 | if (! defined(tst_no_status)) |
---|
63 | { |
---|
64 | string s; |
---|
65 | string keyword = ""; |
---|
66 | link outlink = ""; |
---|
67 | |
---|
68 | // Check # of args |
---|
69 | if (size(#) < 1 || size(#) > 3) |
---|
70 | { |
---|
71 | "Error tst_ignore: Wrong number of arguments"; |
---|
72 | "Usage: tst_ignore (any,[keyword], [link]);"; |
---|
73 | return(); |
---|
74 | } |
---|
75 | |
---|
76 | // Get Args |
---|
77 | s = string(#[1]); |
---|
78 | if (size(#) == 3) |
---|
79 | { |
---|
80 | keyword = #[2]; |
---|
81 | outlink = #[3]; |
---|
82 | } |
---|
83 | if (size(#) == 2) |
---|
84 | { |
---|
85 | if (typeof(#[2]) == "string") |
---|
86 | { |
---|
87 | keyword = #[2]; |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | outlink = #[2]; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | // check args |
---|
96 | if (typeof(keyword) != "string") |
---|
97 | { |
---|
98 | "Error tst_ignore: Keyword must be a string"; |
---|
99 | "Usage: tst_ignore (any,[keyword], [link]);"; |
---|
100 | return(); |
---|
101 | } |
---|
102 | |
---|
103 | if (status(outlink, "open", "no")) |
---|
104 | { |
---|
105 | open(outlink); |
---|
106 | } |
---|
107 | |
---|
108 | if (status(outlink, "write", "not ready")) |
---|
109 | { |
---|
110 | "Error tst_ignore: Can not write to link"; |
---|
111 | outlink; |
---|
112 | "Usage: tst_ignore (any,[keyword], [link]);"; |
---|
113 | return(); |
---|
114 | } |
---|
115 | |
---|
116 | // ready -- do the actual work |
---|
117 | if (keyword != "") |
---|
118 | { |
---|
119 | write(outlink,"// tst_ignore:" + keyword + " :: " + tst_system("hostname", 1) + ":" + s); |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | write(outlink, "// tst_ignore: " + s); |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | example |
---|
128 | { |
---|
129 | "EXAMPLE"; |
---|
130 | "System independent data can safely be output in tst files;"; |
---|
131 | "However, system dependent data like dates, or pathnames, should be output"; |
---|
132 | "using the command tst_ignore(...), like"; |
---|
133 | echo = 2; |
---|
134 | tst_ignore(tst_system("date")); |
---|
135 | int t1 = timer; |
---|
136 | tst_ignore(t1, "time"); |
---|
137 | tst_ignore(memory(1), "memory"); |
---|
138 | } |
---|
139 | |
---|
140 | static proc Get_tst_timer() |
---|
141 | { |
---|
142 | if (! defined (tst_timer)) |
---|
143 | { |
---|
144 | string tst_timer = "// tst_ignore:0"; |
---|
145 | export tst_timer; |
---|
146 | return (0); |
---|
147 | } |
---|
148 | else |
---|
149 | { |
---|
150 | execute "int tst_int_timer = " + tst_timer[15,size(tst_timer)] + ";"; |
---|
151 | return (tst_int_timer); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | static proc Set_tst_timer (int this_time) |
---|
156 | { |
---|
157 | tst_timer = tst_timer[1,14] + string(this_time); |
---|
158 | } |
---|
159 | |
---|
160 | static proc GetTstStatusFile() |
---|
161 | { |
---|
162 | if (!defined(tst_status_file)) |
---|
163 | { |
---|
164 | return ("tst_status.out"); |
---|
165 | } |
---|
166 | else |
---|
167 | { |
---|
168 | return (tst_status_file); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | static proc tst_status_out (def prefix, def what, list #) |
---|
173 | { |
---|
174 | string outstring; |
---|
175 | |
---|
176 | outstring = string(prefix) + " >> " + string(what); |
---|
177 | if (size(#) > 0) |
---|
178 | { |
---|
179 | outstring = outstring + " :: " + |
---|
180 | tst_system("hostname", 1) + ":" + string(#[1]); |
---|
181 | } |
---|
182 | write(":a " + GetTstStatusFile(), outstring); |
---|
183 | } |
---|
184 | |
---|
185 | proc tst_status (list #) |
---|
186 | "USAGE: tst_status([any]) |
---|
187 | RETURN: none; writes to stdout the current memory usage and used time |
---|
188 | since last call to tst_status(), if no argument is given, or, |
---|
189 | since start-up of Singular, if an argument is given. |
---|
190 | NOTE: Should be used regularly within tst files to enable automatic |
---|
191 | tracking of memory and time performance. |
---|
192 | EXAMPLE: example tst_status; shows example |
---|
193 | " |
---|
194 | { |
---|
195 | if (! defined(tst_no_status)) |
---|
196 | { |
---|
197 | if (! defined(tst_status_counter)) |
---|
198 | { |
---|
199 | int tst_status_counter = 1; |
---|
200 | export tst_status_counter; |
---|
201 | } |
---|
202 | else |
---|
203 | { |
---|
204 | tst_status_counter++; |
---|
205 | } |
---|
206 | |
---|
207 | tst_status_out(tst_status_counter, "tst_memory_0", memory(0)); |
---|
208 | tst_status_out(tst_status_counter, "tst_memory_1", memory(1)); |
---|
209 | tst_status_out(tst_status_counter, "tst_memory_2", memory(2)); |
---|
210 | if (size(#) > 0) |
---|
211 | { |
---|
212 | tst_status_out(tst_status_counter, "tst_timer_1", timer); |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | tst_status_out(tst_status_counter, "tst_timer", timer - Get_tst_timer()); |
---|
217 | Set_tst_timer(timer); |
---|
218 | } |
---|
219 | string(tst_status_counter) + " >> " + GetTstStatusFile(); |
---|
220 | } |
---|
221 | } |
---|
222 | example |
---|
223 | { |
---|
224 | "EXAMPLE"; echo = 2; |
---|
225 | tst_status(); |
---|
226 | ring r; |
---|
227 | poly p = (x+y+z)^40; |
---|
228 | tst_status(); |
---|
229 | tst_status(1); |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | proc tst_init() |
---|
234 | "USAGE: tst_init() |
---|
235 | RETURN: none; writes some identification data to stdout; |
---|
236 | should be called as first routine in a tst file |
---|
237 | EXAMPLE: example tst_init; shows example |
---|
238 | " |
---|
239 | { |
---|
240 | if (! defined(tst_no_status)) |
---|
241 | { |
---|
242 | write(":w " + GetTstStatusFile(), "Status Output of " + GetTstStatusFile()); |
---|
243 | tst_status_out("init", "USER :" + system("getenv", "USER")); |
---|
244 | tst_status_out("init", "HOSTNAME:" + tst_system("hostname", 1)); |
---|
245 | tst_status_out("init", "uname -a:" + tst_system("uname -a", 1)); |
---|
246 | tst_status_out("init", "date :" + tst_system("date", 1)); |
---|
247 | tst_status_out("init", "version :" + string(system("version"))); |
---|
248 | tst_status_out("init", "ticks :" + system("--ticks-per-sec")); |
---|
249 | "init >> " + GetTstStatusFile(); |
---|
250 | } |
---|
251 | } |
---|
252 | example |
---|
253 | { |
---|
254 | "EXAMPLE"; echo = 2; |
---|
255 | tst_init(); |
---|
256 | } |
---|
257 | |
---|
258 | /////////////////////////////////////////////////////////////////////// |
---|
259 | |
---|
260 | proc tst_groebnerTest(ideal i, list #) |
---|
261 | "USAGE: tst_groebnerTesti,[v]) : ideal i, [int v] |
---|
262 | RETURN: 1, if groebner command produced \"equal\" std as std command |
---|
263 | 0, otherwise |
---|
264 | Two std's are \"equal\" here, if their redSB's are element-wise equal, |
---|
265 | and if they reduce each other to zero, and if their leading ideals |
---|
266 | are equal |
---|
267 | On success, times of std - groebner is written with tst_ignore, and |
---|
268 | times are added to global variables tst_std_time and |
---|
269 | tst_groebner_time. If v given, and <= 0, short ideal |
---|
270 | characteristic is printed, if v > 0, ideals are printed. |
---|
271 | On failure, Error message and ideals are printed. |
---|
272 | EXAMPLE: example tst_groebner; shows an example |
---|
273 | " |
---|
274 | { |
---|
275 | int st = timer; |
---|
276 | ideal si = std(i); |
---|
277 | st = timer - st; |
---|
278 | |
---|
279 | int gt = timer; |
---|
280 | ideal gi = groebner(i); |
---|
281 | gt = timer - gt; |
---|
282 | |
---|
283 | if (tst_stdEqual(si, gi)) |
---|
284 | { |
---|
285 | tst_ignore(string(st) + " - " + string(gt) + " == " + string(st - gt)); |
---|
286 | if (! defined(tst_groebner_time)) |
---|
287 | { |
---|
288 | int tst_groebner_time; |
---|
289 | int tst_std_time; |
---|
290 | export tst_groebner_time, tst_std_time; |
---|
291 | } |
---|
292 | tst_std_time = tst_std_time + st; |
---|
293 | tst_groebner_time = tst_groebner_time + gt; |
---|
294 | if (size(#)) |
---|
295 | { |
---|
296 | if (typeof(#[1] == "int")) |
---|
297 | { |
---|
298 | if (#[1] <= 0) |
---|
299 | { |
---|
300 | idPrintShort(si, "si"); |
---|
301 | idPrintShort(gi, "gi"); |
---|
302 | } |
---|
303 | else |
---|
304 | { |
---|
305 | si; |
---|
306 | gi; |
---|
307 | } |
---|
308 | } |
---|
309 | } |
---|
310 | return (1); |
---|
311 | } |
---|
312 | return (0); |
---|
313 | } |
---|
314 | example |
---|
315 | { |
---|
316 | "EXAMPLE: "; echo = 2; |
---|
317 | ring r = 0, (a,b,c,d), lp; |
---|
318 | ideal i = a+b+c+d, ab+ad+bc+cd, abc+abd+acd+bcd, abcd-1; // cyclic 4 |
---|
319 | tst_groebnerTest(i); |
---|
320 | tst_groebnerTest(i, 0); |
---|
321 | tst_groebnerTest(i, 1); |
---|
322 | } |
---|
323 | |
---|
324 | |
---|
325 | // |
---|
326 | // A routine which test for equality of "std-bases" |
---|
327 | // |
---|
328 | proc tst_stdEqual(ideal i1, ideal i2) |
---|
329 | "USAGE: tst_stdEqual(i1, i2) ideal i1, i2 |
---|
330 | RETURN 1, if i1 \"equald\" i2 as a std bases |
---|
331 | 0, otherwise |
---|
332 | Two std's are \"equal\" here, if their redSB's are element-wise equal, |
---|
333 | and if they reduce each other to zero, and if their leading ideals |
---|
334 | are equal |
---|
335 | On failure, error message is printed. |
---|
336 | EXAMPLE: example tst_stdEqual; shows an example |
---|
337 | " |
---|
338 | { |
---|
339 | int i; |
---|
340 | int back; |
---|
341 | intvec opts = option(get); |
---|
342 | option(redSB); |
---|
343 | |
---|
344 | ideal ri1 = simplify(interred(i1), 1); |
---|
345 | ideal ri2 = simplify(interred(i2), 1); |
---|
346 | |
---|
347 | option(set, opts); |
---|
348 | |
---|
349 | if (size(ri1) != size(ri2)) |
---|
350 | { |
---|
351 | "Error in tst_stdEqual: Reduced sizes differ"; |
---|
352 | size(ri1); |
---|
353 | size(ri2); |
---|
354 | return(0); |
---|
355 | } |
---|
356 | |
---|
357 | for (i=1; i<=size(ri1); i++) |
---|
358 | { |
---|
359 | if (ri1[i] != ri2[i]) |
---|
360 | { |
---|
361 | "Error in tst_stdEqual: " + string(i) + " th poly differ"; |
---|
362 | ri1[i]; |
---|
363 | ri2[i]; |
---|
364 | return(0); |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | // reduced SB are now equal |
---|
369 | if (size(reduce(i1, i2, 1)) == 0) |
---|
370 | { |
---|
371 | if (size(reduce(i2, i1, 1)) == 0) |
---|
372 | { |
---|
373 | poly p1, p2; |
---|
374 | |
---|
375 | ideal si1 = simplify(i1, 7); |
---|
376 | ideal si2 = simplify(i2, 7); |
---|
377 | |
---|
378 | if (size(si1) == size(si2)) |
---|
379 | { |
---|
380 | for (i=1; i<=size(si1); i++) |
---|
381 | { |
---|
382 | p1 = p1 + lead(si1[i]); |
---|
383 | p2 = p2 + lead(si2[i]); |
---|
384 | } |
---|
385 | if (p1 != p2) |
---|
386 | { |
---|
387 | "Error in tst_stdEqual: Lead monoms differ:"; |
---|
388 | p1; |
---|
389 | p2; |
---|
390 | return(0); |
---|
391 | } |
---|
392 | } |
---|
393 | else |
---|
394 | { |
---|
395 | "Error in tst_stdEqual: size differs:"; |
---|
396 | size(si1); |
---|
397 | size(si2); |
---|
398 | return(0); |
---|
399 | } |
---|
400 | } |
---|
401 | else |
---|
402 | { |
---|
403 | "Error in tst_stdEqual: reduce(i2, i1) != 0"; |
---|
404 | return(0); |
---|
405 | } |
---|
406 | } |
---|
407 | else |
---|
408 | { |
---|
409 | back = 1; "Error in tst_stdEqual: reduce(i1, i2) != 0"; |
---|
410 | return(0); |
---|
411 | } |
---|
412 | |
---|
413 | return (1); |
---|
414 | } |
---|
415 | example |
---|
416 | { |
---|
417 | "EXAMPLE: "; echo = 2; |
---|
418 | ring r = 0, (a,b,c,d), lp; |
---|
419 | ideal i = a+b+c+d, ab+ad+bc+cd, abc+abd+acd+bcd, abcd-1; // cyclic 4 |
---|
420 | tst_stdEqual(groebner(i), std(i)); |
---|
421 | tst_stdEqual(std(i), i); |
---|
422 | } |
---|
423 | |
---|
424 | static proc idPrintShort(ideal id, string name) |
---|
425 | { |
---|
426 | "Summary of " + name + " (leading monoms and size of polys):"; |
---|
427 | int i; |
---|
428 | for (i = 1; i<=size(id); i++) |
---|
429 | { |
---|
430 | "[" + string(i) + "]: #" + string(size(id[i])) + ":" + string(lead(id[i])); |
---|
431 | } |
---|
432 | } |
---|
433 | |
---|
434 | |
---|
435 | |
---|
436 | |
---|