RectangleOperationTests.cs 4.5 KB

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