Source file src/image/color/color_test.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package color
     6  
     7  import (
     8  	"testing"
     9  	"testing/quick"
    10  )
    11  
    12  func TestSqDiff(t *testing.T) {
    13  	// canonical sqDiff implementation
    14  	orig := func(x, y uint32) uint32 {
    15  		var d uint32
    16  		if x > y {
    17  			d = uint32(x - y)
    18  		} else {
    19  			d = uint32(y - x)
    20  		}
    21  		return (d * d) >> 2
    22  	}
    23  	testCases := []uint32{
    24  		0,
    25  		1,
    26  		2,
    27  		0x0fffd,
    28  		0x0fffe,
    29  		0x0ffff,
    30  		0x10000,
    31  		0x10001,
    32  		0x10002,
    33  		0xfffffffd,
    34  		0xfffffffe,
    35  		0xffffffff,
    36  	}
    37  	for _, x := range testCases {
    38  		for _, y := range testCases {
    39  			if got, want := sqDiff(x, y), orig(x, y); got != want {
    40  				t.Fatalf("sqDiff(%#x, %#x): got %d, want %d", x, y, got, want)
    41  			}
    42  		}
    43  	}
    44  	if err := quick.CheckEqual(orig, sqDiff, &quick.Config{MaxCountScale: 10}); err != nil {
    45  		t.Fatal(err)
    46  	}
    47  }
    48  

View as plain text