RectTests.cs 13 KB

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