static void Main(string[] args) { var random = new Random(); var list = new ListRandom(); var list1 = new ListRandom(); for (int i = 0; i < 20; i++) { list.Add(random.Next(0, 100)); } Console.WriteLine(list.ToJSON()); using (FileStream fs = File.Open("list.txt", FileMode.Create)) { list.Serialize(fs); } using (FileStream fs = File.Open("list.txt", FileMode.Open)) { list1.Deserialize(fs); } Console.WriteLine(list1.ToJSON()); }
private void bDeserialize_Click(object sender, EventArgs e) { try { list = new ListRandom(); var fs = new FileStream("list.blf", FileMode.Open); list.Deserialize(fs); pictureBox1.Invalidate(); MessageBox.Show("list deserialization complete"); } catch (Exception ex) { MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
static void Main(string[] args) { var node1 = new ListNode() { Data = "1" }; var node2 = new ListNode() { Data = "2" }; var node3 = new ListNode() { Data = "3" }; var node4 = new ListNode() { Data = "4" }; var firstList = new ListRandom(node1); firstList.Add(node2); firstList.Add(node3); firstList.Add(node4); using (FileStream fs = new FileStream("list.txt", FileMode.Create)) { firstList.Serialize(fs); } var secondList = new ListRandom(); using (FileStream fs = new FileStream("list.txt", FileMode.Open)) { secondList.Deserialize(fs); } Console.WriteLine("firstList \t secondList"); for (int i = 0; i < firstList.Count; i++) { Console.WriteLine("data:{0} \t {1}", firstList.Get(i).Data, secondList.Get(i).Data); Console.WriteLine("rand:{0} \t {1}", firstList.Get(i).Random.Data, secondList.Get(i).Random.Data); } Console.ReadLine(); }
public bool SerializeDeserializeTest(ListRandom list) { var buffSizeOfElement = 255; var streamBuffSize = buffSizeOfElement * list.Count; var stream = new MemoryStream(streamBuffSize); list.Serialize(stream); stream.Position = 0; var list2 = new ListRandom(); list2.Deserialize(stream); stream.Close(); return(list.Equals(list2)); }
static void Main(string[] args) { Console.WriteLine("Enter the number of elements in the list"); var countStr = Console.ReadLine(); if (!int.TryParse(countStr, out var count)) { Console.WriteLine("It's not number"); return; } var listRandom = new List <(string collection, int indexes)>(); var rnd = new Random(); for (int i = 0; i < count; i++) { Console.WriteLine($"Enter the {i} element"); var data = Console.ReadLine(); listRandom.Add((data, rnd.Next(count))); } var list = new ListRandom(listRandom); Console.WriteLine(list.ToString()); using (var memStream = new MemoryStream()) { Console.WriteLine("Start Serialize list"); list.Serialize(memStream); Console.WriteLine("Finish Serialize list"); list = new ListRandom(new List <(string collection, int indexes)>()); Console.WriteLine("Start Deserialize list"); list.Deserialize(memStream); Console.WriteLine("Finish Deserialize list"); } Console.WriteLine(list.ToString()); }
public void Node_Random_Data_field_serialization_and_deserialization_check() { ListRandom list1 = ListRandom.GenerateRandomList(); Random rnd = new Random(); int randomNodeIndex = rnd.Next(1, list1.Count + 1); ListNode n = list1.Head; for (int i = 1; i < randomNodeIndex; i++) { n = n.Next; } string dataBeforeSerialization = n.Random.Data; var fs = new FileStream("test.blf", FileMode.Create); list1.Serialize(fs); var fs2 = new FileStream("test.blf", FileMode.Open); ListRandom list2 = new ListRandom(); list2.Deserialize(fs2); n = list2.Head; for (int i = 1; i < randomNodeIndex; i++) { n = n.Next; } string dataAfterSerialization = n.Random.Data; Assert.AreEqual(dataBeforeSerialization, dataAfterSerialization); }