public void CopyRandomList() { var i1 = new Node(7); var i2 = new Node(13); var i3 = new Node(11); var i4 = new Node(10); var i5 = new Node(1); i1.Next = i2; i2.Next = i3; i3.Next = i4; i4.Next = i5; i2.Random = i1; i3.Random = i5; i4.Random = i3; i5.Random = i1; var e1 = new Node(7); var e2 = new Node(13); var e3 = new Node(11); var e4 = new Node(10); var e5 = new Node(1); e1.Next = e2; e2.Next = e3; e3.Next = e4; e4.Next = e5; e2.Random = e1; e3.Random = e5; e4.Random = e3; e5.Random = e1; var sut = new CopyListWithRandomPointer(); var actual = sut.CopyRandomList(i1); Assert.AreNotSame(e1, actual); Assert.AreEqual(JsonConvert.SerializeObject(e1, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }), JsonConvert.SerializeObject(actual, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })); }
public void CopyRandomListTest() { // 此测试只测试了值相等的情况,未测试random的情况 Node head = new Node(0); Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); Node node5 = new Node(5); Node node6 = new Node(6); head.next = node1; node1.next = node2; node2.next = node3; node3.next = node4; node4.next = node5; node5.next = node6; node1.random = node1; node2.random = null; node3.random = node5; node6.random = node2; Node expected = new Node(0); Node expectednode1 = new Node(1); Node expectednode2 = new Node(2); Node expectednode3 = new Node(3); Node expectednode4 = new Node(4); Node expectednode5 = new Node(5); Node expectednode6 = new Node(6); expected.next = expectednode1; expectednode1.next = expectednode2; expectednode2.next = expectednode3; expectednode3.next = expectednode4; expectednode4.next = expectednode5; expectednode5.next = expectednode6; expectednode1.random = expectednode1; expectednode2.random = null; expectednode3.random = expectednode5; expectednode6.random = expectednode2; var result = new CopyListWithRandomPointer().CopyRandomList(head); Assert.IsTrue(expected.IsEqual(result)); }
public void CopyListWithRandonPointerResultsInSuccess() { Node L2 = new Node(); L2.val = 2; L2.next = null; L2.random = L2; Node L1 = new Node(); L1.val = 1; L1.next = L2; L1.random = L2; var result = new CopyListWithRandomPointer().CopyRandomList(L1); Assert.IsTrue(EnumerateLinkedLists(result, L1)); }