-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathConstantComparison.cs
More file actions
96 lines (73 loc) · 2.68 KB
/
ConstantComparison.cs
File metadata and controls
96 lines (73 loc) · 2.68 KB
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
using System;
class Test
{
char charValue;
byte byteValue;
sbyte sbyteValue;
short shortValue;
ushort ushortValue;
int intValue;
uint uintValue;
long longValue;
ulong ulongValue;
void f()
{
bool good, bad;
bad = uintValue < 0; // $ Alert
bad = 0 > uintValue; // $ Alert
bad = 0 <= uintValue; // $ Alert
bad = uintValue >= 0; // $ Alert
bad = uintValue == -1; // $ Alert
bad = uintValue != -1; // $ Alert
bad = 256 == byteValue; // $ Alert
bad = 256 != byteValue; // $ Alert
bad = 1 != 0; // $ Alert
good = byteValue == 50;
good = 50 != byteValue;
good = 1u < intValue;
good = intValue > 1u;
good = intValue <= 1u;
good = 1u >= intValue;
good = charValue >= '0';
good = charValue < '0';
// Test ranges
bad = charValue <= 65535; // $ Alert
bad = charValue >= 0; // $ Alert
good = charValue < 255;
good = charValue > 0;
bad = byteValue >= byte.MinValue; // $ Alert
bad = byteValue <= byte.MaxValue; // $ Alert
good = byteValue > byte.MinValue;
good = byteValue < byte.MaxValue;
bad = sbyteValue >= sbyte.MinValue; // $ Alert
bad = sbyteValue <= sbyte.MaxValue; // $ Alert
good = sbyteValue < sbyte.MaxValue;
good = sbyteValue > sbyte.MinValue;
bad = shortValue >= short.MinValue; // $ Alert
bad = shortValue <= short.MaxValue; // $ Alert
good = shortValue > short.MinValue;
good = shortValue < short.MaxValue;
bad = ushortValue >= ushort.MinValue; // $ Alert
bad = ushortValue <= ushort.MaxValue; // $ Alert
good = ushortValue > ushort.MinValue;
good = ushortValue < ushort.MaxValue;
bad = intValue >= int.MinValue; // $ Alert
bad = intValue <= int.MaxValue; // $ Alert
good = intValue > int.MinValue;
good = intValue < int.MaxValue;
bad = uintValue >= uint.MinValue; // $ Alert
good = uintValue > uint.MinValue;
bad = ulongValue >= ulong.MinValue; // $ Alert
good = ulongValue > ulong.MinValue;
// Explicit casts can cause large values to be truncated or
// to wrap into negative values.
good = (sbyte)byteValue >= 0;
good = (sbyte)byteValue == -1;
bad = (sbyte)byteValue > 127; // $ Alert
bad = (sbyte)byteValue > (sbyte)127; // $ Alert
good = (int)uintValue == -1;
good = (sbyte)uintValue == -1;
bad = (sbyte)uintValue == 256; // $ Alert
System.Diagnostics.Debug.Assert(ulongValue >= ulong.MinValue); // GOOD
}
}