OperationHelperTests.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using ChunkyImageLib;
  4. using ChunkyImageLib.DataHolders;
  5. using ChunkyImageLib.Operations;
  6. using PixiEditor.DrawingApi.Core.Numerics;
  7. using PixiEditor.Numerics;
  8. using Xunit;
  9. namespace ChunkyImageLibTest;
  10. public class OperationHelperTests
  11. {
  12. [Theory]
  13. [InlineData(0, 0, 0, 0)]
  14. [InlineData(-1, -1, -1, -1)]
  15. [InlineData(32, 32, 1, 1)]
  16. [InlineData(-32, -32, -1, -1)]
  17. [InlineData(-33, -33, -2, -2)]
  18. public void GetChunkPos_32ChunkSize_ReturnsCorrectValues(int x, int y, int expX, int expY)
  19. {
  20. VecI act = OperationHelper.GetChunkPos(new(x, y), 32);
  21. Assert.Equal(expX, act.X);
  22. Assert.Equal(expY, act.Y);
  23. }
  24. [Theory]
  25. [InlineData(0, 0, true, true, 0, 0)]
  26. [InlineData(0, 0, false, true, -1, 0)]
  27. [InlineData(0, 0, true, false, 0, -1)]
  28. [InlineData(0, 0, false, false, -1, -1)]
  29. [InlineData(48.5, 48.5, true, true, 1, 1)]
  30. [InlineData(48.5, 48.5, false, true, 1, 1)]
  31. [InlineData(48.5, 48.5, true, false, 1, 1)]
  32. [InlineData(48.5, 48.5, false, false, 1, 1)]
  33. public void GetChunkPosBiased_32ChunkSize_ReturnsCorrectValues(double x, double y, bool positiveX, bool positiveY, int expX, int expY)
  34. {
  35. VecI act = OperationHelper.GetChunkPosBiased(new(x, y), positiveX, positiveY, 32);
  36. Assert.Equal(expX, act.X);
  37. Assert.Equal(expY, act.Y);
  38. }
  39. [Fact]
  40. public void CreateStretchedHexagon_NonStretched_ReturnsCorrectQuads()
  41. {
  42. var (left, right) = OperationHelper.CreateStretchedHexagon((-3, 5), 10 / Math.Sqrt(3), 1);
  43. Assert.Equal(right.TopLeft.X, left.TopRight.X, 6);
  44. Assert.Equal(right.BottomLeft.X, left.BottomRight.X, 6);
  45. Assert.Equal(-3, right.BottomLeft.X, 2);
  46. Assert.Equal(10.774, right.BottomLeft.Y, 2);
  47. Assert.Equal(2, right.BottomRight.X, 2);
  48. Assert.Equal(7.887, right.BottomRight.Y, 2);
  49. Assert.Equal(2, right.TopRight.X, 2);
  50. Assert.Equal(2.113, right.TopRight.Y, 2);
  51. Assert.Equal(-3, right.TopLeft.X, 2);
  52. Assert.Equal(-0.774, right.TopLeft.Y, 2);
  53. Assert.Equal(-8, left.TopLeft.X, 2);
  54. Assert.Equal(2.113, left.TopLeft.Y, 2);
  55. Assert.Equal(-8, left.BottomLeft.X, 2);
  56. Assert.Equal(7.887, left.BottomLeft.Y, 2);
  57. }
  58. [Fact]
  59. public void CreateStretchedHexagon_Stretched_ReturnsCorrectQuads()
  60. {
  61. const double x = -7;
  62. const double stretch = 4;
  63. var (left, right) = OperationHelper.CreateStretchedHexagon((x, 1), 12 / Math.Sqrt(3), stretch);
  64. Assert.Equal(right.TopLeft.X, left.TopRight.X, 6);
  65. Assert.Equal(right.BottomLeft.X, left.BottomRight.X, 6);
  66. Assert.Equal(-7, right.BottomLeft.X, 2);
  67. Assert.Equal(7.928, right.BottomLeft.Y, 2);
  68. Assert.Equal((-1 - x) * stretch + x, right.BottomRight.X, 2);
  69. Assert.Equal(4.464, right.BottomRight.Y, 2);
  70. Assert.Equal((-1 - x) * stretch + x, right.TopRight.X, 2);
  71. Assert.Equal(-2.464, right.TopRight.Y, 2);
  72. Assert.Equal(-7, right.TopLeft.X, 2);
  73. Assert.Equal(-5.928, right.TopLeft.Y, 2);
  74. Assert.Equal((-13 - x) * stretch + x, left.TopLeft.X, 2);
  75. Assert.Equal(-2.464, left.TopLeft.Y, 2);
  76. Assert.Equal((-13 - x) * stretch + x, left.BottomLeft.X, 2);
  77. Assert.Equal(4.464, left.BottomLeft.Y, 2);
  78. }
  79. [Fact]
  80. public void FindChunksTouchingEllipse_EllipseSpanningTwoChunks_FindsChunks()
  81. {
  82. int cS = ChunkyImage.FullChunkSize;
  83. var chunks = OperationHelper.FindChunksTouchingEllipse((cS, cS / 2.0), cS / 2.0, cS / 4.0, cS);
  84. Assert.Equal(new HashSet<VecI>() { (0, 0), (1, 0) }, chunks);
  85. }
  86. }