point_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package point2d
  2. import (
  3. "testing"
  4. )
  5. func TestPoint2Set(t *testing.T) {
  6. p := &Point{
  7. 1,
  8. 3,
  9. }
  10. if p.X != 1 || p.Y != 3 {
  11. t.Fail()
  12. t.Logf("Point expected to be '1 3' got '%d %d'", p.X, p.Y)
  13. }
  14. if p.String() != "1 3" {
  15. if !t.Failed() {
  16. t.Fail()
  17. }
  18. t.Logf("Point.String() expected to be '1 3' got '%s'", p.String())
  19. }
  20. p.Set(2)
  21. if p.X != 2 || p.Y != 2 {
  22. if !t.Failed() {
  23. t.Fail()
  24. }
  25. t.Logf("Point expected to be '2 2' got '%s'", p)
  26. }
  27. p.SetTo(&Point{3, 4})
  28. if p.X != 3 || p.Y != 4 {
  29. if !t.Failed() {
  30. t.Fail()
  31. }
  32. t.Logf("Point expected to be '3 4' got '%s'", p)
  33. }
  34. }
  35. func TestPoint2Move(t *testing.T) {
  36. p := &Point{1, 1}
  37. if p.X != 1 || p.Y != 1 {
  38. t.Fail()
  39. t.Logf("Point expected to be '1 1' got '%s'", p)
  40. }
  41. p.Move(1, -1)
  42. if p.X != 2 || p.Y != 0 {
  43. if !t.Failed() {
  44. t.Fail()
  45. }
  46. t.Logf("Point expected to be '2 0' got '%s'", p)
  47. }
  48. p.Set(1, 1)
  49. p.MoveBy(&Point{1, -1})
  50. if p.X != 2 || p.Y != 0 {
  51. if !t.Failed() {
  52. t.Fail()
  53. }
  54. t.Logf("Point expected to be '2 0' got '%s'", p)
  55. }
  56. p.Move(-2, 2).Move(1, -1)
  57. if p.X != 1 || p.Y != 1 {
  58. if !t.Failed() {
  59. t.Fail()
  60. }
  61. t.Logf("Point expected to be '1 1' got '%s'", p)
  62. }
  63. p.Set(1, 1)
  64. p.Move(3)
  65. if !p.Equal(4) {
  66. if !t.Failed() {
  67. t.Fail()
  68. }
  69. t.Logf("Point expected to be '4 4' got '%s'", p)
  70. }
  71. if p.Equal() {
  72. if !t.Failed() {
  73. t.Fail()
  74. }
  75. t.Logf("Point.Equal called with no values, should always return false")
  76. }
  77. }
  78. func TestPoint2Zero(t *testing.T) {
  79. p := &Point{1, 1}
  80. if p.X != 1 || p.Y != 1 {
  81. t.Fail()
  82. t.Logf("Point expected to be '1 1' got '%s'", p)
  83. }
  84. if p.IsZero() {
  85. if !t.Failed() {
  86. t.Fail()
  87. }
  88. t.Logf("Point not zero, Point is '%s'", p)
  89. }
  90. p.Zero()
  91. if p.X != 0 || p.Y != 0 {
  92. if !t.Failed() {
  93. t.Fail()
  94. }
  95. t.Logf("Point expected to be '0 0' got '%s'", p)
  96. }
  97. if !p.IsZero() {
  98. if !t.Failed() {
  99. t.Fail()
  100. }
  101. t.Logf("Point is zero, Point is '%s'", p)
  102. }
  103. }
  104. func TestPoint2Negate(t *testing.T) {
  105. p := &Point{1, 1}
  106. if !p.Equal(1) {
  107. t.Fail()
  108. t.Logf("Point expected to be '1 1' got '%s'", p)
  109. }
  110. p.Negate()
  111. if p.X != -1 || p.Y != -1 {
  112. if !t.Failed() {
  113. t.Fail()
  114. }
  115. t.Logf("Point expected to be '-1 -1' got '%s'", p)
  116. }
  117. p.Negate()
  118. if !p.EqualTo(&Point{1, 1}) {
  119. if !t.Failed() {
  120. t.Fail()
  121. }
  122. t.Logf("Point expected to be '1 1' got '%s'", p)
  123. }
  124. p.Set(-3, -4)
  125. if !p.Equal(-3, -4) {
  126. if !t.Failed() {
  127. t.Fail()
  128. }
  129. t.Logf("Point expected ot be '-3 -4' got '%s'", p)
  130. }
  131. p.Abs()
  132. if !p.Equal(3, 4) {
  133. if !t.Failed() {
  134. t.Fail()
  135. }
  136. t.Logf("Point expected to be '3 4' got '%s'", p)
  137. }
  138. }
  139. func TestPoint2Copy(t *testing.T) {
  140. p := &Point{3, 4}
  141. p2 := p.Copy()
  142. p.Set(2)
  143. if p2.EqualTo(p) {
  144. t.Fail()
  145. t.Logf("p2 expected to not equal '2 2' got '%s' (Point = '%s')", p2, p)
  146. }
  147. }
  148. func TestPoint2Axis(t *testing.T) {
  149. p := &Point{3, 4}
  150. if p.IsX(2) {
  151. t.Fail()
  152. t.Logf("Point not on X axis 2, Point is '%s'", p)
  153. }
  154. if !p.IsY(4) {
  155. if !t.Failed() {
  156. t.Fail()
  157. }
  158. t.Logf("Point is on Y axis 4, Point is '%s'", p)
  159. }
  160. }
  161. func TestPoint2LessGreater(t *testing.T) {
  162. p := &Point{3, 4}
  163. if !p.Equal(3, 4) {
  164. t.Fail()
  165. t.Logf("Point expected to be '3 4' got '%s'", p)
  166. }
  167. if p.Greater(5) {
  168. if !t.Failed() {
  169. t.Fail()
  170. }
  171. t.Logf("Point should be less than 5, Point is '%s'", p)
  172. }
  173. if !p.Greater(3, 3) {
  174. if !t.Failed() {
  175. t.Fail()
  176. }
  177. t.Logf("Point should be greater than 3 3, Point is '%s'", p)
  178. }
  179. if p.Greater() {
  180. if !t.Failed() {
  181. t.Fail()
  182. }
  183. t.Logf("Point.Greater() should always return false")
  184. }
  185. if !p.GreaterThan(&Point{2, 2}) {
  186. if !t.Failed() {
  187. t.Fail()
  188. }
  189. t.Logf("Point should be greater than '2 2', Point is '%s'", p)
  190. }
  191. if p.Less(1) {
  192. if !t.Failed() {
  193. t.Fail()
  194. }
  195. t.Logf("Point should be greater than 1, Point is '%s'", p)
  196. }
  197. if !p.Less(10, 10) {
  198. if !t.Failed() {
  199. t.Fail()
  200. }
  201. t.Logf("Point should be less than 10 10, Point is '%s'", p)
  202. }
  203. if p.Less() {
  204. if !t.Failed() {
  205. t.Fail()
  206. }
  207. t.Logf("Point.Less() should always return false")
  208. }
  209. if !p.LessThan(&Point{15, 15}) {
  210. if !t.Failed() {
  211. t.Fail()
  212. }
  213. t.Logf("Point should be less than '15 15', Point is '%s'", p)
  214. }
  215. }
  216. func TestPoint2Distance(t *testing.T) {
  217. p := &Point{3, 4}
  218. if !p.Equal(3, 4) {
  219. t.Fail()
  220. t.Logf("Point expected to be '3 4' got '%s'", p)
  221. }
  222. if p.Distance(0, 0) != 4 {
  223. if !t.Failed() {
  224. t.Fail()
  225. }
  226. t.Logf("Point distance to 0 0 should be 4, got %d", p.Distance(0, 0))
  227. }
  228. if p.DistanceTo(&Point{0, 0}) != 4 {
  229. if !t.Failed() {
  230. t.Fail()
  231. }
  232. t.Logf("Point distance to '0 0' should be 4, got %d", p.DistanceTo(&Point{0, 0}))
  233. }
  234. }
  235. func TestPoint2Within(t *testing.T) {
  236. p := &Point{5, 5}
  237. if !p.Equal(5) {
  238. t.Fail()
  239. t.Logf("Points not equal assigned values p='%s'", p)
  240. }
  241. if !p.Within(&Point{5, 5}, &Point{10, 10}) {
  242. if !t.Failed() {
  243. t.Fail()
  244. }
  245. t.Logf("p == topLeft, but returned false")
  246. }
  247. p.Set(10, 10)
  248. if !p.Within(&Point{5, 5}, &Point{10, 10}) {
  249. if !t.Failed() {
  250. t.Fail()
  251. }
  252. t.Logf("p == botRight, but returned false")
  253. }
  254. p.Set(5, 5)
  255. var (
  256. tmp *Point = &Point{}
  257. y int
  258. x int
  259. )
  260. for y = 0; y <= 11; y++ {
  261. for x = 0; x <= 11; x++ {
  262. tmp.Set(x, y)
  263. if tmp.X == 0 || tmp.Y == 0 {
  264. if !tmp.Within(&Point{0, 0}, &Point{11, 11}) {
  265. if !t.Failed() {
  266. t.Fail()
  267. }
  268. t.Logf("'%s' not in '0 0' '11 11'?", tmp)
  269. }
  270. }
  271. if !tmp.Within(&Point{}, &Point{11, 11}) {
  272. if !t.Failed() {
  273. t.Fail()
  274. }
  275. t.Logf("'%s' not in '0 0' '11 11'?", tmp)
  276. }
  277. }
  278. }
  279. if p.Within(&Point{20, 25}, &Point{15, 12}) {
  280. if !t.Failed() {
  281. t.Fail()
  282. }
  283. t.Logf("'%s' in '15 12' '20 25'?", p)
  284. }
  285. if p.Within(&Point{10, 10}, &Point{10, 10}) {
  286. if !t.Failed() {
  287. t.Fail()
  288. }
  289. t.Logf("'%s' in '10 10' '10 10'?", p)
  290. }
  291. }
  292. func TestPointNew(t *testing.T) {
  293. var p *Point = NewPoint()
  294. if p == nil {
  295. t.Fail()
  296. t.Logf("Expected a point at '0 0', but got nil")
  297. }
  298. if !p.IsZero() {
  299. if !t.Failed() {
  300. t.Fail()
  301. }
  302. t.Logf("Expected a point at '0 0', but got '%s'", p)
  303. }
  304. }
  305. func TestPointAs(t *testing.T) {
  306. var p *Point = AsPoint(3, 2)
  307. if p == nil {
  308. t.Fail()
  309. t.Logf("Expected a point, got nil")
  310. }
  311. if !p.Equal(3, 2) {
  312. if !t.Failed() {
  313. t.Fail()
  314. }
  315. t.Logf("Expected a point at '3 2', got '%s'", p)
  316. }
  317. p = AsPoint(5)
  318. if p == nil {
  319. if !t.Failed() {
  320. t.Fail()
  321. }
  322. t.Logf("Expected a point, got nil")
  323. }
  324. if !p.Equal(5, 5) {
  325. if !t.Failed() {
  326. t.Fail()
  327. }
  328. t.Logf("Expected a point at '5 5', got '%s'", p)
  329. }
  330. }