Esempio n. 1
0
        public static void EnsureInitalized_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);
        }
Esempio n. 2
0
        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); }));
        }
Esempio n. 3
0
        public static void TestLazyInitializerComplexRefTypes()
        {
            string         strTemplate = "foo";
            HasDefaultCtor hdcTemplate = new HasDefaultCtor();

            // Activator.CreateInstance (uninitialized).
            HasDefaultCtor a     = null;
            bool           aInit = false;
            object         aLock = null;

            Assert.NotNull(LazyInitializer.EnsureInitialized <HasDefaultCtor>(ref a, ref aInit, ref aLock));
            Assert.NotNull(a);

            // Activator.CreateInstance (already initialized).
            HasDefaultCtor b     = hdcTemplate;
            bool           bInit = true;
            object         bLock = null;

            Assert.Equal(hdcTemplate, LazyInitializer.EnsureInitialized <HasDefaultCtor>(ref b, ref bInit, ref bLock));
            Assert.Equal(hdcTemplate, b);

            // Func based initialization (uninitialized).
            string c     = null;
            bool   cInit = false;
            object cLock = null;

            Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized <string>(ref c, ref cInit, ref cLock, () => strTemplate));
            Assert.Equal(strTemplate, c);

            // Func based initialization (already initialized).
            string d     = strTemplate;
            bool   dInit = true;
            object dLock = null;

            Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized <string>(ref d, ref dInit, ref dLock, () => strTemplate + "bar"));
            Assert.Equal(strTemplate, d);

            // Func based initialization (nulls *ARE* permitted).
            string e         = null;
            bool   einit     = false;
            object elock     = null;
            int    initCount = 0;

            Assert.Null(LazyInitializer.EnsureInitialized <string>(ref e, ref einit, ref elock, () => { initCount++; return(null); }));
            Assert.Null(e);
            Assert.Equal(1, initCount);
            Assert.Null(LazyInitializer.EnsureInitialized <string>(ref e, ref einit, ref elock, () => { initCount++; return(null); }));

            // Activator.CreateInstance (for a type without a default ctor).
            NoDefaultCtor ndc     = null;
            bool          ndcInit = false;
            object        ndcLock = null;

            Assert.Throws <MissingMemberException>(() => LazyInitializer.EnsureInitialized <NoDefaultCtor>(ref ndc, ref ndcInit, ref ndcLock));
        }
Esempio n. 4
0
        public static void TestLazyInitializerSimpleRefTypes()
        {
            HasDefaultCtor hdcTemplate = new HasDefaultCtor();
            string         strTemplate = "foo";

            // Activator.CreateInstance (uninitialized).
            HasDefaultCtor a = null;

            Assert.NotNull(LazyInitializer.EnsureInitialized <HasDefaultCtor>(ref a));
            Assert.NotNull(a);

            // Activator.CreateInstance (already initialized).
            HasDefaultCtor b = hdcTemplate;

            Assert.Equal(hdcTemplate, LazyInitializer.EnsureInitialized <HasDefaultCtor>(ref b));
            Assert.Equal(hdcTemplate, b);

            // Func based initialization (uninitialized).
            string c = null;

            Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized <string>(ref c, () => strTemplate));
            Assert.Equal(strTemplate, c);

            // Func based initialization (already initialized).
            string d = strTemplate;

            Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized <string>(ref d, () => strTemplate + "bar"));
            Assert.Equal(strTemplate, d);

            // Func based initialization (nulls not permitted).
            string e = null;

            Assert.Throws <InvalidOperationException>(() => LazyInitializer.EnsureInitialized <string>(ref e, () => null));

            // Activator.CreateInstance (for a type without a default ctor).
            NoDefaultCtor ndc = null;

            Assert.Throws <MissingMemberException>(() => LazyInitializer.EnsureInitialized <NoDefaultCtor>(ref ndc));
        }
