Пример #1
0
        public void TestMethods()
        {
            var root = TreeNode.ConstructFromArray(new int?[] {1, 2, 3, null, null, 4, 5});
            
            Codec codec = new Codec();
            var serlizeresult = codec.Serialize(root);
            var result = codec.Deserialize(serlizeresult);

            Assert.IsNotNull(result);
        }
Пример #2
0
        public void TestMethods2()
        {
            var root = TreeNode.ConstructFromArray(new int?[] {1, 10, null, 9, 11, null, 8, 12, null, 7, null, null, 13, null, 6, 14, null, 5, null, null, 15, null, 4, 16, null, 3, null, null, 17, null, 2, 18, null, 1, null, null, 19 });

            Codec codec = new Codec();
            var serlizeresult = codec.Serialize(root);
            var result = codec.Deserialize(serlizeresult);

            Assert.IsNotNull(result);
        }
Пример #3
0
        public void TestDeserializeSerialize()
        {
            AlgorithmPractice.Tree.Problems.TreeNode node = new AlgorithmPractice.Tree.Problems.TreeNode(1);

            node.left       = new AlgorithmPractice.Tree.Problems.TreeNode(2);
            node.right      = new AlgorithmPractice.Tree.Problems.TreeNode(3);
            node.right.left = new AlgorithmPractice.Tree.Problems.TreeNode(4);

            node.right.right = new AlgorithmPractice.Tree.Problems.TreeNode(5);

            Codec cd               = new Codec();
            var   output           = cd.Serialize(node);
            var   deserializedNode = cd.Deserialize(output);
        }
Пример #4
0
        public async Task <IReadOnlyList <ServiceDiscoveryInfo> > QueryServiceAsync(string serviceName, CancellationToken cancellationToken = default)
        {
            try
            {
                if (await ZooKeeper.existsAsync($"{Root}/{serviceName}", true) == null)
                {
                    return(new List <ServiceDiscoveryInfo>());
                }
                var nodes = await ZooKeeper.getChildrenAsync($"{Root}/{serviceName}", true);

                if (nodes == null)
                {
                    return(new List <ServiceDiscoveryInfo>());
                }
                var result = new List <ServiceDiscoveryInfo>();
                foreach (var node in nodes.Children)
                {
                    var data = await ZooKeeper.getDataAsync($"{Root}/{serviceName}/{node}");

                    if (data?.Data == null)
                    {
                        throw new ArgumentNullException("data", "The node data is null.");
                    }
                    var serviceData = Codec.Deserialize <ZooKeeperNodeInfo>(data.Data);
                    if (serviceData == null)
                    {
                        continue;
                    }
                    result.Add(new ServiceDiscoveryInfo(node, serviceData.Address, serviceData.Port, serviceData.Weight, serviceData.EnableTls, null));
                }
                return(result);
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Query zookeeper services error:{0}", e.Message);
                throw;
            }
        }
Пример #5
0
        public void TestWrap()
        {
            /*
             *          1
             *         / \
             *        2   3
             *           / \
             *          4   5
             */
            var s    = "1,2,X,X,3,4,X,X,5";
            var obj  = new Codec();
            var root = obj.Deserialize(s);

            var res = WidthOfBinaryTree(root);

            Assert.Equal(2, res);


            /*
             *         1
             *        / \
             *       2   3
             *      / \   \
             *     4   5   6
             */

            s    = "1,2,4,X,X,5,X,X,3,X,6";
            obj  = new Codec();
            root = obj.Deserialize(s);

            res = WidthOfBinaryTree(root);
            Assert.Equal(3, res);

            /*
             *          1
             *         / \
             *        3   2
             *       /
             *      5
             */

            s    = "1,3,5,X,X,X,2";
            obj  = new Codec();
            root = obj.Deserialize(s);

            res = WidthOfBinaryTree(root);
            Assert.Equal(2, res);

            /*
             *          1
             *         /
             *        3
             *       /
             *      5
             */

            s    = "1,3,5,X,X,X";
            obj  = new Codec();
            root = obj.Deserialize(s);

            res = WidthOfBinaryTree(root);
            Assert.Equal(1, res);
        }