File: callbacks.test

package info (click to toggle)
regina-normal 7.4.1-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 154,244 kB
  • sloc: cpp: 295,026; xml: 9,992; sh: 1,344; python: 1,225; perl: 616; ansic: 138; makefile: 26
file content (279 lines) | stat: -rw-r--r-- 9,976 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# Regina - A Normal Surface Theory Calculator
# Python Test Suite Component
#
# Copyright (c) 2015-2025, Ben Burton
# For further details contact Ben Burton (bab@debian.org).
#
# Tests the use of pure Python functions as C++ callbacks.
#
# This file is a single component of Regina's python test suite.  To run
# the python test suite, move to the main python directory in the source
# tree and run "make check".
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# As an exception, when this program is distributed through (i) the
# App Store by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or
# (iii) Google Play by Google Inc., then that store may impose any
# digital rights management, device limits and/or redistribution
# restrictions that are required by its terms of service.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

########################################################################
# Callback actions that we pass to retriangulate() or rewrite():
########################################################################

def display(sig, tri):
    # A callback that uses both sig and tri arguments.
    print('    ' + sig, tri.size())
    return False

sigs = None
def collect(sig, tri):
    # A callback that uses a global variable.
    global sigs
    sigs.append(sig)

count = None
def stopAfter10(sig, tri):
    # A callback that uses a global variable, and that also uses the
    # return value to terminate the search.
    global count
    count = count + 1
    return (count >= 10)

########################################################################
# Callback actions that we pass to enumerateCovers():
########################################################################

def displayCover(tri, type):
    t = Triangulation3(tri)
    t.simplify()
    print('    ' + str(type) + ': ' + t.homology().str())

########################################################################
# Callback actions that we pass to isomorphism searches:
########################################################################

isoCount = None
def stopAfter5Iso(iso):
    # A callback that uses a global variable, and that also uses the
    # return value to terminate the search.
    global isoCount
    isoCount = isoCount + 1
    return (isoCount >= 5)

def displayIso(iso):
    print(iso.detail())
    return False

########################################################################
# Full tests that we can run on a triangulation or link:
########################################################################

def printBanner(name, test, height, threads):
    print(name + ' -> ' + test + ', height = ' + str(height) + \
        ', threads = ' + str(threads))

def testDisplay(obj, name, height, threads):
    printBanner(name, 'display', height, threads)
    if obj.__class__ == Link:
        obj.rewrite(height, threads, display)
    else:
        obj.retriangulate(height, threads, display)

def testStopAfter10(obj, name, height, threads):
    printBanner(name, 'stop after 10', height, threads)
    global count
    count = 0
    if obj.__class__ == Link:
        ans = obj.rewrite(height, threads, stopAfter10)
    else:
        ans = obj.retriangulate(height, threads, stopAfter10)
    if ans:
        print('    search terminated early, count:', count)
    else:
        print('    search ran to completion, count:', count)

def testCollect(obj, name, height, threads):
    printBanner(name, 'collect', height, threads)
    global sigs
    sigs = []
    if obj.__class__ == Link:
        ans = obj.rewrite(height, threads, collect)
    else:
        ans = obj.retriangulate(height, threads, collect)
    sigs.sort()
    for i in sigs:
        print('    ' + i)

def testCovers(link, name, sheets):
    print(name + ' -> all covers, sheets = ' + str(sheets))
    tri = SnapPeaTriangulation(link.complement())
    tri.enumerateCovers(sheets, SnapPeaTriangulation.CoverEnumeration.All, displayCover)

def testIso(inner, innerName, outer, outerName, countOnly = False):
    comp = innerName + ' vs ' + outerName
    if not countOnly:
        print(comp + ' -> isomorphisms, display')
        inner.findAllIsomorphisms(outer, displayIso)
        print(comp + ' -> subcomplexes, display')
        inner.findAllSubcomplexesIn(outer, displayIso)
    
    global isoCount
    print(comp + ' -> isomorphisms, stop after 5')
    isoCount = 0
    ans = inner.findAllIsomorphisms(outer, stopAfter5Iso)
    if ans:
        print('    search terminated early, count:', isoCount)
    else:
        print('    search ran to completion, count:', isoCount)
    print(comp + ' -> subcomplexes, stop after 5')
    
    isoCount = 0
    ans = inner.findAllSubcomplexesIn(outer, stopAfter5Iso)
    if ans:
        print('    search terminated early, count:', isoCount)
    else:
        print('    search ran to completion, count:', isoCount)

########################################################################
# Individual test cases:
########################################################################

