apprenda take home assessment
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.

50 lines
1.2 KiB

  1. package rect
  2. import "testing"
  3. func TestOnLine(t *testing.T) {
  4. var onLineTest = []struct {
  5. p []Point
  6. expected bool
  7. }{
  8. {[]Point{{1, 1}, {3, 3}, {2, 2}}, true},
  9. {[]Point{{1, 1}, {3, 3}, {4, 4}}, false},
  10. {[]Point{{-1, -1}, {-3, -3}, {-4, -4}}, false},
  11. {[]Point{{-1, -1}, {-3, -3}, {-2, -2}}, true},
  12. }
  13. for _, rt := range onLineTest {
  14. actual := onLine(rt.p[0], rt.p[1], rt.p[2])
  15. if actual != rt.expected {
  16. t.Errorf(
  17. "failed onLine:\n\texpected: %t\n\t actual: %t",
  18. rt.expected,
  19. actual,
  20. )
  21. }
  22. }
  23. }
  24. func TestLineIntersection(t *testing.T) {
  25. var lineIntersectionTest = []struct {
  26. l []line
  27. expected Point
  28. }{
  29. {[]line{{Point{0, 0}, Point{2, 2}}, {Point{2, 0}, Point{0, 2}}}, Point{1, 1}},
  30. {[]line{{Point{0, 0}, Point{-2, -2}}, {Point{-2, 0}, Point{0, -2}}}, Point{-1, -1}},
  31. {[]line{{Point{0, 0}, Point{-2, -2}}, {Point{1, 0}, Point{1, -7}}}, Point{0, 0}},
  32. {[]line{{Point{5, 8}, Point{8, 5}}, {Point{3, 7}, Point{7, 3}}}, Point{0, 0}},
  33. }
  34. for _, rt := range lineIntersectionTest {
  35. actual, _ := lineIntersection(rt.l[0], rt.l[1])
  36. if actual != rt.expected {
  37. t.Errorf(
  38. "failed lineIntersection:\n\texpected: %v\n\t actual: %v",
  39. rt.expected,
  40. actual,
  41. )
  42. }
  43. }
  44. }