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>
0 commit comments