Exemplo n.º 1
0
        public void CanSerializeSingleItem_WithBoundItems()
        {
            ListNode node = new ListNode("Value1");

            ListNode nodePrev = new ListNode("prev");
            ListNode nodeNext = new ListNode("next");
            ListNode nodeRand = new ListNode("rand");

            node.Next = nodeNext;
            node.Rand = nodeRand;
            node.Prev = nodePrev;

            ListRand rnd    = new ListRand();
            string   result = "";

            Assert.DoesNotThrow(() =>
            {
                result = rnd.SerializeSingleItem(node);
            });

            Assert.That(result.Contains("<ListNode>"));

            Assert.That(result.Contains("<Prev>prev</Prev>"));
            Assert.That(result.Contains("<Next>next</Next>"));
            Assert.That(result.Contains("<Rand>rand</Rand>"));

            Assert.That(result.Contains("</ListNode>"));
        }
        public void DeserializeList_ReferencedNodeBeforeReferencing_Success()
        {
            ListFacade expected;
            ListRand   actual;
            string     path = GetTempFilepath();

            expected = CreateFileValuesList();

            expected.Lst.Tail.Rand = expected.Lst.Head;

            using (var fs = new FileStream(path, FileMode.CreateNew))
            {
                expected.Serialize(fs);
                fs.Flush();
            }

            using (var fs = new FileStream(path, FileMode.Open))
            {
                actual = ListRand.Deserialize(fs);
            }

            Assert.True(AreEqual(expected.Lst, actual));

            Assert.AreEqual(actual.Tail.Rand, actual.Head);
        }
    static void AddRandNodeToNode(ListRand listRand, List <int> indices, ListNode node)
    {
        var randomIdx = GetRandomInt(indices.Count);

        node.rand = GetNode(listRand, indices[randomIdx]);
        indices.RemoveAt(randomIdx);
    }
        private bool AreEqual(ListRand expected, ListRand actual)
        {
            if (expected == null && actual != null)
            {
                return(false);
            }
            if (expected != null && actual == null)
            {
                return(false);
            }

            if (expected.Count != actual.Count)
            {
                return(false);
            }

            var expectedDec = new ListFacade(expected);
            var actualDec   = new ListFacade(actual);

            bool result = true;

            for (int i = 0; i < expected.Count; i++)
            {
                var expectedNode = expectedDec.ElementAt(i);
                var actualNode   = actualDec.ElementAt(i);

                bool sameData       = string.Compare(expectedNode.Data, actualNode.Data, System.StringComparison.Ordinal) == 0;
                bool sameRandomRefs = (expectedNode.Rand != null && actualNode.Rand != null) || (expectedNode.Rand == null && actualNode.Rand == null);

                result &= sameData & sameRandomRefs;
            }

            return(result);
        }
Exemplo n.º 5
0
        public void TestSerializationIFNullDataValue()
        {
            var head = new ListNode()
            {
                Prev = null,
                Next = null,
                Rand = null,
                Data = null
            };

            var tail = new ListNode()
            {
                Prev = head,
                Next = null,
                Rand = head,
                Data = null
            };

            head.Next = tail;

            var list = new ListRand()
            {
                Head  = head,
                Tail  = tail,
                Count = 2
            };

            var ser = list.Serialize();

            Assert.That(ser, Is.EqualTo(@"nnn0"));
        }
Exemplo n.º 6
0
        public void TestSerializationIFAllParamsIsNotEmptyOrNull()
        {
            var head = new ListNode()
            {
                Prev = null,
                Next = null,
                Rand = null,
                Data = "test\" data"
            };

            var tail = new ListNode()
            {
                Prev = head,
                Next = null,
                Rand = head,
                Data = "test data2"
            };

            head.Next = tail;

            var list = new ListRand()
            {
                Head  = head,
                Tail  = tail,
                Count = 2
            };

            var ser = list.Serialize();

            Assert.That(ser, Is.EqualTo("\"test\"\" data\"n\"test data2\"0"));
            //Assert.AreEqual(ser, "\"test data\"n\"test data2\"0");
        }
