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.

82 lines
1.4 KiB

  1. package render_test
  2. import (
  3. "strconv"
  4. "testing"
  5. "git.kirsle.net/go/render"
  6. )
  7. func TestIntersection(t *testing.T) {
  8. newRect := func(x, y, w, h int) render.Rect {
  9. return render.Rect{
  10. X: x,
  11. Y: y,
  12. W: w,
  13. H: h,
  14. }
  15. }
  16. type TestCase struct {
  17. A render.Rect
  18. B render.Rect
  19. Expect bool
  20. }
  21. var tests = []TestCase{
  22. {
  23. A: newRect(0, 0, 1000, 1000),
  24. B: newRect(200, 200, 100, 100),
  25. Expect: true,
  26. },
  27. {
  28. A: newRect(200, 200, 100, 100),
  29. B: newRect(0, 0, 1000, 1000),
  30. Expect: true,
  31. },
  32. {
  33. A: newRect(0, 0, 100, 100),
  34. B: newRect(100, 0, 100, 100),
  35. Expect: true,
  36. },
  37. {
  38. A: newRect(0, 0, 99, 99),
  39. B: newRect(100, 0, 99, 99),
  40. Expect: false,
  41. },
  42. {
  43. // Real coords of a test doodad!
  44. A: newRect(183, 256, 283, 356),
  45. B: newRect(0, -232, 874, 490),
  46. Expect: true,
  47. },
  48. {
  49. A: newRect(183, 256, 283, 356),
  50. B: newRect(0, -240, 874, 490),
  51. Expect: false, // XXX: must be true
  52. },
  53. {
  54. A: newRect(0, 30, 9, 62),
  55. B: newRect(16, 0, 32, 64),
  56. Expect: false,
  57. },
  58. {
  59. A: newRect(0, 30, 11, 62),
  60. B: newRect(7, 4, 17, 28),
  61. Expect: true,
  62. },
  63. }
  64. for _, test := range tests {
  65. actual := test.A.Intersects(test.B)
  66. if actual != test.Expect {
  67. t.Errorf(
  68. "%s collision with %s: expected %s, got %s",
  69. test.A,
  70. test.B,
  71. strconv.FormatBool(test.Expect),
  72. strconv.FormatBool(actual),
  73. )
  74. }
  75. }
  76. }