JustifierTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. using Xunit.Sdk;
  4. namespace Terminal.Gui.DrawingTests;
  5. public class JustifierTests (ITestOutputHelper output)
  6. {
  7. private readonly ITestOutputHelper _output = output;
  8. [Fact]
  9. public void TestLeftJustification ()
  10. {
  11. int [] sizes = { 10, 20, 30 };
  12. var positions = Justifier.Justify (sizes, Justification.Left, 100);
  13. Assert.Equal (new List<int> { 0, 10, 30 }, positions);
  14. }
  15. [Fact]
  16. public void TestRightJustification ()
  17. {
  18. int [] sizes = { 10, 20, 30 };
  19. var positions = Justifier.Justify (sizes, Justification.Right, 100);
  20. Assert.Equal (new List<int> { 40, 50, 70 }, positions);
  21. }
  22. [Fact]
  23. public void TestCenterJustification ()
  24. {
  25. int [] sizes = { 10, 20, 30 };
  26. var positions = Justifier.Justify (sizes, Justification.Centered, 100);
  27. Assert.Equal (new List<int> { 20, 30, 50 }, positions);
  28. }
  29. [Fact]
  30. public void TestJustifiedJustification ()
  31. {
  32. int [] sizes = { 10, 20, 30 };
  33. var positions = Justifier.Justify (sizes, Justification.Justified, 100);
  34. Assert.Equal (new List<int> { 0, 30, 70 }, positions);
  35. }
  36. [Fact]
  37. public void TestNoItems ()
  38. {
  39. int [] sizes = { };
  40. var positions = Justifier.Justify (sizes, Justification.Left, 100);
  41. Assert.Equal (new int [] { }, positions);
  42. }
  43. [Fact]
  44. public void TestTenItems ()
  45. {
  46. int [] sizes = { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 };
  47. var positions = Justifier.Justify (sizes, Justification.Left, 100);
  48. Assert.Equal (new int [] { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 }, positions);
  49. }
  50. [Fact]
  51. public void TestZeroLengthItems ()
  52. {
  53. int [] sizes = { 0, 0, 0 };
  54. var positions = Justifier.Justify (sizes, Justification.Left, 100);
  55. Assert.Equal (new int [] { 0, 0, 0 }, positions);
  56. }
  57. [Fact]
  58. public void TestLongItems ()
  59. {
  60. int [] sizes = { 1000, 2000, 3000 };
  61. Assert.Throws<ArgumentException> (() => Justifier.Justify (sizes, Justification.Left, 100));
  62. }
  63. [Fact]
  64. public void TestNegativeLengths ()
  65. {
  66. int [] sizes = { -10, -20, -30 };
  67. Assert.Throws<ArgumentException> (() => Justifier.Justify (sizes, Justification.Left, 100));
  68. }
  69. [Theory]
  70. [InlineData (Justification.Left, new int [] { 10, 20, 30 }, 100, new int [] { 0, 10, 30 })]
  71. [InlineData (Justification.Left, new int [] { 33, 33, 33 }, 100, new int [] { 0, 33, 66 })]
  72. [InlineData (Justification.Left, new int [] { 10 }, 101, new int [] { 0 })]
  73. [InlineData (Justification.Left, new int [] { 10, 20 }, 101, new int [] { 0, 10 })]
  74. [InlineData (Justification.Left, new int [] { 10, 20, 30 }, 101, new int [] { 0, 10, 30 })]
  75. [InlineData (Justification.Left, new int [] { 10, 20, 30, 40 }, 101, new int [] { 0, 10, 30, 60 })]
  76. [InlineData (Justification.Left, new int [] { 10, 20, 30, 40, 50 }, 151, new int [] { 0, 10, 30, 60, 100 })]
  77. [InlineData (Justification.Right, new int [] { 10, 20, 30 }, 100, new int [] { 40, 50, 70 })]
  78. [InlineData (Justification.Right, new int [] { 33, 33, 33 }, 100, new int [] { 1, 34, 67 })]
  79. [InlineData (Justification.Right, new int [] { 10 }, 101, new int [] { 91 })]
  80. [InlineData (Justification.Right, new int [] { 10, 20 }, 101, new int [] { 71, 81 })]
  81. [InlineData (Justification.Right, new int [] { 10, 20, 30 }, 101, new int [] { 41, 51, 71 })]
  82. [InlineData (Justification.Right, new int [] { 10, 20, 30, 40 }, 101, new int [] { 1, 11, 31, 61 })]
  83. [InlineData (Justification.Right, new int [] { 10, 20, 30, 40, 50 }, 151, new int [] { 1, 11, 31, 61, 101 })]
  84. [InlineData (Justification.Centered, new int [] { 10, 20, 30 }, 100, new int [] { 20, 30, 50 })]
  85. [InlineData (Justification.Centered, new int [] { 33, 33, 33 }, 99, new int [] { 0, 33, 66 })]
  86. [InlineData (Justification.Centered, new int [] { 33, 33, 33 }, 100, new int [] { 0, 33, 66 })]
  87. [InlineData (Justification.Centered, new int [] { 33, 33, 33 }, 101, new int [] { 1, 34, 67 })]
  88. [InlineData (Justification.Centered, new int [] { 33, 33, 33 }, 102, new int [] { 1, 34, 67 })]
  89. [InlineData (Justification.Centered, new int [] { 33, 33, 33 }, 104, new int [] { 2, 35, 68 })]
  90. [InlineData (Justification.Centered, new int [] { 10 }, 101, new int [] { 45 })]
  91. [InlineData (Justification.Centered, new int [] { 10, 20 }, 101, new int [] { 35, 45 })]
  92. [InlineData (Justification.Centered, new int [] { 10, 20, 30 }, 101, new int [] { 20, 30, 50 })]
  93. [InlineData (Justification.Centered, new int [] { 10, 20, 30, 40 }, 101, new int [] { 0, 10, 30, 60 })]
  94. [InlineData (Justification.Centered, new int [] { 10, 20, 30, 40, 50 }, 151, new int [] { 0, 10, 30, 60, 100 })]
  95. [InlineData (Justification.Justified, new int [] { 10, 20, 30, 40, 50 }, 151, new int [] { 0, 11, 31, 61, 101 })]
  96. [InlineData (Justification.Justified, new int [] { 10, 20, 30, 40 }, 101, new int [] { 0, 11, 31, 61 })]
  97. [InlineData (Justification.Justified, new int [] { 10, 20, 30 }, 100, new int [] { 0, 30, 70 })]
  98. [InlineData (Justification.Justified, new int [] { 10, 20, 30 }, 101, new int [] { 0, 31, 71 })]
  99. [InlineData (Justification.Justified, new int [] { 33, 33, 33 }, 100, new int [] { 0, 34, 67 })]
  100. [InlineData (Justification.Justified, new int [] { 11, 17, 23 }, 100, new int [] { 0, 36, 77 })]
  101. [InlineData (Justification.Justified, new int [] { 1, 2, 3 }, 11, new int [] { 0, 4, 8 })]
  102. [InlineData (Justification.Justified, new int [] { 10, 20 }, 101, new int [] { 0, 81 })]
  103. [InlineData (Justification.Justified, new int [] { 10 }, 101, new int [] { 0 })]
  104. [InlineData (Justification.Justified, new int [] { 3, 3, 3 }, 21, new int [] { 0, 9, 18 })]
  105. [InlineData (Justification.Justified, new int [] { 3, 4, 5 }, 21, new int [] { 0, 8, 16 })]
  106. [InlineData (Justification.Justified, new int [] { 3, 4, 5, 6 }, 18, new int [] { 0, 3, 7, 12 })]
  107. [InlineData (Justification.Justified, new int [] { 3, 4, 5, 6 }, 19, new int [] { 0, 4, 8, 13 })]
  108. [InlineData (Justification.Justified, new int [] { 3, 4, 5, 6 }, 20, new int [] { 0, 4, 9, 14 })]
  109. [InlineData (Justification.Justified, new int [] { 3, 4, 5, 6 }, 21, new int [] { 0, 4, 9, 15 })]
  110. [InlineData (Justification.Justified, new int [] { 6, 5, 4, 3 }, 22, new int [] { 0, 8, 14, 19 })]
  111. [InlineData (Justification.Justified, new int [] { 6, 5, 4, 3 }, 23, new int [] { 0, 8, 15, 20, })]
  112. [InlineData (Justification.Justified, new int [] { 6, 5, 4, 3 }, 24, new int [] { 0, 8, 15, 21 })]
  113. [InlineData (Justification.Justified, new int [] { 6, 5, 4, 3 }, 25, new int [] { 0, 9, 16, 22 })]
  114. [InlineData (Justification.Justified, new int [] { 6, 5, 4, 3 }, 26, new int [] { 0, 9, 17, 23 })]
  115. [InlineData (Justification.Justified, new int [] { 6, 5, 4, 3 }, 31, new int [] { 0, 11, 20, 28 })]
  116. [InlineData (Justification.LeftJustified, new int [] { 10, 20, 30 }, 100, new int [] { 0, 11, 70 })]
  117. [InlineData (Justification.LeftJustified, new int [] { 33, 33, 33 }, 100, new int [] { 0, 34, 67 })]
  118. [InlineData (Justification.LeftJustified, new int [] { 10 }, 101, new int [] { 0 })]
  119. [InlineData (Justification.LeftJustified, new int [] { 10, 20 }, 101, new int [] { 0, 81 })]
  120. [InlineData (Justification.LeftJustified, new int [] { 10, 20, 30 }, 101, new int [] { 0, 11, 71 })]
  121. [InlineData (Justification.LeftJustified, new int [] { 10, 20, 30, 40 }, 101, new int [] { 0, 11, 32, 61 })]
  122. [InlineData (Justification.LeftJustified, new int [] { 10, 20, 30, 40, 50 }, 151, new int [] { 0, 11, 32, 63, 101 })]
  123. [InlineData (Justification.LeftJustified, new int [] { 3, 3, 3 }, 21, new int [] { 0, 4, 18 })]
  124. [InlineData (Justification.LeftJustified, new int [] { 3, 4, 5 }, 21, new int [] { 0, 4, 16 })]
  125. [InlineData (Justification.RightJustified, new int [] { 10, 20, 30 }, 100, new int [] { 0, 49, 70 })]
  126. [InlineData (Justification.RightJustified, new int [] { 33, 33, 33 }, 100, new int [] { 0, 33, 67 })]
  127. [InlineData (Justification.RightJustified, new int [] { 10 }, 101, new int [] { 0 })]
  128. [InlineData (Justification.RightJustified, new int [] { 10, 20 }, 101, new int [] { 0, 81 })]
  129. [InlineData (Justification.RightJustified, new int [] { 10, 20, 30 }, 101, new int [] { 0, 50, 71 })]
  130. [InlineData (Justification.RightJustified, new int [] { 10, 20, 30, 40 }, 101, new int [] { 0, 9, 30, 61 })]
  131. [InlineData (Justification.RightJustified, new int [] { 10, 20, 30, 40, 50 }, 151, new int [] { 0, 8, 29, 60, 101 })]
  132. [InlineData (Justification.RightJustified, new int [] { 3, 3, 3 }, 21, new int [] { 0, 14, 18 })]
  133. [InlineData (Justification.RightJustified, new int [] { 3, 4, 5 }, 21, new int [] { 0, 11, 16 })]
  134. public void TestJustifications (Justification justification, int [] sizes, int totalSize, int [] expected)
  135. {
  136. var positions = Justifier.Justify (sizes, justification, totalSize);
  137. AssertJustification (justification, sizes, totalSize, positions, expected);
  138. }
  139. public void AssertJustification (Justification justification, int [] sizes, int totalSize, int [] positions, int [] expected)
  140. {
  141. try
  142. {
  143. _output.WriteLine ($"Testing: {RenderJustification (justification, sizes, totalSize, expected)}");
  144. }
  145. catch (Exception e)
  146. {
  147. _output.WriteLine ($"Exception rendering expected: {e.Message}");
  148. _output.WriteLine ($"Actual: {RenderJustification (justification, sizes, totalSize, positions)}");
  149. }
  150. if (!expected.SequenceEqual(positions))
  151. {
  152. _output.WriteLine ($"Expected: {RenderJustification (justification, sizes, totalSize, expected)}");
  153. _output.WriteLine ($"Actual: {RenderJustification (justification, sizes, totalSize, positions)}");
  154. Assert.Fail(" Expected and actual do not match");
  155. }
  156. }
  157. public string RenderJustification (Justification justification, int [] sizes, int totalSize, int [] positions)
  158. {
  159. var output = new StringBuilder ();
  160. output.AppendLine ($"Justification: {justification}, Positions: {string.Join (", ", positions)}, TotalSize: {totalSize}");
  161. for (int i = 0; i <= totalSize / 10; i++)
  162. {
  163. output.Append (i.ToString ().PadRight (9) + " ");
  164. }
  165. output.AppendLine ();
  166. for (int i = 0; i < totalSize; i++)
  167. {
  168. output.Append (i % 10);
  169. }
  170. output.AppendLine ();
  171. var items = new char [totalSize];
  172. for (int position = 0; position < positions.Length; position++)
  173. {
  174. try
  175. {
  176. for (int j = 0; j < sizes [position]; j++)
  177. {
  178. items [positions [position] + j] = (position + 1).ToString () [0];
  179. }
  180. } catch(Exception e)
  181. {
  182. output.AppendLine ($"{e.Message} - position = {position}, positions[{position}]: {positions[position]}, sizes[{position}]: {sizes[position]}, totalSize: {totalSize}");
  183. output.Append (new string (items).Replace ('\0', ' '));
  184. Assert.Fail(e.Message + output.ToString ());
  185. }
  186. }
  187. output.Append (new string (items).Replace ('\0', ' '));
  188. return output.ToString ();
  189. }
  190. }