RectTest.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // RectTest.swift
  3. //
  4. // Created by Giles Payne on 2020/01/31.
  5. //
  6. import XCTest
  7. import OpenCV
  8. class RectTest: OpenCVTestCase {
  9. let r = Rect()
  10. let rect = Rect(x: 0, y: 0, width: 10, height: 10)
  11. func testArea() {
  12. let area = rect.area()
  13. XCTAssertEqual(100.0, area)
  14. }
  15. func testBr() {
  16. let p_br = rect.br()
  17. let truth = Point(x: 10, y: 10)
  18. XCTAssertEqual(truth, p_br)
  19. }
  20. func testClone() {
  21. let r = rect.clone()
  22. XCTAssertEqual(rect, r)
  23. }
  24. func testContains() {
  25. let rect = Rect(x: 0, y: 0, width: 10, height: 10)
  26. let p_inner = Point(x: 5, y: 5)
  27. let p_outer = Point(x: 5, y: 55)
  28. let p_bl = Point(x: 0, y: 0)
  29. let p_br = Point(x: 10, y: 0)
  30. let p_tl = Point(x: 0, y: 10)
  31. let p_tr = Point(x: 10, y: 10)
  32. XCTAssert(rect.contains(p_inner))
  33. XCTAssert(rect.contains(p_bl))
  34. XCTAssertFalse(rect.contains(p_outer))
  35. XCTAssertFalse(rect.contains(p_br))
  36. XCTAssertFalse(rect.contains(p_tl))
  37. XCTAssertFalse(rect.contains(p_tr))
  38. }
  39. func testEqualsObject() {
  40. var flag = rect == r
  41. XCTAssertFalse(flag)
  42. let r = rect.clone()
  43. flag = rect == r
  44. XCTAssert(flag)
  45. }
  46. func testHashCode() {
  47. XCTAssertEqual(rect.hash(), rect.hash())
  48. }
  49. func testRect() {
  50. let r = Rect()
  51. XCTAssertEqual(0, r.x)
  52. XCTAssertEqual(0, r.y)
  53. XCTAssertEqual(0, r.width)
  54. XCTAssertEqual(0, r.height)
  55. }
  56. func testRectDoubleArray() {
  57. let vals:[Double] = [1, 3, 5, 2]
  58. let r = Rect(vals: vals as [NSNumber])
  59. XCTAssertEqual(1, r.x)
  60. XCTAssertEqual(3, r.y)
  61. XCTAssertEqual(5, r.width)
  62. XCTAssertEqual(2, r.height)
  63. }
  64. func testRectIntIntIntInt() {
  65. let rect = Rect(x: 1, y: 3, width: 5, height: 2)
  66. XCTAssertNotNil(rect)
  67. XCTAssertEqual(1, rect.x)
  68. XCTAssertEqual(3, rect.y)
  69. XCTAssertEqual(5, rect.width)
  70. XCTAssertEqual(2, rect.height)
  71. }
  72. func testRectPointPoint() {
  73. let p1 = Point(x:4, y:4)
  74. let p2 = Point(x: 2, y: 3)
  75. let r = Rect(point: p1, point: p2)
  76. XCTAssertNotNil(r);
  77. XCTAssertEqual(2, r.x);
  78. XCTAssertEqual(3, r.y);
  79. XCTAssertEqual(2, r.width);
  80. XCTAssertEqual(1, r.height);
  81. }
  82. func testRect2fPointPoint() {
  83. let p1 = Point2f(x:4.3, y:4.1)
  84. let p2 = Point2f(x:2.7, y:3.9)
  85. let r = Rect2f(point: p1, point: p2)
  86. XCTAssertNotNil(r);
  87. XCTAssertEqual(2.7, r.x);
  88. XCTAssertEqual(3.9, r.y);
  89. XCTAssertEqual(1.6, r.width, accuracy: OpenCVTestCase.FEPS);
  90. XCTAssertEqual(0.2, r.height, accuracy: OpenCVTestCase.FEPS);
  91. }
  92. func testRect2dPointPoint() {
  93. let p1 = Point2d(x:4.7879839, y:4.9922311)
  94. let p2 = Point2d(x:2.1213123, y:3.1122129)
  95. let r = Rect2d(point: p1, point: p2)
  96. XCTAssertNotNil(r);
  97. XCTAssertEqual(2.1213123, r.x);
  98. XCTAssertEqual(3.1122129, r.y);
  99. XCTAssertEqual(2.6666716, r.width, accuracy: OpenCVTestCase.EPS);
  100. XCTAssertEqual(1.8800182, r.height, accuracy: OpenCVTestCase.EPS);
  101. }
  102. func testRectPointSize() {
  103. let p1 = Point(x: 4, y: 4)
  104. let sz = Size(width: 3, height: 1)
  105. let r = Rect(point: p1, size: sz)
  106. XCTAssertEqual(4, r.x)
  107. XCTAssertEqual(4, r.y)
  108. XCTAssertEqual(3, r.width)
  109. XCTAssertEqual(1, r.height)
  110. }
  111. func testSet() {
  112. let vals1:[Double] = []
  113. let r1 = Rect(vals:vals1 as [NSNumber])
  114. XCTAssertEqual(0, r1.x)
  115. XCTAssertEqual(0, r1.y)
  116. XCTAssertEqual(0, r1.width)
  117. XCTAssertEqual(0, r1.height)
  118. let vals2:[Double] = [2, 2, 10, 5]
  119. let r = Rect(vals: vals2 as [NSNumber])
  120. XCTAssertEqual(2, r.x)
  121. XCTAssertEqual(2, r.y)
  122. XCTAssertEqual(10, r.width)
  123. XCTAssertEqual(5, r.height)
  124. }
  125. func testSize() {
  126. let s1 = Size(width: 0, height: 0)
  127. XCTAssertEqual(s1, r.size())
  128. let s2 = Size(width: 10, height: 10)
  129. XCTAssertEqual(s2, rect.size())
  130. }
  131. func testTl() {
  132. let p_tl = rect.tl()
  133. let truth = Point(x: 0, y: 0)
  134. XCTAssertEqual(truth, p_tl)
  135. }
  136. func testToString() {
  137. let actual = "\(rect)"
  138. let expected = "Rect2i {0,0,10,10}"
  139. XCTAssertEqual(expected, actual);
  140. }
  141. }