imgutil_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package drawimg
  2. import (
  3. "image"
  4. "image/color"
  5. "image/jpeg"
  6. "io"
  7. "testing"
  8. "github.com/bouk/monkey"
  9. "github.com/smartystreets/goconvey/convey"
  10. )
  11. func TestDrawimgString(t *testing.T) {
  12. convey.Convey("String", t, func(ctx convey.C) {
  13. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  14. p1 := JPEG.String()
  15. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  16. ctx.So(p1, convey.ShouldNotBeNil)
  17. })
  18. })
  19. })
  20. }
  21. func TestDrawimgOpen(t *testing.T) {
  22. convey.Convey("Open", t, func(ctx convey.C) {
  23. var (
  24. filename = ""
  25. )
  26. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  27. p1, err := Open(filename)
  28. ctx.Convey("Then err should be nil.p1 should not be nil.", func(ctx convey.C) {
  29. ctx.So(p1, convey.ShouldBeNil)
  30. ctx.So(err, convey.ShouldNotBeNil)
  31. })
  32. })
  33. })
  34. }
  35. func TestDrawimgEncode(t *testing.T) {
  36. convey.Convey("Encode", t, func(ctx convey.C) {
  37. var (
  38. w io.Writer
  39. img = image.NewRGBA(imgRectangle)
  40. format = JPEG
  41. )
  42. monkeyJpegEncode()
  43. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  44. err := Encode(w, img, format)
  45. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  46. ctx.So(err, convey.ShouldBeNil)
  47. })
  48. })
  49. })
  50. }
  51. func TestDrawimgSave(t *testing.T) {
  52. convey.Convey("Save", t, func(ctx convey.C) {
  53. var (
  54. img image.Image
  55. filename = ""
  56. )
  57. ctx.Convey("When everything goes not positive", func(ctx convey.C) {
  58. err := Save(img, filename)
  59. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  60. ctx.So(err, convey.ShouldNotBeNil)
  61. })
  62. })
  63. })
  64. }
  65. func TestDrawimgNewNRGBA(t *testing.T) {
  66. convey.Convey("NewNRGBA", t, func(ctx convey.C) {
  67. var (
  68. width = int(0)
  69. height = int(0)
  70. fillColor color.Color
  71. )
  72. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  73. p1 := NewNRGBA(width, height, fillColor)
  74. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  75. ctx.So(p1, convey.ShouldNotBeNil)
  76. })
  77. })
  78. })
  79. }
  80. func TestDrawimgClone(t *testing.T) {
  81. convey.Convey("Clone", t, func(ctx convey.C) {
  82. var (
  83. rgba = image.NewRGBA(imgRectangle)
  84. rgba64 = image.NewNRGBA64(imgRectangle)
  85. )
  86. ctx.Convey("RGBA", func(ctx convey.C) {
  87. p1 := Clone(rgba)
  88. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  89. ctx.So(p1, convey.ShouldNotBeNil)
  90. })
  91. })
  92. ctx.Convey("RGBA64", func(ctx convey.C) {
  93. p1 := Clone(rgba64)
  94. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  95. ctx.So(p1, convey.ShouldNotBeNil)
  96. })
  97. })
  98. })
  99. }
  100. func TestDrawimgtoNRGBA(t *testing.T) {
  101. convey.Convey("toNRGBA", t, func(ctx convey.C) {
  102. var (
  103. img = image.NewRGBA64(imgRectangle)
  104. )
  105. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  106. p1 := toNRGBA(img)
  107. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  108. ctx.So(p1, convey.ShouldNotBeNil)
  109. })
  110. })
  111. })
  112. }
  113. func TestDrawimgAdjustFunc(t *testing.T) {
  114. convey.Convey("AdjustFunc", t, func(ctx convey.C) {
  115. var (
  116. img = image.NewRGBA64(imgRectangle)
  117. fn = func(c color.NRGBA) color.NRGBA { return c }
  118. )
  119. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  120. p1 := AdjustFunc(img, fn)
  121. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  122. ctx.So(p1, convey.ShouldNotBeNil)
  123. })
  124. })
  125. })
  126. }
  127. func TestDrawimgAdjustGamma(t *testing.T) {
  128. convey.Convey("AdjustGamma", t, func(ctx convey.C) {
  129. var (
  130. img = image.NewRGBA64(imgRectangle)
  131. gamma = float64(0)
  132. )
  133. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  134. p1 := AdjustGamma(img, gamma)
  135. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  136. ctx.So(p1, convey.ShouldNotBeNil)
  137. })
  138. })
  139. })
  140. }
  141. func TestDrawimgsigmoid(t *testing.T) {
  142. convey.Convey("sigmoid", t, func(ctx convey.C) {
  143. var (
  144. a = float64(0)
  145. b = float64(0)
  146. x = float64(0)
  147. )
  148. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  149. p1 := sigmoid(a, b, x)
  150. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  151. ctx.So(p1, convey.ShouldNotBeNil)
  152. })
  153. })
  154. })
  155. }
  156. func TestDrawimgAdjustSigmoid(t *testing.T) {
  157. convey.Convey("AdjustSigmoid", t, func(ctx convey.C) {
  158. var (
  159. img = image.NewRGBA64(imgRectangle)
  160. midpoint = float64(0)
  161. factor = float64(0)
  162. )
  163. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  164. p1 := AdjustSigmoid(img, midpoint, factor)
  165. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  166. ctx.So(p1, convey.ShouldNotBeNil)
  167. })
  168. })
  169. })
  170. }
  171. func TestDrawimgAdjustContrast(t *testing.T) {
  172. convey.Convey("AdjustContrast", t, func(ctx convey.C) {
  173. var (
  174. img = image.NewRGBA64(imgRectangle)
  175. percentage = float64(0)
  176. )
  177. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  178. p1 := AdjustContrast(img, percentage)
  179. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  180. ctx.So(p1, convey.ShouldNotBeNil)
  181. })
  182. })
  183. })
  184. }
  185. func TestDrawimgAdjustBrightness(t *testing.T) {
  186. convey.Convey("AdjustBrightness", t, func(ctx convey.C) {
  187. var (
  188. img = image.NewRGBA64(imgRectangle)
  189. percentage = float64(0)
  190. )
  191. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  192. p1 := AdjustBrightness(img, percentage)
  193. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  194. ctx.So(p1, convey.ShouldNotBeNil)
  195. })
  196. })
  197. })
  198. }
  199. func TestDrawimgGrayscale(t *testing.T) {
  200. convey.Convey("Grayscale", t, func(ctx convey.C) {
  201. var (
  202. img = image.NewRGBA64(imgRectangle)
  203. )
  204. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  205. p1 := Grayscale(img)
  206. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  207. ctx.So(p1, convey.ShouldNotBeNil)
  208. })
  209. })
  210. })
  211. }
  212. func TestDrawimgInvert(t *testing.T) {
  213. convey.Convey("Invert", t, func(ctx convey.C) {
  214. var (
  215. img = image.NewRGBA64(imgRectangle)
  216. )
  217. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  218. p1 := Invert(img)
  219. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  220. ctx.So(p1, convey.ShouldNotBeNil)
  221. })
  222. })
  223. })
  224. }
  225. func TestDrawimgparallel(t *testing.T) {
  226. convey.Convey("parallel", t, func(ctx convey.C) {
  227. var (
  228. dataSize = int(0)
  229. fn func(partStart int, partEnd int)
  230. )
  231. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  232. parallel(dataSize, fn)
  233. ctx.Convey("No return values", func(ctx convey.C) {
  234. })
  235. })
  236. })
  237. }
  238. func TestDrawimgabsint(t *testing.T) {
  239. convey.Convey("absint", t, func(ctx convey.C) {
  240. var (
  241. i = int(0)
  242. )
  243. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  244. p1 := absint(i)
  245. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  246. ctx.So(p1, convey.ShouldNotBeNil)
  247. })
  248. })
  249. })
  250. }
  251. func TestDrawimgclamp(t *testing.T) {
  252. convey.Convey("clamp", t, func(ctx convey.C) {
  253. var (
  254. x = float64(0)
  255. )
  256. ctx.Convey("When everything gose positive", func(ctx convey.C) {
  257. p1 := clamp(x)
  258. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  259. ctx.So(p1, convey.ShouldNotBeNil)
  260. })
  261. })
  262. })
  263. }
  264. func monkeyJpegEncode() {
  265. monkey.Patch(jpeg.Encode, func(_ io.Writer, _ image.Image, _ *jpeg.Options) error {
  266. return nil
  267. })
  268. }