StraightLineExtensionsTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.DrawingTests;
  4. public class StraightLineExtensionsTests (ITestOutputHelper output)
  5. {
  6. [Fact]
  7. [AutoInitShutdown]
  8. public void LineCanvasIntegrationTest ()
  9. {
  10. var lc = new LineCanvas ();
  11. lc.AddLine (Point.Empty, 10, Orientation.Horizontal, LineStyle.Single);
  12. lc.AddLine (new (9, 0), 5, Orientation.Vertical, LineStyle.Single);
  13. lc.AddLine (new (9, 4), -10, Orientation.Horizontal, LineStyle.Single);
  14. lc.AddLine (new (0, 4), -5, Orientation.Vertical, LineStyle.Single);
  15. OutputAssert.AssertEqual (
  16. output,
  17. @"
  18. ┌────────┐
  19. │ │
  20. │ │
  21. │ │
  22. └────────┘",
  23. $"{Environment.NewLine}{lc}"
  24. );
  25. IReadOnlyCollection<StraightLine> origLines = lc.Lines;
  26. lc = new (origLines.Exclude (Point.Empty, 10, Orientation.Horizontal));
  27. OutputAssert.AssertEqual (
  28. output,
  29. @"
  30. │ │
  31. │ │
  32. │ │
  33. └────────┘",
  34. $"{Environment.NewLine}{lc}"
  35. );
  36. lc = new (origLines.Exclude (new (0, 1), 10, Orientation.Horizontal));
  37. OutputAssert.AssertEqual (
  38. output,
  39. @"
  40. ┌────────┐
  41. │ │
  42. │ │
  43. └────────┘",
  44. $"{Environment.NewLine}{lc}"
  45. );
  46. lc = new (origLines.Exclude (new (0, 2), 10, Orientation.Horizontal));
  47. OutputAssert.AssertEqual (
  48. output,
  49. @"
  50. ┌────────┐
  51. │ │
  52. │ │
  53. └────────┘",
  54. $"{Environment.NewLine}{lc}"
  55. );
  56. lc = new (origLines.Exclude (new (0, 3), 10, Orientation.Horizontal));
  57. OutputAssert.AssertEqual (
  58. output,
  59. @"
  60. ┌────────┐
  61. │ │
  62. │ │
  63. └────────┘",
  64. $"{Environment.NewLine}{lc}"
  65. );
  66. lc = new (origLines.Exclude (new (0, 4), 10, Orientation.Horizontal));
  67. OutputAssert.AssertEqual (
  68. output,
  69. @"
  70. ┌────────┐
  71. │ │
  72. │ │
  73. │ │",
  74. $"{Environment.NewLine}{lc}"
  75. );
  76. lc = new (origLines.Exclude (Point.Empty, 10, Orientation.Vertical));
  77. OutputAssert.AssertEqual (
  78. output,
  79. @"
  80. ────────┐
  81. ────────┘",
  82. $"{Environment.NewLine}{lc}"
  83. );
  84. lc = new (origLines.Exclude (new (1, 0), 10, Orientation.Vertical));
  85. OutputAssert.AssertEqual (
  86. output,
  87. @"
  88. ┌ ───────┐
  89. │ │
  90. │ │
  91. │ │
  92. └ ───────┘",
  93. $"{Environment.NewLine}{lc}"
  94. );
  95. lc = new (origLines.Exclude (new (8, 0), 10, Orientation.Vertical));
  96. OutputAssert.AssertEqual (
  97. output,
  98. @"
  99. ┌─────── ┐
  100. │ │
  101. │ │
  102. │ │
  103. └─────── ┘",
  104. $"{Environment.NewLine}{lc}"
  105. );
  106. lc = new (origLines.Exclude (new (9, 0), 10, Orientation.Vertical));
  107. OutputAssert.AssertEqual (
  108. output,
  109. @"
  110. ┌────────
  111. └────────",
  112. $"{Environment.NewLine}{lc}"
  113. );
  114. }
  115. #region Parallel Tests
  116. [Fact]
  117. [AutoInitShutdown]
  118. public void TestExcludeParallel_HorizontalLines_LeftOnly ()
  119. {
  120. // x=1 to x=10
  121. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  122. StraightLine [] after = new [] { l1 }
  123. // exclude x=3 to x=103
  124. .Exclude (new (3, 2), 100, Orientation.Horizontal)
  125. .ToArray ();
  126. // x=1 to x=2
  127. StraightLine afterLine = Assert.Single (after);
  128. Assert.Equal (l1.Start, afterLine.Start);
  129. Assert.Equal (2, afterLine.Length);
  130. }
  131. [Fact]
  132. [AutoInitShutdown]
  133. public void TestExcludeParallel_HorizontalLines_RightOnly ()
  134. {
  135. // x=1 to x=10
  136. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  137. StraightLine [] after = new [] { l1 }
  138. // exclude x=0 to x=2
  139. .Exclude (new (0, 2), 3, Orientation.Horizontal)
  140. .ToArray ();
  141. // x=3 to x=10
  142. StraightLine afterLine = Assert.Single (after);
  143. Assert.Equal (3, afterLine.Start.X);
  144. Assert.Equal (2, afterLine.Start.Y);
  145. Assert.Equal (8, afterLine.Length);
  146. }
  147. [Fact]
  148. [AutoInitShutdown]
  149. public void TestExcludeParallel_HorizontalLines_HorizontalSplit ()
  150. {
  151. // x=1 to x=10
  152. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  153. StraightLine [] after = new [] { l1 }
  154. // exclude x=4 to x=5
  155. .Exclude (new (4, 2), 2, Orientation.Horizontal)
  156. .ToArray ();
  157. // x=1 to x=3,
  158. // x=6 to x=10
  159. Assert.Equal (2, after.Length);
  160. StraightLine afterLeft = after [0];
  161. StraightLine afterRight = after [1];
  162. Assert.Equal (1, afterLeft.Start.X);
  163. Assert.Equal (2, afterLeft.Start.Y);
  164. Assert.Equal (3, afterLeft.Length);
  165. Assert.Equal (6, afterRight.Start.X);
  166. Assert.Equal (2, afterRight.Start.Y);
  167. Assert.Equal (5, afterRight.Length);
  168. }
  169. [Fact]
  170. [AutoInitShutdown]
  171. public void TestExcludeParallel_HorizontalLines_CoverCompletely ()
  172. {
  173. // x=1 to x=10
  174. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  175. StraightLine [] after = new [] { l1 }
  176. // exclude x=4 to x=5
  177. .Exclude (new (1, 2), 10, Orientation.Horizontal)
  178. .ToArray ();
  179. Assert.Empty (after);
  180. }
  181. [Fact]
  182. [AutoInitShutdown]
  183. public void TestExcludeParallel_VerticalLines_TopOnly ()
  184. {
  185. // y=1 to y=10
  186. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  187. StraightLine [] after = new [] { l1 }
  188. // exclude y=3 to y=103
  189. .Exclude (new (2, 3), 100, Orientation.Vertical)
  190. .ToArray ();
  191. // y=1 to y=2
  192. StraightLine afterLine = Assert.Single (after);
  193. Assert.Equal (l1.Start, afterLine.Start);
  194. Assert.Equal (2, afterLine.Length);
  195. }
  196. [Fact]
  197. [AutoInitShutdown]
  198. public void TestExcludeParallel_HorizontalLines_BottomOnly ()
  199. {
  200. // y=1 to y=10
  201. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  202. StraightLine [] after = new [] { l1 }
  203. // exclude y=0 to y=2
  204. .Exclude (new (2, 0), 3, Orientation.Vertical)
  205. .ToArray ();
  206. // y=3 to y=10
  207. StraightLine afterLine = Assert.Single (after);
  208. Assert.Equal (3, afterLine.Start.Y);
  209. Assert.Equal (2, afterLine.Start.X);
  210. Assert.Equal (8, afterLine.Length);
  211. }
  212. [Fact]
  213. [AutoInitShutdown]
  214. public void TestExcludeParallel_VerticalLines_VerticalSplit ()
  215. {
  216. // y=1 to y=10
  217. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  218. StraightLine [] after = new [] { l1 }
  219. // exclude y=4 to y=5
  220. .Exclude (new (2, 4), 2, Orientation.Vertical)
  221. .ToArray ();
  222. // y=1 to y=3,
  223. // y=6 to y=10
  224. Assert.Equal (2, after.Length);
  225. StraightLine afterLeft = after [0];
  226. StraightLine afterRight = after [1];
  227. Assert.Equal (1, afterLeft.Start.Y);
  228. Assert.Equal (2, afterLeft.Start.X);
  229. Assert.Equal (3, afterLeft.Length);
  230. Assert.Equal (6, afterRight.Start.Y);
  231. Assert.Equal (2, afterRight.Start.X);
  232. Assert.Equal (5, afterRight.Length);
  233. }
  234. [Fact]
  235. [AutoInitShutdown]
  236. public void TestExcludeParallel_VerticalLines_CoverCompletely ()
  237. {
  238. // y=1 to y=10
  239. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  240. StraightLine [] after = new [] { l1 }
  241. // exclude y=4 to y=5
  242. .Exclude (new (2, 1), 10, Orientation.Vertical)
  243. .ToArray ();
  244. Assert.Empty (after);
  245. }
  246. #endregion
  247. #region Perpendicular Intersection Tests
  248. [Fact]
  249. [AutoInitShutdown]
  250. public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_Splits ()
  251. {
  252. // x=1 to x=10
  253. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  254. StraightLine [] after = new [] { l1 }
  255. // exclude x=3 y=0-10
  256. .Exclude (new (3, 0), 10, Orientation.Vertical)
  257. .ToArray ();
  258. // x=1 to x=2,
  259. // x=4 to x=10
  260. Assert.Equal (2, after.Length);
  261. StraightLine afterLeft = after [0];
  262. StraightLine afterRight = after [1];
  263. Assert.Equal (1, afterLeft.Start.X);
  264. Assert.Equal (2, afterLeft.Start.Y);
  265. Assert.Equal (2, afterLeft.Length);
  266. Assert.Equal (4, afterRight.Start.X);
  267. Assert.Equal (2, afterRight.Start.Y);
  268. Assert.Equal (7, afterRight.Length);
  269. }
  270. [Fact]
  271. [AutoInitShutdown]
  272. public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_ClipLeft ()
  273. {
  274. // x=1 to x=10
  275. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  276. StraightLine [] after = new [] { l1 }
  277. // exclude x=1 y=0-10
  278. .Exclude (new (1, 0), 10, Orientation.Vertical)
  279. .ToArray ();
  280. // x=2 to x=10,
  281. StraightLine lineAfter = Assert.Single (after);
  282. Assert.Equal (2, lineAfter.Start.X);
  283. Assert.Equal (2, lineAfter.Start.Y);
  284. Assert.Equal (9, lineAfter.Length);
  285. }
  286. [Fact]
  287. [AutoInitShutdown]
  288. public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_ClipRight ()
  289. {
  290. // x=1 to x=10
  291. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  292. StraightLine [] after = new [] { l1 }
  293. // exclude x=10 y=0-10
  294. .Exclude (new (10, 0), 10, Orientation.Vertical)
  295. .ToArray ();
  296. // x=1 to x=9,
  297. StraightLine lineAfter = Assert.Single (after);
  298. Assert.Equal (1, lineAfter.Start.X);
  299. Assert.Equal (2, lineAfter.Start.Y);
  300. Assert.Equal (9, lineAfter.Length);
  301. }
  302. [Fact]
  303. [AutoInitShutdown]
  304. public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_MissLeft ()
  305. {
  306. // x=1 to x=10
  307. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  308. StraightLine [] after = new [] { l1 }
  309. // exclude x=0 y=0-10
  310. .Exclude (Point.Empty, 10, Orientation.Vertical)
  311. .ToArray ();
  312. // Exclusion line is too far to the left so hits nothing
  313. Assert.Same (Assert.Single (after), l1);
  314. }
  315. [Fact]
  316. [AutoInitShutdown]
  317. public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_MissRight ()
  318. {
  319. // x=1 to x=10
  320. var l1 = new StraightLine (new (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
  321. StraightLine [] after = new [] { l1 }
  322. // exclude x=11 y=0-10
  323. .Exclude (new (11, 0), 10, Orientation.Vertical)
  324. .ToArray ();
  325. // Exclusion line is too far to the right so hits nothing
  326. Assert.Same (Assert.Single (after), l1);
  327. }
  328. [Fact]
  329. [AutoInitShutdown]
  330. public void TestExcludePerpendicular_VerticalLine_HorizontalExclusion_ClipTop ()
  331. {
  332. // y=1 to y=10
  333. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  334. StraightLine [] after = new [] { l1 }
  335. // exclude y=1 x=0-10
  336. .Exclude (new (0, 1), 10, Orientation.Horizontal)
  337. .ToArray ();
  338. // y=2 to y=10,
  339. StraightLine lineAfter = Assert.Single (after);
  340. Assert.Equal (2, lineAfter.Start.Y);
  341. Assert.Equal (2, lineAfter.Start.X);
  342. Assert.Equal (9, lineAfter.Length);
  343. }
  344. [Fact]
  345. [AutoInitShutdown]
  346. public void TestExcludePerpendicular_VerticalLine_HorizontalExclusion_ClipBottom ()
  347. {
  348. // y=1 to y=10
  349. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  350. StraightLine [] after = new [] { l1 }
  351. // exclude y=10 x=0-10
  352. .Exclude (new (0, 10), 10, Orientation.Horizontal)
  353. .ToArray ();
  354. // y=1 to y=9,
  355. StraightLine lineAfter = Assert.Single (after);
  356. Assert.Equal (1, lineAfter.Start.Y);
  357. Assert.Equal (2, lineAfter.Start.X);
  358. Assert.Equal (9, lineAfter.Length);
  359. }
  360. [Fact]
  361. [AutoInitShutdown]
  362. public void TestExcludePerpendicular_VerticalLine_HorizontalExclusion_MissTop ()
  363. {
  364. // y=1 to y=10
  365. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  366. StraightLine [] after = new [] { l1 }
  367. // exclude y=0 x=0-10
  368. .Exclude (Point.Empty, 10, Orientation.Horizontal)
  369. .ToArray ();
  370. // Exclusion line is too far above so hits nothing
  371. Assert.Same (Assert.Single (after), l1);
  372. }
  373. [Fact]
  374. [AutoInitShutdown]
  375. public void TestExcludePerpendicular_VerticalLine_HorizontalExclusion_MissBottom ()
  376. {
  377. // y=1 to y=10
  378. var l1 = new StraightLine (new (2, 1), 10, Orientation.Vertical, LineStyle.Single);
  379. StraightLine [] after = new [] { l1 }
  380. // exclude y=11 x=0-10
  381. .Exclude (new (0, 11), 10, Orientation.Horizontal)
  382. .ToArray ();
  383. // Exclusion line is too far to the right so hits nothing
  384. Assert.Same (Assert.Single (after), l1);
  385. }
  386. #endregion Perpendicular Intersection Tests
  387. }