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); }
private static bool RunLazyInitializer_ComplexVal() { TestHarness.TestLog("* RunLazyInitializer_ComplexVal()"); LIX empty = new LIX(); LIX template = new LIX(33); // // Complicated overloads (value types). // LIX a = default(LIX); bool ainit = false; object alock = null; LIX b = template; bool binit = true; object block = null; LIX c = default(LIX); bool cinit = false; object clock = null; LIX d = template; bool dinit = true; object dlock = null; // Activator.CreateInstance (uninitialized). if (!LazyInitializer.EnsureInitialized <LIX>(ref a, ref ainit, ref alock).Equals(empty)) { TestHarness.TestLog(" > EnsureInitialized(ref a) != empty"); return(false); } else if (!a.Equals(empty)) { TestHarness.TestLog(" > the value of a != empty (" + a + ") after a call to EnsureInitialized(ref a)"); return(false); } // Activator.CreateInstance (already initialized). if (!LazyInitializer.EnsureInitialized <LIX>(ref b, ref binit, ref block).Equals(template)) { TestHarness.TestLog(" > EnsureInitialized(ref b) != template -- already initialized, should have been unchanged"); return(false); } else if (!b.Equals(template)) { TestHarness.TestLog(" > the value of b != template (" + b + ") after a call to EnsureInitialized(ref b)"); return(false); } // Func based initialization (uninitialized). if (!LazyInitializer.EnsureInitialized <LIX>(ref c, ref cinit, ref clock, () => template).Equals(template)) { TestHarness.TestLog(" > EnsureInitialized(ref c, ...) != template"); return(false); } else if (!c.Equals(template)) { TestHarness.TestLog(" > the value of c != template (" + c + ") after a call to EnsureInitialized(ref c, ..)"); return(false); } // Func based initialization (already initialized). LIX template2 = new LIX(template.f * 2); if (!LazyInitializer.EnsureInitialized <LIX>(ref d, ref dinit, ref dlock, () => template2).Equals(template)) { TestHarness.TestLog(" > EnsureInitialized(ref c, ...) != template -- already initialized, should have been unchanged"); return(false); } else if (!d.Equals(template)) { TestHarness.TestLog(" > the value of d != template (" + d + ") after a call to EnsureInitialized(ref d, ..)"); return(false); } return(true); }
public static void TestLazyInitializerComplexValueTypes() { LIX empty = new LIX(); LIX template = new LIX(33); // Activator.CreateInstance (uninitialized). LIX a = default(LIX); bool aInit = false; object aLock = null; LIX ensuredValA = LazyInitializer.EnsureInitialized<LIX>(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<LIX>(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<LIX>(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<LIX>(ref d, ref dInit, ref dLock, () => template2); Assert.Equal(template, ensuredValD); Assert.Equal(template, d); }
private static bool RunLazyInitializer_ComplexVal() { TestHarness.TestLog("* RunLazyInitializer_ComplexVal()"); LIX empty = new LIX(); LIX template = new LIX(33); // // Complicated overloads (value types). // LIX a = default(LIX); bool ainit = false; object alock = null; LIX b = template; bool binit = true; object block = null; LIX c = default(LIX); bool cinit = false; object clock = null; LIX d = template; bool dinit = true; object dlock = null; // Activator.CreateInstance (uninitialized). if (!LazyInitializer.EnsureInitialized<LIX>(ref a, ref ainit, ref alock).Equals(empty)) { TestHarness.TestLog(" > EnsureInitialized(ref a) != empty"); return false; } else if (!a.Equals(empty)) { TestHarness.TestLog(" > the value of a != empty (" + a + ") after a call to EnsureInitialized(ref a)"); return false; } // Activator.CreateInstance (already initialized). if (!LazyInitializer.EnsureInitialized<LIX>(ref b, ref binit, ref block).Equals(template)) { TestHarness.TestLog(" > EnsureInitialized(ref b) != template -- already initialized, should have been unchanged"); return false; } else if (!b.Equals(template)) { TestHarness.TestLog(" > the value of b != template (" + b + ") after a call to EnsureInitialized(ref b)"); return false; } // Func based initialization (uninitialized). if (!LazyInitializer.EnsureInitialized<LIX>(ref c, ref cinit, ref clock, () => template).Equals(template)) { TestHarness.TestLog(" > EnsureInitialized(ref c, ...) != template"); return false; } else if (!c.Equals(template)) { TestHarness.TestLog(" > the value of c != template (" + c + ") after a call to EnsureInitialized(ref c, ..)"); return false; } // Func based initialization (already initialized). LIX template2 = new LIX(template.f*2); if (!LazyInitializer.EnsureInitialized<LIX>(ref d, ref dinit, ref dlock, () => template2).Equals(template)) { TestHarness.TestLog(" > EnsureInitialized(ref c, ...) != template -- already initialized, should have been unchanged"); return false; } else if (!d.Equals(template)) { TestHarness.TestLog(" > the value of d != template (" + d + ") after a call to EnsureInitialized(ref d, ..)"); return false; } return true; }