Beispiel #1
0
        public void Serializehand_Test()
        {
            foreach (CardValue value1 in Enum.GetValues(typeof(CardValue)))
            {
                foreach (CardValue value2 in Enum.GetValues(typeof(CardValue)))
                {
                    foreach (CardColor color1 in Enum.GetValues(typeof(CardColor)))
                    {
                        foreach (CardColor color2 in Enum.GetValues(typeof(CardColor)))
                        {
                            var card1 = new Card(value: value1, color: color1);
                            var card2 = new Card(value: value2, color: color2);
                            if (card1.Value == card2.Value && card1.Color == card2.Color)
                            {
                                continue; // Hand cannot consist of 2 identical cards
                            }
                            var hand = new Hand(card1: card1, card2: card2);

                            var s = new HandSerializer();

                            var bytes = new byte[16];
                            int index = 3;

                            s.Serialize(hand: hand, outputData: bytes, index: ref index);

                            // Ensure nothing written before start index and after end index
                            Assert.AreEqual(0, bytes[0]);
                            Assert.AreEqual(0, bytes[1]);
                            Assert.AreEqual(0, bytes[2]);

                            for (var i = index; i < bytes.Length; i++)
                            {
                                Assert.AreEqual(0, bytes[i]);
                            }

                            index = 3;
                            var deserializedHand = s.DeserializeHand(inputData: bytes, index: ref index);
                            Assert.IsNotNull(deserializedHand);

                            Assert.AreEqual(card1.Value, deserializedHand.Card1.Value);
                            Assert.AreEqual(card1.Color, deserializedHand.Card1.Color);
                            Assert.AreEqual(card2.Value, deserializedHand.Card2.Value);
                            Assert.AreEqual(card2.Color, deserializedHand.Card2.Color);
                        }
                    }
                }
            }
        }
        private static void DoRoundtrip()
        {
            var client = new TcpClient();

            Console.WriteLine("Connecting...");
            client.Connect("localhost", 7676);
            Console.WriteLine("Connected. awaiting hand...");
            using (var r = new BinaryReader(client.GetStream()))
            {
                var handBytes  = r.ReadBytes(count: 4);
                var serializer = new HandSerializer();
                var index      = 0;
                var hand       = serializer.DeserializeHand(inputData: handBytes, index: ref index);
                Console.WriteLine("Received hand: " + hand);
            }
            Console.WriteLine("Done");
        }