Esempio n. 5
0
        public static void TestLazyInitializerComplexRefTypes()
        {
            string strTemplate = "foo";
            HasDefaultCtor hdcTemplate = new HasDefaultCtor();

            // Activator.CreateInstance (uninitialized).
            HasDefaultCtor a = null;
            bool aInit = false;
            object aLock = null;
            Assert.NotNull(LazyInitializer.EnsureInitialized<HasDefaultCtor>(ref a, ref aInit, ref aLock));
            Assert.NotNull(a);

            // Activator.CreateInstance (already initialized).
            HasDefaultCtor b = hdcTemplate;
            bool bInit = true;
            object bLock = null;
            Assert.Equal(hdcTemplate, LazyInitializer.EnsureInitialized<HasDefaultCtor>(ref b, ref bInit, ref bLock));
            Assert.Equal(hdcTemplate, b);

            // Func based initialization (uninitialized).
            string c = null;
            bool cInit = false;
            object cLock = null;
            Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized<string>(ref c, ref cInit, ref cLock, () => strTemplate));
            Assert.Equal(strTemplate, c);

            // Func based initialization (already initialized).
            string d = strTemplate;
            bool dInit = true;
            object dLock = null;
            Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized<string>(ref d, ref dInit, ref dLock, () => strTemplate + "bar"));
            Assert.Equal(strTemplate, d);

            // Func based initialization (nulls *ARE* permitted).
            string e = null;
            bool einit = false;
            object elock = null;
            int initCount = 0;

            Assert.Null(LazyInitializer.EnsureInitialized<string>(ref e, ref einit, ref elock, () => { initCount++; return null; }));
            Assert.Null(e);
            Assert.Equal(1, initCount);
            Assert.Null(LazyInitializer.EnsureInitialized<string>(ref e, ref einit, ref elock, () => { initCount++; return null; }));

            // Activator.CreateInstance (for a type without a default ctor).
            NoDefaultCtor ndc = null;
            bool ndcInit = false;
            object ndcLock = null;
            Assert.Throws<MissingMemberException>(() => LazyInitializer.EnsureInitialized<NoDefaultCtor>(ref ndc, ref ndcInit, ref ndcLock));
        }
Esempio n. 6
0
        public static void TestLazyInitializerSimpleRefTypes()
        {
            HasDefaultCtor hdcTemplate = new HasDefaultCtor();
            string strTemplate = "foo";

            // Activator.CreateInstance (uninitialized).
            HasDefaultCtor a = null;
            Assert.NotNull(LazyInitializer.EnsureInitialized<HasDefaultCtor>(ref a));
            Assert.NotNull(a);

            // Activator.CreateInstance (already initialized).
            HasDefaultCtor b = hdcTemplate;
            Assert.Equal(hdcTemplate, LazyInitializer.EnsureInitialized<HasDefaultCtor>(ref b));
            Assert.Equal(hdcTemplate, b);

            // Func based initialization (uninitialized).
            string c = null;
            Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized<string>(ref c, () => strTemplate));
            Assert.Equal(strTemplate, c);

            // Func based initialization (already initialized).
            string d = strTemplate;
            Assert.Equal(strTemplate, LazyInitializer.EnsureInitialized<string>(ref d, () => strTemplate + "bar"));
            Assert.Equal(strTemplate, d);

            // Func based initialization (nulls not permitted).
            string e = null;
            Assert.Throws<InvalidOperationException>(() => LazyInitializer.EnsureInitialized<string>(ref e, () => null));

            // Activator.CreateInstance (for a type without a default ctor).
            NoDefaultCtor ndc = null;
            Assert.Throws<MissingMemberException>(() => LazyInitializer.EnsureInitialized<NoDefaultCtor>(ref ndc));
        }
