This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathLazyTests.cs
613 lines (528 loc) · 26.1 KB
/
LazyTests.cs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using Xunit;
namespace System.Tests
{
public static class LazyTests
{
[Fact]
public static void Ctor()
{
var lazyString = new Lazy<string>();
VerifyLazy(lazyString, "", hasValue: false, isValueCreated: false);
var lazyObject = new Lazy<int>();
VerifyLazy(lazyObject, 0, hasValue: true, isValueCreated: false);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public static void Ctor_Bool(bool isThreadSafe)
{
var lazyString = new Lazy<string>(isThreadSafe);
VerifyLazy(lazyString, "", hasValue: false, isValueCreated: false);
}
[Fact]
public static void Ctor_ValueFactory()
{
var lazyString = new Lazy<string>(() => "foo");
VerifyLazy(lazyString, "foo", hasValue: true, isValueCreated: false);
var lazyInt = new Lazy<int>(() => 1);
VerifyLazy(lazyInt, 1, hasValue: true, isValueCreated: false);
}
[Fact]
public static void Ctor_ValueFactory_NullValueFactory_ThrowsArguentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("valueFactory", () => new Lazy<object>(null)); // Value factory is null
}
[Fact]
public static void Ctor_LazyThreadSafetyMode()
{
var lazyString = new Lazy<string>(LazyThreadSafetyMode.PublicationOnly);
VerifyLazy(lazyString, "", hasValue: false, isValueCreated: false);
}
[Fact]
public static void Ctor_LazyThreadSafetyMode_InvalidMode_ThrowsArgumentOutOfRangeException()
{
AssertExtensions.Throws<ArgumentOutOfRangeException>("mode", () => new Lazy<string>(LazyThreadSafetyMode.None - 1)); // Invalid thread saftety mode
AssertExtensions.Throws<ArgumentOutOfRangeException>("mode", () => new Lazy<string>(LazyThreadSafetyMode.ExecutionAndPublication + 1)); // Invalid thread saftety mode
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public static void Ctor_ValueFactor_Bool(bool isThreadSafe)
{
var lazyString = new Lazy<string>(() => "foo", isThreadSafe);
VerifyLazy(lazyString, "foo", hasValue: true, isValueCreated: false);
}
[Fact]
public static void Ctor_ValueFactory_Bool_NullValueFactory_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("valueFactory", () => new Lazy<object>(null, false)); // Value factory is null
}
[Fact]
public static void Ctor_ValueFactor_LazyThreadSafetyMode()
{
var lazyString = new Lazy<string>(() => "foo", LazyThreadSafetyMode.PublicationOnly);
VerifyLazy(lazyString, "foo", hasValue: true, isValueCreated: false);
var lazyInt = new Lazy<int>(() => 1, LazyThreadSafetyMode.PublicationOnly);
VerifyLazy(lazyInt, 1, hasValue: true, isValueCreated: false);
}
[Fact]
public static void Ctor_ValueFactor_LazyThreadSafetyMode_Invalid()
{
AssertExtensions.Throws<ArgumentNullException>("valueFactory", () => new Lazy<object>(null, LazyThreadSafetyMode.PublicationOnly)); // Value factory is null
AssertExtensions.Throws<ArgumentOutOfRangeException>("mode", () => new Lazy<string>(() => "foo", LazyThreadSafetyMode.None - 1)); // Invalid thread saftety mode
AssertExtensions.Throws<ArgumentOutOfRangeException>("mode", () => new Lazy<string>(() => "foof", LazyThreadSafetyMode.ExecutionAndPublication + 1)); // Invalid thread saftety mode
}
[Fact]
public static void ToString_DoesntForceAllocation()
{
var lazy = new Lazy<object>(() => 1);
Assert.NotEqual("1", lazy.ToString());
Assert.False(lazy.IsValueCreated);
object tmp = lazy.Value;
Assert.Equal("1", lazy.ToString());
}
private static void Value_Invalid_Impl<T>(ref Lazy<T> x, Lazy<T> lazy)
{
x = lazy;
Assert.Throws<InvalidOperationException>(() => lazy.Value);
}
[Fact]
public static void Value_Invalid()
{
Lazy<int> x = null;
Func<int> f = () => x.Value;
Value_Invalid_Impl(ref x, new Lazy<int>(f));
Value_Invalid_Impl(ref x, new Lazy<int>(f, true));
Value_Invalid_Impl(ref x, new Lazy<int>(f, false));
Value_Invalid_Impl(ref x, new Lazy<int>(f, LazyThreadSafetyMode.ExecutionAndPublication));
Value_Invalid_Impl(ref x, new Lazy<int>(f, LazyThreadSafetyMode.None));
// When used with LazyThreadSafetyMode.PublicationOnly this causes a stack overflow
// Value_Invalid_Impl(ref x, new Lazy<int>(f, LazyThreadSafetyMode.PublicationOnly));
}
public class InitiallyExceptionThrowingCtor
{
public static int counter = 0;
public static int getValue()
{
if (++counter < 5)
throw new Exception();
else
return counter;
}
public int Value { get; }
public InitiallyExceptionThrowingCtor()
{
Value = getValue();
}
}
private static IEnumerable<Lazy<InitiallyExceptionThrowingCtor>> Ctor_ExceptionRecovery_MemberData()
{
yield return new Lazy<InitiallyExceptionThrowingCtor>();
yield return new Lazy<InitiallyExceptionThrowingCtor>(true);
yield return new Lazy<InitiallyExceptionThrowingCtor>(false);
yield return new Lazy<InitiallyExceptionThrowingCtor>(LazyThreadSafetyMode.ExecutionAndPublication);
yield return new Lazy<InitiallyExceptionThrowingCtor>(LazyThreadSafetyMode.None);
yield return new Lazy<InitiallyExceptionThrowingCtor>(LazyThreadSafetyMode.PublicationOnly);
}
//
// Do not use [Theory]. XUnit argument formatter can invoke the lazy.Value property underneath you and ruin the assumptions
// made by the test.
//
[Fact]
public static void Ctor_ExceptionRecovery()
{
foreach (Lazy<InitiallyExceptionThrowingCtor> lazy in Ctor_ExceptionRecovery_MemberData())
{
InitiallyExceptionThrowingCtor.counter = 0;
InitiallyExceptionThrowingCtor result = null;
for (int i = 0; i < 10; ++i)
{
try
{ result = lazy.Value; }
catch (Exception) { }
}
Assert.Equal(5, result.Value);
}
}
private static void Value_ExceptionRecovery_IntImpl(Lazy<int> lazy, ref int counter, int expected)
{
counter = 0;
int result = 0;
for (var i = 0; i < 10; ++i)
{
try { result = lazy.Value; } catch (Exception) { }
}
Assert.Equal(result, expected);
}
private static void Value_ExceptionRecovery_StringImpl(Lazy<string> lazy, ref int counter, string expected)
{
counter = 0;
var result = default(string);
for (var i = 0; i < 10; ++i)
{
try { result = lazy.Value; } catch (Exception) { }
}
Assert.Equal(expected, result);
}
[Fact]
public static void Value_ExceptionRecovery()
{
int counter = 0; // set in test function
var fint = new Func<int> (() => { if (++counter < 5) throw new Exception(); else return counter; });
var fobj = new Func<string>(() => { if (++counter < 5) throw new Exception(); else return counter.ToString(); });
Value_ExceptionRecovery_IntImpl(new Lazy<int>(fint), ref counter, 0);
Value_ExceptionRecovery_IntImpl(new Lazy<int>(fint, true), ref counter, 0);
Value_ExceptionRecovery_IntImpl(new Lazy<int>(fint, false), ref counter, 0);
Value_ExceptionRecovery_IntImpl(new Lazy<int>(fint, LazyThreadSafetyMode.ExecutionAndPublication), ref counter, 0);
Value_ExceptionRecovery_IntImpl(new Lazy<int>(fint, LazyThreadSafetyMode.None), ref counter, 0);
Value_ExceptionRecovery_IntImpl(new Lazy<int>(fint, LazyThreadSafetyMode.PublicationOnly), ref counter, 5);
Value_ExceptionRecovery_StringImpl(new Lazy<string>(fobj), ref counter, null);
Value_ExceptionRecovery_StringImpl(new Lazy<string>(fobj, true), ref counter, null);
Value_ExceptionRecovery_StringImpl(new Lazy<string>(fobj, false), ref counter, null);
Value_ExceptionRecovery_StringImpl(new Lazy<string>(fobj, LazyThreadSafetyMode.ExecutionAndPublication), ref counter, null);
Value_ExceptionRecovery_StringImpl(new Lazy<string>(fobj, LazyThreadSafetyMode.None), ref counter, null);
Value_ExceptionRecovery_StringImpl(new Lazy<string>(fobj, LazyThreadSafetyMode.PublicationOnly), ref counter, 5.ToString());
}
class MyException
: Exception
{
public int Value { get; }
public MyException(int value)
{
Value = value;
}
}
public class ExceptionInCtor
{
public ExceptionInCtor() : this(99) { }
public ExceptionInCtor(int value)
{
throw new MyException(value);
}
}
public static IEnumerable<object[]> Value_Func_Exception_MemberData()
{
yield return new object[] { new Lazy<int>(() => { throw new MyException(99); }) };
yield return new object[] { new Lazy<int>(() => { throw new MyException(99); }, true) };
yield return new object[] { new Lazy<int>(() => { throw new MyException(99); }, false) };
yield return new object[] { new Lazy<int>(() => { throw new MyException(99); }, LazyThreadSafetyMode.ExecutionAndPublication) };
yield return new object[] { new Lazy<int>(() => { throw new MyException(99); }, LazyThreadSafetyMode.None) };
yield return new object[] { new Lazy<int>(() => { throw new MyException(99); }, LazyThreadSafetyMode.PublicationOnly) };
}
[Theory]
[MemberData(nameof(Value_Func_Exception_MemberData))]
public static void Value_Func_Exception(Lazy<int> lazy)
{
Assert.Throws<MyException>(() => lazy.Value);
}
public static IEnumerable<object[]> Value_FuncCtor_Exception_MemberData()
{
yield return new object[] { new Lazy<ExceptionInCtor>(() => new ExceptionInCtor(99)) };
yield return new object[] { new Lazy<ExceptionInCtor>(() => new ExceptionInCtor(99), true) };
yield return new object[] { new Lazy<ExceptionInCtor>(() => new ExceptionInCtor(99), false) };
yield return new object[] { new Lazy<ExceptionInCtor>(() => new ExceptionInCtor(99), LazyThreadSafetyMode.ExecutionAndPublication) };
yield return new object[] { new Lazy<ExceptionInCtor>(() => new ExceptionInCtor(99), LazyThreadSafetyMode.None) };
yield return new object[] { new Lazy<ExceptionInCtor>(() => new ExceptionInCtor(99), LazyThreadSafetyMode.PublicationOnly) };
}
[Theory]
[MemberData(nameof(Value_FuncCtor_Exception_MemberData))]
public static void Value_FuncCtor_Exception(Lazy<ExceptionInCtor> lazy)
{
Assert.Throws<MyException>(() => lazy.Value);
}
public static IEnumerable<object[]> Value_TargetInvocationException_MemberData()
{
yield return new object[] { new Lazy<ExceptionInCtor>() };
yield return new object[] { new Lazy<ExceptionInCtor>(true) };
yield return new object[] { new Lazy<ExceptionInCtor>(false) };
yield return new object[] { new Lazy<ExceptionInCtor>(LazyThreadSafetyMode.ExecutionAndPublication) };
yield return new object[] { new Lazy<ExceptionInCtor>(LazyThreadSafetyMode.None) };
yield return new object[] { new Lazy<ExceptionInCtor>(LazyThreadSafetyMode.PublicationOnly) };
}
[Theory]
[MemberData(nameof(Value_TargetInvocationException_MemberData))]
public static void Value_TargetInvocationException(Lazy<ExceptionInCtor> lazy)
{
Assert.Throws<TargetInvocationException>(() => lazy.Value);
}
public static IEnumerable<object[]> Exceptions_Func_Idempotent_MemberData()
{
yield return new object[] { new Lazy<int>(() => { throw new MyException(99); }) };
yield return new object[] { new Lazy<int>(() => { throw new MyException(99); }, true) };
yield return new object[] { new Lazy<int>(() => { throw new MyException(99); }, false) };
yield return new object[] { new Lazy<int>(() => { throw new MyException(99); }, LazyThreadSafetyMode.ExecutionAndPublication) };
yield return new object[] { new Lazy<int>(() => { throw new MyException(99); }, LazyThreadSafetyMode.None) };
}
[Theory]
[MemberData(nameof(Exceptions_Func_Idempotent_MemberData))]
public static void Exceptions_Func_Idempotent(Lazy<int> x)
{
var e = Assert.ThrowsAny<Exception>(() => x.Value);
Assert.Same(e, Assert.ThrowsAny<Exception>(() => x.Value));
}
public static IEnumerable<object[]> Exceptions_Ctor_Idempotent_MemberData()
{
yield return new object[] { new Lazy<ExceptionInCtor>(() => new ExceptionInCtor(99)) };
yield return new object[] { new Lazy<ExceptionInCtor>(() => new ExceptionInCtor(99), true) };
yield return new object[] { new Lazy<ExceptionInCtor>(() => new ExceptionInCtor(99), false) };
yield return new object[] { new Lazy<ExceptionInCtor>(() => new ExceptionInCtor(99), LazyThreadSafetyMode.ExecutionAndPublication) };
yield return new object[] { new Lazy<ExceptionInCtor>(() => new ExceptionInCtor(99), LazyThreadSafetyMode.None) };
}
[Theory]
[MemberData(nameof(Exceptions_Ctor_Idempotent_MemberData))]
public static void Exceptions_Ctor_Idempotent(Lazy<ExceptionInCtor> x)
{
var e = Assert.ThrowsAny<Exception>(() => x.Value);
Assert.Same(e, Assert.ThrowsAny<Exception>(() => x.Value));
}
public static IEnumerable<object[]> Exceptions_Func_NotIdempotent_MemberData()
{
yield return new object[] { new Lazy<int>(() => { throw new MyException(99); }, LazyThreadSafetyMode.PublicationOnly) };
}
public static IEnumerable<object[]> Exceptions_Ctor_NotIdempotent_MemberData()
{
yield return new object[] { new Lazy<ExceptionInCtor>() };
yield return new object[] { new Lazy<ExceptionInCtor>(true) };
yield return new object[] { new Lazy<ExceptionInCtor>(false) };
yield return new object[] { new Lazy<ExceptionInCtor>(LazyThreadSafetyMode.ExecutionAndPublication) };
yield return new object[] { new Lazy<ExceptionInCtor>(LazyThreadSafetyMode.None) };
yield return new object[] { new Lazy<ExceptionInCtor>(LazyThreadSafetyMode.PublicationOnly) };
yield return new object[] { new Lazy<ExceptionInCtor>(() => new ExceptionInCtor(99), LazyThreadSafetyMode.PublicationOnly) };
}
[Theory]
[MemberData(nameof(Exceptions_Func_NotIdempotent_MemberData))]
public static void Exceptions_Func_NotIdempotent(Lazy<int> x)
{
var e = Assert.ThrowsAny<Exception>(() => x.Value);
Assert.NotSame(e, Assert.ThrowsAny<Exception>(() => x.Value));
}
[Theory]
[MemberData(nameof(Exceptions_Ctor_NotIdempotent_MemberData))]
public static void Exceptions_Ctor_NotIdempotent(Lazy<ExceptionInCtor> x)
{
var e = Assert.ThrowsAny<Exception>(() => x.Value);
Assert.NotSame(e, Assert.ThrowsAny<Exception>(() => x.Value));
}
[Theory]
[InlineData(LazyThreadSafetyMode.ExecutionAndPublication)]
[InlineData(LazyThreadSafetyMode.None)]
public static void Value_ThrownException_DoesntCreateValue(LazyThreadSafetyMode mode)
{
var lazy = new Lazy<string>(() => { throw new DivideByZeroException(); }, mode);
Exception exception1 = Assert.Throws<DivideByZeroException>(() => lazy.Value);
Exception exception2 = Assert.Throws<DivideByZeroException>(() => lazy.Value);
Assert.Same(exception1, exception2);
Assert.False(lazy.IsValueCreated);
}
[Fact]
public static void Value_ThrownException_DoesntCreateValue_PublicationOnly()
{
var lazy = new Lazy<string>(() => { throw new DivideByZeroException(); }, LazyThreadSafetyMode.PublicationOnly);
Exception exception1 = Assert.Throws<DivideByZeroException>(() => lazy.Value);
Exception exception2 = Assert.Throws<DivideByZeroException>(() => lazy.Value);
Assert.NotSame(exception1, exception2);
Assert.False(lazy.IsValueCreated);
}
[Fact]
public static void EnsureInitialized_SimpleRefTypes()
{
var hdcTemplate = new HasDefaultCtor();
string strTemplate = "foo";
// Activator.CreateInstance (uninitialized).
HasDefaultCtor a = null;
Assert.NotNull(LazyInitializer.EnsureInitialized(ref a));
Assert.Same(a, LazyInitializer.EnsureInitialized(ref a));
Assert.NotNull(a);
// Activator.CreateInstance (already initialized).
HasDefaultCtor b = hdcTemplate;
Assert.Equal(hdcTemplate, LazyInitializer.EnsureInitialized(ref b));
Assert.Same(b, LazyInitializer.EnsureInitialized(ref b));
Assert.Equal(hdcTemplate, b);
// Func based initialization (uninitialized).
string c = null;
Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized(ref c, () => strTemplate));
Assert.Same(c, LazyInitializer.EnsureInitialized(ref c));
Assert.Equal(strTemplate, c);
// Func based initialization (already initialized).
string d = strTemplate;
Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized(ref d, () => strTemplate + "bar"));
Assert.Same(d, LazyInitializer.EnsureInitialized(ref d));
Assert.Equal(strTemplate, d);
}
[Fact]
public static void EnsureInitialized_SimpleRefTypes_Invalid()
{
// Func based initialization (nulls not permitted).
string e = null;
Assert.Throws<InvalidOperationException>(() => LazyInitializer.EnsureInitialized(ref e, () => null));
// Activator.CreateInstance (for a type without a default ctor).
NoDefaultCtor ndc = null;
Assert.Throws<MissingMemberException>(() => LazyInitializer.EnsureInitialized(ref ndc));
}
[Fact]
public static void EnsureInitialized_ComplexRefTypes()
{
string strTemplate = "foo";
var hdcTemplate = new HasDefaultCtor();
// Activator.CreateInstance (uninitialized).
HasDefaultCtor a = null;
bool aInit = false;
object aLock = null;
Assert.NotNull(LazyInitializer.EnsureInitialized(ref a, ref aInit, ref aLock));
Assert.NotNull(a);
Assert.True(aInit);
Assert.NotNull(aLock);
// Activator.CreateInstance (already initialized).
HasDefaultCtor b = hdcTemplate;
bool bInit = true;
object bLock = null;
Assert.Equal(hdcTemplate, LazyInitializer.EnsureInitialized(ref b, ref bInit, ref bLock));
Assert.Equal(hdcTemplate, b);
Assert.True(bInit);
Assert.Null(bLock);
// Func based initialization (uninitialized).
string c = null;
bool cInit = false;
object cLock = null;
Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized(ref c, ref cInit, ref cLock, () => strTemplate));
Assert.Equal(strTemplate, c);
Assert.True(cInit);
Assert.NotNull(cLock);
// Func based initialization (already initialized).
string d = strTemplate;
bool dInit = true;
object dLock = null;
Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized(ref d, ref dInit, ref dLock, () => strTemplate + "bar"));
Assert.Equal(strTemplate, d);
Assert.True(dInit);
Assert.Null(dLock);
// Func based initialization (nulls *ARE* permitted).
string e = null;
bool einit = false;
object elock = null;
int initCount = 0;
Assert.Null(LazyInitializer.EnsureInitialized(ref e, ref einit, ref elock, () => { initCount++; return null; }));
Assert.Null(e);
Assert.Equal(1, initCount);
Assert.True(einit);
Assert.NotNull(elock);
Assert.Null(LazyInitializer.EnsureInitialized(ref e, ref einit, ref elock, () => { initCount++; return null; }));
}
[Fact]
public static void EnsureInitialized_ComplexRefTypes_Invalid()
{
// Activator.CreateInstance (for a type without a default ctor).
NoDefaultCtor ndc = null;
bool ndcInit = false;
object ndcLock = null;
Assert.Throws<MissingMemberException>(() => LazyInitializer.EnsureInitialized(ref ndc, ref ndcInit, ref ndcLock));
}
[Fact]
public static void LazyInitializerComplexValueTypes()
{
var empty = new LIX();
var template = new LIX(33);
// Activator.CreateInstance (uninitialized).
LIX a = default(LIX);
bool aInit = false;
object aLock = null;
LIX ensuredValA = LazyInitializer.EnsureInitialized(ref a, ref aInit, ref aLock);
Assert.Equal(empty, ensuredValA);
Assert.Equal(empty, a);
// Activator.CreateInstance (already initialized).
LIX b = template;
bool bInit = true;
object bLock = null;
LIX ensuredValB = LazyInitializer.EnsureInitialized(ref b, ref bInit, ref bLock);
Assert.Equal(template, ensuredValB);
Assert.Equal(template, b);
// Func based initialization (uninitialized).
LIX c = default(LIX);
bool cInit = false;
object cLock = null;
LIX ensuredValC = LazyInitializer.EnsureInitialized(ref c, ref cInit, ref cLock, () => template);
Assert.Equal(template, c);
Assert.Equal(template, ensuredValC);
// Func based initialization (already initialized).
LIX d = template;
bool dInit = true;
object dLock = null;
LIX template2 = new LIX(template.f * 2);
LIX ensuredValD = LazyInitializer.EnsureInitialized(ref d, ref dInit, ref dLock, () => template2);
Assert.Equal(template, ensuredValD);
Assert.Equal(template, d);
}
[Fact]
public static void Ctor_Value_ReferenceType()
{
var lazyString = new Lazy<string>("abc");
VerifyLazy(lazyString, "abc", hasValue: true, isValueCreated: true);
}
[Fact]
public static void Ctor_Value_ValueType()
{
var lazyObject = new Lazy<int>(123);
VerifyLazy(lazyObject, 123, hasValue: true, isValueCreated: true);
}
[Fact]
public static void EnsureInitialized_FuncInitializationWithoutTrackingBool_Uninitialized()
{
string template = "foo";
string target = null;
object syncLock = null;
Assert.Equal(template, LazyInitializer.EnsureInitialized(ref target, ref syncLock, () => template));
Assert.Equal(template, target);
Assert.NotNull(syncLock);
}
[Fact]
public static void EnsureInitialized_FuncInitializationWithoutTrackingBool_Initialized()
{
string template = "foo";
string target = template;
object syncLock = null;
Assert.Equal(template, LazyInitializer.EnsureInitialized(ref target, ref syncLock, () => template + "bar"));
Assert.Equal(template, target);
Assert.Null(syncLock);
}
[Fact]
public static void EnsureInitializer_FuncInitializationWithoutTrackingBool_Null()
{
string target = null;
object syncLock = null;
Assert.Throws<InvalidOperationException>(() => LazyInitializer.EnsureInitialized(ref target, ref syncLock, () => null));
}
private static void VerifyLazy<T>(Lazy<T> lazy, T expectedValue, bool hasValue, bool isValueCreated)
{
Assert.Equal(isValueCreated, lazy.IsValueCreated);
if (hasValue)
{
Assert.Equal(expectedValue, lazy.Value);
Assert.True(lazy.IsValueCreated);
}
else
{
Assert.Throws<MissingMemberException>(() => lazy.Value); // Value could not be created
Assert.False(lazy.IsValueCreated);
}
}
private class HasDefaultCtor { }
private class NoDefaultCtor
{
public NoDefaultCtor(int x) { }
}
private struct LIX
{
public int f;
public LIX(int f) { this.f = f; }
public override bool Equals(object other) => other is LIX && ((LIX)other).f == f;
public override int GetHashCode() => f.GetHashCode();
public override string ToString() => "LIX<" + f + ">";
}
}
}