123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- using System;
- using System.Linq;
- using Xunit;
- using Xunit.Abstractions;
- namespace Terminal.Gui.DrawingTests {
- public class StraightLineExtensionsTests
- {
- private ITestOutputHelper _output;
- public StraightLineExtensionsTests(ITestOutputHelper output)
- {
- this._output = output;
- }
- #region Parallel Tests
- [Fact, AutoInitShutdown]
- public void TestExcludeParallel_HorizontalLines_LeftOnly ()
- {
- // x=1 to x=10
- var l1 = new StraightLine (new Point (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude x=3 to x=103
- .Exclude (new Point (3, 2), 100, Orientation.Horizontal)
- .ToArray ();
- // x=1 to x=2
- var afterLine = Assert.Single (after);
- Assert.Equal (l1.Start, afterLine.Start);
- Assert.Equal (2, afterLine.Length);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludeParallel_HorizontalLines_RightOnly ()
- {
- // x=1 to x=10
- var l1 = new StraightLine (new Point (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude x=0 to x=2
- .Exclude (new Point (0, 2), 3, Orientation.Horizontal)
- .ToArray ();
- // x=3 to x=10
- var afterLine = Assert.Single (after);
- Assert.Equal (3, afterLine.Start.X);
- Assert.Equal (2, afterLine.Start.Y);
- Assert.Equal (8, afterLine.Length);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludeParallel_HorizontalLines_HorizontalSplit ()
- {
- // x=1 to x=10
- var l1 = new StraightLine (new Point (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude x=4 to x=5
- .Exclude (new Point (4, 2), 2, Orientation.Horizontal)
- .ToArray ();
- // x=1 to x=3,
- // x=6 to x=10
- Assert.Equal (2, after.Length);
- var afterLeft = after [0];
- var afterRight = after [1];
- Assert.Equal (1, afterLeft.Start.X);
- Assert.Equal (2, afterLeft.Start.Y);
- Assert.Equal (3, afterLeft.Length);
- Assert.Equal (6, afterRight.Start.X);
- Assert.Equal (2, afterRight.Start.Y);
- Assert.Equal (5, afterRight.Length);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludeParallel_HorizontalLines_CoverCompletely ()
- {
- // x=1 to x=10
- var l1 = new StraightLine (new Point (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude x=4 to x=5
- .Exclude (new Point (1, 2), 10, Orientation.Horizontal)
- .ToArray ();
- Assert.Empty (after);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludeParallel_VerticalLines_TopOnly ()
- {
- // y=1 to y=10
- var l1 = new StraightLine (new Point (2, 1), 10, Orientation.Vertical, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude y=3 to y=103
- .Exclude (new Point (2, 3), 100, Orientation.Vertical)
- .ToArray ();
- // y=1 to y=2
- var afterLine = Assert.Single (after);
- Assert.Equal (l1.Start, afterLine.Start);
- Assert.Equal (2, afterLine.Length);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludeParallel_HorizontalLines_BottomOnly ()
- {
- // y=1 to y=10
- var l1 = new StraightLine (new Point (2, 1), 10, Orientation.Vertical, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude y=0 to y=2
- .Exclude (new Point (2,0), 3, Orientation.Vertical)
- .ToArray ();
- // y=3 to y=10
- var afterLine = Assert.Single (after);
- Assert.Equal (3, afterLine.Start.Y);
- Assert.Equal (2, afterLine.Start.X);
- Assert.Equal (8, afterLine.Length);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludeParallel_VerticalLines_VerticalSplit ()
- {
- // y=1 to y=10
- var l1 = new StraightLine (new Point (2,1), 10, Orientation.Vertical, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude y=4 to y=5
- .Exclude (new Point (2, 4), 2, Orientation.Vertical)
- .ToArray ();
- // y=1 to y=3,
- // y=6 to y=10
- Assert.Equal (2, after.Length);
- var afterLeft = after [0];
- var afterRight = after [1];
- Assert.Equal (1, afterLeft.Start.Y);
- Assert.Equal (2, afterLeft.Start.X);
- Assert.Equal (3, afterLeft.Length);
- Assert.Equal (6, afterRight.Start.Y);
- Assert.Equal (2, afterRight.Start.X);
- Assert.Equal (5, afterRight.Length);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludeParallel_VerticalLines_CoverCompletely ()
- {
- // y=1 to y=10
- var l1 = new StraightLine (new Point (2,1), 10, Orientation.Vertical, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude y=4 to y=5
- .Exclude (new Point (2,1), 10, Orientation.Vertical)
- .ToArray ();
- Assert.Empty (after);
- }
- #endregion
- #region Perpendicular Intersection Tests
- [Fact, AutoInitShutdown]
- public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_Splits ()
- {
- // x=1 to x=10
- var l1 = new StraightLine (new Point (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude x=3 y=0-10
- .Exclude (new Point (3, 0), 10, Orientation.Vertical)
- .ToArray ();
- // x=1 to x=2,
- // x=4 to x=10
- Assert.Equal (2, after.Length);
- var afterLeft = after [0];
- var afterRight = after [1];
- Assert.Equal (1, afterLeft.Start.X);
- Assert.Equal (2, afterLeft.Start.Y);
- Assert.Equal (2, afterLeft.Length);
- Assert.Equal (4, afterRight.Start.X);
- Assert.Equal (2, afterRight.Start.Y);
- Assert.Equal (7, afterRight.Length);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_ClipLeft ()
- {
- // x=1 to x=10
- var l1 = new StraightLine (new Point (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude x=1 y=0-10
- .Exclude (new Point (1, 0), 10, Orientation.Vertical)
- .ToArray ();
- // x=2 to x=10,
- var lineAfter = Assert.Single(after);
- Assert.Equal (2, lineAfter.Start.X);
- Assert.Equal (2, lineAfter.Start.Y);
- Assert.Equal (9, lineAfter.Length);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_ClipRight ()
- {
- // x=1 to x=10
- var l1 = new StraightLine (new Point (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude x=10 y=0-10
- .Exclude (new Point (10, 0), 10, Orientation.Vertical)
- .ToArray ();
- // x=1 to x=9,
- var lineAfter = Assert.Single (after);
- Assert.Equal (1, lineAfter.Start.X);
- Assert.Equal (2, lineAfter.Start.Y);
- Assert.Equal (9, lineAfter.Length);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_MissLeft ()
- {
- // x=1 to x=10
- var l1 = new StraightLine (new Point (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude x=0 y=0-10
- .Exclude (new Point (0, 0), 10, Orientation.Vertical)
- .ToArray ();
- // Exclusion line is too far to the left so hits nothing
- Assert.Same(Assert.Single (after),l1);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludePerpendicular_HorizontalLine_VerticalExclusion_MissRight ()
- {
- // x=1 to x=10
- var l1 = new StraightLine (new Point (1, 2), 10, Orientation.Horizontal, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude x=11 y=0-10
- .Exclude (new Point (11, 0), 10, Orientation.Vertical)
- .ToArray ();
- // Exclusion line is too far to the right so hits nothing
- Assert.Same (Assert.Single (after), l1);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludePerpendicular_VerticalLine_HorizontalExclusion_ClipTop ()
- {
- // y=1 to y=10
- var l1 = new StraightLine (new Point (2,1), 10, Orientation.Vertical, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude y=1 x=0-10
- .Exclude (new Point (0,1), 10, Orientation.Horizontal)
- .ToArray ();
- // y=2 to y=10,
- var lineAfter = Assert.Single(after);
- Assert.Equal (2, lineAfter.Start.Y);
- Assert.Equal (2, lineAfter.Start.X);
- Assert.Equal (9, lineAfter.Length);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludePerpendicular_VerticalLine_HorizontalExclusion_ClipBottom ()
- {
- // y=1 to y=10
- var l1 = new StraightLine (new Point (2,1), 10, Orientation.Vertical, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude y=10 x=0-10
- .Exclude (new Point (0,10), 10, Orientation.Horizontal)
- .ToArray ();
- // y=1 to y=9,
- var lineAfter = Assert.Single (after);
- Assert.Equal (1, lineAfter.Start.Y);
- Assert.Equal (2, lineAfter.Start.X);
- Assert.Equal (9, lineAfter.Length);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludePerpendicular_VerticalLine_HorizontalExclusion_MissTop ()
- {
- // y=1 to y=10
- var l1 = new StraightLine (new Point (2,1), 10, Orientation.Vertical, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude y=0 x=0-10
- .Exclude (new Point (0, 0), 10, Orientation.Horizontal)
- .ToArray ();
- // Exclusion line is too far above so hits nothing
- Assert.Same(Assert.Single (after),l1);
- }
- [Fact, AutoInitShutdown]
- public void TestExcludePerpendicular_VerticalLine_HorizontalExclusion_MissBottom ()
- {
- // y=1 to y=10
- var l1 = new StraightLine (new Point (2,1), 10, Orientation.Vertical, LineStyle.Single);
- var after = new StraightLine [] { l1 }
- // exclude y=11 x=0-10
- .Exclude (new Point (0,11), 10, Orientation.Horizontal)
- .ToArray ();
- // Exclusion line is too far to the right so hits nothing
- Assert.Same (Assert.Single (after), l1);
- }
- #endregion Perpendicular Intersection Tests
- [Fact, AutoInitShutdown]
- public void LineCanvasIntegrationTest()
- {
- var lc = new LineCanvas();
- lc.AddLine(new Point(0,0),10,Orientation.Horizontal,LineStyle.Single);
- lc.AddLine(new Point(9,0),5,Orientation.Vertical,LineStyle.Single);
- lc.AddLine(new Point(9,4),-10,Orientation.Horizontal,LineStyle.Single);
- lc.AddLine(new Point(0,4),-5,Orientation.Vertical,LineStyle.Single);
- TestHelpers.AssertEqual (this._output,
- @"
- ┌────────┐
- │ │
- │ │
- │ │
- └────────┘",$"{Environment.NewLine}{lc}");
- var origLines = lc.Lines;
- lc = new LineCanvas(origLines.Exclude(new Point(0,0),10,Orientation.Horizontal));
- TestHelpers.AssertEqual (this._output,
- @"
- │ │
- │ │
- │ │
- └────────┘",$"{Environment.NewLine}{lc}");
- lc = new LineCanvas(origLines.Exclude(new Point(0,1),10,Orientation.Horizontal));
- TestHelpers.AssertEqual (this._output,
- @"
- ┌────────┐
-
- │ │
- │ │
- └────────┘",$"{Environment.NewLine}{lc}");
- lc = new LineCanvas(origLines.Exclude(new Point(0,2),10,Orientation.Horizontal));
- TestHelpers.AssertEqual (this._output,
- @"
- ┌────────┐
- │ │
-
- │ │
- └────────┘",$"{Environment.NewLine}{lc}");
- lc = new LineCanvas(origLines.Exclude(new Point(0,3),10,Orientation.Horizontal));
- TestHelpers.AssertEqual (this._output,
- @"
- ┌────────┐
- │ │
- │ │
-
- └────────┘",$"{Environment.NewLine}{lc}");
- lc = new LineCanvas(origLines.Exclude(new Point(0,4),10,Orientation.Horizontal));
- TestHelpers.AssertEqual (this._output,
- @"
- ┌────────┐
- │ │
- │ │
- │ │",$"{Environment.NewLine}{lc}");
- lc = new LineCanvas(origLines.Exclude(new Point(0,0),10,Orientation.Vertical));
- TestHelpers.AssertEqual (this._output,
- @"
- ────────┐
- │
- │
- │
- ────────┘",$"{Environment.NewLine}{lc}");
- lc = new LineCanvas(origLines.Exclude(new Point(1,0),10,Orientation.Vertical));
- TestHelpers.AssertEqual (this._output,
- @"
- ┌ ───────┐
- │ │
- │ │
- │ │
- └ ───────┘",$"{Environment.NewLine}{lc}");
- lc = new LineCanvas(origLines.Exclude(new Point(8,0),10,Orientation.Vertical));
- TestHelpers.AssertEqual (this._output,
- @"
- ┌─────── ┐
- │ │
- │ │
- │ │
- └─────── ┘",$"{Environment.NewLine}{lc}");
- lc = new LineCanvas(origLines.Exclude(new Point(9,0),10,Orientation.Vertical));
- TestHelpers.AssertEqual (this._output,
- @"
- ┌────────
- │
- │
- │
- └────────",$"{Environment.NewLine}{lc}");
- }
- }
- }
|