RectTests.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. using System;
  2. using Terminal.Gui;
  3. using Xunit;
  4. namespace Terminal.Gui.TypeTests {
  5. public class RectTests {
  6. [Fact]
  7. public void Rect_New ()
  8. {
  9. var rect = new Rect ();
  10. Assert.True (rect.IsEmpty);
  11. rect = new Rect (new Point (), new Size ());
  12. Assert.True (rect.IsEmpty);
  13. rect = new Rect (1, 2, 3, 4);
  14. Assert.False (rect.IsEmpty);
  15. rect = new Rect (-1, -2, 3, 4);
  16. Assert.False (rect.IsEmpty);
  17. Action action = () => new Rect (1, 2, -3, 4);
  18. var ex = Assert.Throws<ArgumentException> (action);
  19. Assert.Equal ("Width must be greater or equal to 0.", ex.Message);
  20. action = () => new Rect (1, 2, 3, -4);
  21. ex = Assert.Throws<ArgumentException> (action);
  22. Assert.Equal ("Height must be greater or equal to 0.", ex.Message);
  23. action = () => new Rect (1, 2, -3, -4);
  24. ex = Assert.Throws<ArgumentException> (action);
  25. Assert.Equal ("Width must be greater or equal to 0.", ex.Message);
  26. }
  27. [Fact]
  28. public void Rect_SetsValue ()
  29. {
  30. var rect = new Rect () {
  31. X = 0,
  32. Y = 0
  33. };
  34. Assert.True (rect.IsEmpty);
  35. rect = new Rect () {
  36. X = -1,
  37. Y = -2
  38. };
  39. Assert.False (rect.IsEmpty);
  40. rect = new Rect () {
  41. Width = 3,
  42. Height = 4
  43. };
  44. Assert.False (rect.IsEmpty);
  45. rect = new Rect () {
  46. X = -1,
  47. Y = -2,
  48. Width = 3,
  49. Height = 4
  50. };
  51. Assert.False (rect.IsEmpty);
  52. Action action = () => {
  53. rect = new Rect () {
  54. X = -1,
  55. Y = -2,
  56. Width = -3,
  57. Height = 4
  58. };
  59. };
  60. var ex = Assert.Throws<ArgumentException> (action);
  61. Assert.Equal ("Width must be greater or equal to 0.", ex.Message);
  62. action = () => {
  63. rect = new Rect () {
  64. X = -1,
  65. Y = -2,
  66. Width = 3,
  67. Height = -4
  68. };
  69. };
  70. ex = Assert.Throws<ArgumentException> (action);
  71. Assert.Equal ("Height must be greater or equal to 0.", ex.Message);
  72. action = () => {
  73. rect = new Rect () {
  74. X = -1,
  75. Y = -2,
  76. Width = -3,
  77. Height = -4
  78. };
  79. };
  80. ex = Assert.Throws<ArgumentException> (action);
  81. Assert.Equal ("Width must be greater or equal to 0.", ex.Message);
  82. }
  83. [Fact]
  84. public void Rect_Equals ()
  85. {
  86. var rect1 = new Rect ();
  87. var rect2 = new Rect ();
  88. Assert.Equal (rect1, rect2);
  89. rect1 = new Rect (1, 2, 3, 4);
  90. rect2 = new Rect (1, 2, 3, 4);
  91. Assert.Equal (rect1, rect2);
  92. rect1 = new Rect (1, 2, 3, 4);
  93. rect2 = new Rect (-1, 2, 3, 4);
  94. Assert.NotEqual (rect1, rect2);
  95. }
  96. [Fact]
  97. public void Positive_X_Y_Positions ()
  98. {
  99. var rect = new Rect (10, 5, 100, 50);
  100. int yCount = 0, xCount = 0, yxCount = 0;
  101. for (int line = rect.Y; line < rect.Y + rect.Height; line++) {
  102. yCount++;
  103. xCount = 0;
  104. for (int col = rect.X; col < rect.X + rect.Width; col++) {
  105. xCount++;
  106. yxCount++;
  107. }
  108. }
  109. Assert.Equal (yCount, rect.Height);
  110. Assert.Equal (xCount, rect.Width);
  111. Assert.Equal (yxCount, rect.Height * rect.Width);
  112. }
  113. [Fact]
  114. public void Negative_X_Y_Positions ()
  115. {
  116. var rect = new Rect (-10, -5, 100, 50);
  117. int yCount = 0, xCount = 0, yxCount = 0;
  118. for (int line = rect.Y; line < rect.Y + rect.Height; line++) {
  119. yCount++;
  120. xCount = 0;
  121. for (int col = rect.X; col < rect.X + rect.Width; col++) {
  122. xCount++;
  123. yxCount++;
  124. }
  125. }
  126. Assert.Equal (yCount, rect.Height);
  127. Assert.Equal (xCount, rect.Width);
  128. Assert.Equal (yxCount, rect.Height * rect.Width);
  129. }
  130. [Theory]
  131. // Empty
  132. [InlineData (
  133. 0, 0, 0, 0,
  134. 0, 0,
  135. 0, 0, 0, 0)]
  136. [InlineData (
  137. 0, 0, 0, 0,
  138. 1, 0,
  139. -1, 0, 2, 0)]
  140. [InlineData (
  141. 0, 0, 0, 0,
  142. 0, 1,
  143. 0, -1, 0, 2)]
  144. [InlineData (
  145. 0, 0, 0, 0,
  146. 1, 1,
  147. -1, -1, 2, 2)]
  148. [InlineData (
  149. 0, 0, 0, 0,
  150. -1, -1, // Throws
  151. 0, 0, 0, 0)]
  152. // Zero location, Size of 1
  153. [InlineData (
  154. 0, 0, 1, 1,
  155. 0, 0,
  156. 0, 0, 1, 1)]
  157. [InlineData (
  158. 0, 0, 1, 1,
  159. 1, 0,
  160. -1, 0, 3, 1)]
  161. [InlineData (
  162. 0, 0, 1, 1,
  163. 0, 1,
  164. 0, -1, 1, 3)]
  165. [InlineData (
  166. 0, 0, 1, 1,
  167. 1, 1,
  168. -1, -1, 3, 3)]
  169. // Positive location, Size of 1
  170. [InlineData (
  171. 1, 1, 1, 1,
  172. 0, 0,
  173. 1, 1, 1, 1)]
  174. [InlineData (
  175. 1, 1, 1, 1,
  176. 1, 0,
  177. 0, 1, 3, 1)]
  178. [InlineData (
  179. 1, 1, 1, 1,
  180. 0, 1,
  181. 1, 0, 1, 3)]
  182. [InlineData (
  183. 1, 1, 1, 1,
  184. 1, 1,
  185. 0, 0, 3, 3)]
  186. public void Inflate (int x, int y, int width, int height, int inflateWidth, int inflateHeight, int expectedX, int exptectedY, int expectedWidth, int expectedHeight)
  187. {
  188. var rect = new Rect (x, y, width, height);
  189. if (rect.Width + inflateWidth < 0 || rect.Height + inflateHeight < 0) {
  190. Assert.Throws<ArgumentException> (() => rect.Inflate (inflateWidth, inflateHeight));
  191. } else {
  192. rect.Inflate (inflateWidth, inflateHeight);
  193. }
  194. Assert.Equal (expectedWidth, rect.Width);
  195. Assert.Equal (expectedHeight, rect.Height);
  196. Assert.Equal (expectedX, rect.X);
  197. Assert.Equal (exptectedY, rect.Y);
  198. // Use the other overload (Size)
  199. rect = new Rect (x, y, width, height);
  200. if (rect.Width + inflateWidth < 0 || rect.Height + inflateHeight < 0) {
  201. Assert.Throws<ArgumentException> (() => rect.Inflate (new Size (inflateWidth, inflateHeight)));
  202. } else {
  203. rect.Inflate (new Size (inflateWidth, inflateHeight));
  204. }
  205. Assert.Equal (expectedWidth, rect.Width);
  206. Assert.Equal (expectedHeight, rect.Height);
  207. Assert.Equal (expectedX, rect.X);
  208. Assert.Equal (exptectedY, rect.Y);
  209. }
  210. [Fact]
  211. public void Union_PositiveCoords ()
  212. {
  213. var r1 = new Rect (0, 0, 2, 2);
  214. var r2 = new Rect (1, 1, 2, 2);
  215. var result = Rect.Union (r1, r2);
  216. Assert.Equal (new Rect (0, 0, 3, 3), result);
  217. }
  218. [Fact]
  219. public void Union_NegativeCoords ()
  220. {
  221. // arrange
  222. Rect rect1 = new Rect (-2, -2, 4, 4);
  223. Rect rect2 = new Rect (-1, -1, 5, 5);
  224. // act
  225. Rect result = Rect.Union (rect1, rect2);
  226. // assert
  227. Assert.Equal (new Rect (-2, -2, 6, 6), result);
  228. }
  229. [Fact]
  230. public void Union_EmptyRectangles ()
  231. {
  232. var r1 = new Rect (0, 0, 0, 0);
  233. var r2 = new Rect (1, 1, 0, 0);
  234. var result = Rect.Union (r1, r2);
  235. Assert.Equal (new Rect (0, 0, 1, 1), result);
  236. }
  237. [Fact]
  238. public void Union_SameRectangle ()
  239. {
  240. var r1 = new Rect (0, 0, 2, 2);
  241. var r2 = new Rect (0, 0, 2, 2);
  242. var result = Rect.Union (r1, r2);
  243. Assert.Equal (new Rect (0, 0, 2, 2), result);
  244. }
  245. [Fact]
  246. public void Union_RectanglesOverlap_ReturnsCombinedRectangle ()
  247. {
  248. // arrange
  249. var rect1 = new Rect (1, 1, 3, 3);
  250. var rect2 = new Rect (2, 2, 3, 3);
  251. // act
  252. var result = Rect.Union (rect1, rect2);
  253. // assert
  254. Assert.Equal (new Rect (1, 1, 4, 4), result);
  255. }
  256. [Fact]
  257. public void Union_RectanglesTouchHorizontally_ReturnsCombinedRectangle ()
  258. {
  259. // arrange
  260. var rect1 = new Rect (1, 1, 3, 3);
  261. var rect2 = new Rect (4, 2, 3, 3);
  262. // act
  263. var result = Rect.Union (rect1, rect2);
  264. // assert
  265. Assert.Equal (new Rect (1, 1, 6, 4), result);
  266. }
  267. [Fact]
  268. public void Union_RectanglesTouchVertically_ReturnsCombinedRectangle ()
  269. {
  270. // arrange
  271. var rect1 = new Rect (1, 1, 3, 3);
  272. var rect2 = new Rect (2, 4, 3, 3);
  273. // act
  274. var result = Rect.Union (rect1, rect2);
  275. // assert
  276. Assert.Equal (new Rect (1, 1, 4, 6), result);
  277. }
  278. [Fact]
  279. public void Union_RectanglesDoNotOverlap_ReturnsCombinedRectangle ()
  280. {
  281. // arrange
  282. var rect1 = new Rect (1, 1, 3, 3);
  283. var rect2 = new Rect (5, 5, 3, 3);
  284. // act
  285. var result = Rect.Union (rect1, rect2);
  286. // assert
  287. Assert.Equal (new Rect (1, 1, 7, 7), result);
  288. }
  289. [Fact]
  290. public void Union_RectangleBIsLarger_ReturnsB ()
  291. {
  292. // arrange
  293. var rect1 = new Rect (1, 1, 3, 3);
  294. var rect2 = new Rect (2, 2, 6, 6);
  295. // act
  296. var result = Rect.Union (rect1, rect2);
  297. // assert
  298. Assert.Equal (new Rect (1, 1, 7, 7), result);
  299. }
  300. [Fact]
  301. public void Union_RectangleAIsLarger_ReturnsA ()
  302. {
  303. // arrange
  304. var rect1 = new Rect (1, 1, 6, 6);
  305. var rect2 = new Rect (2, 2, 3, 3);
  306. // act
  307. var result = Rect.Union (rect1, rect2);
  308. // assert
  309. Assert.Equal (new Rect (1, 1, 6, 6), result);
  310. }
  311. [Fact]
  312. public void Union_RectangleAHasNegativeCoordinates_ReturnsCombinedRectangle ()
  313. {
  314. // arrange
  315. var rect1 = new Rect (-2, -2, 5, 5);
  316. var rect2 = new Rect (3, 3, 4, 4);
  317. // act
  318. var result = Rect.Union (rect1, rect2);
  319. // assert
  320. Assert.Equal (new Rect (-2, -2, 9, 9), result);
  321. }
  322. [Fact]
  323. public void Rect_Contains ()
  324. {
  325. var rect = new Rect (0, 0, 3, 3);
  326. Assert.True (rect.Contains (new Point (1, 1)));
  327. Assert.True (rect.Contains (new Point (1, 2)));
  328. Assert.True (rect.Contains (new Point (2, 1)));
  329. Assert.True (rect.Contains (new Point (2, 2)));
  330. Assert.False (rect.Contains (new Point (-1, 1)));
  331. Assert.False (rect.Contains (new Point (1, -1)));
  332. Assert.False (rect.Contains (new Point (3, 2)));
  333. Assert.False (rect.Contains (new Point (2, 3)));
  334. Assert.False (rect.Contains (new Point (3, 3)));
  335. Assert.True (rect.Contains (new Rect (1, 1, 2, 2)));
  336. Assert.True (rect.Contains (new Rect (1, 2, 2, 1)));
  337. Assert.True (rect.Contains (new Rect (2, 1, 1, 2)));
  338. Assert.True (rect.Contains (new Rect (2, 2, 1, 1)));
  339. Assert.True (rect.Contains (new Rect (0, 0, 3, 3)));
  340. Assert.False (rect.Contains (new Rect (-1, 1, 3, 3)));
  341. Assert.False (rect.Contains (new Rect (1, -1, 3, 3)));
  342. Assert.False (rect.Contains (new Rect (3, 2, 3, 3)));
  343. Assert.False (rect.Contains (new Rect (2, 3, 3, 3)));
  344. Assert.False (rect.Contains (new Rect (3, 3, 3, 3)));
  345. Assert.True (rect.Contains (1, 1));
  346. Assert.True (rect.Contains (1, 2));
  347. Assert.True (rect.Contains (2, 1));
  348. Assert.True (rect.Contains (2, 2));
  349. Assert.False (rect.Contains (-1, 1));
  350. Assert.False (rect.Contains (1, -1));
  351. Assert.False (rect.Contains (3, 2));
  352. Assert.False (rect.Contains (2, 3));
  353. Assert.False (rect.Contains (3, 3));
  354. }
  355. }
  356. }