StraightLineExtensionsTests.cs 12 KB

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