thumbnail_test.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package resize
  2. import (
  3. "image"
  4. "runtime"
  5. "testing"
  6. )
  7. func init() {
  8. runtime.GOMAXPROCS(runtime.NumCPU())
  9. }
  10. var thumbnailTests = []struct {
  11. origWidth int
  12. origHeight int
  13. maxWidth uint
  14. maxHeight uint
  15. expectedWidth uint
  16. expectedHeight uint
  17. }{
  18. {5, 5, 10, 10, 5, 5},
  19. {10, 10, 5, 5, 5, 5},
  20. {10, 50, 10, 10, 2, 10},
  21. {50, 10, 10, 10, 10, 2},
  22. {50, 100, 60, 90, 45, 90},
  23. {120, 100, 60, 90, 60, 50},
  24. {200, 250, 200, 150, 120, 150},
  25. }
  26. func TestThumbnail(t *testing.T) {
  27. for i, tt := range thumbnailTests {
  28. img := image.NewGray16(image.Rect(0, 0, tt.origWidth, tt.origHeight))
  29. outImg := Thumbnail(tt.maxWidth, tt.maxHeight, img, NearestNeighbor)
  30. newWidth := uint(outImg.Bounds().Dx())
  31. newHeight := uint(outImg.Bounds().Dy())
  32. if newWidth != tt.expectedWidth ||
  33. newHeight != tt.expectedHeight {
  34. t.Errorf("%d. Thumbnail(%v, %v, img, NearestNeighbor) => "+
  35. "width: %v, height: %v, want width: %v, height: %v",
  36. i, tt.maxWidth, tt.maxHeight,
  37. newWidth, newHeight, tt.expectedWidth, tt.expectedHeight,
  38. )
  39. }
  40. }
  41. }