forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBNotOperator.Tests.ps1
More file actions
142 lines (118 loc) · 4.11 KB
/
Copy pathBNotOperator.Tests.ps1
File metadata and controls
142 lines (118 loc) · 4.11 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
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
$baseTypes = @{
[SByte] = 'sbyte'; [Byte] = 'byte'
[Int16] = 'short'; [UInt16] = 'ushort'
[Int32] = 'int'; [UInt32] = 'uint'
[Int64] = 'long'; [UInt64] = 'ulong'
}
$ns = [Guid]::NewGuid() -replace '-',''
$typeDefinition = "namespace ns_$ns`n{"
$enumTypeNames = foreach ($baseType in $baseTypes.Keys)
{
$baseTypeName = $baseTypes[$baseType]
$typeDefinition += @"
public enum E_$baseTypeName : $baseTypeName
{
Min = $($baseType::MinValue),
MinPlus1 = $($baseType::MinValue + 1),
MaxMinus1 = $($baseType::MaxValue - 1),
Max = $($baseType::MaxValue)
}
"@
"ns_$ns.E_$baseTypeName"
}
$typeDefinition += "`n}"
Write-Verbose $typeDefinition
Add-Type $typeDefinition
Describe "bnot on enums" -Tags "CI" {
foreach ($enumType in [type[]]$enumTypeNames)
{
Context $enumType.Name {
It "max - 1" {
$res = -bnot $enumType::MaxMinus1
$res | Should Be $enumType::MinPlus1
$res | Should BeOfType $enumType
}
It "min + 1" {
$res = -bnot $enumType::MinPlus1
$res | Should Be $enumType::MaxMinus1
$res | Should BeOfType $enumType
}
It "Max" {
$res = -bnot $enumType::Max
$res | Should Be $enumType::Min
$res | Should BeOfType $enumType
}
It "Min" {
$res = -bnot $enumType::Min
$res | Should Be $enumType::Max
$res | Should BeOfType $enumType
}
}
}
}
Describe "bnot on integral types" -Tags "CI" {
foreach ($baseType in $baseTypes.Keys)
{
Context $baseType.Name {
$max = $baseType::MaxValue
$maxMinus1 = $max - 1
$min = $baseType::MinValue
$minPlus1 = $min + 1
if ([System.Runtime.InteropServices.Marshal]::SizeOf([type]$baseType) -lt 4)
{
$expectedResultType = [int]
}
else
{
$expectedResultType = $baseType
}
if ($baseType -eq [byte] -or $baseType -eq [uint16])
{
# Because of type promotion rules, unsigned types smaller than int
# don't quite follow the pattern of "flip all the bits", so our
# tests are a little different.
It "max - 1" {
$res = -bnot $maxMinus1
$res | Should Be (-bnot [int]$maxMinus1)
$res | Should BeOfType $expectedResultType
}
It "min + 1" {
$res = -bnot $minPlus1
$res | Should Be (-bnot [int]$minPlus1)
$res | Should BeOfType $expectedResultType
}
It "max" {
$res = -bnot $max
$res | Should Be (-bnot [int]$max)
$res | Should BeOfType $expectedResultType
}
It "min" {
$res = -bnot $min
$res | Should Be (-bnot [int]$min)
$res | Should BeOfType $expectedResultType
}
return
}
It "max - 1" {
$res = -bnot $maxMinus1
$res | Should Be $minPlus1
$res | Should BeOfType $expectedResultType
}
It "min + 1" {
$res = -bnot $minPlus1
$res | Should Be $maxMinus1
$res | Should BeOfType $expectedResultType
}
It "max" {
$res = -bnot $max
$res | Should Be $min
$res | Should BeOfType $expectedResultType
}
It "min" {
$res = -bnot $min
$res | Should Be $max
$res | Should BeOfType $expectedResultType
}
}
}
}