VectorTests.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Xunit;
  4. using Avalonia;
  5. using System;
  6. namespace Avalonia.Visuals.UnitTests
  7. {
  8. public class VectorTests
  9. {
  10. [Fact]
  11. public void Length_Should_Return_Correct_Length_Of_Vector()
  12. {
  13. var vector = new Vector(2, 4);
  14. var length = Math.Sqrt(2 * 2 + 4 * 4);
  15. Assert.Equal(length, vector.Length);
  16. }
  17. [Fact]
  18. public void Length_Squared_Should_Return_Correct_Length_Of_Vector()
  19. {
  20. var vectorA = new Vector(2, 4);
  21. var squaredLengthA = 2 * 2 + 4 * 4;
  22. Assert.Equal(squaredLengthA, vectorA.SquaredLength);
  23. }
  24. [Fact]
  25. public void Normalize_Should_Return_Normalized_Vector()
  26. {
  27. // the length of a normalized vector must be 1
  28. var vectorA = new Vector(13, 84);
  29. var vectorB = new Vector(-34, 345);
  30. var vectorC = new Vector(-34, -84);
  31. Assert.Equal(1.0, vectorA.Normalize().Length);
  32. Assert.Equal(1.0, vectorB.Normalize().Length);
  33. Assert.Equal(1.0, vectorC.Normalize().Length);
  34. }
  35. [Fact]
  36. public void Negate_Should_Return_Negated_Vector()
  37. {
  38. var vector = new Vector(2, 4);
  39. var negated = new Vector(-2, -4);
  40. Assert.Equal(negated, vector.Negate());
  41. }
  42. [Fact]
  43. public void Dot_Should_Return_Correct_Value()
  44. {
  45. var a = new Vector(-6, 8.0);
  46. var b = new Vector(5, 12.0);
  47. Assert.Equal(66.0, Vector.Dot(a, b));
  48. }
  49. [Fact]
  50. public void Cross_Should_Return_Correct_Value()
  51. {
  52. var a = new Vector(-6, 8.0);
  53. var b = new Vector(5, 12.0);
  54. Assert.Equal(-112.0, Vector.Cross(a, b));
  55. }
  56. [Fact]
  57. public void Divied_By_Vector_Should_Return_Correct_Value()
  58. {
  59. var a = new Vector(10, 2);
  60. var b = new Vector(5, 2);
  61. var expected = new Vector(2, 1);
  62. Assert.Equal(expected, Vector.Divide(a, b));
  63. }
  64. [Fact]
  65. public void Divied_Should_Return_Correct_Value()
  66. {
  67. var vector = new Vector(10, 2);
  68. var expected = new Vector(5, 1);
  69. Assert.Equal(expected, Vector.Divide(vector, 2));
  70. }
  71. [Fact]
  72. public void Multiply_By_Vector_Should_Return_Correct_Value()
  73. {
  74. var a = new Vector(10, 2);
  75. var b = new Vector(2, 2);
  76. var expected = new Vector(20, 4);
  77. Assert.Equal(expected, Vector.Multiply(a, b));
  78. }
  79. [Fact]
  80. public void Multiply_Should_Return_Correct_Value()
  81. {
  82. var vector = new Vector(10, 2);
  83. var expected = new Vector(20, 4);
  84. Assert.Equal(expected, Vector.Multiply(vector, 2));
  85. }
  86. }
  87. }