SafeMathTests.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using Admin.NET.Core;
  7. using Xunit;
  8. namespace Admin.NET.Test.Utils;
  9. public class SafeMathTests
  10. {
  11. [Fact]
  12. public void Add_IntAndDouble_ReturnsCorrectResult()
  13. {
  14. // Arrange
  15. int left = 10;
  16. double right = 20.5;
  17. // Act
  18. var result = SafeMath.Add<int>(left, right, precision: 2);
  19. // Assert
  20. Assert.Equal(30, result); // 10 + 20.5 = 30.5,四舍五入后为 30
  21. }
  22. [Fact]
  23. public void Add_StringAndDecimal_ReturnsCorrectResult()
  24. {
  25. // Arrange
  26. string left = "15.75";
  27. decimal right = 4.25m;
  28. // Act
  29. var result = SafeMath.Add<decimal>(left, right, precision: 2);
  30. // Assert
  31. Assert.Equal(20.00m, result); // 15.75 + 4.25 = 20.00
  32. }
  33. [Fact]
  34. public void Sub_DoubleAndInt_ReturnsCorrectResult()
  35. {
  36. // Arrange
  37. double left = 50.75;
  38. int right = 25;
  39. // Act
  40. var result = SafeMath.Sub<double>(left, right, precision: 2);
  41. // Assert
  42. Assert.Equal(25.75, result); // 50.75 - 25 = 25.75
  43. }
  44. [Fact]
  45. public void Mult_DecimalAndFloat_ReturnsCorrectResult()
  46. {
  47. // Arrange
  48. decimal left = 10.5m;
  49. float right = 2.0f;
  50. // Act
  51. var result = SafeMath.Mult<decimal>(left, right, precision: 2);
  52. // Assert
  53. Assert.Equal(21.00m, result); // 10.5 * 2.0 = 21.00
  54. }
  55. [Fact]
  56. public void Div_IntAndInt_ReturnsCorrectResult()
  57. {
  58. // Arrange
  59. int left = 10;
  60. int right = 3;
  61. // Act
  62. var result = SafeMath.Div<double>(left, right, precision: 4);
  63. // Assert
  64. Assert.Equal(3.3333, result); // 10 / 3 = 3.3333
  65. }
  66. [Fact]
  67. public void Div_ByZero_ReturnsDefaultValue()
  68. {
  69. // Arrange
  70. int left = 10;
  71. int right = 0;
  72. // Act
  73. int result = SafeMath.Div(left, right, defaultValue: -1, throwOnDivideByZero: false);
  74. // Assert
  75. Assert.Equal(-1, result); // 除数为 0,返回默认值 -1
  76. }
  77. [Fact]
  78. public void Div_ByZero_ThrowsException()
  79. {
  80. // Arrange
  81. int left = 10;
  82. int right = 0;
  83. // Act & Assert
  84. Assert.Throws<DivideByZeroException>(() =>
  85. {
  86. SafeMath.Div<double>(left, right, throwOnDivideByZero: true);
  87. });
  88. }
  89. [Fact]
  90. public void SafeConvert_StringToInt_ReturnsCorrectResult()
  91. {
  92. // Arrange
  93. string value = "42";
  94. // Act
  95. int result = SafeMath.SafeConvert(value, defaultValue: -1);
  96. // Assert
  97. Assert.Equal(42, result); // 字符串 "42" 转换为 int 42
  98. }
  99. [Fact]
  100. public void SafeConvert_InvalidString_ReturnsDefaultValue()
  101. {
  102. // Arrange
  103. string value = "invalid";
  104. // Act
  105. int result = SafeMath.SafeConvert(value, defaultValue: -1);
  106. // Assert
  107. Assert.Equal(-1, result); // 转换失败,返回默认值 -1
  108. }
  109. [Fact]
  110. public void ConvertToDecimal_Int_ReturnsCorrectResult()
  111. {
  112. // Arrange
  113. int value = 42;
  114. // Act
  115. decimal result = SafeMath.ConvertToDecimal(value);
  116. // Assert
  117. Assert.Equal(42m, result); // int 42 转换为 decimal 42m
  118. }
  119. [Fact]
  120. public void ConvertToDecimal_String_ReturnsCorrectResult()
  121. {
  122. // Arrange
  123. string value = "42.75";
  124. // Act
  125. decimal result = SafeMath.ConvertToDecimal(value);
  126. // Assert
  127. Assert.Equal(42.75m, result); // 字符串 "42.75" 转换为 decimal 42.75m
  128. }
  129. [Fact]
  130. public void ConvertToDecimal_InvalidString_ReturnsZero()
  131. {
  132. // Arrange
  133. string value = "invalid";
  134. // Act & Assert
  135. Assert.Throws<InvalidCastException>(() => SafeMath.ConvertToDecimal(value));
  136. }
  137. [Fact]
  138. public void Add_LeftNull_ReturnsDefaultValue()
  139. {
  140. // Arrange
  141. object left = null;
  142. int right = 20;
  143. // Act
  144. int result = SafeMath.Add<int>(left, right);
  145. // Assert
  146. Assert.Equal(20, result); // 左操作数为 null
  147. }
  148. [Fact]
  149. public void Add_RightNull_ReturnsDefaultValue()
  150. {
  151. // Arrange
  152. int left = 10;
  153. object right = null;
  154. // Act
  155. var result = SafeMath.Add<int>(left, right);
  156. // Assert
  157. Assert.Equal(10, result); // 右操作数为 null
  158. }
  159. [Fact]
  160. public void Sub_LeftNull_ReturnsDefaultValue()
  161. {
  162. // Arrange
  163. object left = null;
  164. int right = 20;
  165. // Act
  166. int result = SafeMath.Sub<int>(left, right);
  167. // Assert
  168. Assert.Equal(-20, result); // 左操作数为 null
  169. }
  170. [Fact]
  171. public void Sub_RightNull_ReturnsDefaultValue()
  172. {
  173. // Arrange
  174. int left = 10;
  175. object right = null;
  176. // Act
  177. var result = SafeMath.Sub<int>(left, right);
  178. // Assert
  179. Assert.Equal(10, result); // 右操作数为 null
  180. }
  181. [Fact]
  182. public void Mult_LeftNull_ReturnsDefaultValue()
  183. {
  184. // Arrange
  185. object left = null;
  186. int right = 20;
  187. // Act
  188. int result = SafeMath.Mult<int>(left, right);
  189. // Assert
  190. Assert.Equal(0, result); // 左操作数为 null
  191. }
  192. [Fact]
  193. public void Mult_RightNull_ReturnsDefaultValue()
  194. {
  195. // Arrange
  196. int left = 10;
  197. object right = null;
  198. // Act
  199. int result = SafeMath.Mult<int>(left, right);
  200. // Assert
  201. Assert.Equal(0, result); // 右操作数为 null
  202. }
  203. [Fact]
  204. public void Div_LeftNull_ReturnsDefaultValue()
  205. {
  206. // Arrange
  207. object left = null;
  208. int right = 20;
  209. // Act
  210. int result = SafeMath.Div<int>(left, right);
  211. // Assert
  212. Assert.Equal(0, result); // 左操作数为 null
  213. }
  214. [Fact]
  215. public void Div_RightNull_ReturnsDefaultValue()
  216. {
  217. // Arrange
  218. int left = 10;
  219. object right = null;
  220. // Act
  221. Assert.Throws<DivideByZeroException>(() =>
  222. {
  223. int result = SafeMath.Div<int>(left, right);
  224. // Assert
  225. Assert.Equal(-1, result); // 右操作数为 null,返回默认值 -1
  226. });
  227. }
  228. [Fact]
  229. public void SafeConvert_NullInput_ReturnsDefaultValue()
  230. {
  231. // Arrange
  232. object value = null;
  233. // Act
  234. int result = SafeMath.SafeConvert<int>(value, defaultValue: -1);
  235. // Assert
  236. Assert.Equal(-1, result); // 输入为 null,返回默认值 -1
  237. }
  238. [Fact]
  239. public void ConvertToDecimal_NullInput_ReturnsZero()
  240. {
  241. // Arrange
  242. object value = null;
  243. // Act
  244. decimal result = SafeMath.ConvertToDecimal(value);
  245. // Assert
  246. Assert.Equal(0m, result); // 输入为 null,返回默认值 0m
  247. }
  248. }