Skip to content

Commit 58a918f

Browse files
committed
Add ValueTypeTest
1 parent 99e5415 commit 58a918f

2 files changed

Lines changed: 66 additions & 12 deletions

File tree

src/Tests/StructTest/Program.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
using StructTest;
1+
namespace StructTest;
22

3-
var m1 = new Measurement();
4-
Console.WriteLine(m1); // output: NaN (Undefined)
3+
internal class Program
4+
{
55

6-
var m2 = default(Measurement);
7-
Console.WriteLine(m2); // output: 0 ()
6+
#region Constants & Statics
87

9-
var ms = new Measurement[2];
10-
Console.WriteLine(string.Join(", ", ms)); // output: 0 (), 0 ()
8+
private static void Main(string[] args)
9+
{
10+
var m1 = new Measurement();
11+
Console.WriteLine(m1); // output: NaN (Undefined)
1112

12-
ValueTypeTest.ShowSize();
13+
var m2 = default(Measurement);
14+
Console.WriteLine(m2); // output: 0 ()
1315

14-
ValueTypeTest.OutOfPrecisionTrueTest();
15-
ValueTypeTest.OutOfPrecisionFalseTest();
16+
var ms = new Measurement[2];
17+
Console.WriteLine(string.Join(", ", ms)); // output: 0 (), 0 ()
18+
19+
ValueTypeTest.ShowSize();
20+
21+
ValueTypeTest.OutOfPrecisionTrueTest();
22+
ValueTypeTest.OutOfPrecisionFalseTest();
23+
24+
Console.WriteLine();
25+
ValueTypeTest.EffectiveLength_4();
26+
27+
Console.WriteLine();
28+
ValueTypeTest.RoundAlgorithm();
29+
}
30+
31+
#endregion
32+
33+
protected Program()
34+
{
35+
}
36+
}

src/Tests/StructTest/ValueTypeTest.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@ public class ValueTypeTest
55

66
#region Constants & Statics
77

8+
public static void EffectiveLength_4()
9+
{
10+
var a = 999999.9999;
11+
var b = 999999999.9999;
12+
var c = 999999999999.9999;
13+
var e = 999999999999999.9999;
14+
var f = 999999999999999999.9999;
15+
16+
var ee = 999999999999999.9999m;
17+
var ff = 999999999999999999.9999m;
18+
19+
Console.WriteLine($"double 6+4 {a}");
20+
Console.WriteLine($"double 9+4 {b}");
21+
Console.WriteLine($"double 12+4 {c}");
22+
Console.WriteLine($"double 15+4 {e}"); // 无效,15 位整数部分 + 4 位小数
23+
Console.WriteLine($"double 18+4 {f}"); // 无效,18 位整数部分 + 4 位小数
24+
25+
Console.WriteLine($"decimal 15+4 {ee}");
26+
Console.WriteLine($"decimal 18+4 {ff}");
27+
}
28+
829
public static void OutOfPrecisionFalseTest()
930
{
1031
//(15 - 16 位有效数字)
@@ -19,14 +40,26 @@ public static void OutOfPrecisionFalseTest()
1940
public static void OutOfPrecisionTrueTest()
2041
{
2142
//(15 - 16 位有效数字)
22-
var maxValue = 1_999_999_999_999.9999;
23-
var beyondMax = 2_000_000_000_000; // 超过 12 位整数部分 + 4 位小数
43+
var maxValue = 1_999_999_999_999.9999; // 超过 12 位整数部分 + 4 位小数
44+
var beyondMax = 2_000_000_000_000;
2445

2546
Console.WriteLine($"Max value with 4 decimals: {maxValue}");
2647
Console.WriteLine($"Beyond max value with 4 decimals: {beyondMax}");
2748
Console.WriteLine($"Are maxValue and beyondMax equal? {maxValue == beyondMax}");
2849
}
2950

51+
public static void RoundAlgorithm()
52+
{
53+
// 银行家舍入法(四舍六入五成双)
54+
var rounded = Math.Round(1.235m, 2, MidpointRounding.ToEven);
55+
56+
// 会计常用舍入(总是远离零)
57+
var accountingRound = Math.Round(1.235m, 2, MidpointRounding.AwayFromZero);
58+
59+
Console.WriteLine(rounded);
60+
Console.WriteLine(accountingRound);
61+
}
62+
3063
public static void ShowSize()
3164
{
3265
Console.WriteLine($"sbyte: Min={sbyte.MinValue}, Max={sbyte.MaxValue}, Size={sizeof(sbyte)} bytes");

0 commit comments

Comments
 (0)