Exemplo n.º 7
0
        public ListRand Generate()
        {
            var test1 = new ListNode()
            {
                Data = "1"
            };
            var test2 = new ListNode()
            {
                Data = "2"
            };

            NodeGenerate(test2, test1);
            var test3 = new ListNode()
            {
                Data = "3"
            };

            NodeGenerate(test3, test2);
            var a = new ListRand()
            {
                Count = 3,
                Head  = test1,
                Tail  = test3
            };

            return(a);
        }
Exemplo n.º 8
0
        public void Test_Deserialize_Crush(string path, string testCase)
        {
            bool assert = false;

            try
            {
                string result = string.Empty;
                using (FileStream s = new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    Test_FileWorker.Write(s, testCase);
                }

                using (FileStream s = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    var resultObj = new ListRand();
                    resultObj.Deserialize(s);
                }
            }
            catch
            {
                assert = true;
            }

            Assert.True(assert);
        }
Exemplo n.º 9
0
        public void Deserialize_ShouldThrowException_WhenWrongFieldName()
        {
            ListRand list = new ListRand();

            WriteToFileStream("[{Dota='',Prev=null,Next=1,Rand=null}]");
            Assert.Throws <KeyNotFoundException>(() => { list.Deserialize(fileStream); });
        }
Exemplo n.º 10
0
        public void Deserialize_ShouldThrowException_WhenNoOpenBracket()
        {
            ListRand list = new ListRand();

            WriteToFileStream("{}");
            Assert.Throws <EmptyStreamException>(() => { list.Deserialize(fileStream); });
        }
Exemplo n.º 11
0
        public void Deserialize_ShouldThrowException_WhenNoField()
        {
            ListRand list = new ListRand();

            WriteToFileStream("[{Data='',Prev=null,Next=null}]");
            Assert.Throws <UnexpectedEndException>(() => { list.Deserialize(fileStream); });
        }
Exemplo n.º 12
0
        public void Deserialize_ShouldThrowException_WhenNoCommaBetweenNodes()
        {
            ListRand list = new ListRand();

            WriteToFileStream("[{Data='',Prev=null,Next=1,Rand=null}{Data='',Prev=0,Next=null,Rand=null}]");
            Assert.Throws <UnexpectedCharException>(() => { list.Deserialize(fileStream); });
        }
Exemplo n.º 13
0
        public void Deserialize_ShouldThrowException_WhenIncorrectReference(string input)
        {
            ListRand list = new ListRand();

            WriteToFileStream(input);
            Assert.Throws <FormatException>(() => { list.Deserialize(fileStream); });
        }
Exemplo n.º 14
0
        public void Deserialize_ShouldNotThrowException_WhenTrailingCommasBetweenNodes()
        {
            ListRand list = new ListRand();

            WriteToFileStream("[{Data='',Prev=null,Next=null,Rand=0},]");
            Assert.DoesNotThrow(() => { list.Deserialize(fileStream); });
        }
Exemplo n.º 15
0
        public void Deserialize_ShouldThrowException_WhenNoTrailingBracket(string input)
        {
            ListRand list = new ListRand();

            WriteToFileStream(input);
            Assert.Throws <UnexpectedEndException>(() => { list.Deserialize(fileStream); });
        }
    static void AddNodeToList(ListRand listRand, string data)
    {
        var node = new ListNode();

        node.data = data;

        if (listRand.count == 0)
        {
            listRand.head = node;
            listRand.tail = node;
        }
        else if (listRand.count == 1)
        {
            listRand.tail      = node;
            listRand.head.next = listRand.tail;
            listRand.tail.prev = listRand.head;
        }
        else
        {
            var temp = listRand.tail;
            listRand.tail = node;

            listRand.tail.prev = temp;
            temp.next          = listRand.tail;
        }
        listRand.count++;
    }
        public void ShouldSerialize_EmptyList()
        {
            ListRand list = new ListRand();

            list.Serialize(fileStream);
            AssertFileStream("[]");
        }
Exemplo n.º 18
0
        public void Deserialize_ShouldThrowException_WhenFoundUnconnectedNode()
        {
            ListRand list = new ListRand();

            WriteToFileStream(
                "[{Data='',Prev=null,Next=2,Rand=null},{Data='',Prev=null,Next=null,Rand=null},{Data='',Prev=0,Next=null,Rand=null}]");
            Assert.Throws <UnconnectedNodeException>(() => { list.Deserialize(fileStream); });
        }
