github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/gift/utils_test.go (about) 1 package gift 2 3 import ( 4 "image" 5 "image/color" 6 "runtime" 7 "testing" 8 ) 9 10 func testParallelizeN(enabled bool, n, procs int) bool { 11 data := make([]bool, n) 12 runtime.GOMAXPROCS(procs) 13 parallelize(enabled, 0, n, func(start, end int) { 14 for i := start; i < end; i++ { 15 data[i] = true 16 } 17 }) 18 for i := 0; i < n; i++ { 19 if data[i] != true { 20 return false 21 } 22 } 23 return true 24 } 25 26 func TestParallelize(t *testing.T) { 27 for _, e := range []bool{true, false} { 28 for _, n := range []int{1, 10, 100, 1000} { 29 for _, p := range []int{1, 2, 4, 8, 16, 100} { 30 if testParallelizeN(e, n, p) != true { 31 t.Errorf("failed testParallelizeN(%v, %d, %d)", e, n, p) 32 } 33 } 34 } 35 } 36 } 37 38 func TestTempImageCopy(t *testing.T) { 39 tmp1 := createTempImage(image.Rect(-1, -2, 1, 2)) 40 if !tmp1.Bounds().Eq(image.Rect(-1, -2, 1, 2)) { 41 t.Error("unexpected temp image bounds") 42 } 43 tmp2 := createTempImage(image.Rect(-3, -4, 3, 4)) 44 if !tmp2.Bounds().Eq(image.Rect(-3, -4, 3, 4)) { 45 t.Error("unexpected temp image bounds") 46 } 47 copyimage(tmp1, tmp2, nil) 48 } 49 50 func TestQSort(t *testing.T) { 51 testData := []struct { 52 a, b []float32 53 }{ 54 { 55 []float32{}, 56 []float32{}, 57 }, 58 { 59 []float32{0.1}, 60 []float32{0.1}, 61 }, 62 { 63 []float32{0.4, 0.2, 0.5, -0.5, 0.3, 0.0, 0.1}, 64 []float32{-0.5, 0.0, 0.1, 0.2, 0.3, 0.4, 0.5}, 65 }, 66 { 67 []float32{-10, 10, -20, 20, -30, 30}, 68 []float32{-30, -20, -10, 10, 20, 30}, 69 }, 70 } 71 72 for _, d := range testData { 73 qsortf32(d.a) 74 for i := range d.a { 75 if d.a[i] != d.b[i] { 76 t.Errorf("qsort failed: %#v", d.a) 77 } 78 } 79 } 80 } 81 82 func TestDisk(t *testing.T) { 83 testData := []struct { 84 ksize int 85 k []float32 86 }{ 87 { 88 -5, 89 []float32{}, 90 }, 91 { 92 0, 93 []float32{}, 94 }, 95 { 96 1, 97 []float32{1}, 98 }, 99 { 100 2, 101 []float32{1}, 102 }, 103 { 104 3, 105 []float32{ 106 0, 1, 0, 107 1, 1, 1, 108 0, 1, 0, 109 }, 110 }, 111 { 112 4, 113 []float32{ 114 0, 1, 0, 115 1, 1, 1, 116 0, 1, 0, 117 }, 118 }, 119 { 120 5, 121 []float32{ 122 0, 0, 1, 0, 0, 123 0, 1, 1, 1, 0, 124 1, 1, 1, 1, 1, 125 0, 1, 1, 1, 0, 126 0, 0, 1, 0, 0, 127 }, 128 }, 129 { 130 6, 131 []float32{ 132 0, 0, 1, 0, 0, 133 0, 1, 1, 1, 0, 134 1, 1, 1, 1, 1, 135 0, 1, 1, 1, 0, 136 0, 0, 1, 0, 0, 137 }, 138 }, 139 { 140 7, 141 []float32{ 142 0, 0, 0, 1, 0, 0, 0, 143 0, 1, 1, 1, 1, 1, 0, 144 0, 1, 1, 1, 1, 1, 0, 145 1, 1, 1, 1, 1, 1, 1, 146 0, 1, 1, 1, 1, 1, 0, 147 0, 1, 1, 1, 1, 1, 0, 148 0, 0, 0, 1, 0, 0, 0, 149 }, 150 }, 151 } 152 153 for _, d := range testData { 154 disk := genDisk(d.ksize) 155 for i := range disk { 156 if disk[i] != d.k[i] { 157 t.Errorf("gen disk failed: %d %#v", d.ksize, disk) 158 } 159 } 160 } 161 } 162 163 func TestIsOpaque(t *testing.T) { 164 type opqt struct { 165 img image.Image 166 opaque bool 167 } 168 var testData []opqt 169 170 testData = append(testData, opqt{image.NewNRGBA(image.Rect(0, 0, 1, 1)), false}) 171 testData = append(testData, opqt{image.NewNRGBA64(image.Rect(0, 0, 1, 1)), false}) 172 testData = append(testData, opqt{image.NewRGBA(image.Rect(0, 0, 1, 1)), false}) 173 testData = append(testData, opqt{image.NewRGBA64(image.Rect(0, 0, 1, 1)), false}) 174 testData = append(testData, opqt{image.NewGray(image.Rect(0, 0, 1, 1)), true}) 175 testData = append(testData, opqt{image.NewGray16(image.Rect(0, 0, 1, 1)), true}) 176 testData = append(testData, opqt{image.NewYCbCr(image.Rect(0, 0, 1, 1), image.YCbCrSubsampleRatio444), true}) 177 testData = append(testData, opqt{image.NewAlpha(image.Rect(0, 0, 1, 1)), false}) 178 179 img1 := image.NewNRGBA(image.Rect(0, 0, 1, 1)) 180 img1.Set(0, 0, color.NRGBA{0x00, 0x00, 0x00, 0xff}) 181 testData = append(testData, opqt{img1, true}) 182 img2 := image.NewNRGBA64(image.Rect(0, 0, 1, 1)) 183 img2.Set(0, 0, color.NRGBA{0x00, 0x00, 0x00, 0xff}) 184 testData = append(testData, opqt{img2, true}) 185 img3 := image.NewRGBA(image.Rect(0, 0, 1, 1)) 186 img3.Set(0, 0, color.NRGBA{0x00, 0x00, 0x00, 0xff}) 187 testData = append(testData, opqt{img3, true}) 188 img4 := image.NewRGBA64(image.Rect(0, 0, 1, 1)) 189 img4.Set(0, 0, color.NRGBA{0x00, 0x00, 0x00, 0xff}) 190 testData = append(testData, opqt{img4, true}) 191 imgp1 := image.NewPaletted(image.Rect(0, 0, 1, 1), []color.Color{color.NRGBA{0x00, 0x00, 0x00, 0xff}}) 192 imgp1.SetColorIndex(0, 0, 0) 193 testData = append(testData, opqt{imgp1, true}) 194 imgp2 := image.NewPaletted(image.Rect(0, 0, 1, 1), []color.Color{color.NRGBA{0x00, 0x00, 0x00, 0xfe}}) 195 imgp2.SetColorIndex(0, 0, 0) 196 testData = append(testData, opqt{imgp2, false}) 197 198 for _, d := range testData { 199 isop := isOpaque(d.img) 200 if isop != d.opaque { 201 t.Errorf("isOpaque failed %#v, %v", d.img, isop) 202 } 203 } 204 } 205 206 func checkBoundsAndPix(b1, b2 image.Rectangle, pix1, pix2 []uint8) bool { 207 if !b1.Eq(b2) { 208 return false 209 } 210 if len(pix1) != len(pix2) { 211 return false 212 } 213 for i := 0; i < len(pix1); i++ { 214 if pix1[i] != pix2[i] { 215 return false 216 } 217 } 218 return true 219 }