Skip to content

Commit

Permalink
Moved fsharpqa/Libraries/Core/NativeInterop/stackalloc test cases to …
Browse files Browse the repository at this point in the history
…NUnit (dotnet#9521)

* Migrated zerosize01.fs test case

* Migrated ofint6401.fs test case

* Migrated ofint01.fs test case

* Cleanup asserts

* Migrated ofDateTime01.fs test case

* Migrated constraint01.fs test case

* Migrated E_constraint02.fs test case

* Migrated E_constraint03.fs test case

* Migrated ofenum01.fs test case

* Reordered tests

* Removed obsolete directory
  • Loading branch information
ThorstenReichert authored and nosami committed Feb 22, 2021
1 parent b61db99 commit f6a63e7
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 188 deletions.
187 changes: 187 additions & 0 deletions tests/fsharp/Compiler/Libraries/Core/NativeInterop/StackallocTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// 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 FSharp.Compiler.SourceCodeServices

#nowarn "9"

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

type E = | A = 1
| B = 2

[<Test>]
let ``Stackalloc of DateTime``() =
let data = NativeInterop.NativePtr.stackalloc<System.DateTime> 100
let now = System.DateTime.Now
for i = 0 to 99 do
NativeInterop.NativePtr.set data i now
for i = 0 to 99 do
Assert.areEqual (NativeInterop.NativePtr.get data i) now

let later = now.AddDays 1.
for i = 0 to 99 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
datai <- later
for i = 0 to 99 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
Assert.areEqual datai later

[<Test>]
let ``Stackalloc of enum``() =
let data = NativeInterop.NativePtr.stackalloc<E> 10

for i = 0 to 9 do
NativeInterop.NativePtr.set data i (if (i % 2)=0 then E.A else E.B)

for i = 0 to 9 do
let expected = if (i % 2) = 0 then E.A else E.B
Assert.areEqual (NativeInterop.NativePtr.get data i) expected

for i = 0 to 9 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
datai <- (if (i % 2)=1 then E.A else E.B)

for i = 0 to 9 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
let expected = if (i % 2)=1 then E.A else E.B
Assert.areEqual datai expected

[<Test>]
let ``Stackalloc of imported enum``() =
Assert.DoesNotThrow (TestDelegate (fun () ->
NativeInterop.NativePtr.stackalloc<System.DayOfWeek> 1 |> ignore))

[<Test>]
let ``Stackalloc of imported struct``() =
Assert.DoesNotThrow (TestDelegate (fun () ->
NativeInterop.NativePtr.stackalloc<System.TimeSpan> 1 |> ignore))

[<Test>]
let ``Stackalloc of imported class``() =
CompilerAssert.TypeCheckSingleError
"""
#nowarn "9"
let _ = NativeInterop.NativePtr.stackalloc<System.Object> 1
"""
FSharpErrorSeverity.Error
1
(4, 9, 4, 43)
"A generic construct requires that the type 'System.Object' is an unmanaged type"

[<Test>]
let ``Stackalloc of imported interface``() =
CompilerAssert.TypeCheckSingleError
"""
#nowarn "9"
let _ = NativeInterop.NativePtr.stackalloc<System.Collections.IEnumerable> 1
"""
FSharpErrorSeverity.Error
1
(4, 9, 4, 43)
"A generic construct requires that the type 'System.Collections.IEnumerable' is an unmanaged type"

[<Test>]
let ``Stackalloc of imported delegate``() =
CompilerAssert.TypeCheckSingleError
"""
#nowarn "9"
let _ = NativeInterop.NativePtr.stackalloc<System.EventHandler> 1
"""
FSharpErrorSeverity.Error
1
(4, 9, 4, 43)
"A generic construct requires that the type 'System.EventHandler' is an unmanaged type"

[<Test>]
let ``Stackalloc of int``() =
let data = NativeInterop.NativePtr.stackalloc<int> 100

for i = 0 to 99 do
NativeInterop.NativePtr.set data i (i*i)

for i = 0 to 99 do
Assert.areEqual (NativeInterop.NativePtr.get data i) (i*i)

for i = 0 to 99 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
datai <- 1-i

for i = 0 to 99 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
Assert.areEqual datai (1-i)

[<Test>]
let ``Stackalloc of int64``() =
let data = NativeInterop.NativePtr.stackalloc<int64> 100

for i = 0 to 99 do
NativeInterop.NativePtr.set data i (int64 (i*i))
for i = 0 to 99 do
Assert.areEqual (NativeInterop.NativePtr.get data i) (int64 (i*i))

for i = 0 to 99 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
datai <- int64 (1-i)

for i = 0 to 99 do
let datai = NativeInterop.NativePtr.toByRef (NativeInterop.NativePtr.add data i)
Assert.areEqual datai (int64 (1-i))

[<Test>]
let ``Stackalloc of managed class``() =
CompilerAssert.TypeCheckSingleError
"""
#nowarn "9"
type C() =
class
member __.M = 10
member __.N(x) = x + 1
end
let _ = NativeInterop.NativePtr.stackalloc<C> 1
"""
FSharpErrorSeverity.Error
1
(10, 9, 10, 43)
"A generic construct requires that the type 'C' is an unmanaged type"

[<Test>]
let ``Stackalloc of managed record``() =
CompilerAssert.TypeCheckSingleError
"""
#nowarn "9"
type R = { A : int }
let _ = NativeInterop.NativePtr.stackalloc<R> 1
"""
FSharpErrorSeverity.Error
1
(6, 9, 6, 43)
"A generic construct requires that the type 'R' is an unmanaged type"

[<Test>]
let ``Stackalloc zero-size``() =
// Regression test for FSHARP1.0:
// stackalloc<System.DateTime> 0

let testDelegate = TestDelegate (fun () ->
// check stackalloc 0 -- ok
let data = NativeInterop.NativePtr.stackalloc<System.DateTime> 0

// The returned pointer is undefined
// No allocation should happen
let _ = NativeInterop.NativePtr.toNativeInt data

())

Assert.DoesNotThrow testDelegate
1 change: 1 addition & 0 deletions tests/fsharp/FSharpSuite.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<Compile Include="Compiler\Libraries\Core\Collections\CollectionTests.fs" />
<Compile Include="Compiler\Libraries\Core\Collections\ListTests.fs" />
<Compile Include="Compiler\Libraries\Core\ExtraTopLevelOperators\DictionaryTests.fs" />
<Compile Include="Compiler\Libraries\Core\NativeInterop\StackallocTests.fs" />
<None Include="app.config" />
<None Include="update.base.line.with.actuals.fsx" />

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
SOURCE=constraint01.fs PRECMD="\$CSC_PIPE /target:library publictypes.cs" PEVER=/MD COMPILE_ONLY=1 SCFLAGS="-r:publictypes.dll" # constraint01.fs
SOURCE=E_constraint02.fs PRECMD="\$CSC_PIPE /target:library publictypes.cs" PEVER=/MD COMPILE_ONLY=1 SCFLAGS="-r:publictypes.dll --test:ErrorRanges" # E_constraint02.fs
SOURCE=E_constraint03.fs PEVER=/MD COMPILE_ONLY=1 SCFLAGS="--test:ErrorRanges" # E_constraint03.fs

SOURCE=negativesize01.fs PEVER=/MD COMPILE_ONLY=1 # negativesize01.fs
SOURCE=ofDateTime01.fs PEVER=/MD # ofDateTime01.fs
SOURCE=ofenum01.fs PEVER=/MD # ofenum01.fs
SOURCE=ofint01.fs PEVER=/MD # ofint01.fs
SOURCE=ofint6401.fs PEVER=/MD # ofint6401.fs
SOURCE=zerosize01.fs PEVER=/MD # zerosize01.fs
SOURCE=negativesize01.fs PEVER=/MD COMPILE_ONLY=1 # negativesize01.fs

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit f6a63e7

Please sign in to comment.