RectangleOperationTests.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System.Collections.Generic;
  2. using ChunkyImageLib;
  3. using ChunkyImageLib.Operations;
  4. using Drawie.Backend.Core.Bridge;
  5. using Drawie.Backend.Core.ColorsImpl;
  6. using Drawie.Numerics;
  7. using Drawie.Skia;
  8. using Xunit;
  9. namespace ChunkyImageLibTest;
  10. public class RectangleOperationTests
  11. {
  12. const int chunkSize = ChunkPool.FullChunkSize;
  13. public RectangleOperationTests()
  14. {
  15. try
  16. {
  17. DrawingBackendApi.SetupBackend(new SkiaDrawingBackend(), null);
  18. }
  19. catch
  20. {
  21. }
  22. }
  23. // to keep expected rectangles aligned
  24. #pragma warning disable format
  25. [Fact]
  26. public void FindAffectedArea_SmallStrokeOnly_FindsCorrectChunks()
  27. {
  28. var (x, y, w, h) = (chunkSize / 2, chunkSize / 2, chunkSize, chunkSize);
  29. RectangleOperation operation = new(new(new(x, y), new(w, h), 0, 0, 1, Colors.Black, Colors.Transparent));
  30. HashSet<VecI> expected = new() { new(0, 0) };
  31. var actual = operation.FindAffectedArea(new(chunkSize)).Chunks;
  32. Assert.Equal(expected, actual);
  33. }
  34. [Fact]
  35. public void FindAffectedArea_2by2StrokeOnly_FindsCorrectChunks()
  36. {
  37. var (x, y, w, h) = (0, 0, chunkSize * 2, chunkSize * 2);
  38. RectangleOperation operation = new(new(new(x, y), new(w, h), 0, 0, 1, Colors.Black, Colors.Transparent));
  39. HashSet<VecI> expected = new() { new(-1, -1), new(0, -1), new(-1, 0), new(0, 0) };
  40. var actual = operation.FindAffectedArea(new(chunkSize)).Chunks;
  41. Assert.Equal(expected, actual);
  42. }
  43. [Fact]
  44. public void FindAffectedArea_3x3PositiveStrokeOnly_FindsCorrectChunks()
  45. {
  46. var (x, y, w, h) = (2 * chunkSize + chunkSize / 2, 2 * chunkSize + chunkSize / 2, chunkSize * 2, chunkSize * 2);
  47. RectangleOperation operation = new(new(new(x, y), new(w, h), 0, 0, 1, Colors.Black, Colors.Transparent));
  48. HashSet<VecI> expected = new()
  49. {
  50. new(1, 1), new(2, 1), new(3, 1),
  51. new(1, 2), new(3, 2),
  52. new(1, 3), new(2, 3), new(3, 3),
  53. };
  54. var actual = operation.FindAffectedArea(new(chunkSize)).Chunks;
  55. Assert.Equal(expected, actual);
  56. }
  57. [Fact]
  58. public void FindAffectedArea_3x3NegativeStrokeOnly_FindsCorrectChunks()
  59. {
  60. var (x, y, w, h) = (-chunkSize * 2 - chunkSize / 2, -chunkSize * 2 - chunkSize / 2, chunkSize * 2,
  61. chunkSize * 2);
  62. RectangleOperation operation = new(new(new(x, y), new(w, h), 0, 0, 1, Colors.Black, Colors.Transparent));
  63. HashSet<VecI> expected = new()
  64. {
  65. new(-4, -4), new(-3, -4), new(-2, -4),
  66. new(-4, -3), new(-2, -3),
  67. new(-4, -2), new(-3, -2), new(-2, -2),
  68. };
  69. var actual = operation.FindAffectedArea(new(chunkSize)).Chunks;
  70. Assert.Equal(expected, actual);
  71. }
  72. [Fact]
  73. public void FindAffectedArea_3x3PositiveFilled_FindsCorrectChunks()
  74. {
  75. var (x, y, w, h) = (2 * chunkSize + chunkSize / 2, 2 * chunkSize + chunkSize / 2, chunkSize * 2, chunkSize * 2);
  76. RectangleOperation operation = new(new(new(x, y), new(w, h), 0, 0, 1, Colors.Black, Colors.White));
  77. HashSet<VecI> expected = new()
  78. {
  79. new(1, 1), new(2, 1), new(3, 1),
  80. new(1, 2), new(2, 2), new(3, 2),
  81. new(1, 3), new(2, 3), new(3, 3),
  82. };
  83. var actual = operation.FindAffectedArea(new(chunkSize)).Chunks;
  84. Assert.Equal(expected, actual);
  85. }
  86. [Fact]
  87. public void FindAffectedArea_ThickPositiveStroke_FindsCorrectChunks()
  88. {
  89. var (x, y, w, h) = (2 * chunkSize + chunkSize / 2, 2 * chunkSize + chunkSize / 2, chunkSize * 4, chunkSize * 4);
  90. RectangleOperation operation =
  91. new(new(new(x, y), new(w, h), 0, 0, chunkSize, Colors.Black, Colors.Transparent));
  92. HashSet<VecI> expected = new()
  93. {
  94. new(0, 0), new(1, 0), new(2, 0), new(3, 0), new(4, 0),
  95. new(0, 1), new(1, 1), new(2, 1), new(3, 1), new(4, 1),
  96. new(0, 2), new(1, 2), new(3, 2), new(4, 2),
  97. new(0, 3), new(1, 3), new(2, 3), new(3, 3), new(4, 3),
  98. new(0, 4), new(1, 4), new(2, 4), new(3, 4), new(4, 4),
  99. };
  100. var actual = operation.FindAffectedArea(new(chunkSize)).Chunks;
  101. Assert.Equal(expected, actual);
  102. }
  103. [Fact]
  104. public void FindAffectedArea_SmallButThick_FindsCorrectChunks()
  105. {
  106. var (x, y, w, h) = (chunkSize / 2f - 0.5, chunkSize / 2f - 0.5, 1, 1);
  107. RectangleOperation operation = new(new(new(x, y), new(w, h), 0, 0, chunkSize, Colors.Black, Colors.White));
  108. HashSet<VecI> expected = new() { new(0, 0) };
  109. var actual = operation.FindAffectedArea(new(chunkSize)).Chunks;
  110. Assert.Equal(expected, actual);
  111. }
  112. #pragma warning restore format
  113. }