Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions tests/fsharp/Compiler/Libraries/Core/Operators/AbsTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace FSharp.Compiler.UnitTests

open NUnit.Framework
open FSharp.Compiler.SourceCodeServices
open FSharp.Test.Utilities

[<TestFixture>]
module ``Abs Tests`` =

[<Test>]
let ``Abs of signed integral types``() =
// Regression test for FSHARP1.0:3470 - exception on abs of native integer

Assert.areEqual (abs -1y) 1y // signed byte
Assert.areEqual (abs -1s) 1s // int16
Assert.areEqual (abs -1l) 1l // int32
Assert.areEqual (abs -1n) 1n // nativeint
Assert.areEqual (abs -1L) 1L // int64
Assert.areEqual (abs -1I) 1I // bigint

[<Test>]
let ``Abs of byte``() =
CompilerAssert.TypeCheckSingleError
"""
abs -1uy |> ignore
"""
FSharpErrorSeverity.Error
1
(2, 6, 2, 9)
"The type 'byte' does not support the operator 'Abs'"

[<Test>]
let ``Abs of uint16``() =
CompilerAssert.TypeCheckSingleError
"""
abs -1us |> ignore
"""
FSharpErrorSeverity.Error
1
(2, 6, 2, 9)
"The type 'uint16' does not support the operator 'Abs'"

[<Test>]
let ``Abs of uint32``() =
CompilerAssert.TypeCheckSingleError
"""
abs -1ul |> ignore
"""
FSharpErrorSeverity.Error
1
(2, 6, 2, 9)
"The type 'uint32' does not support the operator 'Abs'"

CompilerAssert.TypeCheckSingleError
"""
abs -1u |> ignore
"""
FSharpErrorSeverity.Error
1
(2, 6, 2, 8)
"The type 'uint32' does not support the operator 'Abs'"

[<Test>]
let ``Abs of unativeint``() =
CompilerAssert.TypeCheckSingleError
"""
abs -1un |> ignore
"""
FSharpErrorSeverity.Error
1
(2, 6, 2, 9)
"The type 'unativeint' does not support the operator 'Abs'"

[<Test>]
let ``Abs of uint64``() =
CompilerAssert.TypeCheckSingleError
"""
abs -1uL |> ignore
"""
FSharpErrorSeverity.Error
1
(2, 6, 2, 9)
"The type 'uint64' does not support the operator 'Abs'"

CompilerAssert.TypeCheckSingleError
"""
abs -1UL |> ignore
"""
FSharpErrorSeverity.Error
1
(2, 6, 2, 9)
"The type 'uint64' does not support the operator 'Abs'"
19 changes: 19 additions & 0 deletions tests/fsharp/Compiler/Libraries/Core/Operators/CastTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace FSharp.Compiler.UnitTests

open NUnit.Framework
open FSharp.Test.Utilities
open System

[<TestFixture>]
module ``Cast Tests`` =

[<Test>]
let ``Cast precedence over expression forms``() =
// Regression test for FSHARP1.0:1247
// Precedence of type annotations :> and :?> over preceeding expression forms, e.g. if-then-else etc.

Assert.IsInstanceOf<Object> (2 :> Object)
Assert.IsInstanceOf<Object list> [(2 :> Object)]
Assert.IsInstanceOf<Object list> [2 :> Object]
40 changes: 40 additions & 0 deletions tests/fsharp/Compiler/Libraries/Core/Operators/HashTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace FSharp.Compiler.UnitTests

open NUnit.Framework
open FSharp.Compiler.SourceCodeServices
open FSharp.Test.Utilities

[<TestFixture>]
module ``Hash Tests`` =

[<Test>]
let ``Hash of function values``() =
// Regression test for FSHARP1.0:5436
// You should not be able to hash F# function values
// Note: most positive cases already covered under fsharp\typecheck\sigs
// I'm adding this simple one since I did not see it there.

CompilerAssert.TypeCheckSingleError
"""
hash id |> ignore
"""
FSharpErrorSeverity.Error
1
(2, 6, 2, 8)
"The type '('a -> 'a)' does not support the 'equality' constraint because it is a function type"

[<Test>]
let ``Unchecked hash of function values``() =
// Regression test for FSHARP1.0:5436
// You should not be able to hash F# function values
// Note: most positive cases already covered under fsharp\typecheck\sigs
// I'm adding this simple one since I did not see it there.

// This is ok (unchecked)
CompilerAssert.TypeCheckWithErrors
"""
Unchecked.hash id |> ignore
"""
[||]
25 changes: 25 additions & 0 deletions tests/fsharp/Compiler/Libraries/Core/Operators/PowTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace FSharp.Compiler.UnitTests

open NUnit.Framework

[<TestFixture>]
module ``Pow Tests`` =

type T() =
static let mutable m = false
static member Pow (g: T, _: float) =
m <- true
g
static member Check() = m

[<Test>]
let ``Pow of custom type``() =
// Regression test for FSHARP1.0:4487
// Feature request: loosen Pow operator constraints

let t = T()
let _ = t ** 3.

Assert.IsTrue (T.Check())
58 changes: 58 additions & 0 deletions tests/fsharp/Compiler/Libraries/Core/Operators/RoundTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace FSharp.Compiler.UnitTests

