StraightLineExtensionsTests.cs 16 KB

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