Skip to content

Commit 504d537

Browse files
Moved fsharpqa/Libraries/Core/Unchecked test cases to NUnit (#9576) (#9599)
Co-authored-by: Thorsten Reichert <[email protected]>
1 parent 99d8356 commit 504d537

File tree

5 files changed

+64
-70
lines changed

5 files changed

+64
-70
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 ``DefaultOf Tests`` =
9+
10+
type DUType =
11+
| A
12+
| B of int
13+
| C of DUType * DUType
14+
15+
type RecordType = { A : int; B : string; C : DUType }
16+
17+
type ClassType = string
18+
19+
type InterfaceType =
20+
abstract DoStuff : unit -> unit
21+
22+
type EnumType =
23+
| A = 1
24+
| B = 2
25+
| C = 4
26+
27+
type StructType = struct
28+
val m_ivalue : int
29+
val m_svalue : string
30+
member this.IValue = this.m_ivalue
31+
member this.SValue = this.m_svalue
32+
end
33+
34+
[<Test>]
35+
let `` Unchecked defaultof reference types``() =
36+
Assert.areEqual Unchecked.defaultof<ClassType> null
37+
Assert.areEqual (box Unchecked.defaultof<DUType>) null
38+
Assert.areEqual (box Unchecked.defaultof<RecordType>) null
39+
Assert.areEqual (box Unchecked.defaultof<InterfaceType>) null
40+
41+
[<Test>]
42+
let ``Unchecked defaultof stack types``() =
43+
Assert.areEqual Unchecked.defaultof<int> 0
44+
Assert.areEqual Unchecked.defaultof<float> 0.0
45+
Assert.areEqual Unchecked.defaultof<EnumType> (enum 0)
46+
Assert.areEqual Unchecked.defaultof<StructType>.IValue 0
47+
Assert.areEqual Unchecked.defaultof<StructType>.SValue null
48+
49+
type R = { x : int; y : string }
50+
type U = | A of int | B of string
51+
type S = struct val mutable x : int end
52+
type C() = class end
53+
54+
[<Test>]
55+
let ``Unchecked defaultof and equality``() =
56+
// FSharp1.0:5417 - Unchecked.defaultof<_> on records/unions can cause structural equality check to throw
57+
// Check that Unchecked.defaultof<_> works correctly on various types, mostly structs/unions/records
58+
59+
Assert.areEqual Unchecked.defaultof<R> Unchecked.defaultof<R>
60+
Assert.areEqual Unchecked.defaultof<U> Unchecked.defaultof<U>
61+
Assert.areEqual Unchecked.defaultof<S> Unchecked.defaultof<S>
62+
Assert.areEqual Unchecked.defaultof<C> Unchecked.defaultof<C>
63+
Assert.areEqual Unchecked.defaultof<int> Unchecked.defaultof<int>

tests/fsharp/FSharpSuite.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<Compile Include="Compiler\Libraries\Core\Operators\RoundTests.fs" />
7777
<Compile Include="Compiler\Libraries\Core\Operators\SignTests.fs" />
7878
<Compile Include="Compiler\Libraries\Core\Operators\StringTests.fs" />
79+
<Compile Include="Compiler\Libraries\Core\Unchecked\DefaultOfTests.fs" />
7980
<None Include="app.config" />
8081
<None Include="update.base.line.with.actuals.fsx" />
8182

tests/fsharpqa/Source/Libraries/Core/Unchecked/DefaultOf01.fs

Lines changed: 0 additions & 51 deletions
This file was deleted.

tests/fsharpqa/Source/Libraries/Core/Unchecked/DefaultOf02.fs

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/fsharpqa/Source/Libraries/Core/Unchecked/env.lst

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)