File size: 4,165 Bytes
7d54ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import unittest


class TestCalculateTiles(unittest.TestCase):
    def setUp(self):
        """初始化测试数据"""
        self.image_width = 1920
        self.image_height = 1080

    def test_exact_match_workers(self):
        """测试 Worker 数量与网格数完全匹配"""
        from common import calculate_grid

        rows, cols = calculate_grid(self.image_width, self.image_height, 3)
        self.assertEqual(rows * cols, 3)
        self.assertEqual((rows, cols), (1, 3))  # 宽大于高时优先横向切割

    def test_square_preference(self):
        """测试尽量生成接近正方形的网格"""
        from common import calculate_grid

        rows, cols = calculate_grid(self.image_width, self.image_height, 4)
        self.assertEqual(rows * cols, 4)
        self.assertEqual((rows, cols), (2, 2))  # 4块时优先正方形分割

    def test_large_workers(self):
        """测试较多 Worker 数量"""
        from common import calculate_grid

        rows, cols = calculate_grid(self.image_width, self.image_height, 12)
        self.assertEqual(rows * cols, 12)
        self.assertEqual((rows, cols), (3, 4))  # 优化为接近宽高比的分割

    def test_one_worker(self):
        """测试单个 Worker 的情况"""
        from common import calculate_grid

        rows, cols = calculate_grid(self.image_width, self.image_height, 1)
        self.assertEqual(rows * cols, 1)
        self.assertEqual((rows, cols), (1, 1))

    def test_invalid_worker_count(self):
        """测试 Worker 数量为 0 或负数的情况"""
        from common import calculate_grid

        with self.assertRaises(ValueError):
            calculate_grid(self.image_width, self.image_height, 0)

        with self.assertRaises(ValueError):
            calculate_grid(self.image_width, self.image_height, -1)

    def test_aspect_ratio_preservation(self):
        """测试长宽比优先调整"""
        from common import calculate_grid

        rows, cols = calculate_grid(1080, 1920, 4)  # 竖向图片
        self.assertEqual(rows * cols, 4)
        self.assertEqual((rows, cols), (2, 2))  # 竖向也应保持正方形优先

    def test_non_divisible_workers(self):
        """测试当 workers 不能被均匀分割时"""
        from common import calculate_grid

        rows, cols = calculate_grid(self.image_width, self.image_height, 5)
        self.assertEqual(rows * cols, 5)
        # 检查返回的行列数是否正确
        self.assertTrue(rows == 1 and cols == 5 or rows == 5 and cols == 1)

    def test_large_image_size(self):
        """测试非常大的图像尺寸"""
        from common import calculate_grid

        rows, cols = calculate_grid(8000, 8000, 16)
        self.assertEqual(rows * cols, 16)
        self.assertEqual((rows, cols), (4, 4))  # 优先正方形分割

    def test_wide_image(self):
        """测试宽幅图像"""
        from common import calculate_grid

        rows, cols = calculate_grid(4000, 1000, 8)
        self.assertEqual(rows * cols, 8)
        # 检查是否更倾向于横向分割
        self.assertTrue(cols > rows)

    def test_tall_image(self):
        """测试高幅图像"""
        from common import calculate_grid

        rows, cols = calculate_grid(1000, 4000, 8)
        self.assertEqual(rows * cols, 8)
        # 检查是否更倾向于纵向分割
        self.assertTrue(rows > cols)

    def test_prime_number_workers(self):
        """测试工作者数量为质数的情况"""
        from common import calculate_grid

        rows, cols = calculate_grid(self.image_width, self.image_height, 7)
        self.assertEqual(rows * cols, 7)
        # 检查返回的行列数是否正确
        self.assertTrue(rows == 1 and cols == 7 or rows == 7 and cols == 1)

    def test_zero_image_size(self):
        """测试图像尺寸为 0 的情况"""
        from common import calculate_grid

        with self.assertRaises(ZeroDivisionError):
            calculate_grid(0, self.image_height, 4)
        with self.assertRaises(ZeroDivisionError):
            calculate_grid(self.image_width, 0, 4)


if __name__ == "__main__":
    unittest.main()