示例#1
0
    public static int Main()
    {
        Random r = new Random();

        try
        {
            // test for ConditionalWeakTable<string>
            Driver <string, string> stringDriver = new Driver <string, string>();

            string[] stringArr = new string[100];
            for (int i = 0; i < 100; i++)
            {
                stringArr[i] = "SomeTestString" + i.ToString();
            }

            // test with generic object
            // test for ConditionalWeakTable<RefX1<int>>
            Driver <string, RefX1 <int> > refIntDriver = new Driver <string, RefX1 <int> >();

            RefX1 <int>[] refIntArr = new RefX1 <int> [100];
            for (int i = 0; i < 100; i++)
            {
                refIntArr[i] = new RefX1 <int>(i);
            }

            // test with generic object
            // test for ConditionalWeakTable<RefX1<string>>
            Driver <string, RefX1 <string> > refStringDriver = new Driver <string, RefX1 <string> >();

            RefX1 <string>[] refStringArr = new RefX1 <string> [100];
            for (int i = 0; i < 100; i++)
            {
                refStringArr[i] = new RefX1 <string>("SomeTestString" + i.ToString());
            }


            stringDriver.BasicAdd(stringArr, stringArr);
            refIntDriver.BasicAdd(stringArr, refIntArr);
            refStringDriver.BasicAdd(stringArr, refStringArr);

            //===============================================================
            // test various boundary conditions
            // - add/remove/lookup of null key
            // - remove/lookup of non-existing key in an empty dictionary and a non-empty dictionary
            stringDriver.AddValidations(stringArr, stringArr, stringArr[0]);
            refIntDriver.AddValidations(stringArr, refIntArr, refIntArr[0]);
            refStringDriver.AddValidations(stringArr, refStringArr, refStringArr[0]);

            //===============================================================
            stringDriver.RemoveValidations(stringArr, stringArr, r.Next().ToString(), stringArr[0]);
            refIntDriver.RemoveValidations(stringArr, refIntArr, r.Next().ToString(), refIntArr[0]);
            refStringDriver.RemoveValidations(stringArr, refStringArr, r.Next().ToString(), refStringArr[0]);

            //===============================================================
            stringDriver.TryGetValueValidations(stringArr, stringArr, r.Next().ToString(), stringArr[0]);
            refIntDriver.TryGetValueValidations(stringArr, refIntArr, r.Next().ToString(), refIntArr[0]);
            refStringDriver.TryGetValueValidations(stringArr, refStringArr, r.Next().ToString(), refStringArr[0]);

            //===============================================================
            // this method generates a dictionary with keys and values to be used for GetValue() method testing
            stringDriver.GenerateValuesForStringKeys(stringArr);
            stringDriver.GetValueValidations(stringArr, stringArr);

            Driver <RefX1 <int>, string> refIntDriver2 = new Driver <RefX1 <int>, string>();
            refIntDriver2.GenerateValuesForIntRefKeys(refIntArr, stringArr);
            refIntDriver2.GetValueValidations(refIntArr, stringArr);

            Driver <RefX1 <string>, string> refStringDriver2 = new Driver <RefX1 <string>, string>();
            refStringDriver2.GenerateValuesForStringRefKeys(refStringArr, stringArr);
            refStringDriver2.GetValueValidations(refStringArr, stringArr);

            //===============================================================
            stringDriver.AddSameKey(stringArr, stringArr, 0, 2);
            stringDriver.AddSameKey(stringArr, stringArr, 99, 3);
            stringDriver.AddSameKey(stringArr, stringArr, 50, 4);
            stringDriver.AddSameKey(stringArr, stringArr, 1, 5);
            stringDriver.AddSameKey(stringArr, stringArr, 98, 6);

            refIntDriver.AddSameKey(stringArr, refIntArr, 0, 2);
            refIntDriver.AddSameKey(stringArr, refIntArr, 99, 3);
            refIntDriver.AddSameKey(stringArr, refIntArr, 50, 4);
            refIntDriver.AddSameKey(stringArr, refIntArr, 1, 5);
            refIntDriver.AddSameKey(stringArr, refIntArr, 98, 6);

            refStringDriver.AddSameKey(stringArr, refStringArr, 0, 2);
            refStringDriver.AddSameKey(stringArr, refStringArr, 99, 3);
            refStringDriver.AddSameKey(stringArr, refStringArr, 50, 4);
            refStringDriver.AddSameKey(stringArr, refStringArr, 1, 5);
            refStringDriver.AddSameKey(stringArr, refStringArr, 98, 6);

            //===============================================================
            stringDriver.AddRemoveKeyValPair(stringArr, stringArr, 0, 2);
            stringDriver.AddRemoveKeyValPair(stringArr, stringArr, 99, 3);
            stringDriver.AddRemoveKeyValPair(stringArr, stringArr, 50, 4);
            stringDriver.AddRemoveKeyValPair(stringArr, stringArr, 1, 5);
            stringDriver.AddRemoveKeyValPair(stringArr, stringArr, 98, 6);

            refIntDriver.AddRemoveKeyValPair(stringArr, refIntArr, 0, 2);
            refIntDriver.AddRemoveKeyValPair(stringArr, refIntArr, 99, 3);
            refIntDriver.AddRemoveKeyValPair(stringArr, refIntArr, 50, 4);
            refIntDriver.AddRemoveKeyValPair(stringArr, refIntArr, 1, 5);
            refIntDriver.AddRemoveKeyValPair(stringArr, refIntArr, 98, 6);

            refStringDriver.AddRemoveKeyValPair(stringArr, refStringArr, 0, 2);
            refStringDriver.AddRemoveKeyValPair(stringArr, refStringArr, 99, 3);
            refStringDriver.AddRemoveKeyValPair(stringArr, refStringArr, 50, 4);
            refStringDriver.AddRemoveKeyValPair(stringArr, refStringArr, 1, 5);
            refStringDriver.AddRemoveKeyValPair(stringArr, refStringArr, 98, 6);

            //==============================================================
            // new tests for GetOrCreateValue
            (new Driver <string, WithDefaultConstructor>()).BasicGetOrCreateValue(stringArr);
            WithDefaultConstructor[] wvalues = new WithDefaultConstructor[stringArr.Length];
            for (int i = 0; i < wvalues.Length; i++)
            {
                wvalues[i] = new WithDefaultConstructor(stringArr[i]);
            }
            (new Driver <string, WithDefaultConstructor>()).BasicAddThenGetOrCreateValue(stringArr, wvalues);

            NegativeTestCases.NoDefaulConstructor();

            //===============================================================
            if (Test.result)
            {
                Console.WriteLine("Test Passed");
                return(100);
            }
            else
            {
                Console.WriteLine("Test Failed");
                return(101);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Test threw unexpected exception:\n{0}", e);
            return(102);
        }
    }