RectangleOperationTests.cs 4.5 KB

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