Exemplo n.º 1
0
        public void WithoutSolution()
        {
            // [Test a set which contains no solution]

            /*  Goal: 2. No route.
             *      1
             */
            var nodeWithoutSolution = new BinaryTreeNode <int>(1);

            Assert.IsNull(BinaryTreePathSum.Algorithm(nodeWithoutSolution, 2));
        }
Exemplo n.º 2
0
        public void WithSolution()
        {
            // [Test a set which contains a solution]

            /*  Goal: 2. Root, left.
             *      1
             *    1   0
             *          0
             */
            var nodeWithSolution = new BinaryTreeNode <int>(1)
            {
                Left = new BinaryTreeNode <int>(1)
            };
            var solutionResult = BinaryTreePathSum.Algorithm(nodeWithSolution, 2);

            Assert.IsNotNull(solutionResult);
            Assert.AreEqual(2, solutionResult.Count);
        }
Exemplo n.º 3
0
        public void IntBoundaries()
        {
            // [Testing int boundaries]
            // Create nodes that will cause wrapping since C# handles overflow by default this way
            // 2147483647 += 1 == -2147483648
            // 2147483648 -= 1 ==  2147483647

            var overflowNodeNegative = new BinaryTreeNode <int>(int.MaxValue)
            {
                Left = new BinaryTreeNode <int>(1)
            };
            var overflowNodePositive = new BinaryTreeNode <int>(int.MinValue)
            {
                Left = new BinaryTreeNode <int>(-1)
            };

            Assert.IsNull(BinaryTreePathSum.Algorithm(overflowNodeNegative, int.MinValue));
            Assert.IsNull(BinaryTreePathSum.Algorithm(overflowNodePositive, int.MaxValue));
        }
Exemplo n.º 4
0
        public void SubOptimalSolution()
        {
            // [Test a set which contains an optimal and sub-optimal solution]

            /*  Goal: 10. Root, left, left, for a total of 3 steps. Root, right, for a total of 2 steps.
             *      1
             *    1   10
             *  8  1
             */
            var nodeWithSubOptimalSolution = new BinaryTreeNode <int>(1)
            {
                Left = new BinaryTreeNode <int>(1), Right = new BinaryTreeNode <int>(10)
            };

            nodeWithSubOptimalSolution.Left.Left  = new BinaryTreeNode <int>(8);
            nodeWithSubOptimalSolution.Left.Right = new BinaryTreeNode <int>(1);

            var subOptimalSolutionResult = BinaryTreePathSum.Algorithm(nodeWithSubOptimalSolution, 11);

            Assert.IsNotNull(subOptimalSolutionResult);
            Assert.AreEqual(2, subOptimalSolutionResult.Count);
        }
Exemplo n.º 5
0
 public void NullRef()
 {
     // [Test 'null' for ref types]
     Assert.IsNull(BinaryTreePathSum.Algorithm(null, 1));
 }