StraightLineExtensionsTests.cs 16 KB

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