Kaynağa Gözat

Added more tests

disintegration 9 yıl önce
ebeveyn
işleme
83458641ee
4 değiştirilmiş dosya ile 248 ekleme ve 13 silme
  1. 67 5
      effects_test.go
  2. 46 8
      helpers_test.go
  3. 115 0
      resize_test.go
  4. 20 0
      utils_test.go

+ 67 - 5
effects_test.go

@@ -12,6 +12,28 @@ func TestBlur(t *testing.T) {
 		sigma float64
 		want  *image.NRGBA
 	}{
+		{
+			"Blur 3x3 0",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 2, 2),
+				Stride: 3 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+					0x00, 0x00, 0x00, 0x00, 0x66, 0xaa, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
+					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+				},
+			},
+			0.0,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 3, 3),
+				Stride: 3 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+					0x00, 0x00, 0x00, 0x00, 0x66, 0xaa, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
+					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+				},
+			},
+		},
 		{
 			"Blur 3x3 0.5",
 			&image.NRGBA{
@@ -35,7 +57,6 @@ func TestBlur(t *testing.T) {
 			},
 		},
 		{
-
 			"Blur 3x3 10",
 			&image.NRGBA{
 				Rect:   image.Rect(-1, -1, 2, 2),
@@ -74,6 +95,28 @@ func TestSharpen(t *testing.T) {
 		sigma float64
 		want  *image.NRGBA
 	}{
+		{
+			"Sharpen 3x3 0",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 2, 2),
+				Stride: 3 * 4,
+				Pix: []uint8{
+					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
+					0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
+					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
+				},
+			},
+			0,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 3, 3),
+				Stride: 3 * 4,
+				Pix: []uint8{
+					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
+					0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
+					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
+				},
+			},
+		},
 		{
 			"Sharpen 3x3 0.5",
 			&image.NRGBA{
@@ -92,19 +135,20 @@ func TestSharpen(t *testing.T) {
 				Pix: []uint8{
 					0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66,
 					0x64, 0x64, 0x64, 0x64, 0x7e, 0x7e, 0x7e, 0x7e, 0x64, 0x64, 0x64, 0x64,
-					0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66},
+					0x66, 0x66, 0x66, 0x66, 0x64, 0x64, 0x64, 0x64, 0x66, 0x66, 0x66, 0x66,
+				},
 			},
 		},
 		{
-
-			"Sharpen 3x3 10",
+			"Sharpen 3x3 100",
 			&image.NRGBA{
 				Rect:   image.Rect(-1, -1, 2, 2),
 				Stride: 3 * 4,
 				Pix: []uint8{
 					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
 					0x66, 0x66, 0x66, 0x66, 0x77, 0x77, 0x77, 0x77, 0x66, 0x66, 0x66, 0x66,
-					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66},
+					0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
+				},
 			},
 			100,
 			&image.NRGBA{
@@ -117,6 +161,24 @@ func TestSharpen(t *testing.T) {
 				},
 			},
 		},
+		{
+			"Sharpen 3x1 10",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 2, 0),
+				Stride: 3 * 4,
+				Pix: []uint8{
+					0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+				},
+			},
+			10,
+			&image.NRGBA{
+				Rect:   image.Rect(0, 0, 3, 1),
+				Stride: 3 * 4,
+				Pix: []uint8{
+					0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+				},
+			},
+		},
 	}
 	for _, d := range td {
 		got := Sharpen(d.src, d.sigma)

+ 46 - 8
helpers_test.go

@@ -74,10 +74,22 @@ func TestEncodeDecode(t *testing.T) {
 	}
 
 	buf := &bytes.Buffer{}
-	err := Encode(buf, imgWithAlpha, Format(100))
+	err := Encode(buf, imgWithAlpha, JPEG)
+	if err != nil {
+		t.Errorf("failed encoding alpha to JPEG format %s", err)
+	}
+
+	buf = &bytes.Buffer{}
+	err = Encode(buf, imgWithAlpha, Format(100))
 	if err != ErrUnsupportedFormat {
 		t.Errorf("expected ErrUnsupportedFormat")
 	}
+
+	buf = bytes.NewBuffer([]byte("bad data"))
+	_, err = Decode(buf)
+	if err == nil {
+		t.Errorf("decoding bad data, expected error")
+	}
 }
 
 func TestNew(t *testing.T) {
@@ -109,6 +121,13 @@ func TestNew(t *testing.T) {
 			image.Rect(0, 0, 2, 1),
 			[]uint8{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
 		},
+		{
+			"New 0x0 white",
+			0, 0,
+			color.NRGBA{255, 255, 255, 255},
+			image.Rect(0, 0, 0, 0),
+			nil,
+		},
 	}
 
 	for _, d := range td {
@@ -159,30 +178,31 @@ func TestClone(t *testing.T) {
 		{
 			"Clone RGBA",
 			&image.RGBA{
-				Rect:   image.Rect(-1, -1, 0, 1),
+				Rect:   image.Rect(-1, -1, 0, 2),
 				Stride: 1 * 4,
-				Pix:    []uint8{0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff},
+				Pix:    []uint8{0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33, 0xcc, 0xdd, 0xee, 0xff},
 			},
 			&image.NRGBA{
-				Rect:   image.Rect(0, 0, 1, 2),
+				Rect:   image.Rect(0, 0, 1, 3),
 				Stride: 1 * 4,
-				Pix:    []uint8{0x00, 0x55, 0xaa, 0x33, 0xcc, 0xdd, 0xee, 0xff},
+				Pix:    []uint8{0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa, 0x33, 0xcc, 0xdd, 0xee, 0xff},
 			},
 		},
 		{
 			"Clone RGBA64",
 			&image.RGBA64{
-				Rect:   image.Rect(-1, -1, 0, 1),
+				Rect:   image.Rect(-1, -1, 0, 2),
 				Stride: 1 * 8,
 				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 					0x00, 0x00, 0x11, 0x11, 0x22, 0x22, 0x33, 0x33,
 					0xcc, 0xcc, 0xdd, 0xdd, 0xee, 0xee, 0xff, 0xff,
 				},
 			},
 			&image.NRGBA{
-				Rect:   image.Rect(0, 0, 1, 2),
+				Rect:   image.Rect(0, 0, 1, 3),
 				Stride: 1 * 4,
-				Pix:    []uint8{0x00, 0x55, 0xaa, 0x33, 0xcc, 0xdd, 0xee, 0xff},
+				Pix:    []uint8{0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa, 0x33, 0xcc, 0xdd, 0xee, 0xff},
 			},
 		},
 		{
@@ -359,3 +379,21 @@ func TestClone(t *testing.T) {
 		}
 	}
 }
+
+func TestFormats(t *testing.T) {
+	formatNames := map[Format]string{
+		JPEG:       "JPEG",
+		PNG:        "PNG",
+		GIF:        "GIF",
+		BMP:        "BMP",
+		TIFF:       "TIFF",
+		Format(-1): "Unsupported",
+	}
+	for format, name := range formatNames {
+		got := format.String()
+		if got != name {
+			t.Errorf("test [Format names] failed: got %#v want %#v", got, name)
+			continue
+		}
+	}
+}

+ 115 - 0
resize_test.go

@@ -115,6 +115,45 @@ func TestResize(t *testing.T) {
 				},
 			},
 		},
