Skip to content

Commit 75a914b

Browse files
committed
src/sage/libs/gap: move a few tests to pytest
There are two files, src/sage/libs/gap/test{,_long}.py that contain only tests for sage.libs.gap. These are doctests for no other reason than because we had no other way to run them in the past. This commit moves them to a new file, gap_test.py, that will be run by pytest rather than the doctest runner. In the process I've tried to explain what I think the tests are doing.
1 parent cb03043 commit 75a914b

File tree

4 files changed

+99
-82
lines changed

4 files changed

+99
-82
lines changed

src/sage/libs/gap/gap_test.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import pytest
2+
from sage.libs.gap.libgap import libgap
3+
4+
5+
@pytest.fixture
6+
def tmpfile():
7+
r"""
8+
Temporary file fixture that can be reopened/closed and still
9+
clean itself up afterwards.
10+
"""
11+
from tempfile import NamedTemporaryFile
12+
from os import unlink
13+
t = NamedTemporaryFile(delete=False)
14+
yield t
15+
unlink(t.name)
16+
17+
18+
def test_libgap_can_read_and_write_files(tmpfile):
19+
"""
20+
Test that libgap can write to a temporary file and
21+
subsequently read it.
22+
23+
See :issue:`16502`, :issue:`15833`.
24+
"""
25+
message = "Ceci n'est pas une groupe"
26+
libgap.PrintTo(tmpfile.name, message)
27+
with open(tmpfile.name) as f:
28+
contents = f.read()
29+
assert contents == message
30+
SystemFile = libgap.function_factory('StringFile')
31+
assert SystemFile(tmpfile.name) == libgap(message)
32+
33+
34+
def test_gc_loop_1():
35+
r"""
36+
First stress test for garbage collection in libgap.
37+
38+
Manually runs the GAP garbage collector, and then creates 10,000
39+
instances of the cyclic group of order two in a loop. In each
40+
iteration, the python variable is overwritten, meaning that python
41+
is free to garbage collect the object.
42+
"""
43+
libgap.collect()
44+
for _ in range(10000):
45+
G = libgap.CyclicGroup(2)
46+
assert True
47+
48+
49+
def test_gc_loop_2():
50+
r"""
51+
Second stress test for garbage collection in libgap.
52+
53+
Create the free group on two elements (``a`` and ``b``) and then
54+
construct a quotient group of order two in a loop by specifying
55+
some relations. The python variables are overwritten in each
56+
iteration, meaning that python is free to garbage-collect them.
57+
(We also ensure that the quotient group has the expected order.)
58+
59+
After that loop, we take one of the generators of the quotient
60+
group (from the final iteration), and compute its order in a
61+
loop. The python reference from the final iteration lives on,
62+
so this generator should not be collected.
63+
"""
64+
G = libgap.FreeGroup(2)
65+
a, b = G.GeneratorsOfGroup()
66+
two = libgap(2)
67+
68+
for _ in range(100):
69+
rel = libgap([a**2, b**2, a*b*a*b])
70+
H = G / rel
71+
H1 = H.GeneratorsOfGroup()[0]
72+
n = H1.Order()
73+
assert n == two
74+
75+
result = True
76+
for i in range(300000):
77+
n = libgap.Order(H1)
78+
result |= (n == two)
79+
assert result
80+
81+
82+
def test_gc_loop_3():
83+
r"""
84+
Third stress test for garbage collection in libgap.
85+
86+
Create the free group on two elements (``a`` and ``b``) and then
87+
add some of its elements to a list in a loop. A new, empty list is
88+
created at each iteration, so this mainly serves to guarantee that
89+
the generators of the group are not garbage-collected.
90+
"""
91+
G = libgap.FreeGroup(2)
92+
a, b = G.GeneratorsOfGroup()
93+
for _ in range(300000):
94+
lis = libgap([])
95+
lis.Add(a ** 2)
96+
lis.Add(b ** 2)
97+
lis.Add(b * a)
98+
assert True

src/sage/libs/gap/meson.build

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ py.install_sources(
88
'gap_functions.py',
99
'gap_globals.py',
1010
'gap_includes.pxd',
11+
'gap_test.py',
1112
'operations.py',
1213
'sage.gaprc',
1314
'saved_workspace.py',
14-
'test.py',
15-
'test_long.py',
1615
'util.pxd',
1716
subdir: 'sage/libs/gap',
1817
)

src/sage/libs/gap/test.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/sage/libs/gap/test_long.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)