2 // LazyTest.cs - NUnit Test Cases for Lazy
5 // Zoltan Varga (vargaz@gmail.com)
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Reflection;
31 using System.Threading;
32 using NUnit.Framework;
34 #pragma warning disable 219
35 #pragma warning disable 168
37 namespace MonoTests.System
43 [ExpectedException (typeof (ArgumentNullException))]
44 public void Ctor_Null_1 () {
50 [ExpectedException (typeof (ArgumentNullException))]
51 public void Ctor_Null_2 () {
52 new Lazy<int> (null, false);
56 public void IsValueCreated () {
57 var l1 = new Lazy<int> ();
59 Assert.IsFalse (l1.IsValueCreated);
63 Assert.IsTrue (l1.IsValueCreated);
67 public void DefaultCtor () {
68 var l1 = new Lazy<DefaultCtorClass> ();
71 Assert.AreEqual (5, o.Prop);
74 class DefaultCtorClass {
75 public DefaultCtorClass () {
85 public void NoDefaultCtor () {
86 var l1 = new Lazy<NoDefaultCtorClass> ();
91 } catch (MissingMemberException) {
95 class NoDefaultCtorClass {
96 public NoDefaultCtorClass (int i) {
101 public void NotThreadSafe () {
102 var l1 = new Lazy<int> ();
104 Assert.AreEqual (0, l1.Value);
106 var l2 = new Lazy<int> (delegate () { return 42; });
108 Assert.AreEqual (42, l2.Value);
114 public void EnsureSingleThreadSafeExecution ()
117 bool started = false;
119 var l = new Lazy<int> (delegate () { return counter ++; }, true);
121 object monitor = new object ();
122 var threads = new Thread [4];
123 for (int i = 0; i < threads.Length; ++i) {
124 threads [i] = new Thread (delegate () {
127 if (!Monitor.Wait (monitor, 2000))
134 for (int i = 0; i < threads.Length; ++i)
135 threads [i].Start ();
138 Monitor.PulseAll (monitor);
141 for (int i = 0; i < threads.Length; ++i)
144 Assert.IsFalse (failed);
145 Assert.AreEqual (42, l.Value);
149 public void InitRecursion ()
151 Lazy<DefaultCtorClass> c = null;
152 c = new Lazy<DefaultCtorClass> (() => { Console.WriteLine (c.Value); return null; });
157 } catch (InvalidOperationException) {
162 public void ModeNone ()
166 Lazy<int> lz = new Lazy<int> (() => { if (fail) throw new Exception (); else return 99; }, LazyThreadSafetyMode.None);
170 Console.WriteLine (x);
171 } catch (Exception ex) { }
176 } catch (Exception ex) { }
182 } catch (Exception ex) { }
185 lz = new Lazy<int> (() => rec ? lz.Value : 99, LazyThreadSafetyMode.None);
190 } catch (InvalidOperationException ex) { }
196 } catch (InvalidOperationException ex) { }
200 public void ModePublicationOnly () {
203 Lazy<int> lz = new Lazy<int> (() => { ++invoke; if (fail) throw new Exception (); else return 99; }, LazyThreadSafetyMode.PublicationOnly);
208 Console.WriteLine (x);
209 } catch (Exception ex) { }
214 } catch (Exception ex) { }
217 Assert.AreEqual (2, invoke, "#3");
219 Assert.AreEqual (99, lz.Value, "#4");
220 Assert.AreEqual (3, invoke, "#5");
224 lz = new Lazy<int> (() => { ++invoke; bool r = rec; rec = false; return r ? lz.Value : 88; }, LazyThreadSafetyMode.PublicationOnly);
226 Assert.AreEqual (88, lz.Value, "#6");
227 Assert.AreEqual (2, invoke, "#7");
231 public void ModeExecutionAndPublication () {
234 Lazy<int> lz = new Lazy<int> (() => { ++invoke; if (fail) throw new Exception (); else return 99; }, LazyThreadSafetyMode.ExecutionAndPublication);
239 Console.WriteLine (x);
240 } catch (Exception ex) { }
241 Assert.AreEqual (1, invoke, "#2");
246 } catch (Exception ex) { }
247 Assert.AreEqual (1, invoke, "#4");
253 } catch (Exception ex) { }
254 Assert.AreEqual (1, invoke, "#6");
257 lz = new Lazy<int> (() => rec ? lz.Value : 99, LazyThreadSafetyMode.ExecutionAndPublication);
262 } catch (InvalidOperationException ex) { }
268 } catch (InvalidOperationException ex) { }
271 static int Return22 () {
276 public void Trivial_Lazy () {
277 var x = new Lazy<int> (Return22, false);
278 Assert.AreEqual (22, x.Value, "#1");
282 public void ConcurrentInitialization ()
284 var init = new AutoResetEvent (false);
285 var e1_set = new AutoResetEvent (false);
287 var lazy = new Lazy<string> (() => {
290 throw new ApplicationException ();
294 var thread = new Thread (() => {
296 string value = lazy.Value;
297 } catch (Exception ex) {
304 Assert.IsTrue (init.WaitOne (3000), "#1");
308 string value = lazy.Value;
309 } catch (Exception ex) {
315 string value = lazy.Value;
316 } catch (Exception ex) {
320 Assert.IsTrue (e1_set.WaitOne (3000), "#2");
321 Assert.AreSame (e1, e2, "#3");
322 Assert.AreSame (e1, e3, "#4");