Esempio n. 7
0
        private static bool RunLazyInitializer_ComplexRef()
        {
            TestHarness.TestLog("* RunLazyInitializer_ComplexRef()");

            string         strTemplate = "foo";
            HasDefaultCtor hdcTemplate = new HasDefaultCtor();

            //
            // Complicated overloads (ref types).
            //

            HasDefaultCtor a = null; bool ainit = false; object alock = null;
            HasDefaultCtor b = hdcTemplate; bool binit = true; object block = null;
            string         c = null; bool cinit = false; object clock = null;
            string         d = strTemplate; bool dinit = true; object dlock = null;
            string         e = null; bool einit = false; object elock = null;

            // Activator.CreateInstance (uninitialized).
            if (LazyInitializer.EnsureInitialized <HasDefaultCtor>(ref a, ref ainit, ref alock) == null)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref a) == null");
                return(false);
            }
            else if (a == null)
            {
                TestHarness.TestLog(" > the value of a == null after a call to EnsureInitialized(ref a)");
                return(false);
            }

            // Activator.CreateInstance (already initialized).
            if (LazyInitializer.EnsureInitialized <HasDefaultCtor>(ref b, ref binit, ref block) != hdcTemplate)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref b) != hdcTemplate -- already initialized, should have been unchanged");
                return(false);
            }
            else if (b != hdcTemplate)
            {
                TestHarness.TestLog(" > the value of b != hdcTemplate (" + b + ") after a call to EnsureInitialized(ref b)");
                return(false);
            }

            // Func based initialization (uninitialized).
            if (LazyInitializer.EnsureInitialized <string>(ref c, ref cinit, ref clock, () => strTemplate) != strTemplate)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref c, ...) != strTemplate");
                return(false);
            }
            else if (c != strTemplate)
            {
                TestHarness.TestLog(" > the value of c != strTemplate (" + c + ") after a call to EnsureInitialized(ref c, ..)");
                return(false);
            }

            // Func based initialization (already initialized).
            if (LazyInitializer.EnsureInitialized <string>(ref d, ref dinit, ref dlock, () => strTemplate + "bar") != strTemplate)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref c, ...) != strTemplate -- already initialized, should have been unchanged");
                return(false);
            }
            else if (d != strTemplate)
            {
                TestHarness.TestLog(" > the value of c != strTemplate (" + d + ") after a call to EnsureInitialized(ref d, ..)");
                return(false);
            }

            // Func based initialization (nulls *ARE* permitted).
            int runs = 0;

            if (LazyInitializer.EnsureInitialized <string>(ref e, ref einit, ref elock, () => { runs++; return(null); }) != null)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref e, ...) != null");
                return(false);
            }
            else if (e != null)
            {
                TestHarness.TestLog(" > the value of e != null (" + d + ") after a call to EnsureInitialized(ref d, ..)");
                return(false);
            }
            else if (LazyInitializer.EnsureInitialized <string>(ref e, ref einit, ref elock, () => { runs++; return(null); }) != null || runs > 1)
            {
                TestHarness.TestLog(" > erroneously ran the initialization routine twice... " + runs);
                return(false);
            }

            // Activator.CreateInstance (for a type w/out a default ctor).
            NoDefaultCtor ndc = null; bool ndcinit = false; object ndclock = null;

            try
            {
                LazyInitializer.EnsureInitialized <NoDefaultCtor>(ref ndc, ref ndcinit, ref ndclock);
                TestHarness.TestLog(" > EnsureInitialized(ref ndc) should have thrown an exception - no default ctor");
                return(false);
            }
            catch (MissingMemberException)
            {
            }

            return(true);
        }
