BiDiAlgorithmTests.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Avalonia.Media.TextFormatting.Unicode;
  2. using Avalonia.Utilities;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Avalonia.Visuals.UnitTests.Media.TextFormatting
  6. {
  7. public class BiDiAlgorithmTests
  8. {
  9. private readonly ITestOutputHelper _outputHelper;
  10. public BiDiAlgorithmTests(ITestOutputHelper outputHelper)
  11. {
  12. _outputHelper = outputHelper;
  13. }
  14. [Fact(Skip = "Only run when the Unicode spec changes.")]
  15. public void Should_Process()
  16. {
  17. var generator = new BiDiTestDataGenerator();
  18. foreach(var testData in generator)
  19. {
  20. Assert.True(Run(testData));
  21. }
  22. }
  23. private bool Run(BiDiTestData testData)
  24. {
  25. var bidi = new BidiAlgorithm();
  26. // Run the algorithm...
  27. ArraySlice<sbyte> resultLevels;
  28. bidi.Process(
  29. testData.Classes,
  30. ArraySlice<BidiPairedBracketType>.Empty,
  31. ArraySlice<int>.Empty,
  32. testData.ParagraphEmbeddingLevel,
  33. false,
  34. null,
  35. null,
  36. null);
  37. resultLevels = bidi.ResolvedLevels;
  38. // Check the results match
  39. var pass = true;
  40. if (resultLevels.Length == testData.Levels.Length)
  41. {
  42. for (var i = 0; i < testData.Levels.Length; i++)
  43. {
  44. if (testData.Levels[i] == -1)
  45. {
  46. continue;
  47. }
  48. if (resultLevels[i] != testData.Levels[i])
  49. {
  50. pass = false;
  51. break;
  52. }
  53. }
  54. }
  55. else
  56. {
  57. pass = false;
  58. }
  59. if (!pass)
  60. {
  61. _outputHelper.WriteLine($"Failed line {testData.LineNumber}");
  62. _outputHelper.WriteLine($" Data: {string.Join(" ", testData.Classes)}");
  63. _outputHelper.WriteLine($" Embed Level: {testData.ParagraphEmbeddingLevel}");
  64. _outputHelper.WriteLine($" Expected: {string.Join(" ", testData.Levels)}");
  65. _outputHelper.WriteLine($" Actual: {string.Join(" ", resultLevels)}");
  66. return false;
  67. }
  68. return true;
  69. }
  70. }
  71. }