Skip to content

Commit 1ad239b

Browse files
committed
update tests
1 parent 99d58b8 commit 1ad239b

1 file changed

Lines changed: 46 additions & 72 deletions

File tree

‎tests/tests.py‎

Lines changed: 46 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
Image, ImageU8, Fill, Filter, Adjust, Transform, Resize, Blend, Effect,
88
load, save, upload, download, copy, convert_float_to_u8, convert_u8_to_float
99
)
10-
from pyimagecuda.utils import ensure_capacity
1110

1211

1312
# ============================================================================
@@ -131,57 +130,51 @@ class TestBufferCapacity:
131130

132131
def test_get_max_capacity(self):
133132
img = Image(1024, 768)
134-
max_w, max_h = img.get_max_capacity()
133+
max_pixels = img.get_max_capacity()
135134

136-
assert max_w == 1024
137-
assert max_h == 768
135+
assert max_pixels == 1024 * 768
138136
img.free()
139137

140138
def test_resize_within_capacity(self):
141139
img = Image(1024, 1024)
142140

143-
img.width = 512
144-
img.height = 512
141+
img.resize(512, 512)
145142

146143
assert img.width == 512
147144
assert img.height == 512
148145

149-
max_w, max_h = img.get_max_capacity()
150-
assert max_w == 1024
151-
assert max_h == 1024
146+
max_pixels = img.get_max_capacity()
147+
assert max_pixels == 1024 * 1024
152148

153149
img.free()
154150

155151
def test_resize_exceeds_capacity_fails(self):
156152
img = Image(512, 512)
157153

158-
with pytest.raises(ValueError, match="exceeds buffer capacity"):
159-
img.width = 1024
160-
161-
with pytest.raises(ValueError, match="exceeds buffer capacity"):
162-
img.height = 1024
154+
with pytest.raises(ValueError, match="exceed buffer capacity"):
155+
img.resize(1024, 1024)
163156

164157
img.free()
165158

166159
def test_resize_to_zero_fails(self):
167160
img = Image(512, 512)
168161

169162
with pytest.raises(ValueError, match="must be positive"):
170-
img.width = 0
163+
img.resize(0, 512)
171164

172165
with pytest.raises(ValueError, match="must be positive"):
173-
img.height = 0
166+
img.resize(512, 0)
174167

175168
img.free()
176169

177170
def test_resize_to_negative_fails(self):
178171
img = Image(512, 512)
179172

180173
with pytest.raises(ValueError, match="must be positive"):
181-
img.width = -100
174+
img.resize(-100, 512)
182175

183176
with pytest.raises(ValueError, match="must be positive"):
184-
img.height = -100
177+
img.resize(512, -100)
185178

186179
img.free()
187180

@@ -191,12 +184,33 @@ def test_multiple_resizes(self):
191184
sizes = [(512, 512), (256, 768), (1024, 512), (100, 100), (1024, 1024)]
192185

193186
for w, h in sizes:
194-
img.width = w
195-
img.height = h
187+
img.resize(w, h)
196188
assert img.width == w
197189
assert img.height == h
198190

199191
img.free()
192+
193+
def test_resize_different_aspect_ratios(self):
194+
img = Image(1920, 1080)
195+
max_pixels = 1920 * 1080
196+
197+
# Same total pixels, different aspect ratios
198+
img.resize(3840, 540)
199+
assert img.width == 3840
200+
assert img.height == 540
201+
assert img.get_max_capacity() == max_pixels
202+
203+
img.resize(2160, 960)
204+
assert img.width == 2160
205+
assert img.height == 960
206+
assert img.get_max_capacity() == max_pixels
207+
208+
img.resize(1080, 1920)
209+
assert img.width == 1080
210+
assert img.height == 1920
211+
assert img.get_max_capacity() == max_pixels
212+
213+
img.free()
200214

201215

202216
# ============================================================================
@@ -206,8 +220,7 @@ def test_multiple_resizes(self):
206220
class TestBufferReuse:
207221

208222
def test_buffer_reuse_same_size(self, reusable_buffer):
209-
reusable_buffer.width = 512
210-
reusable_buffer.height = 512
223+
reusable_buffer.resize(512, 512)
211224
Fill.color(reusable_buffer, (1.0, 0.0, 0.0, 1.0))
212225

213226
Fill.color(reusable_buffer, (0.0, 1.0, 0.0, 1.0))
@@ -219,8 +232,7 @@ def test_buffer_reuse_different_sizes(self, reusable_buffer):
219232
sizes = [(512, 512), (256, 768), (1024, 512), (100, 100)]
220233

221234
for w, h in sizes:
222-
reusable_buffer.width = w
223-
reusable_buffer.height = h
235+
reusable_buffer.resize(w, h)
224236
Fill.color(reusable_buffer, (1.0, 0.0, 0.0, 1.0))
225237

226238
assert reusable_buffer.width == w
@@ -229,8 +241,7 @@ def test_buffer_reuse_different_sizes(self, reusable_buffer):
229241
def test_buffer_reuse_with_operations(self, reusable_buffer):
230242
for i in range(10):
231243
size = 256 + i * 64
232-
reusable_buffer.width = size
233-
reusable_buffer.height = size
244+
reusable_buffer.resize(size, size)
234245

