From ef56998f5aef7e458a3e72ddc5cd50c41dc45e9b Mon Sep 17 00:00:00 2001 From: tznind Date: Mon, 23 Sep 2024 19:51:05 +0100 Subject: [PATCH] Tidy up test file and comments in NetDriver --- Terminal.Gui/ConsoleDrivers/NetDriver.cs | 7 - UnitTests/Drawing/SixelEncoderTests.cs | 207 +++++++++++------------ 2 files changed, 96 insertions(+), 118 deletions(-) diff --git a/Terminal.Gui/ConsoleDrivers/NetDriver.cs b/Terminal.Gui/ConsoleDrivers/NetDriver.cs index 6914bbef4..e23b52fcb 100644 --- a/Terminal.Gui/ConsoleDrivers/NetDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/NetDriver.cs @@ -1021,13 +1021,6 @@ internal class NetDriver : ConsoleDriver Console.Write (output); } - /* - // Hard-coded sixel content from the file - string knownGood = "\u001bP0;0;0q\"1;1;60;23#0;2;0;0;0#1;2;99;36;0#2;2;96;40;0#3;2;98;39;0#4;2;95;42;0#5;2;93;46;0#6;2;91;50;0#7;2;92;49;0#8;2;94;44;0#9;2;0;100;7#10;2;87;56;0#11;2;86;58;0#12;2;84;61;0#13;2;85;60;0#14;2;90;52;0#15;2;91;50;0#16;2;88;54;0#17;2;81;65;0#18;2;80;67;0#19;2;82;64;0#20;2;78;71;0#21;2;79;70;0#22;2;76;74;0#23;2;75;76;0#24;2;75;76;0#25;2;75;76;0#26;2;73;78;0#27;2;73;78;0#28;2;74;77;0#29;2;74;77;0#30;2;74;77;0#31;2;75;77;0#32;2;74;78;0#33;2;74;78;0#34;2;74;77;0#35;2;72;80;0#36;2;71;81;0#37;2;72;81;0#38;2;71;81;0#39;2;72;81;0#40;2;73;78;0#41;2;73;78;0#42;2;73;79;0#43;2;73;79;0#44;2;73;80;0#45;2;73;79;0#46;2;72;80;0#47;2;73;80;0#48;2;72;80;0#49;2;73;79;0#50;2;69;85;0#51;2;68;86;0#52;2;69;86;0#53;2;68;86;0#54;2;69;86;0#55;2;68;87;0#56;2;68;87;0#57;2;68;87;0#58;2;68;87;0#59;2;68;87;0#60;2;71;82;0#61;2;71;82;0#62;2;71;82;0#63;2;71;82;0#64;2;71;82;0#65;2;71;83;0#66;2;70;83;0#67;2;70;84;0#68;2;69;84;0#69;2;70;84;0#70;2;70;84;0#71;2;70;84;0#72;2;71;83;0#73;2;71;83;0#74;2;69;85;0#75;2;69;85;0#76;2;69;85;0#77;2;70;85;0#78;2;69;85;0#79;2;69;86;0#80;2;69;85;0#81;2;62;96;0#82;2;62;96;0#83;2;62;96;0#84;2;62;97;0#85;2;62;97;0#86;2;62;96;0#87;2;61;98;0#88;2;62;97;0#89;2;61;98;0#90;2;62;98;0#91;2;62;97;0#92;2;61;98;0#93;2;62;98;0#94;2;61;98;0#95;2;60;99;0#96;2;61;99;0#97;2;60;99;0#98;2;61;99;0#99;2;60;100;0#100;2;60;100;0#101;2;60;100;0#102;2;62;98;0#103;2;65;90;0#104;2;67;88;0#105;2;67;89;0#106;2;67;89;0#107;2;67;89;0#108;2;67;89;0#109;2;67;88;0#110;2;68;88;0#111;2;67;88;0#112;2;66;89;0#113;2;66;90;0#114;2;66;90;0#115;2;67;89;0#116;2;66;90;0#117;2;66;90;0#118;2;66;91;0#119;2;67;89;0#120;2;65;91;0#121;2;65;91;0#122;2;65;92;0#123;2;65;92;0#124;2;64;93;0#125;2;64;94;0#126;2;64;94;0#127;2;64;93;0#128;2;65;93;0#129;2;64;93;0#130;2;64;93;0#131;2;65;92;0#132;2;66;91;0#133;2;63;95;0#134;2;64;94;0#135;2;64;94;0#136;2;64;95;0#137;2;63;95;0#138;2;63;95;0#139;2;63;95;0#140;2;64;95;0#141;2;63;96;0#142;2;63;96;0#143;2;64;95;0#144;2;75;75;0#4~{w#7??B^}o#14F^_#10BFo_#13F~o#19?BN{#0{Ez|llJ}KsSsKw#37@FW#70BEw_#107?@Mo#121B]w#135@Mw#88BMo_$#8?BCo#6???@#15Nw_#16^{w#11N^w#12?N~{o#17BB#9wCAQQs?oGGGo#45F{o#65F{_#55?@Fw_#132?C#129@Co#141@Eo#87@#98N^~~$#5??BN~{_#18!17?@#22!5?@BB_#32B#26B#48?AG_#74?@F[o#110EO#120??_#124BMo#83@Ko$#23!33?B#72!7?W#52?AG#112?@Nw-#4@F^~w#5FN{o#15?F]o#16N~[_#13?D~w_#19@?{wo#18?Gw_#20Gw#22www_#32Fo_#37DMo#72C_#52@W_#110G_#121@Nw_#83?@^w_$#2EW_#8?Fwo#7BN~w_#10???b]o#12??F^}{#9@@AAAB?@AAA@#23[w#45F]o#65@Mo#74B]_#107@FWo#129?FW_#88??F[_$#3w_#14!9?@No#11??@Ny#0!4?BAEDDDCFEDDDEB#26?G#48@Io#70@J[_#55F]o#112FMo#135?BN}_#87?AO$#17!26?Gwo#21?Wo#124!21?CO#98???@N-#0MIyAyYmggWowGggwGgggGggG}I}wGgggWgggGw}A}??wKuyYYU{wGw?wG}I}$#1o_#4?@@@#8@A#7?@FFE#14?BE#10?BF#11F#13FFC#19?@@@C#18?@F#21FC#22?BFFE#23@#32@?o#37AA#70?@#74@@#55@#110@#107BE#121@F}#124@#135F#141@#83@#88@$#3@O#5!5?DFEG#15?@FC#16@FC#12!4?BF#17???BFE#20??BFC#144??@#45??@Nw#65@B#112!6?@C#129?@E$#9?CC{CcOOO_??oOO?oOOOoOOo?s??oOOO_OOOo??{!4?oGCccg??o???o?s$#2?@@#48!39?DC#120!8?A-#1^^OO#3OO#2O#4OOO#8O#5OO#7O^O#15O#14OO#10??O#11OO#13OO#12OOO#19OWO#18?OO#21O#20OO#22OOO#23O#32O#26O#45WO#48O#65OO#70O#74OO#55O#110O#112@O#121OO#129O#135O$#0??NGNK!4INNGN?NGN?NGNNGNGNNGN?NGGIIGNNGNNHNCLJJJGNFKJIJGNGN$#9???F?B!4D??F???F???F??F?F??F???FFDDF??F??E?BACCCF??BCCCF?F$#16!18?NOO#17!9?F?O#52!18?G#107??O-\u001b\\"; - - Application.Sixel = knownGood; - */ - if (!string.IsNullOrWhiteSpace(Application.Sixel)) { Console.SetCursorPosition (0,0); diff --git a/UnitTests/Drawing/SixelEncoderTests.cs b/UnitTests/Drawing/SixelEncoderTests.cs index 7608f113d..12b1a90e2 100644 --- a/UnitTests/Drawing/SixelEncoderTests.cs +++ b/UnitTests/Drawing/SixelEncoderTests.cs @@ -1,52 +1,44 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Color = Terminal.Gui.Color; +using Color = Terminal.Gui.Color; namespace UnitTests.Drawing; public class SixelEncoderTests { - [Fact] public void EncodeSixel_RedSquare12x12_ReturnsExpectedSixel () { - - var expected = "\u001bP" + // Start sixel sequence - "0;0;0" + // Defaults for aspect ratio and grid size - "q" + // Signals beginning of sixel image data - "\"1;1;12;2" + // no scaling factors (1x1) and filling 12px width with 2 'sixel' height = 12 px high - - /* - * Definition of the color palette - */ - "#0;2;100;0;0" + // Red color definition in the format "#;;;;" - 2 means RGB. The values range 0 to 100 - - /* - * Start of the Pixel data - * We draw 6 rows at once, so end up with 2 'lines' - * Both are basically the same and terminate with dollar hyphen (except last row) - * Format is: - * #0 (selects to use color palette index 0 i.e. red) - * !12 (repeat next byte 12 times i.e. the whole length of the row) - * ~ (the byte 111111 i.e. fill completely) - * $ (return to start of line) - * - (move down to next line) - */ - "#0!12~$-" + - "#0!12~$" + // Next 6 rows of red pixels - - "\u001b\\"; // End sixel sequence + string expected = "\u001bP" // Start sixel sequence + + "0;0;0" // Defaults for aspect ratio and grid size + + "q" // Signals beginning of sixel image data + + "\"1;1;12;2" // no scaling factors (1x1) and filling 12px width with 2 'sixel' height = 12 px high + /* + * Definition of the color palette + * #;;;;" - 2 means RGB. The values range 0 to 100 + */ + + "#0;2;100;0;0" // Red color definition + /* + * Start of the Pixel data + * We draw 6 rows at once, so end up with 2 'lines' + * Both are basically the same and terminate with dollar hyphen (except last row) + * Format is: + * #0 (selects to use color palette index 0 i.e. red) + * !12 (repeat next byte 12 times i.e. the whole length of the row) + * ~ (the byte 111111 i.e. fill completely) + * $ (return to start of line) + * - (move down to next line) + */ + + "#0!12~$-" + + "#0!12~$" // Next 6 rows of red pixels + + "\u001b\\"; // End sixel sequence // Arrange: Create a 12x12 bitmap filled with red - var pixels = new Color [12, 12]; - for (int x = 0; x < 12; x++) + Color [,] pixels = new Color [12, 12]; + + for (var x = 0; x < 12; x++) { - for (int y = 0; y < 12; y++) + for (var y = 0; y < 12; y++) { - pixels [x, y] = new Color(255,0,0); + pixels [x, y] = new (255, 0, 0); } } @@ -57,8 +49,7 @@ public class SixelEncoderTests // Since image is only red we should only have 1 color definition Color c1 = Assert.Single (encoder.Quantizer.Palette); - Assert.Equal (new Color(255,0,0),c1); - + Assert.Equal (new (255, 0, 0), c1); Assert.Equal (expected, result); } @@ -66,85 +57,79 @@ public class SixelEncoderTests public void EncodeSixel_12x12GridPattern3x3_ReturnsExpectedSixel () { /* - * Each block is a 3x3 square, alternating black and white. - * The pattern alternates between rows, creating a checkerboard. - * We have 4 blocks per row, and this repeats over 12x12 pixels. - - ███...███... - ███...███... - ███...███... - ...███...███ - ...███...███ - ...███...███ - ███...███... - ███...███... - ███...███... - ...███...███ - ...███...███ - ...███...███ + * Each block is a 3x3 square, alternating black and white. + * The pattern alternates between rows, creating a checkerboard. + * We have 4 blocks per row, and this repeats over 12x12 pixels. + * + * ███...███... + * ███...███... + * ███...███... + * ...███...███ + * ...███...███ + * ...███...███ + * ███...███... + * ███...███... + * ███...███... + * ...███...███ + * ...███...███ + * ...███...███ + * + * Because we are dealing with sixels (drawing 6 rows at once), we will + * see 2 bands being drawn. We will also see how we have to 'go back over' + * the current line after drawing the black (so we can draw the white). + */ - Because we are dealing with sixels (drawing 6 rows at once) we will - see 2 bands being drawn. We will also see how we have to 'go back over' - the current line after drawing the black (so we can draw the white). - - */ - - - var expected = "\u001bP" + // Start sixel sequence - "0;0;0" + // Defaults for aspect ratio and grid size - "q" + // Signals beginning of sixel image data - "\"1;1;12;2" + // no scaling factors (1x1) and filling 12px width with 2 'sixel' height = 12 px high - - /* - * Definition of the color palette - */ - "#0;2;0;0;0" + // Black color definition (index 0: RGB 0,0,0) - "#1;2;100;100;100" + // White color definition (index 1: RGB 100,100,100) - - /* - * Start of the Pixel data - * - * Lets consider only the first 6 pixel (vertically). We have to fill the top 3 black and bottom 3 white. - * So we need to select black and fill 000111. To convert this into a character we must +63 and convert to ASCII - * Later on we will also need to select white and fill the inverse i.e. 111000. - * - * 111000 (binary) → w (ASCII 119). - * 000111 (binary) → F (ASCII 70). - * - * Therefore the lines become - * - * #0 (Select black) - * FFF (fill first 3 pixels horizontally - and top half of band black) - * www (fill next 3 pixels horizontally - bottom half of band black) - * FFFwww (as above to finish the line - * - * Next we must go back and fill the white (on the same band) - * #1 (Select white) - * - */ - "#0FFFwwwFFFwww$" + // First pass of top band (Filling black) - "#1wwwFFFwwwFFF$-" + // Second pass of top band (Filling white) - - // Sequence repeats exactly the same because top band is actually identical pixels to bottom band - "#0FFFwwwFFFwww$" + // First pass of bottom band (Filling white) - "#1wwwFFFwwwFFF$" + // Second pass of bottom band (Filling black) - - "\u001b\\"; // End sixel sequence + string expected = "\u001bP" // Start sixel sequence + + "0;0;0" // Defaults for aspect ratio and grid size + + "q" // Signals beginning of sixel image data + + "\"1;1;12;2" // no scaling factors (1x1) and filling 12px width with 2 'sixel' height = 12 px high + /* + * Definition of the color palette + */ + + "#0;2;0;0;0" // Black color definition (index 0: RGB 0,0,0) + + "#1;2;100;100;100" // White color definition (index 1: RGB 100,100,100) + /* + * Start of the Pixel data + * + * Lets consider only the first 6 pixel (vertically). We have to fill the top 3 black and bottom 3 white. + * So we need to select black and fill 000111. To convert this into a character we must +63 and convert to ASCII. + * Later on we will also need to select white and fill the inverse, i.e. 111000. + * + * 111000 (binary) → w (ASCII 119). + * 000111 (binary) → F (ASCII 70). + * + * Therefore the lines become + * + * #0 (Select black) + * FFF (fill first 3 pixels horizontally - and top half of band black) + * www (fill next 3 pixels horizontally - bottom half of band black) + * FFFwww (as above to finish the line) + * + * Next we must go back and fill the white (on the same band) + * #1 (Select white) + */ + + "#0FFFwwwFFFwww$" // First pass of top band (Filling black) + + "#1wwwFFFwwwFFF$-" // Second pass of top band (Filling white) + // Sequence repeats exactly the same because top band is actually identical pixels to bottom band + + "#0FFFwwwFFFwww$" // First pass of bottom band (Filling black) + + "#1wwwFFFwwwFFF$" // Second pass of bottom band (Filling white) + + "\u001b\\"; // End sixel sequence // Arrange: Create a 12x12 bitmap with a 3x3 checkerboard pattern - var pixels = new Color [12, 12]; - for (int y = 0; y < 12; y++) + Color [,] pixels = new Color [12, 12]; + + for (var y = 0; y < 12; y++) { - for (int x = 0; x < 12; x++) + for (var x = 0; x < 12; x++) { // Create a 3x3 checkerboard by alternating the color based on pixel coordinates - if (((x / 3) + (y / 3)) % 2 == 0) + if ((x / 3 + y / 3) % 2 == 0) { - pixels [x, y] = new Color (0, 0, 0); // Black + pixels [x, y] = new (0, 0, 0); // Black } else { - pixels [x, y] = new Color (255, 255, 255); // White + pixels [x, y] = new (255, 255, 255); // White } } } @@ -156,10 +141,10 @@ public class SixelEncoderTests // We should have only black and white in the palette Assert.Equal (2, encoder.Quantizer.Palette.Count); Color black = encoder.Quantizer.Palette.ElementAt (0); - Color white = encoder.Quantizer.Palette.ElementAt(1); + Color white = encoder.Quantizer.Palette.ElementAt (1); - Assert.Equal (new Color (0, 0, 0), black); - Assert.Equal (new Color (255, 255, 255), white); + Assert.Equal (new (0, 0, 0), black); + Assert.Equal (new (255, 255, 255), white); // Compare the generated SIXEL string with the expected one Assert.Equal (expected, result);