Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit d6651ff

Browse files
ThorstenReichertnosami
authored andcommitted
Moved fsharpqa/Libraries/Core/Operators test cases to NUnit (dotnet#9570)
1 parent 7fa075a commit d6651ff

20 files changed

+439
-308
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace FSharp.Compiler.UnitTests
4+
5+
open NUnit.Framework
6+
open FSharp.Compiler.SourceCodeServices
7+
open FSharp.Test.Utilities
8+
9+
[<TestFixture>]
10+
module ``Abs Tests`` =
11+
12+
[<Test>]
13+
let ``Abs of signed integral types``() =
14+
// Regression test for FSHARP1.0:3470 - exception on abs of native integer
15+
16+
Assert.areEqual (abs -1y) 1y // signed byte
17+
Assert.areEqual (abs -1s) 1s // int16
18+
Assert.areEqual (abs -1l) 1l // int32
19+
Assert.areEqual (abs -1n) 1n // nativeint
20+
Assert.areEqual (abs -1L) 1L // int64
21+
Assert.areEqual (abs -1I) 1I // bigint
22+
23+
[<Test>]
24+
let ``Abs of byte``() =
25+
CompilerAssert.TypeCheckSingleError
26+
"""
27+
abs -1uy |> ignore
28+
"""
29+
FSharpErrorSeverity.Error
30+
1
31+
(2, 6, 2, 9)
32+
"The type 'byte' does not support the operator 'Abs'"
33+
34+
[<Test>]
35+
let ``Abs of uint16``() =
36+
CompilerAssert.TypeCheckSingleError
37+
"""
38+
abs -1us |> ignore
39+
"""
40+
FSharpErrorSeverity.Error
41+
1
42+
(2, 6, 2, 9)
43+
"The type 'uint16' does not support the operator 'Abs'"
44+
45+
[<Test>]
46+
let ``Abs of uint32``() =
47+
CompilerAssert.TypeCheckSingleError
48+
"""
49+
abs -1ul |> ignore
50+
"""
51+
FSharpErrorSeverity.Error
52+
1
53+
(2, 6, 2, 9)
54+
"The type 'uint32' does not support the operator 'Abs'"
55+
56+
CompilerAssert.TypeCheckSingleError
57+
"""
58+
abs -1u |> ignore
59+
"""
60+
FSharpErrorSeverity.Error
61+
1
62+
(2, 6, 2, 8)
63+
"The type 'uint32' does not support the operator 'Abs'"
64+
65+
[<Test>]
66+
let ``Abs of unativeint``() =
67+
CompilerAssert.TypeCheckSingleError
68+
"""
69+
abs -1un |> ignore
70+
"""
71+
FSharpErrorSeverity.Error
72+
1
73+
(2, 6, 2, 9)
74+
"The type 'unativeint' does not support the operator 'Abs'"
75+
76+
[<Test>]
77+
let ``Abs of uint64``() =
78+
CompilerAssert.TypeCheckSingleError
79+
"""
80+
abs -1uL |> ignore
81+
"""
82+
FSharpErrorSeverity.Error
83+
1
84+
(2, 6, 2, 9)
85+
"The type 'uint64' does not support the operator 'Abs'"
86+
87+
CompilerAssert.TypeCheckSingleError
88+
"""
89+
abs -1UL |> ignore
90+
"""
91+
FSharpErrorSeverity.Error
92+
1
93+
(2, 6, 2, 9)
94+
"The type 'uint64' does not support the operator 'Abs'"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace FSharp.Compiler.UnitTests
4+
5+
open NUnit.Framework
6+
open FSharp.Test.Utilities
7+
open System
8+
9+
[<TestFixture>]
10+
module ``Cast Tests`` =
11+
12+
[<Test>]
13+
let ``Cast precedence over expression forms``() =
14+
// Regression test for FSHARP1.0:1247
15+
// Precedence of type annotations :> and :?> over preceeding expression forms, e.g. if-then-else etc.
16+
17+
Assert.IsInstanceOf<Object> (2 :> Object)
18+
Assert.IsInstanceOf<Object list> [(2 :> Object)]
19+
Assert.IsInstanceOf<Object list> [2 :> Object]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace FSharp.Compiler.UnitTests
4+
5+
open NUnit.Framework
6+
open FSharp.Compiler.SourceCodeServices
7+
open FSharp.Test.Utilities
8+
9+
[<TestFixture>]
10+
module ``Hash Tests`` =
11+
12+
[<Test>]
13+
let ``Hash of function values``() =
14+
// Regression test for FSHARP1.0:5436
15+
// You should not be able to hash F# function values
16+
// Note: most positive cases already covered under fsharp\typecheck\sigs
17+
// I'm adding this simple one since I did not see it there.
18+
19+
CompilerAssert.TypeCheckSingleError
20+
"""
21+
hash id |> ignore
22+
"""
23+
FSharpErrorSeverity.Error
24+
1
25+
(2, 6, 2, 8)
26+
"The type '('a -> 'a)' does not support the 'equality' constraint because it is a function type"
27+
28+
[<Test>]
29+
let ``Unchecked hash of function values``() =
30+
// Regression test for FSHARP1.0:5436
31+
// You should not be able to hash F# function values
32+
// Note: most positive cases already covered under fsharp\typecheck\sigs
33+
// I'm adding this simple one since I did not see it there.
34+
35+
// This is ok (unchecked)
36+
CompilerAssert.TypeCheckWithErrors
37+
"""
38+
Unchecked.hash id |> ignore
39+
"""
40+
[||]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace FSharp.Compiler.UnitTests
4+
5+
open NUnit.Framework
6+
7+
[<TestFixture>]
8+
module ``Pow Tests`` =
9+
10+
type T() =
11+
static let mutable m = false
12+
static member Pow (g: T, _: float) =
13+
m <- true
14+
g
15+
static member Check() = m
16+
17+
[<Test>]
18+
let ``Pow of custom type``() =
19+
// Regression test for FSHARP1.0:4487
20+
// Feature request: loosen Pow operator constraints
21+
22+
let t = T()
23+
let _ = t ** 3.
24+
25+
Assert.IsTrue (T.Check())
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace FSharp.Compiler.UnitTests
4+
5+
open NUnit.Framework
6+
7+
[<TestFixture>]
8+
module ``Round Tests`` =
9+
10+
[<Test>]
11+
let ``Round of integers``() =
12+
for i in [1 .. 10000] do
13+
Assert.areEqual (i |> float |> round) (float i)
14+
Assert.areEqual (i |> float32 |> round) (float32 i)
15+
Assert.areEqual (i |> decimal |> round) (decimal i)
16+
17+
[<Test>]
18+
let ``Round of floats``() =
19+
// Round down
20+
Assert.areEqual (round 1.1) 1.0
21+
Assert.areEqual (round 1.2) 1.0
22+
Assert.areEqual (round 1.3) 1.0
23+
Assert.areEqual (round 1.4) 1.0
24+
Assert.areEqual (round 1.1f) 1.0f
25+
Assert.areEqual (round 1.2f) 1.0f
26+
Assert.areEqual (round 1.3f) 1.0f
27+
Assert.areEqual (round 1.4f) 1.0f
28+
Assert.areEqual (round 1.1m) 1.0m
29+
Assert.areEqual (round 1.2m) 1.0m
30+
Assert.areEqual (round 1.3m) 1.0m
31+
Assert.areEqual (round 1.4m) 1.0m
32+
33+
// Round down
34+
Assert.areEqual (round 1.6) 2.0
35+
Assert.areEqual (round 1.7) 2.0
36+
Assert.areEqual (round 1.8) 2.0
37+
Assert.areEqual (round 1.9) 2.0
38+
Assert.areEqual (round 1.6f) 2.0f
39+
Assert.areEqual (round 1.7f) 2.0f
40+
Assert.areEqual (round 1.8f) 2.0f
41+
Assert.areEqual (round 1.9f) 2.0f
42+
Assert.areEqual (round 1.6m) 2.0m
43+
Assert.areEqual (round 1.7m) 2.0m
44+
Assert.areEqual (round 1.8m) 2.0m
45+
Assert.areEqual (round 1.9m) 2.0m
46+
47+
// Midpoint rounding. If between two numbers, round to the 'even' one.
48+
Assert.areEqual (round 1.5 ) 2.0
49+
Assert.areEqual (round 1.5f) 2.0f
50+
Assert.areEqual (round 1.5m) 2.0m
51+
Assert.areEqual (round 2.5 ) 2.0
52+
Assert.areEqual (round 2.5f) 2.0f
53+
Assert.areEqual (round 2.5m) 2.0m
54+
55+
// If not midpoint, round to nearest as usual
56+
Assert.areEqual (round 2.500001 ) 3.0
57+
Assert.areEqual (round 2.500001f) 3.0f
58+
Assert.areEqual (round 2.500001m) 3.0m
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace FSharp.Compiler.UnitTests
4+
5+
open NUnit.Framework
6+
open FSharp.Compiler.SourceCodeServices
7+
open FSharp.Test.Utilities
8+
9+
[<TestFixture>]
10+
module ``Sign Tests`` =
11+
12+
[<Test>]
13+
let ``Sign of signed types``() =
14+
Assert.areEqual (sign 1y) 1 // byte
15+
Assert.areEqual (sign 1s) 1 // int16
16+
Assert.areEqual (sign 1) 1 // int32
17+
Assert.areEqual (sign 1L) 1 // int64
18+
Assert.areEqual (sign 1.0f) 1 // float
19+
Assert.areEqual (sign 1.0) 1 // double
20+
Assert.areEqual (sign 1.0m) 1 // decimal
21+
Assert.areEqual (sign 0y) 0 // byte
22+
Assert.areEqual (sign 0s) 0 // int16
23+
Assert.areEqual (sign 0) 0 // int32
24+
Assert.areEqual (sign 0L) 0 // int64
25+
Assert.areEqual (sign 0.0f) 0 // float
26+
Assert.areEqual (sign 0.0) 0 // double
27+
Assert.areEqual (sign 0.0m) 0 // decimal
28+
Assert.areEqual (sign -1y) -1 // byte
29+
Assert.areEqual (sign -1s) -1 // int16
30+
Assert.areEqual (sign -1) -1 // int32
31+
Assert.areEqual (sign -1L) -1 // int64
32+
Assert.areEqual (sign -1.0f) -1 // float
33+
Assert.areEqual (sign -1.0) -1 // double
34+
Assert.areEqual (sign -1.0m) -1 // decimal
35+
36+
// #Regression #Libraries #Operators
37+
// Test sign function on unsigned primitives, should get error.
38+
39+
[<Test>]
40+
let ``Sign of byte``() =
41+
CompilerAssert.TypeCheckSingleError
42+
"""
43+
sign 0uy |> ignore
44+
"""
45+
FSharpErrorSeverity.Error
46+
1
47+
(2, 6, 2, 9)
48+
"The type 'byte' does not support the operator 'get_Sign'"
49+
50+
[<Test>]
51+
let ``Sign of uint16``() =
52+
CompilerAssert.TypeCheckSingleError
53+
"""
54+
sign 0us |> ignore
55+
"""
56+
FSharpErrorSeverity.Error
57+
1
58+
(2, 6, 2, 9)
59+
"The type 'uint16' does not support the operator 'get_Sign'"
60+
61+
[<Test>]
62+
let ``Sign of uint32``() =
63+
CompilerAssert.TypeCheckSingleError
64+
"""
65+
sign 0u |> ignore
66+
"""
67+
FSharpErrorSeverity.Error
68+
1
69+
(2, 6, 2, 8)
70+
"The type 'uint32' does not support the operator 'get_Sign'"
71+
72+
[<Test>]
73+
let ``Sign of uint64``() =
74+
CompilerAssert.TypeCheckSingleError
75+
"""
76+
sign 0uL |> ignore
77+
"""
78+
FSharpErrorSeverity.Error
79+
1
80+
(2, 6, 2, 9)
81+
"The type 'uint64' does not support the operator 'get_Sign'"

0 commit comments

Comments
 (0)