static void Main(string[] args)
        {
            SmartArray sa = new SmartArray();
            const int  SMART_ARRAY_SIZE = 5;
            bool       testPassed       = false;
            ErrorCode  ec;

            sa = new SmartArray(SMART_ARRAY_SIZE);
            Console.WriteLine("CHECK THIS: SmartArray starts with all zeros");
            sa.PrintAllElements();
            Console.WriteLine("\n*******************\n");


            Console.WriteLine("================= SetAtIndex =================");
            Console.WriteLine("AutoChecked: Can add at slot 0?");
            if ((ec = sa.SetAtIndex(0, 10)) != ErrorCode.NoError)
            {
                Console.WriteLine("TEST FAILED: UNABLE TO SET ELEMENT 0! (ErrorCode: {0})", ec);
            }
            else
            {
                Console.WriteLine("Test Passed: Able to set element 0!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can add at slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                if ((ec = sa.SetAtIndex(i, 10 * i)) != ErrorCode.NoError)
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO SET ELEMENT {0}! (ErrorCode: {1})", i, ec);
                    testPassed = false;
                    break;             // out of the loop
                }
            }
            if (testPassed)
            {
                Console.WriteLine("Test Passed: Able to set all elements!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot {0}?", SMART_ARRAY_SIZE);

            if ((ec = sa.SetAtIndex(SMART_ARRAY_SIZE, 10)) != ErrorCode.Overflow)
            {
                Console.WriteLine("TEST FAILED: SET ELEMENT {0} DIDN'T OVERFLOW (ErrorCode: {1})", SMART_ARRAY_SIZE, ec);
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to set element {0}!", SMART_ARRAY_SIZE);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot {0}?", SMART_ARRAY_SIZE + 10);
            if ((ec = sa.SetAtIndex(SMART_ARRAY_SIZE + 10, 10)) != ErrorCode.Overflow)
            {
                Console.WriteLine("TEST FAILED: SET ELEMENT {0} DIDN'T OVERFLOW (ErrorCode: {1})", SMART_ARRAY_SIZE + 10, ec);
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to set element {0}!", SMART_ARRAY_SIZE + 10);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot -1?");
            if ((ec = sa.SetAtIndex(-1, 10)) != ErrorCode.Underflow)
            {
                Console.WriteLine("TEST FAILED: SET ELEMENT -1 DIDN'T UNDERFLOW! ErrorCode: {0})", ec);
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to set element -1!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot -10?");
            if ((ec = sa.SetAtIndex(-10, 10)) != ErrorCode.Underflow)
            {
                Console.WriteLine("TEST FAILED: SET ELEMENT -10 DIDN'T UNDERFLOW! ErrorCode: {0})", ec);
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to set element -1!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("CHECK THIS: Should see 0, 10, 20, 30, 40");
            sa.PrintAllElements();
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("================= GetAtIndex =================");
            int valueGotten;

            Console.WriteLine("AutoChecked: Can get from slot 0?");
            ec = sa.GetAtIndex(0, out valueGotten);
            if (ec != ErrorCode.NoError)
            {
                Console.WriteLine("TEST FAILED: UNABLE TO GET FROM SLOT 0 (ErrorCode: {0})", ec);
            }
            else if (valueGotten != 0)
            {
                Console.WriteLine("TEST FAILED: UNEXPECTED VALUE FROM SLOT 0: (EXPECTED 0, GOT {0})", valueGotten);
            }
            else
            {
                Console.WriteLine("Test Passed: Able to get expected value from slot 0!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can get from slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                ec = sa.GetAtIndex(i, out valueGotten);
                if (ec != ErrorCode.NoError)
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO GET FROM SLOT {0} (ErrorCode: {1})", i, ec);
                }
                else if (valueGotten != 10 * i)
                {
                    Console.WriteLine("TEST FAILED:  UNEXPECTED VALUE AT SLOT {0} (EXPECTED {1}, GOT {2})", i, i * 10, valueGotten);
                    testPassed = false;
                    break;                 // out of the loop
                }
            }
            if (testPassed)
            {
                Console.WriteLine("Test Passed: Able to get expected values!");
            }
            Console.WriteLine("\n*******************\n");


            Console.WriteLine("AutoChecked: Should NOT be able to get from slot {0}?", SMART_ARRAY_SIZE);
            if ((ec = sa.GetAtIndex(SMART_ARRAY_SIZE, out valueGotten)) != ErrorCode.Overflow)
            {
                Console.WriteLine("TEST FAILED: GET FROM ELEMENT {0} DIDN'T OVERFLOW (ErrorCode: {1})?", SMART_ARRAY_SIZE, ec);
            }
            else if (valueGotten != Int32.MinValue)
            {
                Console.WriteLine("TEST FAILED: GET FROM ELEMENT {0} DIDN'T PRODUCE Int32.MinValue", SMART_ARRAY_SIZE);
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to get from slot {0}!", SMART_ARRAY_SIZE);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot {0}?", SMART_ARRAY_SIZE + 10);
            if ((ec = sa.GetAtIndex(SMART_ARRAY_SIZE + 10, out valueGotten)) != ErrorCode.Overflow)
            {
                Console.WriteLine("TEST FAILED: GET FROM ELEMENT {0} DIDN'T OVERFLOW (ErrorCode: {1})?", SMART_ARRAY_SIZE + 10, ec);
            }
            else if (valueGotten != Int32.MinValue)
            {
                Console.WriteLine("TEST FAILED: GET FROM ELEMENT {0} DIDN'T PRODUCE Int32.MinValue", SMART_ARRAY_SIZE + 10);
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to get from slot {0}!", SMART_ARRAY_SIZE + 10);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot -1?");
            if ((ec = sa.GetAtIndex(-1, out valueGotten)) != ErrorCode.Underflow)
            {
                Console.WriteLine("TEST FAILED: GET FROM ELEMENT {0} DIDN'T UNDERFLOW(ErrorCode: {1})", -1, ec);
            }
            else if (valueGotten != Int32.MinValue)
            {
                Console.WriteLine("TEST FAILED: GET FROM ELEMENT {0} DIDN'T PRODUCE Int32.MinValue", -1);
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to get from slot {0}!", SMART_ARRAY_SIZE);
            }

            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot -10?");
            if ((ec = sa.GetAtIndex(-10, out valueGotten)) != ErrorCode.Underflow)
            {
                Console.WriteLine("TEST FAILED: GET FROM ELEMENT {0} DIDN'T UNDERFLOW(ErrorCode:{1})", -10, ec);
            }
            else if (valueGotten != Int32.MinValue)
            {
                Console.WriteLine("TEST FAILED: GET FROM ELEMENT {0} DIDN'T PRODUCE Int32.MinValue", -10);
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to get from slot {0}!", SMART_ARRAY_SIZE);
            }
            Console.WriteLine("\n*******************\n");


            Console.WriteLine("================= Find =================");
            Console.WriteLine("AutoChecked: Can find 0?");
            if (!sa.Find(0))
            {
                Console.WriteLine("TEST FAILED: UNABLE TO FIND VALUE 0!");
            }
            else
            {
                Console.WriteLine("Test Passed: Able to find value 0!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can find the values in slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                ec = sa.GetAtIndex(i, out valueGotten);
                if (ec != ErrorCode.NoError)
                {
                    Console.WriteLine("TEST FAILED:UNABLE TO GET ELEMENT AT SLOT {0} (ErrorCode: {1})!", i, ec);
                    testPassed = false;
                    break;
                }                               // technically don't need the 'else'....
                else if (!sa.Find(valueGotten)) // test by getting from array
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO FIND {0}!", valueGotten);
                    testPassed = false;
                    break;                 // out of the loop
                }
            }
            if (testPassed)
            {
                Console.WriteLine("Test Passed: Able to find values that are already in the array!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can find the values calculated to be in slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                if (!sa.Find(i * 10))                 // test by re-calculating the result
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO FIND {0}!", i * 10);
                    testPassed = false;
                    break;                 // out of the loop
                }
            }
            if (testPassed)
            {
                Console.WriteLine("Test Passed: Able to find values calculated to be in the array!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to find -1?");
            if (sa.Find(-1))
            {
                Console.WriteLine("TEST FAILED: ABLE TO FIND -1, WHICH SHOULD NOT BE PRESENT");
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to find nonexistent value -1!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to find -10?");
            if (sa.Find(-10))
            {
                Console.WriteLine("TEST FAILED: ABLE TO FIND -10, WHICH SHOULD NOT BE PRESENT");
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to find nonexistent value -10!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to find 11?");
            if (sa.Find(11))
            {
                Console.WriteLine("TEST FAILED: ABLE TO FIND 11, WHICH SHOULD NOT BE PRESENT");
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to find nonexistent value 11!");
            }
            Console.WriteLine("\n*******************\n");
            Console.ReadLine();
        }
Пример #2
0
        static void Main(string[] args)
        {
            SmartArray sa = new SmartArray();
            const int  SMART_ARRAY_SIZE = 5;
            bool       testPassed       = false;

            Console.WriteLine("CHECK THIS: SmartArray starts with all zeros");
            sa.PrintAllElements();
            Console.WriteLine("\n*******************\n");

            try
            {
                Console.WriteLine("================= SetAtIndex =================");
                Console.WriteLine("AutoChecked: Can add at slot 0?");
                sa.SetAtIndex(0, 10);
                Console.WriteLine("Test Passed: Able to set element 0!");
            }
            catch (Exception e)
            {
                Console.WriteLine("TEST FAILED: UNABLE TO SET ELEMENT 0!");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can add at slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                try
                {
                    sa.SetAtIndex(i, 10 * i);
                }
                catch (Exception e)
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO SET ELEMENT {0}!", i);
                    Console.WriteLine(e.Message);
                    testPassed = false;
                    break; // out of the loop
                }
            }
            if (testPassed)
            {
                Console.WriteLine("Test Passed: Able to set all elements!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot {0}?", SMART_ARRAY_SIZE);

            try
            {
                sa.SetAtIndex(SMART_ARRAY_SIZE, 10);
                Console.WriteLine("TEST FAILED: SET ELEMENT {0} DID NOT OVERFLOW (but should have)", SMART_ARRAY_SIZE);
            }
            catch (OverflowException e)
            {
                Console.WriteLine("Test Passed: Unable to set element {0}!", SMART_ARRAY_SIZE);
            }
            catch (Exception e)
            {
                Console.WriteLine("TEST FAILED: SET ELEMENT {0} FAILED, BUT FOR THE WRONG REASON", SMART_ARRAY_SIZE);
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("\n*******************\n");


            Console.WriteLine("AutoChecked: Should NOT be able to add at slot {0}?", SMART_ARRAY_SIZE + 10);
            try
            {
                sa.SetAtIndex(SMART_ARRAY_SIZE + 10, 10);
                Console.WriteLine("TEST FAILED: SET ELEMENT {0} DIDN'T OVERFLOW", SMART_ARRAY_SIZE + 10);
            }
            catch (OverflowException e)
            {
                Console.WriteLine("Test Passed: Unable to set element {0}!", SMART_ARRAY_SIZE);
            }
            catch (Exception e)
            {
                Console.WriteLine("TEST FAILED: SET ELEMENT {0} FAILED, BUT FOR THE WRONG REASON", SMART_ARRAY_SIZE);
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot -1?");
            try
            {
                sa.SetAtIndex(-1, 10);
                Console.WriteLine("TEST FAILED: SET ELEMENT -1 DIDN'T UNDERFLOW");
            }
            catch (UnderflowException e)
            {
                Console.WriteLine("Test Passed: Unable to set element -1!");
            }
            catch (Exception e)
            {
                Console.WriteLine("TEST FAILED: SET ELEMENT -1 FAILED, BUT FOR THE WRONG REASON");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot -10?");
            try
            {
                sa.SetAtIndex(-10, 10);
                Console.WriteLine("TEST FAILED: SET ELEMENT -10 DIDN'T UNDERFLOW");
            }
            catch (UnderflowException e)
            {
                Console.WriteLine("Test Passed: Unable to set element -10!");
            }
            catch (Exception e)
            {
                Console.WriteLine("TEST FAILED: SET ELEMENT -10 FAILED, BUT FOR THE WRONG REASON");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("CHECK THIS: Should see 0, 10, 20, 30, 40");
            sa.PrintAllElements();
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("================= GetAtIndex =================");
            int valueGotten;

            Console.WriteLine("AutoChecked: Can get from slot 0?");
            try
            {
                valueGotten = sa.GetAtIndex(0);
                if (valueGotten != 0)
                {
                    Console.WriteLine("TEST FAILED: UNEXPECTED VALUE FROM SLOT 0: (EXPECTED 0, GOT {0})", valueGotten);
                }
                else
                {
                    Console.WriteLine("Test Passed: Able to get expected value from slot 0!");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("TEST FAILED: UNABLE TO GET FROM SLOT 0");
                Console.WriteLine(e.Message);
            }

            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can get from slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                try
                {
                    valueGotten = sa.GetAtIndex(i);
                    if (valueGotten != 10 * i)
                    {
                        Console.WriteLine("TEST FAILED:  UNEXPECTED VALUE AT SLOT {0} (EXPECTED {1}, GOT {2})", i, i * 10, valueGotten);
                        testPassed = false;
                        break; // out of the loop
                    }
                    else
                    {
                        Console.WriteLine("Test Passed: Able to get expected value from slot 0!");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO GET FROM SLOT {0}", i);
                    Console.WriteLine(e.Message);
                    break;
                }
            }
            if (testPassed)
            {
                Console.WriteLine("Test Passed: Able to get expected values!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot {0}?", SMART_ARRAY_SIZE);
            try
            {
                valueGotten = sa.GetAtIndex(SMART_ARRAY_SIZE);
                Console.WriteLine("TEST FAILED: GET FROM ELEMENT {0} DIDN'T OVERFLOW?", SMART_ARRAY_SIZE);
            }
            catch (OverflowException e)
            {
                Console.WriteLine("Test Passed: Unable to get element at SMART_ARRAY_SIZE!");
            }
            catch (Exception e)
            {
                Console.WriteLine("TEST FAILED: GET ELEMENT AT SMART_ARRAY_SIZE FAILED, BUT FOR THE WRONG REASON");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot {0}?", SMART_ARRAY_SIZE + 10);
            try
            {
                valueGotten = sa.GetAtIndex(SMART_ARRAY_SIZE + 10);
                Console.WriteLine("TEST FAILED: GET FROM ELEMENT {0} DIDN'T OVERFLOW?", SMART_ARRAY_SIZE + 10);
            }
            catch (OverflowException e)
            {
                Console.WriteLine("Test Passed: Unable to get element at SMART_ARRAY_SIZE!");
            }
            catch (Exception e)
            {
                Console.WriteLine("TEST FAILED: GET ELEMENT AT SMART_ARRAY_SIZE FAILED, BUT FOR THE WRONG REASON");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot -1?");
            try
            {
                valueGotten = sa.GetAtIndex(-1);
                Console.WriteLine("TEST FAILED: GET FROM ELEMENT -1 DIDN'T UNDERFLOW");
            }
            catch (UnderflowException e)
            {
                Console.WriteLine("Test Passed: Unable to get element at -1!");
            }
            catch (Exception e)
            {
                Console.WriteLine("TEST FAILED: GET ELEMENT AT -1 FAILED, BUT FOR THE WRONG REASON");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot -10?");
            try
            {
                valueGotten = sa.GetAtIndex(-10);
                Console.WriteLine("TEST FAILED: GET FROM ELEMENT -10 DIDN'T UNDERFLOW");
            }
            catch (UnderflowException e)
            {
                Console.WriteLine("Test Passed: Unable to get element at -10!");
            }
            catch (Exception e)
            {
                Console.WriteLine("TEST FAILED: GET ELEMENT AT -10 FAILED, BUT FOR THE WRONG REASON");
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("================= Find =================");
            Console.WriteLine("AutoChecked: Can find 0?");
            if (!sa.Find(0))
            {
                Console.WriteLine("TEST FAILED: UNABLE TO FIND VALUE 0!");
            }
            else
            {
                Console.WriteLine("Test Passed: Able to find value 0!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can find the values in slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                try
                {
                    valueGotten = sa.GetAtIndex(i);
                    if (!sa.Find(valueGotten)) // test by getting from array
                    {
                        Console.WriteLine("TEST FAILED: UNABLE TO FIND {0}!", valueGotten);
                        testPassed = false;
                        break; // out of the loop
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("TEST FAILED: FIND (iteration " + i + ") FAILED BECAUSE GETATINDEX FAILED");
                }
            }
            if (testPassed)
            {
                Console.WriteLine("Test Passed: Able to find values that are already in the array!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can find the values calculated to be in slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                if (!sa.Find(i * 10)) // test by re-calculating the result
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO FIND {0}!", i * 10);
                    testPassed = false;
                    break; // out of the loop
                }
            }
            if (testPassed)
            {
                Console.WriteLine("Test Passed: Able to find values calculated to be in the array!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to find -1?");
            if (sa.Find(-1))
            {
                Console.WriteLine("TEST FAILED: ABLE TO FIND -1, WHICH SHOULD NOT BE PRESENT");
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to find nonexistent value -1!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to find -10?");
            if (sa.Find(-10))
            {
                Console.WriteLine("TEST FAILED: ABLE TO FIND -10, WHICH SHOULD NOT BE PRESENT");
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to find nonexistent value -10!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to find 11?");
            if (sa.Find(11))
            {
                Console.WriteLine("TEST FAILED: ABLE TO FIND 11, WHICH SHOULD NOT BE PRESENT");
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to find nonexistent value 11!");
            }
            Console.WriteLine("\n*******************\n");
            Console.ReadLine();
        }
Пример #3
0
        static void Main(string[] args)
        {
            SmartArray sa = new SmartArray();
            const int  SMART_ARRAY_SIZE = 5;
            bool       testPassed       = false;

            Console.WriteLine("CHECK THIS: SmartArray starts with all zeros");
            sa.PrintAllElements();
            Console.WriteLine("\n*******************\n");


            Console.WriteLine("================= SetAtIndex =================");
            Console.WriteLine("AutoChecked: Can add at slot 0?");
            if (!sa.SetAtIndex(0, 10))
            {
                Console.WriteLine("TEST FAILED: UNABLE TO SET ELEMENT 0!");
            }
            else
            {
                Console.WriteLine("Test Passed: Able to set element 0!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can add at slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                if (!sa.SetAtIndex(i, 10 * i))
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO SET ELEMENT {0}!", i);
                    testPassed = false;
                    break; // out of the loop
                }
            }
            if (testPassed)
            {
                Console.WriteLine("Test Passed: Able to set all elements!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot {0}?", SMART_ARRAY_SIZE);
            if (sa.SetAtIndex(SMART_ARRAY_SIZE, 10))
            {
                Console.WriteLine("TEST FAILED: ABLE TO SET ELEMENT {0}?", SMART_ARRAY_SIZE);
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to set element {0}!", SMART_ARRAY_SIZE);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot {0}?", SMART_ARRAY_SIZE + 10);
            if (sa.SetAtIndex(SMART_ARRAY_SIZE + 10, 10))
            {
                Console.WriteLine("TEST FAILED: ABLE TO SET ELEMENT {0}?", SMART_ARRAY_SIZE + 10);
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to set element {0}!", SMART_ARRAY_SIZE + 10);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot -1?");
            if (sa.SetAtIndex(-1, 10))
            {
                Console.WriteLine("TEST FAILED: ABLE TO SET ELEMENT -1!");
            }
            else
            {
                Console.WriteLine("Test Passed: UNable to set element -1!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot -10?");
            if (sa.SetAtIndex(-10, 10))
            {
                Console.WriteLine("TEST FAILED: ABLE TO SET ELEMENT -10!");
            }
            else
            {
                Console.WriteLine("Test Passed: UNable to set element -10!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("CHECK THIS: Should see 0, 10, 20, 30, 40");
            sa.PrintAllElements();
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("================= GetAtIndex =================");
            int valueGotten;

            Console.WriteLine("AutoChecked: Can get from slot 0?");
            valueGotten = sa.GetAtIndex(0);
            if (valueGotten != 0)
            {
                Console.WriteLine("TEST FAILED: UNABLE TO GET ELEMENT 0, OR UNEXPECTED VALUE!");
                Console.WriteLine("             (EXPECTED 0, GOT {0})", valueGotten);
            }
            else
            {
                Console.WriteLine("Test Passed: Able to get expected value from slot 0!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can get from slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                valueGotten = sa.GetAtIndex(i);
                if (valueGotten != 10 * i)
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO GET ELEMENT {0}, OR UNEXPECTED VALUE!", i);
                    Console.WriteLine("             (EXPECTED {0}, GOT {1})", i * 10, valueGotten);
                    testPassed = false;
                    break; // out of the loop
                }
            }
            if (testPassed)
            {
                Console.WriteLine("Test Passed: Able to get expected values!");
            }
            Console.WriteLine("\n*******************\n");


            Console.WriteLine("AutoChecked: Should NOT be able to get from slot {0}?", SMART_ARRAY_SIZE);
            if (sa.GetAtIndex(SMART_ARRAY_SIZE) != Int32.MinValue)
            {
                Console.WriteLine("TEST FAILED: ABLE TO GET FROM ELEMENT {0}?", SMART_ARRAY_SIZE);
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to get from slot {0}!", SMART_ARRAY_SIZE);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot {0}?", SMART_ARRAY_SIZE + 10);
            if (sa.GetAtIndex(SMART_ARRAY_SIZE + 10) != Int32.MinValue)
            {
                Console.WriteLine("TEST FAILED: ABLE TO GET FROM ELEMENT {0}?", SMART_ARRAY_SIZE + 10);
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to get from slot {0}!", SMART_ARRAY_SIZE + 10);
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot -1?");
            if (sa.GetAtIndex(-1) != Int32.MinValue)
            {
                Console.WriteLine("TEST FAILED: ABLE TO GET FROM SLOT -1!");
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to get from slot -1!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot -10?");
            if (sa.GetAtIndex(-10) != Int32.MinValue)
            {
                Console.WriteLine("TEST FAILED: ABLE TO GET FROM SLOT -10!");
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to get from slot -10!");
            }
            Console.WriteLine("\n*******************\n");


            Console.WriteLine("================= Find =================");
            Console.WriteLine("AutoChecked: Can find 0?");
            if (!sa.Find(0))
            {
                Console.WriteLine("TEST FAILED: UNABLE TO FIND VALUE 0!");
            }
            else
            {
                Console.WriteLine("Test Passed: Able to find value 0!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can find the values in slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                valueGotten = sa.GetAtIndex(i);
                if (!sa.Find(valueGotten)) // test by getting from array
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO FIND {0}!", valueGotten);
                    testPassed = false;
                    break; // out of the loop
                }
            }
            if (testPassed)
            {
                Console.WriteLine("Test Passed: Able to find values that are already in the array!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can find the values calculated to be in slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                if (!sa.Find(i * 10)) // test by re-calculating the result
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO FIND {0}!", i * 10);
                    testPassed = false;
                    break; // out of the loop
                }
            }
            if (testPassed)
            {
                Console.WriteLine("Test Passed: Able to find values calculated to be in the array!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to find -1?");
            if (sa.Find(-1))
            {
                Console.WriteLine("TEST FAILED: ABLE TO FIND -1, WHICH SHOULD NOT BE PRESENT");
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to find nonexistent value -1!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to find -10?");
            if (sa.Find(-10))
            {
                Console.WriteLine("TEST FAILED: ABLE TO FIND -10, WHICH SHOULD NOT BE PRESENT");
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to find nonexistent value -10!");
            }
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to find 11?");
            if (sa.Find(11))
            {
                Console.WriteLine("TEST FAILED: ABLE TO FIND 11, WHICH SHOULD NOT BE PRESENT");
            }
            else
            {
                Console.WriteLine("Test Passed: Unable to find nonexistent value 11!");
            }
            Console.WriteLine("\n*******************\n");
        }
Пример #4
0
        static void Main(string[] args)
        {
            SmartArray sa = new SmartArray();
            const int SMART_ARRAY_SIZE = 5;
            bool testPassed = false;

            Console.WriteLine("CHECK THIS: SmartArray starts with all zeros");
            sa.PrintAllElements();
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("================= SetAtIndex =================");
            Console.WriteLine("AutoChecked: Can add at slot 0?");
            if (!sa.SetAtIndex(0, 10))
                Console.WriteLine("TEST FAILED: UNABLE TO SET ELEMENT 0!");
            else
                Console.WriteLine("Test Passed: Able to set element 0!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can add at slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                if (!sa.SetAtIndex(i, 10 * i))
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO SET ELEMENT {0}!", i);
                    testPassed = false;
                    break; // out of the loop
                }
            }
            if(testPassed)
                Console.WriteLine("Test Passed: Able to set all elements!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot {0}?", SMART_ARRAY_SIZE);
            if (sa.SetAtIndex(SMART_ARRAY_SIZE, 10))
                Console.WriteLine("TEST FAILED: ABLE TO SET ELEMENT {0}?", SMART_ARRAY_SIZE);
            else
                Console.WriteLine("Test Passed: Unable to set element {0}!", SMART_ARRAY_SIZE);
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot {0}?", SMART_ARRAY_SIZE+10);
            if (sa.SetAtIndex(SMART_ARRAY_SIZE + 10, 10))
                Console.WriteLine("TEST FAILED: ABLE TO SET ELEMENT {0}?", SMART_ARRAY_SIZE + 10);
            else
                Console.WriteLine("Test Passed: Unable to set element {0}!", SMART_ARRAY_SIZE + 10);
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot -1?");
            if (sa.SetAtIndex(-1, 10))
                Console.WriteLine("TEST FAILED: ABLE TO SET ELEMENT -1!");
            else
                Console.WriteLine("Test Passed: UNable to set element -1!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to add at slot -10?");
            if (sa.SetAtIndex(-10, 10))
                Console.WriteLine("TEST FAILED: ABLE TO SET ELEMENT -10!");
            else
                Console.WriteLine("Test Passed: UNable to set element -10!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("CHECK THIS: Should see 0, 10, 20, 30, 40");
            sa.PrintAllElements();
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("================= GetAtIndex =================");
            int valueGotten;
            Console.WriteLine("AutoChecked: Can get from slot 0?");
            valueGotten = sa.GetAtIndex(0);
            if (valueGotten != 0)
            {
                Console.WriteLine("TEST FAILED: UNABLE TO GET ELEMENT 0, OR UNEXPECTED VALUE!");
                Console.WriteLine("             (EXPECTED 0, GOT {0})", valueGotten);
            }
            else
                Console.WriteLine("Test Passed: Able to get expected value from slot 0!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can get from slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                valueGotten = sa.GetAtIndex(i);
                if (valueGotten != 10 * i)
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO GET ELEMENT {0}, OR UNEXPECTED VALUE!", i);
                    Console.WriteLine("             (EXPECTED {0}, GOT {1})", i*10, valueGotten);
                    testPassed = false;
                    break; // out of the loop
                }
            }
            if (testPassed)
                Console.WriteLine("Test Passed: Able to get expected values!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot {0}?", SMART_ARRAY_SIZE);
            if (sa.GetAtIndex(SMART_ARRAY_SIZE) != Int32.MinValue)
                Console.WriteLine("TEST FAILED: ABLE TO GET FROM ELEMENT {0}?", SMART_ARRAY_SIZE);
            else
                Console.WriteLine("Test Passed: Unable to get from slot {0}!", SMART_ARRAY_SIZE);
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot {0}?", SMART_ARRAY_SIZE+10);
            if (sa.GetAtIndex(SMART_ARRAY_SIZE + 10) != Int32.MinValue)
                Console.WriteLine("TEST FAILED: ABLE TO GET FROM ELEMENT {0}?", SMART_ARRAY_SIZE + 10);
            else
                Console.WriteLine("Test Passed: Unable to get from slot {0}!", SMART_ARRAY_SIZE + 10);
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot -1?");
            if (sa.GetAtIndex(-1) != Int32.MinValue)
                Console.WriteLine("TEST FAILED: ABLE TO GET FROM SLOT -1!");
            else
                Console.WriteLine("Test Passed: Unable to get from slot -1!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to get from slot -10?");
            if (sa.GetAtIndex(-10) != Int32.MinValue)
                Console.WriteLine("TEST FAILED: ABLE TO GET FROM SLOT -10!");
            else
                Console.WriteLine("Test Passed: Unable to get from slot -10!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("================= Find =================");
            Console.WriteLine("AutoChecked: Can find 0?");
            if (! sa.Find(0))
                Console.WriteLine("TEST FAILED: UNABLE TO FIND VALUE 0!");
            else
                Console.WriteLine("Test Passed: Able to find value 0!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can find the values in slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                valueGotten = sa.GetAtIndex(i);
                if (!sa.Find(valueGotten)) // test by getting from array
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO FIND {0}!", valueGotten);
                    testPassed = false;
                    break; // out of the loop
                }
            }
            if (testPassed)
                Console.WriteLine("Test Passed: Able to find values that are already in the array!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Can find the values calculated to be in slots 0-4?");
            testPassed = true;
            for (int i = 0; i < SMART_ARRAY_SIZE; i++)
            {
                if (!sa.Find(i * 10)) // test by re-calculating the result
                {
                    Console.WriteLine("TEST FAILED: UNABLE TO FIND {0}!", i * 10);
                    testPassed = false;
                    break; // out of the loop
                }
            }
            if (testPassed)
                Console.WriteLine("Test Passed: Able to find values calculated to be in the array!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to find -1?");
            if (sa.Find(-1))
                Console.WriteLine("TEST FAILED: ABLE TO FIND -1, WHICH SHOULD NOT BE PRESENT");
            else
                Console.WriteLine("Test Passed: Unable to find nonexistent value -1!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to find -10?");
            if (sa.Find(-10))
                Console.WriteLine("TEST FAILED: ABLE TO FIND -10, WHICH SHOULD NOT BE PRESENT");
            else
                Console.WriteLine("Test Passed: Unable to find nonexistent value -10!");
            Console.WriteLine("\n*******************\n");

            Console.WriteLine("AutoChecked: Should NOT be able to find 11?");
            if (sa.Find(11))
                Console.WriteLine("TEST FAILED: ABLE TO FIND 11, WHICH SHOULD NOT BE PRESENT");
            else
                Console.WriteLine("Test Passed: Unable to find nonexistent value 11!");
            Console.WriteLine("\n*******************\n");
        }