You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.7 KiB

  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. package identicon
  5. import (
  6. "image"
  7. "image/color"
  8. "image/png"
  9. "os"
  10. "strconv"
  11. "testing"
  12. "github.com/issue9/assert"
  13. )
  14. var (
  15. back = color.RGBA{255, 0, 0, 100}
  16. fore = color.RGBA{0, 255, 255, 100}
  17. fores = []color.Color{color.Black, color.RGBA{200, 2, 5, 100}, color.RGBA{2, 200, 5, 100}}
  18. size = 128
  19. )
  20. // 依次画出各个网络的图像。
  21. func TestBlocks(t *testing.T) {
  22. p := []color.Color{back, fore}
  23. a := assert.New(t)
  24. for k, v := range blocks {
  25. img := image.NewPaletted(image.Rect(0, 0, size*4, size), p) // 横向4张图片大小
  26. for i := 0; i < 4; i++ {
  27. v(img, float64(i*size), 0, float64(size), i)
  28. }
  29. fi, err := os.Create("./testdata/block-" + strconv.Itoa(k) + ".png")
  30. a.NotError(err).NotNil(fi)
  31. a.NotError(png.Encode(fi, img))
  32. a.NotError(fi.Close()) // 关闭文件
  33. }
  34. }
  35. // 产生一组测试图片
  36. func TestDrawBlocks(t *testing.T) {
  37. a := assert.New(t)
  38. for i := 0; i < 20; i++ {
  39. p := image.NewPaletted(image.Rect(0, 0, size, size), []color.Color{back, fore})
  40. c := (i + 1) % len(centerBlocks)
  41. b1 := (i + 2) % len(blocks)
  42. b2 := (i + 3) % len(blocks)
  43. drawBlocks(p, size, centerBlocks[c], blocks[b1], blocks[b2], 0)
  44. fi, err := os.Create("./testdata/draw-" + strconv.Itoa(i) + ".png")
  45. a.NotError(err).NotNil(fi)
  46. a.NotError(png.Encode(fi, p))
  47. a.NotError(fi.Close()) // 关闭文件
  48. }
  49. }
  50. func TestMake(t *testing.T) {
  51. a := assert.New(t)
  52. for i := 0; i < 20; i++ {
  53. img, err := Make(size, back, fore, []byte("make-"+strconv.Itoa(i)))
  54. a.NotError(err).NotNil(img)
  55. fi, err := os.Create("./testdata/make-" + strconv.Itoa(i) + ".png")
  56. a.NotError(err).NotNil(fi)
  57. a.NotError(png.Encode(fi, img))
  58. a.NotError(fi.Close()) // 关闭文件
  59. }
  60. }
  61. func TestIdenticon(t *testing.T) {
  62. a := assert.New(t)
  63. ii, err := New(size, back, fores...)
  64. a.NotError(err).NotNil(ii)
  65. for i := 0; i < 20; i++ {
  66. img := ii.Make([]byte("identicon-" + strconv.Itoa(i)))
  67. a.NotNil(img)
  68. fi, err := os.Create("./testdata/identicon-" + strconv.Itoa(i) + ".png")
  69. a.NotError(err).NotNil(fi)
  70. a.NotError(png.Encode(fi, img))
  71. a.NotError(fi.Close()) // 关闭文件
  72. }
  73. }
  74. // BenchmarkMake 5000 229378 ns/op
  75. func BenchmarkMake(b *testing.B) {
  76. a := assert.New(b)
  77. for i := 0; i < b.N; i++ {
  78. img, err := Make(size, back, fore, []byte("Make"))
  79. a.NotError(err).NotNil(img)
  80. }
  81. }
  82. // BenchmarkIdenticon_Make 10000 222127 ns/op
  83. func BenchmarkIdenticon_Make(b *testing.B) {
  84. a := assert.New(b)
  85. ii, err := New(size, back, fores...)
  86. a.NotError(err).NotNil(ii)
  87. for i := 0; i < b.N; i++ {
  88. img := ii.Make([]byte("Make"))
  89. a.NotNil(img)
  90. }
  91. }