open NUnit.Framework

[<TestFixture>]
module ``Round Tests`` =

[<Test>]
let ``Round of integers``() =
for i in [1 .. 10000] do
Assert.areEqual (i |> float |> round) (float i)
Assert.areEqual (i |> float32 |> round) (float32 i)
Assert.areEqual (i |> decimal |> round) (decimal i)

[<Test>]
let ``Round of floats``() =
// Round down
Assert.areEqual (round 1.1) 1.0
Assert.areEqual (round 1.2) 1.0
Assert.areEqual (round 1.3) 1.0
Assert.areEqual (round 1.4) 1.0
Assert.areEqual (round 1.1f) 1.0f
Assert.areEqual (round 1.2f) 1.0f
Assert.areEqual (round 1.3f) 1.0f
Assert.areEqual (round 1.4f) 1.0f
Assert.areEqual (round 1.1m) 1.0m
Assert.areEqual (round 1.2m) 1.0m
Assert.areEqual (round 1.3m) 1.0m
Assert.areEqual (round 1.4m) 1.0m

// Round down
Assert.areEqual (round 1.6) 2.0
Assert.areEqual (round 1.7) 2.0
Assert.areEqual (round 1.8) 2.0
Assert.areEqual (round 1.9) 2.0
Assert.areEqual (round 1.6f) 2.0f
Assert.areEqual (round 1.7f) 2.0f
Assert.areEqual (round 1.8f) 2.0f
Assert.areEqual (round 1.9f) 2.0f
Assert.areEqual (round 1.6m) 2.0m
Assert.areEqual (round 1.7m) 2.0m
Assert.areEqual (round 1.8m) 2.0m
Assert.areEqual (round 1.9m) 2.0m

// Midpoint rounding. If between two numbers, round to the 'even' one.
Assert.areEqual (round 1.5 ) 2.0
Assert.areEqual (round 1.5f) 2.0f
Assert.areEqual (round 1.5m) 2.0m
Assert.areEqual (round 2.5 ) 2.0
Assert.areEqual (round 2.5f) 2.0f
Assert.areEqual (round 2.5m) 2.0m

// If not midpoint, round to nearest as usual
Assert.areEqual (round 2.500001 ) 3.0
Assert.areEqual (round 2.500001f) 3.0f
Assert.areEqual (round 2.500001m) 3.0m
81 changes: 81 additions & 0 deletions tests/fsharp/Compiler/Libraries/Core/Operators/SignTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace FSharp.Compiler.UnitTests

open NUnit.Framework
open FSharp.Compiler.SourceCodeServices
open FSharp.Test.Utilities

[<TestFixture>]
module ``Sign Tests`` =

[<Test>]
let ``Sign of signed types``() =
Assert.areEqual (sign 1y) 1 // byte
Assert.areEqual (sign 1s) 1 // int16
Assert.areEqual (sign 1) 1 // int32
Assert.areEqual (sign 1L) 1 // int64
Assert.areEqual (sign 1.0f) 1 // float
Assert.areEqual (sign 1.0) 1 // double
Assert.areEqual (sign 1.0m) 1 // decimal
Assert.areEqual (sign 0y) 0 // byte
Assert.areEqual (sign 0s) 0 // int16
Assert.areEqual (sign 0) 0 // int32
Assert.areEqual (sign 0L) 0 // int64
Assert.areEqual (sign 0.0f) 0 // float
Assert.areEqual (sign 0.0) 0 // double
Assert.areEqual (sign 0.0m) 0 // decimal
Assert.areEqual (sign -1y) -1 // byte
Assert.areEqual (sign -1s) -1 // int16
Assert.areEqual (sign -1) -1 // int32
Assert.areEqual (sign -1L) -1 // int64
Assert.areEqual (sign -1.0f) -1 // float
Assert.areEqual (sign -1.0) -1 // double
Assert.areEqual (sign -1.0m) -1 // decimal

// #Regression #Libraries #Operators
// Test sign function on unsigned primitives, should get error.

[<Test>]
let ``Sign of byte``() =
CompilerAssert.TypeCheckSingleError
"""
sign 0uy |> ignore
"""
FSharpErrorSeverity.Error
1
(2, 6, 2, 9)
"The type 'byte' does not support the operator 'get_Sign'"

[<Test>]
let ``Sign of uint16``() =
CompilerAssert.TypeCheckSingleError
"""
sign 0us |> ignore
"""
FSharpErrorSeverity.Error
1
(2, 6, 2, 9)
"The type 'uint16' does not support the operator 'get_Sign'"

[<Test>]
let ``Sign of uint32``() =
CompilerAssert.TypeCheckSingleError
"""
sign 0u |> ignore
"""
FSharpErrorSeverity.Error
1
(2, 6, 2, 8)
"The type 'uint32' does not support the operator 'get_Sign'"

[<Test>]
let ``Sign of uint64``() =
CompilerAssert.TypeCheckSingleError
"""
sign 0uL |> ignore
"""
FSharpErrorSeverity.Error
1
(2, 6, 2, 9)
"The type 'uint64' does not support the operator 'get_Sign'"
Loading