public void ClearElementsFromEmptyList()
        {
            var list = new SortedLinkedList <string>((a, b) => a.CompareTo(b));

            Assert.Empty(list);
            list.Clear();
            Assert.Empty(list);
        }
Example #2
0
        public void TestClear()
        {
            var list = new SortedLinkedList <int>();

            list.Insert(5);
            list.Insert(-4);
            list.Clear();
            Assert.AreEqual(0, list.Size);
            Assert.True(list.IsEmpty());
        }
        public void ClearElementsFromList()
        {
            var list = new SortedLinkedList <string>((a, b) => a.CompareTo(b));

            list.Add("A");
            list.Add("B");
            list.Add("C");

            Assert.Equal(list.Count, 3);

            list.Clear();
            Assert.Empty(list);
        }
Example #4
0
        public void Clear()
        {
            T[] array = EnsureArray();

            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] != null)
                {
                    array[i].LastList = null;
                }
            }

            m_list.Clear();
            m_array        = new T[0];
            m_arrayVersion = ++m_version;
        }
Example #5
0
        /// <summary>
        /// Tests the SortedLinkedList and Traveler classes
        /// </summary>
        /// <param name="args">command-line args</param>
        static void Main(string[] args)
        {
            // Graph test objects
            Graph <Waypoint> correctSmallGraph;
            Graph <Waypoint> correctMediumGraph;
            Graph <Waypoint> correctLargeGraph;

            // SearchNode test objects
            SearchNode <Waypoint> searchNode;

            // SortedLinkedList test objects
            SortedLinkedList <SearchNode <Waypoint> > testList =
                new SortedLinkedList <SearchNode <Waypoint> >();

            // GraphBuilder test object
            GraphBuilder graphBuilder;

            // Traveler test object
            Traveler traveler;

            // set up UnityEngine delegates
            GameObject.AddGetComponentDelegate(typeof(Waypoint), GetWaypointComponent);
            GameObject.AddFindObjectByTagDelegate(FindGameObjectWithTag);
            GameObject.AddFindObjectsByTagDelegate(FindGameObjectsWithTag);

            // correct graphs
            correctSmallGraph  = GraphUtils.GetCorrectGraph(GraphSize.Small);
            correctMediumGraph = GraphUtils.GetCorrectGraph(GraphSize.Medium);
            correctLargeGraph  = GraphUtils.GetCorrectGraph(GraphSize.Large);

            // loop while there's more input
            string input = Console.ReadLine();

            while (input[0] != 'q')
            {
                // extract test case number from string
                GetInputValueFromString(input);

                // execute selected test case
                switch (testCaseNumber)
                {
                case 1:
                    // test SortedLinkedList Add method with single node
                    testList.Clear();
                    testList.Add(SearchNodeUtils.GetSearchNode(Vector3.zero, 1, 0));
                    if (testList.Count == 1 &&
                        ListContentsEqual(testList, "0"))
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine("FAILED");
                    }
                    break;

                case 2:
                    // test SortedLinkedList Add method with two equal nodes
                    // (comparison is based on distance of search nodes)
                    testList.Clear();
                    testList.Add(SearchNodeUtils.GetSearchNode(Vector3.zero, 1, 0));
                    testList.Add(SearchNodeUtils.GetSearchNode(Vector3.zero, 2, 0));
                    if (testList.Count == 2 &&
                        ListContentsEqual(testList, "0c0"))
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine("FAILED");
                    }
                    break;

                case 3:
                    // test SortedLinkedList Add method with multiple unequal nodes
                    // (comparison is based on distance of search nodes)
                    testList.Clear();
                    testList.Add(SearchNodeUtils.GetSearchNode(Vector3.zero, 1, 1));
                    testList.Add(SearchNodeUtils.GetSearchNode(Vector3.zero, 2, 0));
                    testList.Add(SearchNodeUtils.GetSearchNode(Vector3.zero, 3, 2));
                    if (testList.Count == 3 &&
                        ListContentsEqual(testList, "0c1c2"))
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine("FAILED");
                    }
                    break;

                case 4:
                    // test SortedLinkedList Reposition method with
                    // no change in position
                    // (comparison is based on distance of search nodes)
                    testList.Clear();
                    testList.Add(SearchNodeUtils.GetSearchNode(Vector3.zero, 1, 1));
                    testList.Add(SearchNodeUtils.GetSearchNode(Vector3.zero, 2, 0));
                    searchNode = SearchNodeUtils.GetSearchNode(Vector3.zero, 3, 2);
                    testList.Add(searchNode);
                    testList.Reposition(searchNode);
                    if (testList.Count == 3 &&
                        ListContentsEqual(testList, "0c1c2"))
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine("FAILED");
                    }
                    break;

                case 5:
                    // test SortedLinkedList Reposition method with
                    // change in position
                    // (comparison is based on distance of search nodes)
                    testList.Clear();
                    testList.Add(SearchNodeUtils.GetSearchNode(Vector3.zero, 1, 1));
                    testList.Add(SearchNodeUtils.GetSearchNode(Vector3.zero, 2, 0));
                    searchNode = SearchNodeUtils.GetSearchNode(Vector3.zero, 3, 2);
                    testList.Add(searchNode);
                    searchNode.Distance = 0.5f;
                    testList.Reposition(searchNode);
                    if (testList.Count == 3 &&
                        ListContentsEqual(testList, "0c0.5c1"))
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine("FAILED");
                    }
                    break;

                case 6:
                    // test GraphBuilder class for small graph
                    currentSize = GraphSize.Small;
                    BuildGameObjectIdWaypointDictionary(currentSize);
                    graphBuilder = new GraphBuilder(new GameObject(int.MaxValue,
                                                                   new Transform(Vector3.zero)));
                    graphBuilder.Awake();
                    if (GraphUtils.WaypointGraphsEqual(correctSmallGraph,
                                                       GraphBuilder.Graph))
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine("FAILED");
                    }
                    break;

                case 7:
                    // test GraphBuilder class for large graph
                    currentSize = GraphSize.Large;
                    BuildGameObjectIdWaypointDictionary(currentSize);
                    graphBuilder = new GraphBuilder(new GameObject(int.MaxValue,
                                                                   new Transform(Vector3.zero)));
                    graphBuilder.Awake();
                    if (GraphUtils.WaypointGraphsEqual(correctLargeGraph,
                                                       GraphBuilder.Graph))
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine("FAILED");
                    }
                    break;

                case 8:
                    // test Traveler class for small graph
                    currentSize = GraphSize.Small;
                    BuildGameObjectIdWaypointDictionary(currentSize);
                    GraphBuilder.Graph = correctSmallGraph;
                    traveler           = new Traveler(new GameObject(int.MaxValue,
                                                                     new Transform(Vector3.zero)));
                    traveler.Start();
                    if (WithinOneHundredth(traveler.PathLength, 9))
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine("FAILED");
                    }
                    break;

                case 9:
                    // test Traveler class for medium graph
                    currentSize = GraphSize.Medium;
                    BuildGameObjectIdWaypointDictionary(currentSize);
                    GraphBuilder.Graph = correctMediumGraph;
                    traveler           = new Traveler(new GameObject(int.MaxValue,
                                                                     new Transform(Vector3.zero)));
                    traveler.Start();
                    if (WithinOneHundredth(traveler.PathLength, 18.0111f))
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine("FAILED");
                    }
                    break;

                case 10:
                    // test Traveler class for large graph
                    currentSize = GraphSize.Large;
                    BuildGameObjectIdWaypointDictionary(currentSize);
                    GraphBuilder.Graph = correctLargeGraph;
                    traveler           = new Traveler(new GameObject(int.MaxValue,
                                                                     new Transform(Vector3.zero)));
                    traveler.Start();
                    if (WithinOneHundredth(traveler.PathLength, 19.4451f))
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine("FAILED");
                    }
                    break;

                    /*
                     * // The following cases were for testing the ListContentsEqual
                     * // method and are never run by the autograder
                     * case 11:
                     * // testing ListContentsEqual method
                     * // empty sorted list
                     * testList = new SortedLinkedList<SearchNode<Waypoint>>();
                     * Console.WriteLine(ListContentsEqual(testList, ""));
                     * break;
                     * case 12:
                     * // testing ListContentsEqual method
                     * // 2 more elements in sorted list than expected
                     * testList = new SortedLinkedList<SearchNode<Waypoint>>();
                     * testList.Add(GetSearchNode(Vector3.zero,0,0));
                     * testList.Add(GetSearchNode(Vector3.zero, 0, 0));
                     * testList.Add(GetSearchNode(Vector3.zero, 0, 0));
                     * Console.WriteLine(ListContentsEqual(testList, "0"));
                     * break;
                     * case 13:
                     * // testing ListContentsEqual method
                     * // 1 more element in sorted list than expected
                     * testList = new SortedLinkedList<SearchNode<Waypoint>>();
                     * testList.Add(GetSearchNode(Vector3.zero, 0, 0));
                     * testList.Add(GetSearchNode(Vector3.zero, 0, 0));
                     * testList.Add(GetSearchNode(Vector3.zero, 0, 0));
                     * Console.WriteLine(ListContentsEqual(testList, "0,0"));
                     * break;
                     * case 14:
                     * // testing ListContentsEqual method
                     * // fewer elements in sorted list than expected
                     * testList = new SortedLinkedList<SearchNode<Waypoint>>();
                     * testList.Add(GetSearchNode(Vector3.zero, 0, 0));
                     * Console.WriteLine(ListContentsEqual(testList, "0,0"));
                     * break;
                     */
                }

                input = Console.ReadLine();
            }
        }