Skip to content

Commit ec2daa7

Browse files
committed
Add RangeAttribute Test
1 parent 586fea0 commit ec2daa7

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

src/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,9 @@ dotnet_diagnostic.S1312.severity = none
14441444
# 如何让检测通过?
14451445
dotnet_diagnostic.S1451.severity = none
14461446

1447+
# Underscores should be used to make large numbers readable
1448+
dotnet_diagnostic.S2148.severity = none
1449+
14471450
# Optional parameters should not be used
14481451
# 尽量使用方法重载,少用默认值
14491452
dotnet_diagnostic.S2360.severity = none

src/Tests/StructTest/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ private static void Main(string[] args)
1919
ValueTypeTest.ShowSize();
2020

2121
ValueTypeTest.OutOfPrecision_True_Test();
22+
ValueTypeTest.OutOfPrecision_True2_Test();
2223
ValueTypeTest.OutOfPrecision_False_Test();
2324

2425
Console.WriteLine();
@@ -29,7 +30,7 @@ private static void Main(string[] args)
2930

3031
ValidationTest.ValidationError();
3132
Console.WriteLine();
32-
ValidationTest.ValidationAllow();
33+
//ValidationTest.ValidationAllow();
3334

3435
//var config = DefaultConfig.Instance
3536
// .WithArtifactsPath(

src/Tests/StructTest/ValidationTest.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ public static void ValidationAllow()
1414
Price = 100_000.00m,
1515
Price2 = 0.01m,
1616
Price3 = 1_000_000_000_000_000, // no error, is wrong!
17-
Price33 = 1_000_000_000_000_000, // no error, is wrong!
18-
Price4 = 999_999_999_999_999.99981m // no error
17+
Price4 = 999_999_999_999_999.99981m // no error, is right
1918
};
2019
var context = new ValidationContext(product);
2120
var results = new List<ValidationResult>();
@@ -38,8 +37,10 @@ public static void ValidationError()
3837
Price = -1.00m,
3938
Price2 = 0.001m,
4039
Price3 = 999_999_999_999_999.99991m, // no error, is wrong!
41-
Price33 = 999_999_999_999_999.99991, // no error, is wrong!
42-
Price4 = 999_999_999_999_999.99991m // error
40+
Price4 = 999_999_999_999_999.99991m, // error, is right
41+
Price5 = 999_999_999_999.99991, // no error, is wrong!
42+
Price55 = 999_999_999_999.99991, // no error, is wrong!
43+
Price555 = 999_999_999_999.99991, // error, is right
4344
};
4445
var context = new ValidationContext(product);
4546
var results = new List<ValidationResult>();
@@ -53,6 +54,10 @@ public static void ValidationError()
5354
Console.WriteLine($"{string.Join(',', error.MemberNames)} : {error.ErrorMessage}");
5455
}
5556
}
57+
//Price : 价格必须在0.01到100,000.00之间
58+
//Price2 : The field Price2 must be between 0.01 and 100000.00.
59+
//Price4 : The field Price4 must be between 0.0001 and 999999999999999.9999.
60+
//Price555 : The field Price555 must be between 0.0001 and 999999999999.9998.
5661
}
5762

5863
#endregion
@@ -73,12 +78,18 @@ public class Product
7378
[Range(0.0001, 999_999_999_999_999.9999)] // Range参数只有double,所以不能超过 double 有效位数,between 0.0001 and 1000000000000000
7479
public decimal Price3 { get; set; }
7580

76-
[Range(typeof(double), 0.0001, 999_999_999_999_999.9999)] //
77-
public double Price33 { get; set; }
78-
7981
[Range(typeof(decimal), "0.0001", "999999999999999.9999")] // decimal Range条件需要使用string参数,避免double精度问题
8082
public decimal Price4 { get; set; }
8183

84+
[Range(0.0001, 999999999999.9999)]
85+
public double Price5 { get; set; }
86+
87+
[Range(typeof(double), "0.0001", "999999999999.9999")]
88+
public double Price55 { get; set; }
89+
90+
[Range(typeof(double), "0.0001", "999999999999.9998")]
91+
public double Price555 { get; set; }
92+
8293
#endregion
8394
}
8495
}

src/Tests/StructTest/ValueTypeTest.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,22 @@ public static void EffectiveLength_4()
1919
Console.WriteLine($"double 6+4 {a}");
2020
Console.WriteLine($"double 9+4 {b}");
2121
Console.WriteLine($"double 12+4 {c}");
22+
Console.WriteLine($"double 13+4 {9999999999999.9999}"); //无效
2223
Console.WriteLine($"double 15+4 {e}"); // 无效,15 位整数部分 + 4 位小数
2324
Console.WriteLine($"double 18+4 {f}"); // 无效,18 位整数部分 + 4 位小数
2425

2526
Console.WriteLine($"decimal 15+4 {ee}");
2627
Console.WriteLine($"decimal 18+4 {ff}");
28+
29+
//double 6+4 999999.9999
30+
//double 9+4 999999999.9999
31+
//double 12+4 999999999999.9999
32+
//double 13+4 10000000000000
33+
//double 15+4 1000000000000000
34+
//double 18+4 1E+18
35+
36+
//decimal 15+4 999999999999999.9999
37+
//decimal 18+4 999999999999999999.9999
2738
}
2839

2940
public static void OutOfPrecision_False_Test()
@@ -43,11 +54,22 @@ public static void OutOfPrecision_True_Test()
4354
var maxValue = 1_999_999_999_999.9999; // 超过 12 位整数部分 + 4 位小数
4455
var beyondMax = 2_000_000_000_000;
4556

46-
Console.WriteLine($"Max value with 4 decimals: {maxValue}");
57+
Console.WriteLine($"Max value with 4 decimals: {maxValue}"); // 近似为 2000000000000
4758
Console.WriteLine($"Beyond max value with 4 decimals: {beyondMax}");
4859
Console.WriteLine($"Are maxValue and beyondMax equal? {maxValue == beyondMax}");
4960
}
5061

62+
public static void OutOfPrecision_True2_Test()
63+
{
64+
//(15 - 16 位有效数字)
65+
var maxValue = 999_999_999_999.9999;
66+
var beyondMax = 999_999_999_999.99991; // 12 位整数部分 + 4 位小数
67+
68+
Console.WriteLine($"Max value with 4 decimals: {maxValue}");
69+
Console.WriteLine($"Beyond max value with 4 decimals: {beyondMax}");// 近似为 999_999_999_999.9999
70+
Console.WriteLine($"Are maxValue and beyondMax equal? {maxValue == beyondMax}");
71+
}
72+
5173
public static void RoundAlgorithm()
5274
{
5375
// 银行家舍入法(四舍六入五成双)

0 commit comments

Comments
 (0)