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