|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +""" |
| 4 | +Tests for memory lifetime safety of pyabacus bindings. |
| 5 | +
|
| 6 | +These tests verify that numpy arrays returned from C++ objects |
| 7 | +are safe to use even after the C++ object is destroyed (because |
| 8 | +they are copies, not views). |
| 9 | +""" |
| 10 | + |
| 11 | +import pytest |
| 12 | +import numpy as np |
| 13 | +from pyabacus import ModuleNAO as nao |
| 14 | + |
| 15 | + |
| 16 | +def test_array_lifetime_after_object_deletion(): |
| 17 | + """Test that arrays remain valid after C++ object deletion.""" |
| 18 | + chi = nao.NumericalRadial() |
| 19 | + |
| 20 | + # Build the object |
| 21 | + sz = 100 |
| 22 | + dr = 0.1 |
| 23 | + grid = np.array([i * dr for i in range(sz)], dtype=np.float64) |
| 24 | + values = np.exp(-grid) |
| 25 | + |
| 26 | + chi.build( |
| 27 | + l=0, |
| 28 | + for_r_space=True, |
| 29 | + ngrid=sz, |
| 30 | + grid=grid, |
| 31 | + value=values, |
| 32 | + p=-1, |
| 33 | + izeta=0, |
| 34 | + symbol="Test", |
| 35 | + itype=0, |
| 36 | + init_sbt=True |
| 37 | + ) |
| 38 | + |
| 39 | + # Get arrays |
| 40 | + rgrid = chi.rgrid |
| 41 | + rvalue = chi.rvalue |
| 42 | + |
| 43 | + # Save copies for comparison |
| 44 | + rgrid_copy = rgrid.copy() |
| 45 | + rvalue_copy = rvalue.copy() |
| 46 | + |
| 47 | + # Delete the C++ object |
| 48 | + del chi |
| 49 | + |
| 50 | + # Arrays should still be valid and contain correct data |
| 51 | + # (because they are copies, not views) |
| 52 | + np.testing.assert_array_equal(rgrid, rgrid_copy) |
| 53 | + np.testing.assert_array_equal(rvalue, rvalue_copy) |
| 54 | + |
| 55 | + |
| 56 | +def test_array_modification_isolation(): |
| 57 | + """Test that modifying returned arrays doesn't affect original data.""" |
| 58 | + chi = nao.NumericalRadial() |
| 59 | + |
| 60 | + sz = 100 |
| 61 | + dr = 0.1 |
| 62 | + grid = np.array([i * dr for i in range(sz)], dtype=np.float64) |
| 63 | + values = np.exp(-grid) |
| 64 | + |
| 65 | + chi.build( |
| 66 | + l=0, |
| 67 | + for_r_space=True, |
| 68 | + ngrid=sz, |
| 69 | + grid=grid, |
| 70 | + value=values |
| 71 | + ) |
| 72 | + |
| 73 | + # Get first array and modify it |
| 74 | + rgrid1 = chi.rgrid |
| 75 | + original_value = rgrid1[0] |
| 76 | + rgrid1[0] = 999.0 |
| 77 | + |
| 78 | + # Get second array - should have original value |
| 79 | + rgrid2 = chi.rgrid |
| 80 | + assert rgrid2[0] == original_value, "Modification should not affect original data" |
| 81 | + assert rgrid1[0] == 999.0, "Modified array should retain modification" |
| 82 | + |
| 83 | + |
| 84 | +def test_multiple_array_accesses(): |
| 85 | + """Test that multiple accesses return independent copies.""" |
| 86 | + chi = nao.NumericalRadial() |
| 87 | + |
| 88 | + sz = 50 |
| 89 | + dr = 0.2 |
| 90 | + grid = np.array([i * dr for i in range(sz)], dtype=np.float64) |
| 91 | + values = np.exp(-grid) |
| 92 | + |
| 93 | + chi.build( |
| 94 | + l=1, |
| 95 | + for_r_space=True, |
| 96 | + ngrid=sz, |
| 97 | + grid=grid, |
| 98 | + value=values |
| 99 | + ) |
| 100 | + |
| 101 | + # Get multiple arrays |
| 102 | + arrays = [chi.rgrid for _ in range(5)] |
| 103 | + |
| 104 | + # Modify each differently |
| 105 | + for i, arr in enumerate(arrays): |
| 106 | + arr[0] = float(i) |
| 107 | + |
| 108 | + # Verify each has its own modification |
| 109 | + for i, arr in enumerate(arrays): |
| 110 | + assert arr[0] == float(i), f"Array {i} should have value {i}" |
| 111 | + |
| 112 | + |
| 113 | +def test_radial_collection_array_lifetime(): |
| 114 | + """Test array lifetime for RadialCollection objects.""" |
| 115 | + orb_dir = '../../../tests/PP_ORB/' |
| 116 | + file_list = [orb_dir + "C_gga_8au_100Ry_2s2p1d.orb"] |
| 117 | + |
| 118 | + try: |
| 119 | + orb = nao.RadialCollection() |
| 120 | + orb.build(1, file_list, 'o') |
| 121 | + |
| 122 | + # Get a NumericalRadial from the collection |
| 123 | + nr = orb(0, 0, 0) |
| 124 | + |
| 125 | + # Get arrays from it |
| 126 | + rgrid = nr.rgrid |
| 127 | + rvalue = nr.rvalue |
| 128 | + |
| 129 | + # Save copies |
| 130 | + rgrid_copy = rgrid.copy() |
| 131 | + rvalue_copy = rvalue.copy() |
| 132 | + |
| 133 | + # Delete the collection (which owns the NumericalRadial) |
| 134 | + del orb |
| 135 | + |
| 136 | + # Arrays should still be valid |
| 137 | + np.testing.assert_array_equal(rgrid, rgrid_copy) |
| 138 | + np.testing.assert_array_equal(rvalue, rvalue_copy) |
| 139 | + except (FileNotFoundError, RuntimeError): |
| 140 | + pytest.skip("Orbital files not available for testing") |
| 141 | + |
| 142 | + |
| 143 | +def test_empty_array_handling(): |
| 144 | + """Test handling of arrays when k-space is not initialized.""" |
| 145 | + chi = nao.NumericalRadial() |
| 146 | + |
| 147 | + sz = 50 |
| 148 | + dr = 0.2 |
| 149 | + grid = np.array([i * dr for i in range(sz)], dtype=np.float64) |
| 150 | + values = np.exp(-grid) |
| 151 | + |
| 152 | + chi.build( |
| 153 | + l=0, |
| 154 | + for_r_space=True, |
| 155 | + ngrid=sz, |
| 156 | + grid=grid, |
| 157 | + value=values, |
| 158 | + init_sbt=False # Don't initialize SBT, so k-space won't be set |
| 159 | + ) |
| 160 | + |
| 161 | + # r-space should be valid |
| 162 | + assert chi.nr == sz |
| 163 | + rgrid = chi.rgrid |
| 164 | + assert len(rgrid) == sz |
| 165 | + |
| 166 | + # k-space should be empty (nk == 0) |
| 167 | + assert chi.nk == 0 |
| 168 | + kgrid = chi.kgrid |
| 169 | + assert len(kgrid) == 0 |
0 commit comments