|
| 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 |
0 commit comments