static void Main(string[] args) { var comparer = new StreamComparer(File.OpenRead(args[0]), File.OpenRead(args[1])); changeset1 = comparer.Compare(); comparer = new StreamComparer(File.OpenRead(args[0]), File.OpenRead(args[2])); changeset2 = comparer.Compare(); var autochanges = changeset1.Except(changeset2, new ConflictChangesComparer()).Union(changeset2.Except(changeset1, new ConflictChangesComparer())).ToArray(); }
public void TestEqual() { var left = new MemoryStream(Encoding.Default.GetBytes(@"line1 line2 line3")); var right = new MemoryStream(Encoding.Default.GetBytes(@"line1 line2 line3")); var comparer = new StreamComparer(left, right); Assert.IsTrue(comparer.Compare().Length == 0); }
public void TestEqualDifferentLen() { var left = new MemoryStream(Encoding.Default.GetBytes(@"line1 line2 line3")); var right = new MemoryStream(Encoding.Default.GetBytes(@"line1 line2 line3 line4")); var comparer = new StreamComparer(left, right); var changeset = comparer.Compare(); Assert.AreEqual(Tuple.Create(-1, 3, (string)null, "line4"), changeset[0]); }
public void TestNotEqualDifferentLen() { var left = new MemoryStream(Encoding.Default.GetBytes(@"line1 line2 line3")); var right = new MemoryStream(Encoding.Default.GetBytes(@"line1 line4 line3 line2")); var comparer = new StreamComparer(left, right); var changeset = comparer.Compare(); Assert.IsTrue(new[] { Tuple.Create(-1, 1, (string)null, "line4"), Tuple.Create(1, 3, "line2", "line2") }.SequenceEqual(changeset)); }
public void TestEqualAddLineToBegin() { var left = new MemoryStream(Encoding.Default.GetBytes(@"line1 line2 line3")); var right = new MemoryStream(Encoding.Default.GetBytes(@"line4 line1 line2 line3")); var comparer = new StreamComparer(left, right); var changeset = comparer.Compare(); Assert.IsTrue(new[] { Tuple.Create(-1, 0, (string)null, "line4"), Tuple.Create(0, 1, "line1", "line1"), Tuple.Create(1, 2, "line2", "line2"), Tuple.Create(2, 3, "line3", "line3"), }.SequenceEqual(changeset)); }
[Test] public void CreateFromFile() { Project project = Instance.Get.ProjectByID("Scope:0"); Attachment attachment; using (FileStream input = new FileStream("logo.png", FileMode.Open)) attachment = project.CreateAttachment("Second Attachment", "logo.png", input); string attachmentID = attachment.ID; ResetInstance(); Attachment newAttachment = Instance.Get.AttachmentByID(attachmentID); Assert.AreEqual("image/png", newAttachment.ContentType); using (MemoryStream output = new MemoryStream()) { newAttachment.WriteTo(output); using (FileStream input = new FileStream("logo.png", FileMode.Open)) Assert.IsTrue(StreamComparer.CompareStream(input, output)); } }
/// <summary> /// Copies a stream into the current data file, optionally only if does not exists and contains different data. /// </summary> /// <param name="input">The Stream to copy data from.</param> /// <param name="updateOnly">If true, file will not be recreated if already exists and contains the same data.</param> /// <returns>Returns true if file has been created or updated.</returns> public bool CopyFrom(Stream input, bool updateOnly) { if (mode == FileMode.Open) { throw new InvalidOperationException("Cannot write into data file retrieved using the FileMode.Open mode."); } // Non-data files such as plugin.txt do not have a request path relative to the data folder // TODO: Do not provide non-data files via DataFileProvider if (RequestedPath == null) { throw new InvalidOperationException("Cannot write into a special data file."); } if (updateOnly) { // Look if file already exists // To check whether it needs to be updated // Retreive new file from the provider with FileMode.Open var existingFile = provider.GetDataFile(FileMode.Open, RequestedPath); if (existingFile.Exists()) { using (var existingStream = existingFile.Open()) { // If input is not memory stream, create one and copy the data from the input stream into it MemoryStream memoryStream = input as MemoryStream; if (memoryStream == null) { // Initialize memory stream as big as the existing stream length if imput steam is not seekable int initialMemoryStreamLength = input.CanSeek ? (int)input.Length : (int)existingStream.Length; memoryStream = new MemoryStream(initialMemoryStreamLength); input.CopyTo(memoryStream); memoryStream.Position = 0; } // Compare existing stream with input stream (or memory stream where input stream was copied into) if (StreamComparer.Compare(existingStream, memoryStream)) { Log.Fine("Cached file {0} is up to date.", existingFile.FullPath); return(false); } // Reset memeory stream after comparison memoryStream.Position = 0; // Use memory stream (where input stream has been copied into) instead of input stream // when updating existing file if (input != memoryStream) { input = memoryStream; } Log.Fine("Cached file {0} exists but is no longer valid and will be updated.", existingFile.FullPath); } } else { Log.Fine("File {0} is not cached and will be created.", existingFile.FullPath); } } using (var newStream = Open()) { input.CopyTo(newStream); } return(true); }
public void TestNotEqualSameLen() { var left = new MemoryStream(Encoding.Default.GetBytes(@"line1 line2 line3")); var right = new MemoryStream(Encoding.Default.GetBytes(@"line1 line4 line3")); var comparer = new StreamComparer(left, right); var changeset = comparer.Compare(); Assert.AreEqual(Tuple.Create(1, 1, "line2", "line4"), changeset[0]); }