+		{
+			"Resize 0x0 1x1 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, -1, -1),
+				Stride: 0,
+				Pix:    []uint8{},
+			},
+			1, 1,
+			Box,
+			&image.NRGBA{},
+		},
+		{
+			"Resize 2x2 0x0 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+			0, 0,
+			Box,
+			&image.NRGBA{},
+		},
+		{
+			"Resize 2x2 -1x0 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+			-1, 0,
+			Box,
+			&image.NRGBA{},
+		},
 	}
 	for _, d := range td {
 		got := Resize(d.src, d.w, d.h, d.f)
@@ -123,6 +162,43 @@ func TestResize(t *testing.T) {
 			t.Errorf("test [%s] failed: %#v", d.desc, got)
 		}
 	}
+
+	for i, filter := range []ResampleFilter{
+		NearestNeighbor,
+		Box,
+		Linear,
+		Hermite,
+		MitchellNetravali,
+		CatmullRom,
+		BSpline,
+		Gaussian,
+		Lanczos,
+		Hann,
+		Hamming,
+		Blackman,
+		Bartlett,
+		Welch,
+		Cosine,
+	} {
+		src := image.NewNRGBA(image.Rect(-1, -1, 2, 3))
+		got := Resize(src, 5, 6, filter)
+		want := image.NewNRGBA(image.Rect(0, 0, 5, 6))
+		if !compareNRGBA(got, want, 0) {
+			t.Errorf("test [Resize all filters #%d] failed: %#v", i, got)
+		}
+
+		if filter.Kernel != nil {
+			x := filter.Kernel(filter.Support + 0.0001)
+			if x != 0 {
+				t.Errorf("test [ResampleFilter edge cases #%d] failed: %f", i, x)
+			}
+		}
+	}
+
+	bcs2 := bcspline(2, 1, 0)
+	if bcs2 != 0 {
+		t.Errorf("test [bcspline 2] failed: %f", bcs2)
+	}
 }
 
 func TestFit(t *testing.T) {
@@ -190,6 +266,45 @@ func TestFit(t *testing.T) {
 				},
 			},
 		},
+		{
+			"Fit 0x0 1x1 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, -1, -1),
+				Stride: 0,
+				Pix:    []uint8{},
+			},
+			1, 1,
+			Box,
+			&image.NRGBA{},
+		},
+		{
+			"Fit 2x2 0x0 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+			0, 0,
+			Box,
+			&image.NRGBA{},
+		},
+		{
+			"Fit 2x2 -1x0 box",
+			&image.NRGBA{
+				Rect:   image.Rect(-1, -1, 1, 1),
+				Stride: 2 * 4,
+				Pix: []uint8{
+					0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
+					0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
+				},
+			},
+			-1, 0,
+			Box,
+			&image.NRGBA{},
+		},
 	}
 	for _, d := range td {
 		got := Fit(d.src, d.w, d.h, d.f)

+ 20 - 0
utils_test.go

@@ -59,3 +59,23 @@ func TestClamp(t *testing.T) {
 		}
 	}
 }
+
+func TestClampint32(t *testing.T) {
+	td := []struct {
+		i int32
+		u uint8
+	}{
+		{0, 0},
+		{255, 255},
+		{128, 128},
+		{256, 255},
+		{2500, 255},
+		{-10, 0},
+	}
+
+	for _, d := range td {
+		if clampint32(d.i) != d.u {
+			t.Errorf("test [clampint32 %v %v] failed: %v", d.i, d.u, clampint32(d.i))
+		}
+	}
+}