FixedPoint2_Tests.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using Content.Shared.FixedPoint;
  3. using NUnit.Framework;
  4. namespace Content.Tests.Shared.Chemistry
  5. {
  6. [TestFixture, TestOf(typeof(FixedPoint2)), Parallelizable]
  7. public sealed class FixedPoint2_Tests
  8. {
  9. [Test]
  10. [TestCase(1, "1")]
  11. [TestCase(0, "0")]
  12. [TestCase(-1, "-1")]
  13. public void FixedPoint2IntegerTests(int value, string expected)
  14. {
  15. var result = FixedPoint2.New(value);
  16. Assert.That($"{result}", Is.EqualTo(expected));
  17. }
  18. [Test]
  19. [TestCase(0.999f, "0.99")]
  20. [TestCase(1.005f, "1")]
  21. [TestCase(1.015f, "1.01")]
  22. [TestCase(1.05f, "1.05")]
  23. [TestCase(-1.05f, "-1.05")]
  24. public void FixedPoint2FloatTests(float value, string expected)
  25. {
  26. var result = FixedPoint2.New(value);
  27. Assert.That($"{result}", Is.EqualTo(expected));
  28. }
  29. [Test]
  30. [TestCase(0.999, "0.99")]
  31. [TestCase(1.005, "1")]
  32. [TestCase(1.015, "1.01")]
  33. [TestCase(1.05, "1.05")]
  34. public void FixedPoint2DoubleTests(double value, string expected)
  35. {
  36. var result = FixedPoint2.New(value);
  37. Assert.That($"{result}", Is.EqualTo(expected));
  38. }
  39. [Test]
  40. [TestCase("0.999", "0.99")]
  41. [TestCase("1.005", "1")]
  42. [TestCase("1.015", "1.01")]
  43. [TestCase("1.05", "1.05")]
  44. public void FixedPoint2StringTests(string value, string expected)
  45. {
  46. var result = FixedPoint2.New(value);
  47. Assert.That($"{result}", Is.EqualTo(expected));
  48. }
  49. [Test]
  50. [TestCase(1, 1, "2")]
  51. [TestCase(1.05f, 1, "2.05")]
  52. public void ArithmeticAddition(float aFloat, float bFloat, string expected)
  53. {
  54. var a = FixedPoint2.New(aFloat);
  55. var b = FixedPoint2.New(bFloat);
  56. var result = a + b;
  57. Assert.That($"{result}", Is.EqualTo(expected));
  58. }
  59. [Test]
  60. [TestCase(1, 1, "0")]
  61. [TestCase(1f, 2.5f, "-1.5")]
  62. public void ArithmeticSubtraction(float aFloat, float bFloat, string expected)
  63. {
  64. var a = FixedPoint2.New(aFloat);
  65. var b = FixedPoint2.New(bFloat);
  66. var result = a - b;
  67. Assert.That($"{result}", Is.EqualTo(expected));
  68. }
  69. [Test]
  70. [TestCase(1.001f, 3f, "0.33")]
  71. [TestCase(0.999f, 3f, "0.33")]
  72. [TestCase(2.1f, 3f, "0.7")]
  73. [TestCase(0.03f, 2f, "0.01")]
  74. public void ArithmeticDivision(float aFloat, float bFloat, string expected)
  75. {
  76. var a = FixedPoint2.New(aFloat);
  77. var b = FixedPoint2.New(bFloat);
  78. var result = a / b;
  79. Assert.That($"{result}", Is.EqualTo(expected));
  80. }
  81. [Test]
  82. [TestCase(1.001f, 3f, "0.33")]
  83. [TestCase(0.999f, 3f, "0.33")]
  84. [TestCase(2.1f, 3f, "0.7")]
  85. [TestCase(0.03f, 2f, "0.01")]
  86. [TestCase(1f, 1 / 1.05f, "1.05")]
  87. public void ArithmeticDivisionFloat(float aFloat, float b, string expected)
  88. {
  89. var a = FixedPoint2.New(aFloat);
  90. var result = a / b;
  91. Assert.That($"{result}", Is.EqualTo(expected));
  92. }
  93. [Test]
  94. [TestCase(1, 1, "1")]
  95. [TestCase(1, 3f, "3")]
  96. public void ArithmeticMultiplication(float aFloat, float bFloat, string expected)
  97. {
  98. var a = FixedPoint2.New(aFloat);
  99. var b = FixedPoint2.New(bFloat);
  100. var result = a * b;
  101. Assert.That($"{result}", Is.EqualTo(expected));
  102. }
  103. [Test]
  104. [TestCase(1, 1, "1")]
  105. [TestCase(1, 1.05f, "1.05")]
  106. public void ArithmeticMultiplicationFloat(float aFloat, float b, string expected)
  107. {
  108. var a = FixedPoint2.New(aFloat);
  109. var result = a * b;
  110. Assert.That($"{result}", Is.EqualTo(expected));
  111. }
  112. [Test]
  113. [TestCase(0.995f, 100)]
  114. [TestCase(1.005f, 101)]
  115. [TestCase(2.005f, 201)]
  116. public void FloatRoundingTest(float a, int expected)
  117. {
  118. var result = (int) MathF.Round(a * MathF.Pow(10, 2), MidpointRounding.AwayFromZero);
  119. Assert.That(result, Is.EqualTo(expected));
  120. }
  121. [Test]
  122. public void FixedPoint2Min()
  123. {
  124. var unorderedList = new[]
  125. {
  126. FixedPoint2.New(5),
  127. FixedPoint2.New(3),
  128. FixedPoint2.New(1),
  129. FixedPoint2.New(2),
  130. FixedPoint2.New(4),
  131. };
  132. var min = FixedPoint2.Min(unorderedList);
  133. Assert.That(min, Is.EqualTo(FixedPoint2.New(1)));
  134. }
  135. [Test]
  136. [TestCase(10.1f, 2.5f, "25.25")]
  137. public void FloatMultiply (float aFloat, float b, string expected)
  138. {
  139. var a = FixedPoint2.New(aFloat);
  140. var result = a*b;
  141. Assert.That($"{result}", Is.EqualTo(expected));
  142. }
  143. [Test]
  144. [TestCase(10.1f, 2.5d, "25.25")]
  145. public void DoubleMultiply(float aFloat, double b, string expected)
  146. {
  147. var a = FixedPoint2.New(aFloat);
  148. var result = a * b;
  149. Assert.That($"{result}", Is.EqualTo(expected));
  150. }
  151. [Test]
  152. [TestCase(10.1f, 2.5f, "4.04")]
  153. public void FloatDivide(float aFloat, float b, string expected)
  154. {
  155. var a = FixedPoint2.New(aFloat);
  156. var result = a / b;
  157. Assert.That($"{result}", Is.EqualTo(expected));
  158. }
  159. [Test]
  160. [TestCase(10.1f, 2.5d, "4.04")]
  161. public void DoubleDivide(float aFloat, double b, string expected)
  162. {
  163. var a = FixedPoint2.New(aFloat);
  164. var result = a / b;
  165. Assert.That($"{result}", Is.EqualTo(expected));
  166. }
  167. [Test]
  168. [TestCase(1, 0, false)]
  169. [TestCase(0, 0, true)]
  170. [TestCase(-1, 0, false)]
  171. [TestCase(1, 1, true)]
  172. [TestCase(0, 1, false)]
  173. [TestCase(-1, 1, false)]
  174. public void FixedPoint2Equals(int a, int b, bool expected)
  175. {
  176. var parameter = FixedPoint2.New(a);
  177. var comparison = FixedPoint2.New(b);
  178. Assert.That(parameter.Equals(comparison), Is.EqualTo(comparison.Equals(parameter)));
  179. Assert.That(comparison.Equals(parameter), Is.EqualTo(expected));
  180. }
  181. [Test]
  182. [TestCase(1.001f, "1.01")]
  183. [TestCase(2f, "2")]
  184. [TestCase(2.5f, "2.5")]
  185. public void NewCeilingTest(float value, string expected)
  186. {
  187. var result = FixedPoint2.NewCeiling(value);
  188. Assert.That($"{result}", Is.EqualTo(expected));
  189. }
  190. }
  191. }