github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/identicon/identicon_test.go (about)

     1  // Copyright 2015 by caixw, All rights reserved.
     2  // Use of this source code is governed by a MIT
     3  // license that can be found in the LICENSE file.
     4  
     5  package identicon
     6  
     7  import (
     8  	"image"
     9  	"image/color"
    10  	"image/png"
    11  	"os"
    12  	"strconv"
    13  	"testing"
    14  
    15  	"github.com/insionng/yougam/libraries/issue9/assert"
    16  )
    17  
    18  var (
    19  	back  = color.RGBA{255, 0, 0, 100}
    20  	fore  = color.RGBA{0, 255, 255, 100}
    21  	fores = []color.Color{color.Black, color.RGBA{200, 2, 5, 100}, color.RGBA{2, 200, 5, 100}}
    22  	size  = 128
    23  )
    24  
    25  // 在不存在testdata目录下的情况下,自动创建一个目录。
    26  func TestInit(t *testing.T) {
    27  	a := assert.New(t)
    28  
    29  	a.NotError(os.MkdirAll("./testdata/", os.ModePerm))
    30  }
    31  
    32  // 依次画出各个网络的图像。
    33  func TestBlocks(t *testing.T) {
    34  	p := []color.Color{back, fore}
    35  
    36  	a := assert.New(t)
    37  
    38  	for k, v := range blocks {
    39  		img := image.NewPaletted(image.Rect(0, 0, size*4, size), p) // 横向4张图片大小
    40  
    41  		for i := 0; i < 4; i++ {
    42  			v(img, float64(i*size), 0, float64(size), i)
    43  		}
    44  
    45  		fi, err := os.Create("./testdata/block-" + strconv.Itoa(k) + ".png")
    46  		a.NotError(err).NotNil(fi)
    47  		a.NotError(png.Encode(fi, img))
    48  		a.NotError(fi.Close()) // 关闭文件
    49  	}
    50  }
    51  
    52  // 产生一组测试图片
    53  func TestDrawBlocks(t *testing.T) {
    54  	a := assert.New(t)
    55  
    56  	for i := 0; i < 20; i++ {
    57  		p := image.NewPaletted(image.Rect(0, 0, size, size), []color.Color{back, fore})
    58  		c := (i + 1) % len(centerBlocks)
    59  		b1 := (i + 2) % len(blocks)
    60  		b2 := (i + 3) % len(blocks)
    61  		drawBlocks(p, size, centerBlocks[c], blocks[b1], blocks[b2], 0)
    62  
    63  		fi, err := os.Create("./testdata/draw-" + strconv.Itoa(i) + ".png")
    64  		a.NotError(err).NotNil(fi)
    65  		a.NotError(png.Encode(fi, p))
    66  		a.NotError(fi.Close()) // 关闭文件
    67  	}
    68  }
    69  
    70  func TestMake(t *testing.T) {
    71  	a := assert.New(t)
    72  
    73  	for i := 0; i < 20; i++ {
    74  		img, err := Make(size, back, fore, []byte("make-"+strconv.Itoa(i)))
    75  		a.NotError(err).NotNil(img)
    76  
    77  		fi, err := os.Create("./testdata/make-" + strconv.Itoa(i) + ".png")
    78  		a.NotError(err).NotNil(fi)
    79  		a.NotError(png.Encode(fi, img))
    80  		a.NotError(fi.Close()) // 关闭文件
    81  	}
    82  }
    83  
    84  func TestIdenticon(t *testing.T) {
    85  	a := assert.New(t)
    86  
    87  	ii, err := New(size, back, fores...)
    88  	a.NotError(err).NotNil(ii)
    89  
    90  	for i := 0; i < 20; i++ {
    91  		img := ii.Make([]byte("identicon-" + strconv.Itoa(i)))
    92  		a.NotNil(img)
    93  
    94  		fi, err := os.Create("./testdata/identicon-" + strconv.Itoa(i) + ".png")
    95  		a.NotError(err).NotNil(fi)
    96  		a.NotError(png.Encode(fi, img))
    97  		a.NotError(fi.Close()) // 关闭文件
    98  	}
    99  }
   100  
   101  // BenchmarkMake	    5000	    229378 ns/op
   102  func BenchmarkMake(b *testing.B) {
   103  	a := assert.New(b)
   104  	for i := 0; i < b.N; i++ {
   105  		img, err := Make(size, back, fore, []byte("Make"))
   106  		a.NotError(err).NotNil(img)
   107  	}
   108  }
   109  
   110  // BenchmarkIdenticon_Make	   10000	    222127 ns/op
   111  func BenchmarkIdenticon_Make(b *testing.B) {
   112  	a := assert.New(b)
   113  
   114  	ii, err := New(size, back, fores...)
   115  	a.NotError(err).NotNil(ii)
   116  
   117  	for i := 0; i < b.N; i++ {
   118  		img := ii.Make([]byte("Make"))
   119  		a.NotNil(img)
   120  	}
   121  }