OperationHelperTests.cs 3.5 KB

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