A rendering engine library for Go supporting both SDL2 and WebAssembly (HTML Canvas) targets.
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.

100 lines
1.8 KiB

  1. package render_test
  2. import (
  3. "strconv"
  4. "testing"
  5. "git.kirsle.net/go/render"
  6. )
  7. func TestPointInside(t *testing.T) {
  8. type testCase struct {
  9. rect render.Rect
  10. p render.Point
  11. shouldPass bool
  12. }
  13. tests := []testCase{
  14. testCase{
  15. rect: render.Rect{
  16. X: 0,
  17. Y: 0,
  18. W: 500,
  19. H: 500,
  20. },
  21. p: render.NewPoint(128, 256),
  22. shouldPass: true,
  23. },
  24. testCase{
  25. rect: render.Rect{
  26. X: 100,
  27. Y: 80,
  28. W: 40,
  29. H: 60,
  30. },
  31. p: render.NewPoint(128, 256),
  32. shouldPass: false,
  33. },
  34. testCase{
  35. // true values when debugging why Doodads weren't
  36. // considered inside the viewport.
  37. rect: render.Rect{
  38. X: 0,
  39. Y: -232,
  40. H: 874,
  41. W: 490,
  42. },
  43. p: render.NewPoint(509, 260),
  44. shouldPass: false,
  45. },
  46. }
  47. for _, test := range tests {
  48. if test.p.Inside(test.rect) != test.shouldPass {
  49. t.Errorf("Failed: %s inside %s should be %s",
  50. test.p,
  51. test.rect,
  52. strconv.FormatBool(test.shouldPass),
  53. )
  54. }
  55. }
  56. }
  57. // Test the Compare function of Point.
  58. func TestPointDelta(t *testing.T) {
  59. var tests = []struct {
  60. A render.Point // source
  61. B render.Point // comparator
  62. D render.Point // expected delta value
  63. }{
  64. {
  65. A: render.NewPoint(0, 0),
  66. B: render.NewPoint(10, 10),
  67. D: render.NewPoint(10, 10),
  68. },
  69. {
  70. A: render.NewPoint(128, 128),
  71. B: render.NewPoint(128, 128),
  72. D: render.NewPoint(0, 0),
  73. },
  74. {
  75. A: render.NewPoint(128, 128),
  76. B: render.NewPoint(127, 129),
  77. D: render.NewPoint(-1, 1),
  78. },
  79. {
  80. A: render.NewPoint(200, 500),
  81. B: render.NewPoint(180, 528),
  82. D: render.NewPoint(-20, 28),
  83. },
  84. }
  85. for _, test := range tests {
  86. actual := test.A.Compare(test.B)
  87. if actual != test.D {
  88. t.Errorf("Failed: (%s).Compare(%s) expected %s but got %s",
  89. test.A, test.B, test.D, actual,
  90. )
  91. }
  92. }
  93. }