1gap> START_TEST("weakptr.tst");
2
3#
4# Low level access functions
5#
6gap> w := WeakPointerObj([1,,2^40*10^10,Z(17),[2,3,4],fail,SymmetricGroup(5),]);;
7gap> Print(w,"\n");
8WeakPointerObj( [ 1, , 10995116277760000000000, Z(17),
9[ 2, 3, 4 ], fail, SymmetricGroup( [ 1 .. 5 ] ) ] )
10gap> IsWeakPointerObject(w);
11true
12gap> IsWeakPointerObject([1, 2, 3]);
13false
14gap> LengthWPObj(w);
157
16gap> w[8];
17Error, <wpobj>[<pos>] must have a value
18gap> val := "cheese";;
19gap> GetWithDefault(w, 1, "cheese");
201
21gap> IsIdenticalObj(val, GetWithDefault(w, 2, val));
22true
23gap> IsIdenticalObj(val, GetWithDefault(w, 8, val));
24true
25gap> List([1..7],x->IsBoundElmWPObj(w,x));
26[ true, false, true, true, true, true, true ]
27gap> List([1..7],x->ElmWPObj(w,x));
28[ 1, fail, 10995116277760000000000, Z(17), [ 2, 3, 4 ], fail,
29  Sym( [ 1 .. 5 ] ) ]
30gap> SetElmWPObj(w,9,[]);
31gap> Print(w,"\n");
32WeakPointerObj( [ 1, , 10995116277760000000000, Z(17),
33[ 2, 3, 4 ], fail, SymmetricGroup( [ 1 .. 5 ] ), , [  ] ] )
34gap> UnbindElmWPObj(w,4);
35gap> Print(w,"\n");
36WeakPointerObj( [ 1, , 10995116277760000000000, ,
37[ 2, 3, 4 ], fail, SymmetricGroup( [ 1 .. 5 ] ), , [  ] ] )
38gap> UnbindElmWPObj(w,9); LengthWPObj(w);
397
40gap> 1;;2;;3;;
41gap> truevec := [1,,2^40*10^10,(1,5,6),[2,3,4],fail,SymmetricGroup(5),];;
42gap> wcopies := List([1..1000], x -> WeakPointerObj([1,,2^40*10^10,(1,5,6),[2,3,4],fail,SymmetricGroup(5),]));;
43gap> GASMAN("collect");
44gap> ForAll(wcopies, x -> LengthWPObj(x) = 6 or LengthWPObj(x) = 7);
45true
46gap> ForAny(wcopies, x -> LengthWPObj(x) = 6);
47true
48gap> ForAll(wcopies, x -> x[1] = 1 and x[6] = fail);
49true
50gap> ForAll([2,3,4,5,7], x -> ForAny(wcopies, y -> not(IsBound(y[x]))));
51true
52gap> ForAll([3,4,5,7], x -> ForAny(wcopies, y -> not(IsBound(y[x])) or y[x] = truevec[x]));
53true
54gap> # Take a filtered list
55gap> w := First(wcopies, x -> ForAll([2,3,4,5,7], y -> not(IsBound(x[y])) ) );;
56gap> Print(w,"\n");
57WeakPointerObj( [ 1, , , , , fail ] )
58gap> LengthWPObj(w);
596
60gap> Print(ShallowCopy(w),"\n");
61WeakPointerObj( [ 1, , , , , fail ] )
62gap> List([1..8], x -> GetWithDefault(w, x, -1));
63[ 1, -1, -1, -1, -1, fail, -1, -1 ]
64gap> GetWithDefault(w, 1, "cheese");
651
66gap> IsIdenticalObj(val, GetWithDefault(w, 2, val));
67true
68gap> IsIdenticalObj(val, GetWithDefault(w, 8, val));
69true
70
71#
72# Access as lists
73#
74gap> w[1];
751
76gap> l := [ 311 ];; # keep ref so that this list is not garbage collected
77gap> w{[2..4]} := [[1,2],E(5),l];
78[ [ 1, 2 ], E(5), [ 311 ] ]
79gap> Print(w,"\n");
80WeakPointerObj( [ 1, [ 1, 2 ], E(5), [ 311 ], , fail ] )
81gap> Print(StructuralCopy(w),"\n");
82WeakPointerObj( [ 1, [ 1, 2 ], E(5), [ 311 ], , fail ] )
83gap> Immutable(w);
84[ 1, [ 1, 2 ], E(5), [ 311 ],, fail ]
85gap> IsBound(w[2]);
86true
87gap> GASMAN("collect");
88gap> IsBound(w[5]);
89false
90gap> Unbind(w[2]);
91gap> Print(w,"\n");
92WeakPointerObj( [ 1, , E(5), [ 311 ], , fail ] )
93gap> Immutable(w);
94[ 1,, E(5), [ 311 ],, fail ]
95gap> w;
96WeakPointerObj( [ 1, , E(5), [ 311 ], , fail ] )
97gap> MakeImmutable(w);
98[ 1,, E(5), [ 311 ],, fail ]
99gap> w;
100[ 1,, E(5), [ 311 ],, fail ]
101gap> IsMutable(w);
102false
103gap> ForAny(w, IsMutable);
104false
105
106#
107# test recursive MakeImmutable
108#
109gap> w := WeakPointerObj([ ~ ]);;
110gap> MakeImmutable(w);
111[ [ ~[1] ] ]
112
113#
114gap> w := WeakPointerObj([ 1 ]);;
115gap> w[1] := w;;
116gap> MakeImmutable(w);
117[ ~ ]
118
119#
120gap> w := WeakPointerObj([ rec() ]);;
121gap> w[1].self := w;;
122gap> MakeImmutable(w);
123[ rec( self := ~ ) ]
124
125#
126gap> STOP_TEST( "weakptr.tst", 1);
127