JustifierTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 [] { 1, 2, 3 }, 10, new int [] { 1, 3, 6 })]
  85. [InlineData (Justification.Centered, new int [] { 10, 20, 30 }, 100, new int [] { 19, 30, 51 })]
  86. [InlineData (Justification.Centered, new int [] { 3, 3, 3 }, 9, new int [] { 0, 3, 6 })]
  87. [InlineData (Justification.Centered, new int [] { 3, 3, 3 }, 10, new int [] { 0, 4, 7 })]
  88. [InlineData (Justification.Centered, new int [] { 3, 3, 3 }, 11, new int [] { 0, 4, 8 })]
  89. [InlineData (Justification.Centered, new int [] { 33, 33, 33 }, 100, new int [] { 0, 33, 66 })]
  90. [InlineData (Justification.Centered, new int [] { 33, 33, 33 }, 101, new int [] { 1, 34, 67 })]
  91. [InlineData (Justification.Centered, new int [] { 33, 33, 33 }, 102, new int [] { 1, 34, 67 })]
  92. [InlineData (Justification.Centered, new int [] { 33, 33, 33 }, 104, new int [] { 2, 35, 68 })]
  93. [InlineData (Justification.Centered, new int [] { 10 }, 101, new int [] { 45 })]
  94. [InlineData (Justification.Centered, new int [] { 10, 20 }, 101, new int [] { 35, 45 })]
  95. [InlineData (Justification.Centered, new int [] { 10, 20, 30 }, 101, new int [] { 20, 30, 50 })]
  96. [InlineData (Justification.Centered, new int [] { 10, 20, 30, 40 }, 101, new int [] { 0, 10, 30, 60 })]
  97. [InlineData (Justification.Centered, new int [] { 10, 20, 30, 40, 50 }, 151, new int [] { 0, 10, 30, 60, 100 })]
  98. [InlineData (Justification.Centered, new int [] { 3, 4, 5, 6 }, 25, new int [] { 2, 6, 11, 17 })]
  99. //[InlineData (Justification.Justified, new int [] { 10, 20, 30, 40, 50 }, 151, new int [] { 0, 11, 31, 61, 101 })]
  100. //[InlineData (Justification.Justified, new int [] { 10, 20, 30, 40 }, 101, new int [] { 0, 11, 31, 61 })]
  101. //[InlineData (Justification.Justified, new int [] { 10, 20, 30 }, 100, new int [] { 0, 30, 70 })]
  102. //[InlineData (Justification.Justified, new int [] { 10, 20, 30 }, 101, new int [] { 0, 31, 71 })]
  103. //[InlineData (Justification.Justified, new int [] { 33, 33, 33 }, 100, new int [] { 0, 34, 67 })]
  104. //[InlineData (Justification.Justified, new int [] { 11, 17, 23 }, 100, new int [] { 0, 36, 77 })]
  105. //[InlineData (Justification.Justified, new int [] { 1, 2, 3 }, 11, new int [] { 0, 4, 8 })]
  106. //[InlineData (Justification.Justified, new int [] { 10, 20 }, 101, new int [] { 0, 81 })]
  107. //[InlineData (Justification.Justified, new int [] { 10 }, 101, new int [] { 0 })]
  108. //[InlineData (Justification.Justified, new int [] { 3, 3, 3 }, 21, new int [] { 0, 9, 18 })]
  109. //[InlineData (Justification.Justified, new int [] { 3, 4, 5 }, 21, new int [] { 0, 8, 16 })]
  110. //[InlineData (Justification.Justified, new int [] { 3, 4, 5, 6 }, 18, new int [] { 0, 3, 7, 12 })]
  111. //[InlineData (Justification.Justified, new int [] { 3, 4, 5, 6 }, 19, new int [] { 0, 4, 8, 13 })]
  112. //[InlineData (Justification.Justified, new int [] { 3, 4, 5, 6 }, 20, new int [] { 0, 4, 9, 14 })]
  113. //[InlineData (Justification.Justified, new int [] { 3, 4, 5, 6 }, 21, new int [] { 0, 4, 9, 15 })]
  114. //[InlineData (Justification.Justified, new int [] { 6, 5, 4, 3 }, 22, new int [] { 0, 8, 14, 19 })]
  115. //[InlineData (Justification.Justified, new int [] { 6, 5, 4, 3 }, 23, new int [] { 0, 8, 15, 20, })]
  116. //[InlineData (Justification.Justified, new int [] { 6, 5, 4, 3 }, 24, new int [] { 0, 8, 15, 21 })]
  117. //[InlineData (Justification.Justified, new int [] { 6, 5, 4, 3 }, 25, new int [] { 0, 9, 16, 22 })]
  118. //[InlineData (Justification.Justified, new int [] { 6, 5, 4, 3 }, 26, new int [] { 0, 9, 17, 23 })]
  119. //[InlineData (Justification.Justified, new int [] { 6, 5, 4, 3 }, 31, new int [] { 0, 11, 20, 28 })]
  120. //[InlineData (Justification.LeftJustified, new int [] { 10, 20, 30 }, 100, new int [] { 0, 11, 70 })]
  121. //[InlineData (Justification.LeftJustified, new int [] { 33, 33, 33 }, 100, new int [] { 0, 34, 67 })]
  122. //[InlineData (Justification.LeftJustified, new int [] { 10 }, 101, new int [] { 0 })]
  123. //[InlineData (Justification.LeftJustified, new int [] { 10, 20 }, 101, new int [] { 0, 81 })]
  124. //[InlineData (Justification.LeftJustified, new int [] { 10, 20, 30 }, 101, new int [] { 0, 11, 71 })]
  125. //[InlineData (Justification.LeftJustified, new int [] { 10, 20, 30, 40 }, 101, new int [] { 0, 11, 32, 61 })]
  126. //[InlineData (Justification.LeftJustified, new int [] { 10, 20, 30, 40, 50 }, 151, new int [] { 0, 11, 32, 63, 101 })]
  127. //[InlineData (Justification.LeftJustified, new int [] { 3, 3, 3 }, 21, new int [] { 0, 4, 18 })]
  128. //[InlineData (Justification.LeftJustified, new int [] { 3, 4, 5 }, 21, new int [] { 0, 4, 16 })]
  129. //[InlineData (Justification.RightJustified, new int [] { 10, 20, 30 }, 100, new int [] { 0, 49, 70 })]
  130. //[InlineData (Justification.RightJustified, new int [] { 33, 33, 33 }, 100, new int [] { 0, 33, 67 })]
  131. //[InlineData (Justification.RightJustified, new int [] { 10 }, 101, new int [] { 0 })]
  132. //[InlineData (Justification.RightJustified, new int [] { 10, 20 }, 101, new int [] { 0, 81 })]
  133. //[InlineData (Justification.RightJustified, new int [] { 10, 20, 30 }, 101, new int [] { 0, 50, 71 })]
  134. //[InlineData (Justification.RightJustified, new int [] { 10, 20, 30, 40 }, 101, new int [] { 0, 9, 30, 61 })]
  135. //[InlineData (Justification.RightJustified, new int [] { 10, 20, 30, 40, 50 }, 151, new int [] { 0, 8, 29, 60, 101 })]
  136. //[InlineData (Justification.RightJustified, new int [] { 3, 3, 3 }, 21, new int [] { 0, 14, 18 })]
  137. //[InlineData (Justification.RightJustified, new int [] { 3, 4, 5 }, 21, new int [] { 0, 11, 16 })]
  138. public void TestJustifications (Justification justification, int [] sizes, int totalSize, int [] expected)
  139. {
  140. var positions = Justifier.Justify (sizes, justification, totalSize);
  141. AssertJustification (justification, sizes, totalSize, positions, expected);
  142. }
  143. public void AssertJustification (Justification justification, int [] sizes, int totalSize, int [] positions, int [] expected)
  144. {
  145. try
  146. {
  147. _output.WriteLine ($"Testing: {RenderJustification (justification, sizes, totalSize, expected)}");
  148. }
  149. catch (Exception e)
  150. {
  151. _output.WriteLine ($"Exception rendering expected: {e.Message}");
  152. _output.WriteLine ($"Actual: {RenderJustification (justification, sizes, totalSize, positions)}");
  153. }
  154. if (!expected.SequenceEqual(positions))
  155. {
  156. _output.WriteLine ($"Expected: {RenderJustification (justification, sizes, totalSize, expected)}");
  157. _output.WriteLine ($"Actual: {RenderJustification (justification, sizes, totalSize, positions)}");
  158. Assert.Fail(" Expected and actual do not match");
  159. }
  160. }
  161. public string RenderJustification (Justification justification, int [] sizes, int totalSize, int [] positions)
  162. {
  163. var output = new StringBuilder ();
  164. output.AppendLine ($"Justification: {justification}, Positions: {string.Join (", ", positions)}, TotalSize: {totalSize}");
  165. for (int i = 0; i <= totalSize / 10; i++)
  166. {
  167. output.Append (i.ToString ().PadRight (9) + " ");
  168. }
  169. output.AppendLine ();
  170. for (int i = 0; i < totalSize; i++)
  171. {
  172. output.Append (i % 10);
  173. }
  174. output.AppendLine ();
  175. var items = new char [totalSize];
  176. for (int position = 0; position < positions.Length; position++)
  177. {
  178. try
  179. {
  180. for (int j = 0; j < sizes [position]; j++)
  181. {
  182. items [positions [position] + j] = (position + 1).ToString () [0];
  183. }
  184. } catch(Exception e)
  185. {
  186. output.AppendLine ($"{e.Message} - position = {position}, positions[{position}]: {positions[position]}, sizes[{position}]: {sizes[position]}, totalSize: {totalSize}");
  187. output.Append (new string (items).Replace ('\0', ' '));
  188. Assert.Fail(e.Message + output.ToString ());
  189. }
  190. }
  191. output.Append (new string (items).Replace ('\0', ' '));
  192. return output.ToString ();
  193. }
  194. }