Fixes 4.0 build
[mono.git] / mcs / class / corlib / Test / System / LazyTest.cs
1 //
2 // LazyTest.cs - NUnit Test Cases for Lazy
3 //
4 // Author:
5 //      Zoltan Varga (vargaz@gmail.com)
6 //
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_4_0
30
31 using System;
32 using System.Reflection;
33 using System.Reflection.Emit;
34 using System.Threading;
35 using NUnit.Framework;
36
37 #pragma warning disable 219
38 #pragma warning disable 168
39
40 namespace MonoTests.System
41 {
42         [TestFixture]
43         public class LazyTest
44         {
45                 [Test]
46                 [ExpectedException (typeof (ArgumentNullException))]
47                 public void Ctor_Null_1 () {
48                         new Lazy<int> (null);
49                 }
50
51                 [Test]
52                 [ExpectedException (typeof (ArgumentNullException))]
53                 public void Ctor_Null_2 () {
54                         new Lazy<int> (null, false);
55                 }
56
57                 [Test]
58                 public void IsValueCreated () {
59                         var l1 = new Lazy<int> ();
60
61                         Assert.IsFalse (l1.IsValueCreated);
62
63                         int i = l1.Value;
64
65                         Assert.IsTrue (l1.IsValueCreated);
66                 }
67
68                 [Test]
69                 public void DefaultCtor () {
70                         var l1 = new Lazy<DefaultCtorClass> ();
71                         
72                         var o = l1.Value;
73                         Assert.AreEqual (5, o.Prop);
74                 }
75
76                 class DefaultCtorClass {
77                         public DefaultCtorClass () {
78                                 Prop = 5;
79                         }
80
81                         public int Prop {
82                                 get; set;
83                         }
84                 }
85
86                 [Test]
87                 public void NoDefaultCtor () {
88                         var l1 = new Lazy<NoDefaultCtorClass> ();
89                         
90                         try {
91                                 var o = l1.Value;
92                                 Assert.Fail ();
93                         } catch (MissingMemberException) {
94                         }
95                 }
96
97                 class NoDefaultCtorClass {
98                         public NoDefaultCtorClass (int i) {
99                         }
100                 }
101
102                 [Test]
103                 public void NotThreadSafe () {
104                         var l1 = new Lazy<int> ();
105
106                         Assert.AreEqual (0, l1.Value);
107
108                         var l2 = new Lazy<int> (delegate () { return 42; });
109
110                         Assert.AreEqual (42, l2.Value);
111                 }
112
113                 static int counter;
114
115                 [Test]
116                 public void EnsureSingleThreadSafeExecution () {
117                         counter = 42;
118
119                         //var l = new Lazy<int> (delegate () { return counter ++; }, LazyExecutionMode.NotThreadSafe);
120                         var l = new Lazy<int> (delegate () { return counter ++; }, true);
121
122                         object monitor = new object ();
123                         var threads = new Thread [10];
124                         for (int i = 0; i < 10; ++i) {
125                                 threads [i] = new Thread (delegate () {
126                                                 lock (monitor) {
127                                                         Monitor.Wait (monitor);
128                                                 }
129                                                 int val = l.Value;
130                                         });
131                         }
132                         for (int i = 0; i < 10; ++i)
133                                 threads [i].Start ();
134                         lock (monitor)
135                                 Monitor.PulseAll (monitor);
136                         
137                         Assert.AreEqual (42, l.Value);
138                 }
139                 
140                 [Test]
141                 public void InitRecursion ()
142                 {
143                         Lazy<DefaultCtorClass> c = null;
144                         c = new Lazy<DefaultCtorClass> (() => { Console.WriteLine (c.Value); return null; });
145                         
146                         try {
147                                 var r = c.Value;
148                                 Assert.Fail ();
149                         } catch (InvalidOperationException) {
150                         }
151                 }
152         }
153 }
154
155 #endif