File: thumbnail_test.go

package info (click to toggle)
golang-github-nfnt-resize 0.0~git20180221.83c6a99-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 164 kB
  • sloc: makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,067 bytes parent folder | download | duplicates (2)
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
package resize

import (
	"image"
	"runtime"
	"testing"
)

func init() {
	runtime.GOMAXPROCS(runtime.NumCPU())
}

var thumbnailTests = []struct {
	origWidth      int
	origHeight     int
	maxWidth       uint
	maxHeight      uint
	expectedWidth  uint
	expectedHeight uint
}{
	{5, 5, 10, 10, 5, 5},
	{10, 10, 5, 5, 5, 5},
	{10, 50, 10, 10, 2, 10},
	{50, 10, 10, 10, 10, 2},
	{50, 100, 60, 90, 45, 90},
	{120, 100, 60, 90, 60, 50},
	{200, 250, 200, 150, 120, 150},
}

func TestThumbnail(t *testing.T) {
	for i, tt := range thumbnailTests {
		img := image.NewGray16(image.Rect(0, 0, tt.origWidth, tt.origHeight))

		outImg := Thumbnail(tt.maxWidth, tt.maxHeight, img, NearestNeighbor)

		newWidth := uint(outImg.Bounds().Dx())
		newHeight := uint(outImg.Bounds().Dy())
		if newWidth != tt.expectedWidth ||
			newHeight != tt.expectedHeight {
			t.Errorf("%d. Thumbnail(%v, %v, img, NearestNeighbor) => "+
				"width: %v, height: %v, want width: %v, height: %v",
				i, tt.maxWidth, tt.maxHeight,
				newWidth, newHeight, tt.expectedWidth, tt.expectedHeight,
			)
		}
	}
}