This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 322
/
LazyType.fs
executable file
·131 lines (97 loc) · 4.02 KB
/
LazyType.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace FSharp.Core.Unittests.FSharp_Core.Microsoft_FSharp_Control
open System
open NUnit.Framework
open Microsoft.FSharp.Collections
open FSharp.Core.Unittests.LibraryTestFx
[<TestFixture>]
type LazyType() =
[<Test>]
member this.Create() =
// int
let intLazy = Lazy<_>.Create(fun () -> 2)
Assert.AreEqual(intLazy.Value, 2)
// string
let stringLazy = Lazy<_>.Create(fun () -> "string")
Assert.AreEqual(stringLazy.Value, "string")
// null
let nullLazy = Lazy<_>.Create(fun () -> ())
Assert.AreEqual(nullLazy.Value, null)
[<Test>]
member this.CreateFromValue() =
// int
let intLazy = Lazy<_>.CreateFromValue( 2)
Assert.AreEqual(intLazy.Value,2)
// string
let stringLazy = Lazy<_>.CreateFromValue( "string")
Assert.AreEqual(stringLazy.Value,"string")
//null
let nullLazy = Lazy<_>.CreateFromValue(null)
Assert.AreEqual(nullLazy.Value,null)
[<Test>]
member this.Force() =
// int
let intLazy = Lazy<_>.CreateFromValue( 2)
let intForce = intLazy.Force()
Assert.AreEqual(intForce,2)
// string
let stringLazy = Lazy<_>.CreateFromValue( "string")
let stringForce = stringLazy.Force()
Assert.AreEqual(stringForce,"string")
//null
let nullLazy = Lazy<_>.CreateFromValue(null)
let nullForce = nullLazy.Force()
Assert.AreEqual(nullForce,null)
[<Test>]
member this.Value() =
// int
let intLazy = Lazy<_>.CreateFromValue( 2)
Assert.AreEqual(intLazy.Value,2)
// string
let stringLazy = Lazy<_>.CreateFromValue( "string")
Assert.AreEqual(stringLazy.Value,"string")
//null
let nullLazy = Lazy<_>.CreateFromValue(null)
Assert.AreEqual(nullLazy.Value,null)
[<Test>]
member this.IsDelayed() =
// int
let intLazy = Lazy<_>.Create( fun () -> 1)
Assert.AreEqual(not intLazy.IsValueCreated,true)
let resultIsDelayed = intLazy.Force()
Assert.AreEqual(not intLazy.IsValueCreated,false)
// string
let stringLazy = Lazy<_>.Create( fun () -> "string")
Assert.AreEqual(not stringLazy.IsValueCreated,true)
let resultIsDelayed = stringLazy.Force()
Assert.AreEqual(not stringLazy.IsValueCreated,false)
//null
let nullLazy = Lazy<_>.Create(fun () -> null)
Assert.AreEqual(not nullLazy.IsValueCreated,true)
let resultIsDelayed = nullLazy.Force()
Assert.AreEqual(not nullLazy.IsValueCreated,false)
[<Test>]
member this.IsForced() =
// int
let intLazy = Lazy<_>.Create( fun () -> 1)
Assert.AreEqual( intLazy.IsValueCreated,false)
let resultIsForced = intLazy.Force()
Assert.AreEqual( intLazy.IsValueCreated,true)
// string
let stringLazy = Lazy<_>.Create( fun () -> "string")
Assert.AreEqual( stringLazy.IsValueCreated,false)
let resultIsForced = stringLazy.Force()
Assert.AreEqual( stringLazy.IsValueCreated,true)
//null
let nullLazy = Lazy<_>.Create(fun () -> null)
Assert.AreEqual( nullLazy.IsValueCreated,false)
let resultIsForced = nullLazy.Force()
Assert.AreEqual( nullLazy.IsValueCreated,true)
[<Test>]
member this.Printing() =
let n = lazy 12
Assert.AreEqual( n.IsValueCreated, false )
// printfn "%A" n
Assert.AreEqual( n.IsValueCreated, false )
// printfn "%s" (n.ToString())
Assert.AreEqual( n.IsValueCreated, false )