|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import blosc2 |
| 5 | + |
| 6 | +# ---------------------------------- |
| 7 | +# Helpers |
| 8 | +# ---------------------------------- |
| 9 | + |
| 10 | +UNICODE_VALUES = [ |
| 11 | + "café", |
| 12 | + "β", |
| 13 | + "こんにちは", |
| 14 | + "mañana", |
| 15 | + "добрый", |
| 16 | + "数据", |
| 17 | +] |
| 18 | + |
| 19 | + |
| 20 | +def make_unicode_array(shape, maxlen=16): |
| 21 | + """ |
| 22 | + Create a NumPy Unicode array with non-ASCII content. |
| 23 | + dtype='U' uses fixed-width UTF-32 internally. |
| 24 | + """ |
| 25 | + total = np.prod(shape) |
| 26 | + data = [UNICODE_VALUES[i % len(UNICODE_VALUES)] for i in range(total)] |
| 27 | + return np.array(data, dtype=f"U{maxlen}").reshape(shape) |
| 28 | + |
| 29 | + |
| 30 | +# ---------------------------------- |
| 31 | +# Parameter grids |
| 32 | +# ---------------------------------- |
| 33 | + |
| 34 | +SHAPES = [ |
| 35 | + (12,), |
| 36 | + (12, 6), |
| 37 | + (3, 4, 5), |
| 38 | +] |
| 39 | + |
| 40 | +CHUNKS = [ |
| 41 | + None, |
| 42 | + (4,), |
| 43 | + (7, 4), |
| 44 | + (2, 2, 5), |
| 45 | +] |
| 46 | + |
| 47 | +BLOCKS = [ |
| 48 | + None, |
| 49 | + (3,), |
| 50 | + (3, 3), |
| 51 | + (1, 2, 5), |
| 52 | +] |
| 53 | + |
| 54 | + |
| 55 | +# ---------------------------------- |
| 56 | +# In-memory tests |
| 57 | +# ---------------------------------- |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize("shape", SHAPES) |
| 61 | +@pytest.mark.parametrize("chunks", CHUNKS) |
| 62 | +@pytest.mark.parametrize("blocks", BLOCKS) |
| 63 | +def test_unicode_roundtrip_in_memory(shape, chunks, blocks): |
| 64 | + arr = make_unicode_array(shape) |
| 65 | + |
| 66 | + b2 = blosc2.asarray( |
| 67 | + arr, |
| 68 | + chunks=chunks if chunks and len(chunks) == arr.ndim else None, |
| 69 | + blocks=blocks if blocks and len(blocks) == arr.ndim else None, |
| 70 | + ) |
| 71 | + |
| 72 | + assert b2.dtype == arr.dtype |
| 73 | + assert np.array_equal(b2, arr) |
| 74 | + |
| 75 | + |
| 76 | +def test_unicode_indexing_and_slicing(): |
| 77 | + arr = make_unicode_array((10,)) |
| 78 | + b2 = blosc2.asarray(arr, chunks=(6,), blocks=(4,)) |
| 79 | + |
| 80 | + assert b2[0] == arr[0] |
| 81 | + assert b2[5] == arr[5] |
| 82 | + assert np.array_equal(b2[2:8], arr[2:8]) |
| 83 | + assert np.array_equal(b2[::2], arr[::2]) |
| 84 | + |
| 85 | + |
| 86 | +def test_unicode_multidimensional_slice(): |
| 87 | + arr = make_unicode_array((6, 8)) |
| 88 | + b2 = blosc2.asarray(arr, chunks=(3, 4), blocks=(1, 4)) |
| 89 | + |
| 90 | + assert np.array_equal( |
| 91 | + b2[1:5, 2:7], |
| 92 | + arr[1:5, 2:7], |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +def test_unicode_partial_assignment(): |
| 97 | + arr = make_unicode_array((10,)) |
| 98 | + b2 = blosc2.asarray(arr) |
| 99 | + |
| 100 | + new_vals = np.array(["Ω", "λ", "plo"], dtype=arr.dtype) |
| 101 | + b2[3:6] = new_vals |
| 102 | + arr[3:6] = new_vals |
| 103 | + |
| 104 | + assert np.array_equal(b2, arr) |
| 105 | + |
| 106 | + |
| 107 | +# ---------------------------------- |
| 108 | +# On-disk tests |
| 109 | +# ---------------------------------- |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.parametrize("shape", SHAPES) |
| 113 | +def test_unicode_roundtrip_on_disk(tmp_path, shape): |
| 114 | + arr = make_unicode_array(shape) |
| 115 | + |
| 116 | + path = tmp_path / "unicode_array.b2nd" |
| 117 | + |
| 118 | + b2 = blosc2.asarray( |
| 119 | + arr, |
| 120 | + urlpath=path, |
| 121 | + mode="w", |
| 122 | + chunks=tuple(max(1, s // 2) for s in shape), |
| 123 | + blocks=tuple(1 for _ in shape), |
| 124 | + ) |
| 125 | + |
| 126 | + # Re-open from disk |
| 127 | + out = blosc2.open(path) |
| 128 | + |
| 129 | + assert out.dtype == arr.dtype |
| 130 | + assert np.array_equal(out, arr) |
| 131 | + |
| 132 | + |
| 133 | +def test_unicode_on_disk_partial_io(tmp_path): |
| 134 | + arr = make_unicode_array((20,)) |
| 135 | + path = tmp_path / "partial_unicode.b2nd" |
| 136 | + |
| 137 | + b2 = blosc2.asarray( |
| 138 | + arr, |
| 139 | + urlpath=path, |
| 140 | + mode="w", |
| 141 | + chunks=(5,), |
| 142 | + blocks=(2,), |
| 143 | + ) |
| 144 | + |
| 145 | + # Partial read |
| 146 | + assert np.array_equal(b2[4:12], arr[4:12]) |
| 147 | + |
| 148 | + # Partial write |
| 149 | + replacement = np.array( |
| 150 | + ["python", "is", "good", "!"], |
| 151 | + dtype=arr.dtype, |
| 152 | + ) |
| 153 | + b2[6:10] = replacement |
| 154 | + arr[6:10] = replacement |
| 155 | + |
| 156 | + reopened = blosc2.open(path) |
| 157 | + assert np.array_equal(reopened, arr) |
| 158 | + |
| 159 | + |
| 160 | +def test_unicode_on_disk_persistence(tmp_path): |
| 161 | + path = tmp_path / "persistent_unicode.b2nd" |
| 162 | + |
| 163 | + arr1 = make_unicode_array((8,)) |
| 164 | + blosc2.asarray(arr1, urlpath=path, mode="w") |
| 165 | + |
| 166 | + arr2 = make_unicode_array((8,)) |
| 167 | + b2 = blosc2.open(path, mode="a") |
| 168 | + b2[:] = arr2 |
| 169 | + |
| 170 | + reopened = blosc2.open(path) |
| 171 | + assert np.array_equal(reopened, arr2) |
0 commit comments