1
+ import numpy as np
2
+ import pytest
3
+
4
+ from ZooProcess_lib .ROI import ROI , unique_visible_key , box_from_key
5
+
6
+
7
+ def test_unique_visible_key ():
8
+ """Test that unique_visible_key generates the expected string format."""
9
+ # Create a simple ROI
10
+ mask = np .ones ((40 , 30 ), dtype = np .uint8 ) # height=40, width=30
11
+ roi = ROI (x = 10 , y = 20 , mask = mask )
12
+
13
+ # Generate the key
14
+ key = unique_visible_key (roi )
15
+
16
+ # Check the format and values
17
+ assert key == "xAy14w1Eh28" # 10 in hex is A, 20 is 14, 30 is 1E, 40 is 28
18
+
19
+
20
+ def test_box_from_key ():
21
+ """Test that box_from_key correctly parses the key string."""
22
+ # Test with a simple key
23
+ key = "xAy14w1Eh28"
24
+ x , y , width , height = box_from_key (key )
25
+
26
+ assert x == 10
27
+ assert y == 20
28
+ assert width == 30
29
+ assert height == 40
30
+
31
+
32
+ def test_roundtrip_conversion ():
33
+ """Test that converting from ROI to key and back gives the original values."""
34
+ # Create a simple ROI
35
+ mask = np .ones ((40 , 30 ), dtype = np .uint8 ) # height=40, width=30
36
+ roi = ROI (x = 10 , y = 20 , mask = mask )
37
+
38
+ # Convert to key and back
39
+ key = unique_visible_key (roi )
40
+ x , y , width , height = box_from_key (key )
41
+
42
+ # Check that we got the original values back
43
+ assert x == roi .x
44
+ assert y == roi .y
45
+ assert width == roi .mask .shape [1 ]
46
+ assert height == roi .mask .shape [0 ]
47
+
48
+
49
+ def test_box_from_key_with_large_values ():
50
+ """Test that box_from_key works with large hexadecimal values."""
51
+ # Test with large values
52
+ key = "x1ABCy2DEFw3456h789A"
53
+ x , y , width , height = box_from_key (key )
54
+
55
+ assert x == 0x1ABC
56
+ assert y == 0x2DEF
57
+ assert width == 0x3456
58
+ assert height == 0x789A
59
+
60
+
61
+ def test_box_from_key_with_zero_values ():
62
+ """Test that box_from_key works with zero values."""
63
+ # Test with zero values
64
+ key = "x0y0w0h0"
65
+ x , y , width , height = box_from_key (key )
66
+
67
+ assert x == 0
68
+ assert y == 0
69
+ assert width == 0
70
+ assert height == 0
0 commit comments