Exemplo n.º 19
0
        //output: {[count][isCyclic]}{[rnd][data]}{[rnd][data]}...
        public string Serialize(ListRand obj)
        {
            Initialize(obj);
            bool isCyclic = CheckListForCyclicity();

            FromListToArray();
            return(FromArrayToString(isCyclic));
        }
    static void FillList(ListRand listRand, int count)
    {
        for (int idx = 0; idx < count; idx++)
        {
            AddNodeToList(listRand, idx.ToString());
        }

        AddRandNodesInList(listRand);
    }
Exemplo n.º 21
0
        public void ShouldDeserialize_NodeData(string data)
        {
            ListRand list = new ListRand();

            WriteToFileStream("[{Data='" + data + "',Prev=null,Next=null,Rand=null}]");
            list.Deserialize(fileStream);
            Assert.NotNull(list.Head);
            Assert.AreEqual(data, list.Head.Data);
        }
Exemplo n.º 22
0
        public void ShouldDeserialize_WhenOneNode_WithRand()
        {
            ListRand list = new ListRand();

            WriteToFileStream("[{Data='',Prev=null,Next=null,Rand=0}]");
            list.Deserialize(fileStream);
            Assert.NotNull(list.Head);
            Assert.AreSame(list.Head, list.Head.Rand);
        }
Exemplo n.º 23
0
        public void ShouldDeserialize_WhenEmptyList()
        {
            ListRand list = new ListRand();

            WriteToFileStream("[]");
            list.Deserialize(fileStream);
            Assert.Null(list.Head);
            Assert.Null(list.Tail);
            Assert.Zero(list.Count);
        }
        public bool SearchIsCorrect(string searchValue)
        {
            var listRand = new ListRand();

            listRand.AddNewToTheEnd(new ListNode("value1"));
            listRand.AddNewToTheEnd(new ListNode("value2"));
            listRand.AddNewToTheEnd(new ListNode("value3"));

            return(listRand.Search(searchValue) != null);
        }
Exemplo n.º 25
0
        public void TestDeserializationIfRandIsNotNull()
        {
            string test = "\"\"0";

            ListRand Test = new ListRand();

            Test.Deserialize(test);
            Assert.That(Test.Head.Data, Is.EqualTo(String.Empty));
            Assert.That(Test.Head.Rand, Is.EqualTo(Test.Head));
        }
Exemplo n.º 26
0
        public void TestDeserializationIfSpaceBeteenQuotes()
        {
            string test = "\" \"n";

            ListRand Test = new ListRand();

            Test.Deserialize(test);
            Assert.That(Test.Head.Data, Is.EqualTo(" "));
            Assert.That(Test.Head.Rand, Is.EqualTo(null));
        }
Exemplo n.º 27
0
        public void TestDeserializationIfNNNInString()
        {
            string test = "\"nnnn\"n";

            ListRand Test = new ListRand();

            Test.Deserialize(test);
            Assert.That(Test.Head.Data, Is.EqualTo("nnnn"));
            Assert.That(Test.Head.Rand, Is.EqualTo(null));
        }
Exemplo n.º 28
0
        public void TestDeserializationIfSymbols()
        {
            string test = "\"{[]\"n";

            ListRand Test = new ListRand();

            Test.Deserialize(test);
            Assert.That(Test.Head.Data, Is.EqualTo("{[]"));
            Assert.That(Test.Head.Rand, Is.EqualTo(null));
        }
Exemplo n.º 29
0
        public void TestDeserializationIfEmptyString()
        {
            string test = "\"\"n";

            ListRand Test = new ListRand();

            Test.Deserialize(test);
            Assert.That(Test.Head.Data, Is.EqualTo(String.Empty));
            Assert.That(Test.Head.Rand, Is.EqualTo(null));
        }
        public void CanAddNewToEmptyList()
        {
            var listRand = new ListRand();
            var node     = new ListNode("value1");

            listRand.AddNewToTheEnd(node);
            Assert.That(listRand.Count, Is.EqualTo(1));
            Assert.That(listRand.Head, Is.Not.Null);
            Assert.That(listRand.Head, Is.EqualTo(listRand.Tail));
        }