forked from h2non/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_fuzz_test.go
110 lines (100 loc) · 4.34 KB
/
image_fuzz_test.go
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
/*
* SPDX-License-Identifier: AGPL-3.0-only
*
* Copyright (c) 2025 sycured
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"math"
"testing"
)
func setupTestCases(f *testing.F) {
testCases := [][4]int{
{1280, 1000, 710, 555},
{1279, 1000, 710, 555},
{900, 500, 312, 173},
{900, 500, 313, 174},
{1299, 2000, 649, 999},
{1500, 2000, 710, 947},
}
for _, tc := range testCases {
f.Add(tc[0], tc[1], tc[2], tc[3])
}
}
func shouldSkipTest(t *testing.T, imageWidth, imageHeight, fitWidth, fitHeight int) bool {
if imageWidth < 0 || imageHeight < 0 || fitWidth < 0 || fitHeight < 0 {
t.Skip("Skipping negative dimensions")
return true
}
if imageWidth > 1000000 || imageHeight > 1000000 || fitWidth > 1000000 || fitHeight > 1000000 {
t.Skip("Skipping very large dimensions")
return true
}
if imageWidth == 0 || imageHeight == 0 {
t.Skip("Skipping zero image dimensions")
return true
}
return false
}
func validateDimensions(t *testing.T, imageWidth, imageHeight, fitWidth, fitHeight int) {
resultWidth, resultHeight := calculateDestinationFitDimension(imageWidth, imageHeight, fitWidth, fitHeight)
validateResultBounds(t, resultWidth, resultHeight, fitWidth, fitHeight, imageWidth, imageHeight)
validateAspectRatio(t, resultWidth, resultHeight, imageWidth, imageHeight, fitWidth, fitHeight)
}
func validateResultBounds(t *testing.T, resultWidth, resultHeight, fitWidth, fitHeight, imageWidth, imageHeight int) {
if resultWidth > fitWidth || resultHeight > fitHeight {
t.Errorf("Result dimensions (%d, %d) exceed input fit dimensions (%d, %d). Input: iW:%d, iH:%d, fW:%d, fH:%d",
resultWidth, resultHeight, fitWidth, fitHeight, imageWidth, imageHeight, fitWidth, fitHeight)
}
}
func validateAspectRatio(t *testing.T, resultWidth, resultHeight, imageWidth, imageHeight, fitWidth, fitHeight int) {
isConstrainedByWidth := imageWidth*fitHeight > fitWidth*imageHeight
if isConstrainedByWidth {
validateWidthConstrained(t, resultWidth, resultHeight, imageWidth, imageHeight, fitWidth, fitHeight)
} else {
validateHeightConstrained(t, resultWidth, resultHeight, imageWidth, imageHeight, fitWidth, fitHeight)
}
}
func validateWidthConstrained(t *testing.T, resultWidth, resultHeight, imageWidth, imageHeight, fitWidth, fitHeight int) {
if resultWidth != fitWidth {
t.Errorf("Constrained by width, but resultWidth (%d) != input fitWidth (%d). Input: iW:%d, iH:%d, fW:%d, fH:%d",
resultWidth, fitWidth, imageWidth, imageHeight, fitWidth, fitHeight)
}
expectedHeight := int(math.Round(float64(resultWidth) * float64(imageHeight) / float64(imageWidth)))
if resultHeight != expectedHeight {
t.Errorf("Aspect ratio mismatch (width constrained). Expected H: %d, Got H: %d. Input: iW:%d, iH:%d, fW:%d, fH:%d",
expectedHeight, resultHeight, imageWidth, imageHeight, fitWidth, fitHeight)
}
}
func validateHeightConstrained(t *testing.T, resultWidth, resultHeight, imageWidth, imageHeight, fitWidth, fitHeight int) {
if resultHeight != fitHeight {
t.Errorf("Constrained by height, but resultHeight (%d) != input fitHeight (%d). Input: iW:%d, iH:%d, fW:%d, fH:%d",
resultHeight, fitHeight, imageWidth, imageHeight, fitWidth, fitHeight)
}
expectedWidth := int(math.Round(float64(resultHeight) * float64(imageWidth) / float64(imageHeight)))
if resultWidth != expectedWidth {
t.Errorf("Aspect ratio mismatch (height constrained). Expected W: %d, Got W: %d. Input: iW:%d, iH:%d, fW:%d, fH:%d",
expectedWidth, resultWidth, imageWidth, imageHeight, fitWidth, fitHeight)
}
}
func FuzzCalculateDestinationFitDimension(f *testing.F) {
setupTestCases(f)
f.Fuzz(func(t *testing.T, imageWidth, imageHeight, fitWidth, fitHeight int) {
if shouldSkipTest(t, imageWidth, imageHeight, fitWidth, fitHeight) {
return
}
validateDimensions(t, imageWidth, imageHeight, fitWidth, fitHeight)
})
}