Esempio n. 8
0
        private static bool RunLazyInitializer_SimpleRef()
        {
            TestHarness.TestLog("* RunLazyInitializer_SimpleRef()");

            HasDefaultCtor hdcTemplate = new HasDefaultCtor();
            string         strTemplate = "foo";

            //
            // Simple overloads (ref types).
            //

            HasDefaultCtor a = null;
            HasDefaultCtor b = hdcTemplate;
            string         c = null;
            string         d = strTemplate;
            string         e = null;

            // Activator.CreateInstance (uninitialized).
            if (LazyInitializer.EnsureInitialized <HasDefaultCtor>(ref a) == null)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref a) == null");
                return(false);
            }
            else if (a == null)
            {
                TestHarness.TestLog(" > the value of a == null after a call to EnsureInitialized(ref a)");
                return(false);
            }

            // Activator.CreateInstance (already initialized).
            if (LazyInitializer.EnsureInitialized <HasDefaultCtor>(ref b) != hdcTemplate)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref b) != hdcTemplate -- already initialized, should have been unchanged");
                return(false);
            }
            else if (b != hdcTemplate)
            {
                TestHarness.TestLog(" > the value of b != hdcTemplate (" + b + ") after a call to EnsureInitialized(ref b)");
                return(false);
            }

            // Func based initialization (uninitialized).
            if (LazyInitializer.EnsureInitialized <string>(ref c, () => strTemplate) != strTemplate)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref c, ...) != strTemplate");
                return(false);
            }
            else if (c != strTemplate)
            {
                TestHarness.TestLog(" > the value of c != strTemplate (" + c + ") after a call to EnsureInitialized(ref c, ..)");
                return(false);
            }

            // Func based initialization (already initialized).
            if (LazyInitializer.EnsureInitialized <string>(ref d, () => strTemplate + "bar") != strTemplate)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref c, ...) != strTemplate -- already initialized, should have been unchanged");
                return(false);
            }
            else if (d != strTemplate)
            {
                TestHarness.TestLog(" > the value of c != strTemplate (" + d + ") after a call to EnsureInitialized(ref d, ..)");
                return(false);
            }

            // Func based initialization (nulls not permitted).
            try
            {
                LazyInitializer.EnsureInitialized <string>(ref e, () => null);
                TestHarness.TestLog(" > EnsureInitialized(ref e, () => null) should have thrown an exception");
                return(false);
            }
            catch (InvalidOperationException)
            {
            }

            // Activator.CreateInstance (for a type w/out a default ctor).
            NoDefaultCtor ndc = null;

            try
            {
                LazyInitializer.EnsureInitialized <NoDefaultCtor>(ref ndc);
                TestHarness.TestLog(" > EnsureInitialized(ref ndc) should have thrown an exception - no default ctor");
                return(false);
            }
            catch (MissingMemberException)
            {
            }

            return(true);
        }
Esempio n. 9
0
        private static bool RunLazyInitializer_ComplexRef()
        {
            TestHarness.TestLog("* RunLazyInitializer_ComplexRef()");

            string strTemplate = "foo";
            HasDefaultCtor hdcTemplate = new HasDefaultCtor();

            //
            // Complicated overloads (ref types).
            //

            HasDefaultCtor a = null; bool ainit = false; object alock = null;
            HasDefaultCtor b = hdcTemplate; bool binit = true; object block = null;
            string c = null; bool cinit = false; object clock = null;
            string d = strTemplate; bool dinit = true; object dlock = null;
            string e = null; bool einit = false; object elock = null;

            // Activator.CreateInstance (uninitialized).
            if (LazyInitializer.EnsureInitialized<HasDefaultCtor>(ref a, ref ainit, ref alock) == null)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref a) == null");
                return false;
            }
            else if (a == null)
            {
                TestHarness.TestLog(" > the value of a == null after a call to EnsureInitialized(ref a)");
                return false;
            }

            // Activator.CreateInstance (already initialized).
            if (LazyInitializer.EnsureInitialized<HasDefaultCtor>(ref b, ref binit, ref block) != hdcTemplate)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref b) != hdcTemplate -- already initialized, should have been unchanged");
                return false;
            }
            else if (b != hdcTemplate)
            {
                TestHarness.TestLog(" > the value of b != hdcTemplate (" + b + ") after a call to EnsureInitialized(ref b)");
                return false;
            }

            // Func based initialization (uninitialized).
            if (LazyInitializer.EnsureInitialized<string>(ref c, ref cinit, ref clock, () => strTemplate) != strTemplate)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref c, ...) != strTemplate");
                return false;
            }
            else if (c != strTemplate)
            {
                TestHarness.TestLog(" > the value of c != strTemplate (" + c + ") after a call to EnsureInitialized(ref c, ..)");
                return false;
            }

            // Func based initialization (already initialized).
            if (LazyInitializer.EnsureInitialized<string>(ref d, ref dinit, ref dlock, () => strTemplate + "bar") != strTemplate)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref c, ...) != strTemplate -- already initialized, should have been unchanged");
                return false;
            }
            else if (d != strTemplate)
            {
                TestHarness.TestLog(" > the value of c != strTemplate (" + d + ") after a call to EnsureInitialized(ref d, ..)");
                return false;
            }

            // Func based initialization (nulls *ARE* permitted).
            int runs = 0;
            if (LazyInitializer.EnsureInitialized<string>(ref e, ref einit, ref elock, () => { runs++; return null; }) != null)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref e, ...) != null");
                return false;
            }
            else if (e != null)
            {
                TestHarness.TestLog(" > the value of e != null (" + d + ") after a call to EnsureInitialized(ref d, ..)");
                return false;
            }
            else if (LazyInitializer.EnsureInitialized<string>(ref e, ref einit, ref elock, () => { runs++; return null; }) != null || runs > 1)
            {
                TestHarness.TestLog(" > erroneously ran the initialization routine twice... " + runs);
                return false;
            }

            // Activator.CreateInstance (for a type w/out a default ctor).
            NoDefaultCtor ndc = null; bool ndcinit = false; object ndclock = null;
            try
            {
                LazyInitializer.EnsureInitialized<NoDefaultCtor>(ref ndc, ref ndcinit, ref ndclock);
                TestHarness.TestLog(" > EnsureInitialized(ref ndc) should have thrown an exception - no default ctor");
                return false;
            }
            catch (MissingMemberException)
            {
            }

            return true;
        }