tri = Example3.poincare()
testDisplay(tri, 'Poincare homology sphere', 1, 1)
testDisplay(tri, 'Poincare homology sphere', 1, 2)
testStopAfter10(tri, 'Poincare homology sphere', 1, 1)
testStopAfter10(tri, 'Poincare homology sphere', 1, 2)
testCollect(tri, 'Poincare homology sphere', 1, 1)
testCollect(tri, 'Poincare homology sphere', 1, 2)
testIso(tri, 'Poincare homology sphere', tri, 'Poincare homology sphere', True)
print()

tri = Example3.weeks()
testDisplay(tri, 'Weeks manifold', 0, 1)
testDisplay(tri, 'Weeks manifold', 0, 2)
testStopAfter10(tri, 'Weeks manifold', 1, 1)
testStopAfter10(tri, 'Weeks manifold', 1, 2)
testCollect(tri, 'Weeks manifold', 0, 1)
testCollect(tri, 'Weeks manifold', 0, 2)
testIso(tri, 'Weeks manifold', tri, 'Weeks manifold')
print()

tri = Example4.cappellShaneson()
testDisplay(tri, 'Cappell-Shaneson knot complement', 0, 1)
testDisplay(tri, 'Cappell-Shaneson knot complement', 0, 2)
testStopAfter10(tri, 'Cappell-Shaneson knot complement', 2, 1)
testStopAfter10(tri, 'Cappell-Shaneson knot complement', 2, 2)
testCollect(tri, 'Cappell-Shaneson knot complement', 2, 1)
testCollect(tri, 'Cappell-Shaneson knot complement', 2, 2)
testIso(tri, 'Cappell-Shaneson knot complement', tri, 'Cappell-Shaneson knot complement')
print()

inner = Example3.lst(1,2)
outer = Example3.lens(8,3)
testIso(inner, 'LST(1,2,3)', outer, 'L(8,3)')
print()

link = ExampleLink.trefoil()
testDisplay(link, 'Trefoil', 0, 1)
testDisplay(link, 'Trefoil', 0, 2)
print()

link = ExampleLink.figureEight()
testStopAfter10(link, 'Figure eight knot', 1, 1)
testStopAfter10(link, 'Figure eight knot', 1, 2)
testCollect(link, 'Figure eight knot', 1, 1)
testCollect(link, 'Figure eight knot', 1, 2)
print()

link = ExampleLink.monster()
testStopAfter10(link, 'Monster unknot', 1, 1)
testStopAfter10(link, 'Monster unknot', 1, 2)
print()

link = ExampleLink.trefoil()
testCovers(link, 'Trefoil', 2)
testCovers(link, 'Trefoil', 3)
testCovers(link, 'Trefoil', 4)
testCovers(link, 'Trefoil', 5)
print()

########################################################################
# Test facet pairing enumeration:
########################################################################

def displayPairing(f, autos):
    print('    ' + f.str(), ' -> automorphisms: ', len(autos))

print('Enumerating facet pairings:')
FacetPairing3.findAllPairings(3, BoolSet(False), 0, displayPairing)
FacetPairing5.findAllPairings(2, BoolSet(False), 0, displayPairing)
print()

########################################################################
# Test splitting surface signature enumeration:
########################################################################

def displaySig(s, autos):
    print('    ' + s.str(), ' -> automorphisms: ', len(autos))

print('Enumerating splitting surface signatures:')
dummy = SigCensus.formCensus(3, displaySig)
print()

########################################################################
# Test enumeration of knots:
########################################################################

def displayKnot(k):
    print('    ' + k.brief())

print('Enumerating knots:')
g = ModelLinkGraph.fromPlantri('bcdd,aeec,abfd,acfa,bffb,ceed')
g.generateMinimalLinks(displayKnot)
print()

########################################################################
# Test location of saturated regions:
########################################################################

def displayRegion(r):
    print(r.detail())
    s = SatRegion(r) # this is a non-trivial copy constructor
    if r.detail() != s.detail():
        print('ERROR - Does not match:\n' + s.detail())

print('Finding regions:')
t = Triangulation3.rehydrate('jofdiaabcceffhiiiedkgiilpsw')
dummy = SatRegion.find(t, False, displayRegion)
print()

########################################################################
# Test the non-callback version of SnapPeaTriangulation::enumerateCovers():
########################################################################

# Now that triangulations are not packets, we cannot use packet listeners
# to verify the lifetimes of the covers that are returned.  For now just
# test that the routine seems to do the right thing.

print('Enumerating covers:')
c = [ (a[0].isoSig(), a[1]) for a in \
    ExampleSnapPea.whiteheadLink().enumerateCovers(3, \
    SnapPeaTriangulation.CoverEnumeration.All) ]
c.sort()
for i in c:
    print('  ' + i[0], i[1])

print()