1#
2# Tests for functions defined in src/ariths.c
3#
4gap> START_TEST("kernel/ariths.tst");
5
6# InUndefined
7gap> 1 in 2;
8Error, operations: IN of integer and integer is not defined
9
10# SUM, DIFF, PROD, QUO
11gap> SUM(1,1);
122
13gap> DIFF(1,1);
140
15gap> PROD(1,1);
161
17gap> QUO(1,1);
181
19gap> MOD(1,1);
200
21
22#
23# suppress paths in trace output
24#
25gap> old_VMETHOD_PRINT_INFO:=VMETHOD_PRINT_INFO;;
26gap> old_NEXT_VMETHOD_PRINT_INFO:=NEXT_VMETHOD_PRINT_INFO;;
27gap> MakeReadWriteGlobal("VMETHOD_PRINT_INFO");
28gap> MakeReadWriteGlobal("NEXT_VMETHOD_PRINT_INFO");
29gap> VMETHOD_PRINT_INFO:=function(methods, i, arity)
30>     local offset;
31>     offset := (arity+BASE_SIZE_METHODS_OPER_ENTRY)*(i-1)+arity;
32>     Print("#I  ", methods[offset+4]);
33>     Print("\n");
34> end;;
35gap> NEXT_VMETHOD_PRINT_INFO:=function(methods, i, arity)
36>     local offset;
37>     offset := (arity+BASE_SIZE_METHODS_OPER_ENTRY)*(i-1)+arity;
38>     Print("#I Trying next: ", methods[offset+4]);
39>     Print("\n");
40> end;;
41
42#
43# Test the various "VerboseFOO" handlers; to this end, create a mock family,
44# category and type, and two instances of that, so that we can safely test all
45# involved unary and binary operations
46#
47gap> fam := NewFamily("MockFamily");;
48gap> cat := NewCategory("IsMockObj",
49>               IsMultiplicativeElementWithInverse and
50>               IsAdditiveElementWithInverse and
51>               IsCommutativeElement and
52>               IsAssociativeElement and
53>               IsAdditivelyCommutativeElement);;
54gap> type := NewType(fam, cat and IsPositionalObjectRep);;
55gap> a := Objectify(type,[2]);;
56gap> b := Objectify(type,[3]);;
57
58# unary
59gap> unary := [
60>   Zero,
61>   ZeroMutable,
62>   ZeroSameMutability,
63>   AdditiveInverse,
64>   AdditiveInverseMutable,
65>   AdditiveInverseSameMutability,
66>   One,
67>   OneMutable,
68>   OneSameMutability,
69>   Inverse,
70>   InverseMutable,
71>   InverseSameMutability,
72> ];;
73
74# binary
75gap> binary := [
76>   \=,
77>   \<,
78>   \in,
79>   \+,
80>   \-,
81>   \*,
82>   \/,
83>   LeftQuotient,
84>   \^,
85>   Comm,
86>   \mod,
87> ];;
88
89#
90# test with regular methods
91#
92gap> for m in unary do InstallMethod(m, [cat], ReturnTrue); od;
93gap> for m in binary do InstallMethod(m, [cat, cat], ReturnTrue); od;
94gap> InstallMethod(SetZeroImmutable, [cat, IsObject], ReturnNothing);
95gap> InstallMethod(SetAdditiveInverseImmutable, [cat, IsObject], ReturnNothing);
96gap> InstallMethod(SetOneImmutable, [cat, IsObject], ReturnNothing);
97gap> InstallMethod(SetInverseImmutable, [cat, IsObject], ReturnNothing);
98
99# ... and also involving TryNextMethod
100gap> for m in unary do InstallMethod(m, [cat], 1, function(x) TryNextMethod(); end); od;
101gap> for m in binary do InstallMethod(m, [cat, cat], 1, function(x,y) TryNextMethod(); end); od;
102
103#
104gap> 0*a;
105true
106gap> Zero(a);
107true
108gap> ZeroMutable(a);
109true
110gap> ZeroSameMutability(a);
111true
112gap> -a;
113true
114gap> AdditiveInverse(a);
115true
116gap> AdditiveInverseMutable(a);
117true
118gap> AdditiveInverseSameMutability(a);
119true
120gap> a^0;
121true
122gap> One(a);
123true
124gap> OneMutable(a);
125true
126gap> OneSameMutability(a);
127true
128gap> a^-1;
129true
130gap> Inverse(a);
131true
132gap> InverseMutable(a);
133true
134gap> InverseSameMutability(a);
135true
136gap> a = b;
137true
138gap> a < b;
139true
140gap> a in b;
141true
142gap> a + b;
143true
144gap> a - b;
145true
146gap> a * b;
147true
148gap> a / b;
149true
150gap> LeftQuotient(a, b);
151true
152gap> a ^ b;
153true
154gap> Comm(a, b);
155true
156gap> a mod b;
157true
158
159#
160gap> meths := Concatenation(unary, binary);;
161gap> TraceMethods(meths);
162
163#
164gap> 0*a;
165#I  *: zero integer * additive element with zero
166#I  ZeroSameMutability
167#I Trying next: ZeroSameMutability
168true
169gap> Zero(a);
170#I  ZeroImmutable
171#I Trying next: ZeroImmutable
172#I  SetZeroImmutable
173true
174gap> ZeroMutable(a);
175#I  ZeroMutable
176#I Trying next: ZeroMutable
177true
178gap> ZeroSameMutability(a);
179#I  ZeroSameMutability
180#I Trying next: ZeroSameMutability
181true
182gap> -a;
183#I  AdditiveInverseSameMutability
184#I Trying next: AdditiveInverseSameMutability
185true
186gap> AdditiveInverse(a);
187#I  AdditiveInverseImmutable
188#I Trying next: AdditiveInverseImmutable
189#I  SetAdditiveInverseImmutable
190true
191gap> AdditiveInverseMutable(a);
192#I  AdditiveInverseMutable
193#I Trying next: AdditiveInverseMutable
194true
195gap> AdditiveInverseSameMutability(a);
196#I  AdditiveInverseSameMutability
197#I Trying next: AdditiveInverseSameMutability
198true
199gap> a^0;
200#I  ^: for mult. element-with-one, and zero
201#I  OneSameMutability
202#I Trying next: OneSameMutability
203true
204gap> One(a);
205#I  OneImmutable
206#I Trying next: OneImmutable
207#I  SetOneImmutable
208true
209gap> OneMutable(a);
210#I  OneMutable
211#I Trying next: OneMutable
212true
213gap> OneSameMutability(a);
214#I  OneSameMutability
215#I Trying next: OneSameMutability
216true
217gap> a^-1;
218#I  ^: for mult. element-with-inverse, and negative integer
219#I  InverseSameMutability
220#I Trying next: InverseSameMutability
221true
222gap> Inverse(a);
223#I  InverseImmutable
224#I Trying next: InverseImmutable
225#I  SetInverseImmutable
226true
227gap> InverseMutable(a);
228#I  InverseMutable
229#I Trying next: InverseMutable
230true
231gap> InverseSameMutability(a);
232#I  InverseSameMutability
233#I Trying next: InverseSameMutability
234true
235gap> a = b;
236#I  =
237#I Trying next: =
238true
239gap> a < b;
240#I  <
241#I Trying next: <
242true
243gap> a in b;
244#I  in
245#I Trying next: in
246true
247gap> a + b;
248#I  +
249#I Trying next: +
250true
251gap> a - b;
252#I  -
253#I Trying next: -
254true
255gap> a * b;
256#I  *
257#I Trying next: *
258true
259gap> a / b;
260#I  /
261#I Trying next: /
262true
263gap> LeftQuotient(a, b);
264true
265gap> a ^ b;
266#I  ^
267#I Trying next: ^
268true
269gap> Comm(a, b);
270#I  Comm
271#I Trying next: Comm
272true
273gap> a mod b;
274#I  mod
275#I Trying next: mod
276true
277
278#
279gap> UntraceMethods(meths);
280
281#
282# test "method should have returned a value" checks
283#
284gap> for m in unary do InstallMethod(m, [cat], 2, ReturnNothing); od;
285gap> for m in binary do InstallMethod(m, [cat, cat], 2, ReturnNothing); od;
286
287#
288gap> 0*a;
289Error, ZEROOp: method should have returned a value
290gap> Zero(a);
291Error, Method for an attribute must return a value
292gap> ZeroMutable(a);
293Error, ZeroOp: method should have returned a value
294gap> ZeroSameMutability(a);
295Error, ZEROOp: method should have returned a value
296gap> -a;
297Error, AInvOp: method should have returned a value
298gap> AdditiveInverse(a);
299Error, Method for an attribute must return a value
300gap> AdditiveInverseMutable(a);
301Error, AdditiveInverseOp: method should have returned a value
302gap> AdditiveInverseSameMutability(a);
303Error, AInvOp: method should have returned a value
304gap> a^0;
305Error, ONEOp: method should have returned a value
306gap> One(a);
307Error, Method for an attribute must return a value
308gap> OneMutable(a);
309Error, OneOp: method should have returned a value
310gap> OneSameMutability(a);
311Error, ONEOp: method should have returned a value
312gap> a^-1;
313Error, INVOp: method should have returned a value
314gap> Inverse(a);
315Error, Method for an attribute must return a value
316gap> InverseMutable(a);
317Error, InvOp: method should have returned a value
318gap> InverseSameMutability(a);
319Error, INVOp: method should have returned a value
320gap> a = b;
321false
322gap> a < b;
323false
324gap> a in b;
325false
326gap> a + b;
327Error, SUM: method should have returned a value
328gap> a - b;
329Error, DIFF: method should have returned a value
330gap> a * b;
331Error, PROD: method should have returned a value
332gap> a / b;
333Error, QUO: method should have returned a value
334gap> LeftQuotient(a, b);
335Error, LeftQuotient: method should have returned a value
336gap> a ^ b;
337Error, POW: method should have returned a value
338gap> Comm(a, b);
339Error, Comm: method should have returned a value
340gap> a mod b;
341Error, mod: method should have returned a value
342
343#
344gap> meths := Concatenation(unary, binary);;
345gap> TraceMethods(meths);
346
347#
348gap> 0*a;
349#I  *: zero integer * additive element with zero
350#I  ZeroSameMutability
351Error, ZEROOp: method should have returned a value
352gap> Zero(a);
353#I  ZeroImmutable
354Error, Method for an attribute must return a value
355gap> ZeroMutable(a);
356#I  ZeroMutable
357Error, ZeroOp: method should have returned a value
358gap> ZeroSameMutability(a);
359#I  ZeroSameMutability
360Error, ZEROOp: method should have returned a value
361gap> -a;
362#I  AdditiveInverseSameMutability
363Error, AInvOp: method should have returned a value
364gap> AdditiveInverse(a);
365#I  AdditiveInverseImmutable
366Error, Method for an attribute must return a value
367gap> AdditiveInverseMutable(a);
368#I  AdditiveInverseMutable
369Error, AdditiveInverseOp: method should have returned a value
370gap> AdditiveInverseSameMutability(a);
371#I  AdditiveInverseSameMutability
372Error, AInvOp: method should have returned a value
373gap> a^0;
374#I  ^: for mult. element-with-one, and zero
375#I  OneSameMutability
376Error, ONEOp: method should have returned a value
377gap> One(a);
378#I  OneImmutable
379Error, Method for an attribute must return a value
380gap> OneMutable(a);
381#I  OneMutable
382Error, OneOp: method should have returned a value
383gap> OneSameMutability(a);
384#I  OneSameMutability
385Error, ONEOp: method should have returned a value
386gap> a^-1;
387#I  ^: for mult. element-with-inverse, and negative integer
388#I  InverseSameMutability
389Error, INVOp: method should have returned a value
390gap> Inverse(a);
391#I  InverseImmutable
392Error, Method for an attribute must return a value
393gap> InverseMutable(a);
394#I  InverseMutable
395Error, InvOp: method should have returned a value
396gap> InverseSameMutability(a);
397#I  InverseSameMutability
398Error, INVOp: method should have returned a value
399gap> a = b;
400#I  =
401false
402gap> a < b;
403#I  <
404false
405gap> a in b;
406#I  in
407false
408gap> a + b;
409#I  +
410Error, SUM: method should have returned a value
411gap> a - b;
412#I  -
413Error, DIFF: method should have returned a value
414gap> a * b;
415#I  *
416Error, PROD: method should have returned a value
417gap> a / b;
418#I  /
419Error, QUO: method should have returned a value
420gap> LeftQuotient(a, b);
421Error, LeftQuotient: method should have returned a value
422gap> a ^ b;
423#I  ^
424Error, POW: method should have returned a value
425gap> Comm(a, b);
426#I  Comm
427Error, Comm: method should have returned a value
428gap> a mod b;
429#I  mod
430Error, mod: method should have returned a value
431
432#
433gap> UntraceMethods(meths);
434
435#
436# restore previous state
437#
438gap> NEXT_VMETHOD_PRINT_INFO:=old_NEXT_VMETHOD_PRINT_INFO;;
439gap> VMETHOD_PRINT_INFO:=old_VMETHOD_PRINT_INFO;;
440gap> MakeReadOnlyGlobal("VMETHOD_PRINT_INFO");
441gap> MakeReadOnlyGlobal("NEXT_VMETHOD_PRINT_INFO");
442
443#
444gap> STOP_TEST("kernel/ariths.tst", 1);
445