RectangleOperationTests.cs 4.4 KB

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