Esempio n. 10
0
        private static bool RunLazyInitializer_SimpleRef()
        {
            TestHarness.TestLog("* RunLazyInitializer_SimpleRef()");

            HasDefaultCtor hdcTemplate = new HasDefaultCtor();
            string strTemplate = "foo";

            //
            // Simple overloads (ref types).
            //

            HasDefaultCtor a = null;
            HasDefaultCtor b = hdcTemplate;
            string c = null;
            string d = strTemplate;
            string e = null;

            // Activator.CreateInstance (uninitialized).
            if (LazyInitializer.EnsureInitialized<HasDefaultCtor>(ref a) == null)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref a) == null");
                return false;
            }
            else if (a == null)
            {
                TestHarness.TestLog(" > the value of a == null after a call to EnsureInitialized(ref a)");
                return false;
            }

            // Activator.CreateInstance (already initialized).
            if (LazyInitializer.EnsureInitialized<HasDefaultCtor>(ref b) != hdcTemplate)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref b) != hdcTemplate -- already initialized, should have been unchanged");
                return false;
            }
            else if (b != hdcTemplate)
            {
                TestHarness.TestLog(" > the value of b != hdcTemplate (" + b + ") after a call to EnsureInitialized(ref b)");
                return false;
            }

            // Func based initialization (uninitialized).
            if (LazyInitializer.EnsureInitialized<string>(ref c, () => strTemplate) != strTemplate)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref c, ...) != strTemplate");
                return false;
            }
            else if (c != strTemplate)
            {
                TestHarness.TestLog(" > the value of c != strTemplate (" + c + ") after a call to EnsureInitialized(ref c, ..)");
                return false;
            }

            // Func based initialization (already initialized).
            if (LazyInitializer.EnsureInitialized<string>(ref d, () => strTemplate + "bar") != strTemplate)
            {
                TestHarness.TestLog(" > EnsureInitialized(ref c, ...) != strTemplate -- already initialized, should have been unchanged");
                return false;
            }
            else if (d != strTemplate)
            {
                TestHarness.TestLog(" > the value of c != strTemplate (" + d + ") after a call to EnsureInitialized(ref d, ..)");
                return false;
            }

            // Func based initialization (nulls not permitted).
            try
            {
                LazyInitializer.EnsureInitialized<string>(ref e, () => null);
                TestHarness.TestLog(" > EnsureInitialized(ref e, () => null) should have thrown an exception");
                return false;
            }
            catch (InvalidOperationException)
            {
            }

            // Activator.CreateInstance (for a type w/out a default ctor).
            NoDefaultCtor ndc = null;
            try
            {
                LazyInitializer.EnsureInitialized<NoDefaultCtor>(ref ndc);
                TestHarness.TestLog(" > EnsureInitialized(ref ndc) should have thrown an exception - no default ctor");
                return false;
            }
            catch (MissingMemberException)
            {
            }

            return true;
        }