@@ -13,8 +13,8 @@ import (
1313// solidImage 创建一个全部填充为指定颜色的 image.Image
1414func solidImage (w , h int , c color.RGBA ) image.Image {
1515 img := image .NewRGBA (image .Rect (0 , 0 , w , h ))
16- for y := 0 ; y < h ; y ++ {
17- for x := 0 ; x < w ; x ++ {
16+ for y := range h {
17+ for x := range w {
1818 img .SetRGBA (x , y , c )
1919 }
2020 }
@@ -24,8 +24,8 @@ func solidImage(w, h int, c color.RGBA) image.Image {
2424// twoColorImage 创建左半部分为 c1、右半部分为 c2 的图像
2525func twoColorImage (w , h int , c1 , c2 color.RGBA ) image.Image {
2626 img := image .NewRGBA (image .Rect (0 , 0 , w , h ))
27- for y := 0 ; y < h ; y ++ {
28- for x := 0 ; x < w ; x ++ {
27+ for y := range h {
28+ for x := range w {
2929 if x < w / 2 {
3030 img .SetRGBA (x , y , c1 )
3131 } else {
@@ -135,7 +135,7 @@ func TestDistance_Symmetry(t *testing.T) {
135135
136136func TestDistance_NonNegative (t * testing.T ) {
137137 rng := rand .New (rand .NewSource (42 ))
138- for i := 0 ; i < 100 ; i ++ {
138+ for range 100 {
139139 a := color.RGBA {uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), 255 }
140140 b := color.RGBA {uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), 255 }
141141 got := distance (a , b )
@@ -263,7 +263,7 @@ func TestTakecolor_TwoDistinctColors(t *testing.T) {
263263 // 多次运行,验证算法至少能在 30 次尝试中有一次正确分离两种颜色。
264264 img := twoColorImage (20 , 20 , Red , Blue )
265265 const maxAttempts = 30
266- for attempt := 0 ; attempt < maxAttempts ; attempt ++ {
266+ for range maxAttempts {
267267 result := takecolor (img , 2 )
268268 if len (result ) == 2 && colorInSlice (Red , result , 5 ) && colorInSlice (Blue , result , 5 ) {
269269 return // 成功分离,测试通过
@@ -286,8 +286,8 @@ func TestTakecolor_Deterministic_SolidImage(t *testing.T) {
286286func TestTakecolor_AllClustersHaveValidRGB (t * testing.T ) {
287287 rng := rand .New (rand .NewSource (99 ))
288288 img := image .NewRGBA (image .Rect (0 , 0 , 16 , 16 ))
289- for y := 0 ; y < 16 ; y ++ {
290- for x := 0 ; x < 16 ; x ++ {
289+ for y := range 16 {
290+ for x := range 16 {
291291 img .SetRGBA (x , y , color.RGBA {
292292 uint8 (rng .Intn (256 )),
293293 uint8 (rng .Intn (256 )),
@@ -310,7 +310,7 @@ func TestTakecolor_AllClustersHaveValidRGB(t *testing.T) {
310310// ---- Benchmark ----
311311
312312func BenchmarkSq (b * testing.B ) {
313- for i := 0 ; i < b .N ; i ++ {
313+ for i := range b .N {
314314 sq (float64 (i ))
315315 }
316316}
@@ -319,7 +319,7 @@ func BenchmarkDistance(b *testing.B) {
319319 a := color.RGBA {100 , 150 , 200 , 255 }
320320 c := color.RGBA {50 , 80 , 30 , 255 }
321321 b .ResetTimer ()
322- for i := 0 ; i < b .N ; i ++ {
322+ for range b .N {
323323 distance (a , c )
324324 }
325325}
@@ -328,7 +328,7 @@ func BenchmarkClustersEqual_Equal(b *testing.B) {
328328 a := []color.RGBA {{255 , 0 , 0 , 255 }, {0 , 255 , 0 , 255 }, {0 , 0 , 255 , 255 }, {128 , 128 , 128 , 255 }}
329329 c := []color.RGBA {{255 , 0 , 0 , 255 }, {0 , 255 , 0 , 255 }, {0 , 0 , 255 , 255 }, {128 , 128 , 128 , 255 }}
330330 b .ResetTimer ()
331- for i := 0 ; i < b .N ; i ++ {
331+ for range b .N {
332332 clustersEqual (a , c )
333333 }
334334}
@@ -337,57 +337,57 @@ func BenchmarkClustersEqual_NotEqual(b *testing.B) {
337337 a := []color.RGBA {{255 , 0 , 0 , 255 }, {0 , 255 , 0 , 255 }, {0 , 0 , 255 , 255 }, {128 , 128 , 128 , 255 }}
338338 c := []color.RGBA {{255 , 0 , 0 , 255 }, {0 , 255 , 0 , 255 }, {0 , 0 , 255 , 255 }, {200 , 200 , 200 , 255 }}
339339 b .ResetTimer ()
340- for i := 0 ; i < b .N ; i ++ {
340+ for range b .N {
341341 clustersEqual (a , c )
342342 }
343343}
344344
345345func BenchmarkTakecolor_16x16_K3 (b * testing.B ) {
346346 rng := rand .New (rand .NewSource (42 ))
347347 img := image .NewRGBA (image .Rect (0 , 0 , 16 , 16 ))
348- for y := 0 ; y < 16 ; y ++ {
349- for x := 0 ; x < 16 ; x ++ {
348+ for y := range 16 {
349+ for x := range 16 {
350350 img .SetRGBA (x , y , color.RGBA {uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), 255 })
351351 }
352352 }
353353 b .ResetTimer ()
354- for i := 0 ; i < b .N ; i ++ {
354+ for range b .N {
355355 takecolor (img , 3 )
356356 }
357357}
358358
359359func BenchmarkTakecolor_64x64_K4 (b * testing.B ) {
360360 rng := rand .New (rand .NewSource (42 ))
361361 img := image .NewRGBA (image .Rect (0 , 0 , 64 , 64 ))
362- for y := 0 ; y < 64 ; y ++ {
363- for x := 0 ; x < 64 ; x ++ {
362+ for y := range 64 {
363+ for x := range 64 {
364364 img .SetRGBA (x , y , color.RGBA {uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), 255 })
365365 }
366366 }
367367 b .ResetTimer ()
368- for i := 0 ; i < b .N ; i ++ {
368+ for range b .N {
369369 takecolor (img , 4 )
370370 }
371371}
372372
373373func BenchmarkTakecolor_128x128_K8 (b * testing.B ) {
374374 rng := rand .New (rand .NewSource (42 ))
375375 img := image .NewRGBA (image .Rect (0 , 0 , 128 , 128 ))
376- for y := 0 ; y < 128 ; y ++ {
377- for x := 0 ; x < 128 ; x ++ {
376+ for y := range 128 {
377+ for x := range 128 {
378378 img .SetRGBA (x , y , color.RGBA {uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), uint8 (rng .Intn (256 )), 255 })
379379 }
380380 }
381381 b .ResetTimer ()
382- for i := 0 ; i < b .N ; i ++ {
382+ for range b .N {
383383 takecolor (img , 8 )
384384 }
385385}
386386
387387func BenchmarkTakecolor_SolidColor_K5 (b * testing.B ) {
388388 img := solidImage (64 , 64 , color.RGBA {200 , 100 , 50 , 255 })
389389 b .ResetTimer ()
390- for i := 0 ; i < b .N ; i ++ {
390+ for range b .N {
391391 takecolor (img , 5 )
392392 }
393393}
0 commit comments