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.
32 using System.Reflection;
33 using System.Threading;
34 using NUnit.Framework;
36 #pragma warning disable 219
37 #pragma warning disable 168
39 namespace MonoTests.System
45 [ExpectedException (typeof (ArgumentNullException))]
46 public void Ctor_Null_1 () {
52 [ExpectedException (typeof (ArgumentNullException))]
53 public void Ctor_Null_2 () {
54 new Lazy<int> (null, false);
58 public void IsValueCreated () {
59 var l1 = new Lazy<int> ();
61 Assert.IsFalse (l1.IsValueCreated);
65 Assert.IsTrue (l1.IsValueCreated);
69 public void DefaultCtor () {
70 var l1 = new Lazy<DefaultCtorClass> ();
73 Assert.AreEqual (5, o.Prop);
76 class DefaultCtorClass {
77 public DefaultCtorClass () {
87 public void NoDefaultCtor () {
88 var l1 = new Lazy<NoDefaultCtorClass> ();
93 } catch (MissingMemberException) {
97 class NoDefaultCtorClass {
98 public NoDefaultCtorClass (int i) {
103 public void NotThreadSafe () {
104 var l1 = new Lazy<int> ();
106 Assert.AreEqual (0, l1.Value);
108 var l2 = new Lazy<int> (delegate () { return 42; });
110 Assert.AreEqual (42, l2.Value);
116 public void EnsureSingleThreadSafeExecution () {
119 var l = new Lazy<int> (delegate () { return counter ++; }, true);
121 object monitor = new object ();
122 var threads = new Thread [10];
123 for (int i = 0; i < 10; ++i) {
124 threads [i] = new Thread (delegate () {
126 Monitor.Wait (monitor);
131 for (int i = 0; i < 10; ++i)
132 threads [i].Start ();
134 Monitor.PulseAll (monitor);
136 Assert.AreEqual (42, l.Value);
140 public void InitRecursion ()
142 Lazy<DefaultCtorClass> c = null;
143 c = new Lazy<DefaultCtorClass> (() => { Console.WriteLine (c.Value); return null; });
148 } catch (InvalidOperationException) {
153 public void ModeNone ()
157 Lazy<int> lz = new Lazy<int> (() => { if (fail) throw new Exception (); else return 99; }, LazyThreadSafetyMode.None);
161 Console.WriteLine (x);
162 } catch (Exception ex) { }
167 } catch (Exception ex) { }
173 } catch (Exception ex) { }
176 lz = new Lazy<int> (() => rec ? lz.Value : 99, LazyThreadSafetyMode.None);
181 } catch (InvalidOperationException ex) { }
187 } catch (InvalidOperationException ex) { }
191 public void ModePublicationOnly () {
194 Lazy<int> lz = new Lazy<int> (() => { ++invoke; if (fail) throw new Exception (); else return 99; }, LazyThreadSafetyMode.PublicationOnly);
199 Console.WriteLine (x);
200 } catch (Exception ex) { }
205 } catch (Exception ex) { }
208 Assert.AreEqual (2, invoke, "#3");
210 Assert.AreEqual (99, lz.Value, "#4");
211 Assert.AreEqual (3, invoke, "#5");
215 lz = new Lazy<int> (() => { ++invoke; bool r = rec; rec = false; return r ? lz.Value : 88; }, LazyThreadSafetyMode.PublicationOnly);
217 Assert.AreEqual (88, lz.Value, "#6");
218 Assert.AreEqual (2, invoke, "#7");
222 public void ModeExecutionAndPublication () {
225 Lazy<int> lz = new Lazy<int> (() => { ++invoke; if (fail) throw new Exception (); else return 99; }, LazyThreadSafetyMode.ExecutionAndPublication);
230 Console.WriteLine (x);
231 } catch (Exception ex) { }
232 Assert.AreEqual (1, invoke, "#2");
237 } catch (Exception ex) { }
238 Assert.AreEqual (1, invoke, "#4");
244 } catch (Exception ex) { }
245 Assert.AreEqual (1, invoke, "#6");
248 lz = new Lazy<int> (() => rec ? lz.Value : 99, LazyThreadSafetyMode.ExecutionAndPublication);
253 } catch (InvalidOperationException ex) { }
259 } catch (InvalidOperationException ex) { }
262 static int Return22 () {
267 public void Trivial_Lazy () {
268 var x = new Lazy<int> (Return22, false);
269 Assert.AreEqual (22, x.Value, "#1");
273 public void ConcurrentInitialization ()
275 var init = new AutoResetEvent (false);
276 var e1_set = new AutoResetEvent (false);
278 var lazy = new Lazy<string> (() => {
281 throw new ApplicationException ();
285 var thread = new Thread (() => {
287 string value = lazy.Value;
288 } catch (Exception ex) {
295 Assert.IsTrue (init.WaitOne (3000), "#1");
299 string value = lazy.Value;
300 } catch (Exception ex) {
306 string value = lazy.Value;
307 } catch (Exception ex) {
311 Assert.IsTrue (e1_set.WaitOne (3000), "#2");
312 Assert.AreSame (e1, e2, "#3");
313 Assert.AreSame (e1, e3, "#4");