235246
Fill.color(reusable_buffer, (1.0, 0.0, 0.0, 1.0))
236247
Filter.invert(reusable_buffer)
@@ -239,8 +250,7 @@ def test_buffer_reuse_with_operations(self, reusable_buffer):
239250

240251
def test_buffer_reuse_stress(self, reusable_buffer):
241252
for _ in range(1000):
242-
reusable_buffer.width = 512
243-
reusable_buffer.height = 512
253+
reusable_buffer.resize(512, 512)
244254
Fill.color(reusable_buffer, (1.0, 0.0, 0.0, 1.0))
245255

246256

@@ -265,7 +275,7 @@ def test_no_leak_on_failed_operations(self):
265275

266276
for _ in range(100):
267277
try:
268-
img.width = 9999
278+
img.resize(9999, 9999)
269279
except ValueError:
270280
pass
271281

@@ -308,8 +318,7 @@ def test_operations_with_new_image(self, medium_image):
308318
blurred.free()
309319

310320
def test_operations_with_dst_buffer(self, medium_image, reusable_buffer):
311-
reusable_buffer.width = medium_image.width
312-
reusable_buffer.height = medium_image.height
321+
reusable_buffer.resize(medium_image.width, medium_image.height)
313322

314323
result = Filter.gaussian_blur(medium_image, radius=5, dst_buffer=reusable_buffer)
315324

@@ -326,38 +335,6 @@ def test_buffer_reuse_across_operations(self, medium_image, reusable_buffer):
326335
assert reusable_buffer.width == medium_image.width
327336

328337

329-
# ============================================================================
330-
# BUFFER VALIDATION
331-
# ============================================================================
332-
333-
class TestBufferValidation:
334-
335-
def test_insufficient_buffer_capacity_fails(self):
336-
small_buffer = Image(256, 256)
337-
338-
with pytest.raises(ValueError, match="capacity too small"):
339-
ensure_capacity(small_buffer, 512, 512)
340-
341-
small_buffer.free()
342-
343-
def test_ensure_capacity_updates_dimensions(self):
344-
buffer = Image(1024, 1024)
345-
ensure_capacity(buffer, 512, 512)
346-
347-
assert buffer.width == 512
348-
assert buffer.height == 512
349-
350-
buffer.free()
351-
352-
def test_operations_validate_buffer_capacity(self, small_image):
353-
tiny_buffer = Image(32, 32)
354-
355-
with pytest.raises(ValueError):
356-
Filter.gaussian_blur(small_image, radius=5, dst_buffer=tiny_buffer)
357-
358-
tiny_buffer.free()
359-
360-
361338
# ============================================================================
362339
# IO MEMORY
363340
# ============================================================================
@@ -437,7 +414,7 @@ def test_copy_requires_sufficient_capacity(self):
437414
src = Image(512, 512)
438415
dst = Image(256, 256)
439416

440-
with pytest.raises(ValueError, match="capacity too small"):
417+
with pytest.raises(ValueError, match="exceed buffer capacity"):
441418
copy(dst, src)
442419

443420
src.free()
@@ -469,6 +446,7 @@ def test_conversion_updates_dimensions(self):
469446
f32 = Image(256, 256)
470447
u8 = ImageU8(1024, 1024)
471448

449+
u8.resize(256, 256)
472450
convert_float_to_u8(u8, f32)
473451

474452
assert u8.width == 256
@@ -497,9 +475,8 @@ def test_invalid_blend_anchor(self, image_pair):
497475
Fill.color(base, (1.0, 0.0, 0.0, 1.0))
498476
Fill.color(overlay, (0.0, 1.0, 0.0, 1.0))
499477

500-
501478
Blend.normal(base, overlay, anchor='invalid')
502-
479+
503480
assert base.width == 512
504481

505482
def test_invalid_gradient_direction(self, medium_image):
@@ -657,8 +634,7 @@ def test_buffer_reuse_doesnt_leak(self):
657634
buffer = Image(1024, 1024)
658635

659636
for i in range(1000):
660-
buffer.width = 512 + (i % 100)
661-
buffer.height = 512 + (i % 100)
637+
buffer.resize(512 + (i % 100), 512 + (i % 100))
662638
Fill.color(buffer, (1.0, 0.0, 0.0, 1.0))
663639

664640
buffer.free()
@@ -798,8 +774,7 @@ def test_batch_load_save_with_buffers(self):
798774
tmp_path = tmp.name
799775
files.append(tmp_path)
800776

801-
f32_buffer.width = 256
802-
f32_buffer.height = 256
777+
f32_buffer.resize(256, 256)
803778
Fill.color(f32_buffer, (float(i) / 10, 0.5, 0.5, 1.0))
804779
save(f32_buffer, tmp_path, u8_buffer=u8_buffer)
805780

@@ -829,8 +804,7 @@ def test_stress_buffer_dimension_changes(self):
829804
for i in range(10000):
830805
w = 64 + (i % 1984)
831806
h = 64 + (i % 1984)
832-
buffer.width = w
833-
buffer.height = h
807+
buffer.resize(w, h)
834808
Fill.color(buffer, (1.0, 0.0, 0.0, 1.0))
835809

836810
buffer.free()

0 commit comments

Comments
 (0)