public void test_fast_compare_ordering_different_length() { var b1 = Utility.GetString("test1test"); var b2 = Utility.GetString("test2"); Assert.AreEqual(FastCompare.CompareBytes(b1, b1), 0); Assert.AreEqual(FastCompare.CompareBytes(b1, b2), -1); Assert.AreEqual(FastCompare.CompareBytes(b2, b1), 1); }
private static unsafe bool GetPtrEquals <T>(T[] left, T[] right) where T : unmanaged { if (left.Length != right.Length) { return(false); } fixed(void *lp = left, rp = right) { return(FastCompare.Equals(lp, rp, left.Length * sizeof(T))); } }
public void test_fast_compare() { const int count = 23; var expected = Utility.GetTestArray(count); var actualOk = Utility.GetTestArray(count); var actualNotOk = new byte[count]; Assert.AreNotEqual(FastCompare.CompareBytes(expected, actualNotOk), 0); Assert.AreEqual(FastCompare.CompareBytes(expected, actualOk), 0); actualOk[count - 1] = 0; Assert.AreNotEqual(FastCompare.CompareBytes(expected, actualOk), 0); }
private static void AssertEqual(byte[] left, byte[] right) { using (MemoryStream leftstream = new(left)) { using (MemoryStream rightstream = new(right)) { AssertEqual(leftstream, rightstream); leftstream.Seek(0, SeekOrigin.Begin); rightstream.Seek(0, SeekOrigin.Begin); AssertEqual(leftstream, rightstream, left.Length); leftstream.Seek(0, SeekOrigin.Begin); rightstream.Seek(0, SeekOrigin.Begin); Assert.IsFalse(FastCompare.NotEquals(leftstream, rightstream)); } } }
public void SmallEqualArrays() { Random r = new(Seed); byte[] left = new byte[15]; byte[] right = new byte[10]; r.NextBytes(left); Array.Copy(left, right, right.Length); using (MemoryStream leftstream = new(left)) { using (MemoryStream rightstream = new(right)) { Assert.IsFalse(FastCompare.Equals(leftstream, rightstream)); leftstream.Seek(0, SeekOrigin.Begin); rightstream.Seek(0, SeekOrigin.Begin); Assert.IsTrue(FastCompare.Equals(leftstream, rightstream, right.Length)); } } }
public void DifferentLengthFull() { Random r = new(Seed); byte[] left = new byte[8192 * 4]; byte[] right = new byte[8192 * 3]; r.NextBytes(left); Array.Copy(left, right, right.Length); using (MemoryStream leftstream = new(left)) { using (MemoryStream rightstream = new(right)) { Assert.IsFalse(FastCompare.Equals(leftstream, rightstream)); leftstream.Seek(0, SeekOrigin.Begin); rightstream.Seek(0, SeekOrigin.Begin); Assert.IsTrue(FastCompare.NotEquals(leftstream, rightstream)); } } }
public void GetMacBytesTest() { byte[] bytes = WakeBroadcaster.GetMacBytes(MacString); Assert.IsTrue(FastCompare.Equals(s_macBytes, bytes)); }
public void GetPacketTest() { byte[] packet = WakeBroadcaster.GetPacket(s_macBytes); Assert.IsTrue(FastCompare.Equals(s_wolPacket, packet)); }
public static bool FastEquals <T>(this ReadOnlySpan <T> left, ReadOnlySpan <T> right) where T : unmanaged => FastCompare.Equals(left, right);
private static void AssertEqual(Stream left, Stream right, int len) => Assert.IsTrue(FastCompare.Equals(left, right, len));
private static void AssertDiff(Stream left, Stream right) => Assert.IsTrue(FastCompare.NotEquals(left, right));
private static async Task AssertEqualAsync(Stream left, Stream right) => Assert.IsTrue(await FastCompare.EqualsAsync(left, right));
/// <summary> /// Copies a <paramref name="source"/> file to a <paramref name="destination"/> splitting it into segments each sized <paramref name="splitSize"/> /// </summary> /// <param name="source">Source <see cref="FileInfo"/> to copy</param> /// <param name="destination">Destination <see cref="FileInfo"/> to copy to</param> /// <param name="splitSize">Size of split segments</param> /// <param name="onWrite">Action will be called everytime theres an advance in bytes with the number of bytes as parameter, to track progress. Can be null.</param> public static void Copy(FileInfo source, FileInfo destination, long splitSize, Action <long>?onWrite = null) { var sourceSplit = new SplitFile(source, splitSize); var destSplit = new SplitFile(destination, splitSize); int bound; if (sourceSplit.SplitInfos.Length > destSplit.SplitInfos.Length) { // If source is bigger than destination copy from source at last destination pos to end bound = destSplit.SplitInfos.Length; SplitInfo infoSource = sourceSplit.SplitInfos[bound]; using var sourceStream = new FileStream(sourceSplit.File.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); using var destStream = new FileStream(destSplit.File.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); sourceStream.Position = infoSource.StartIndex; destStream.Position = infoSource.StartIndex; int len = (int)(source.Length - infoSource.StartIndex); StreamCopy(sourceStream, destStream, len, onWrite); } else if (sourceSplit.SplitInfos.Length < destSplit.SplitInfos.Length || sourceSplit.SplitInfos[^ 1].Length < destSplit.SplitInfos[^ 1].Length) { // If destination is bigger than source throw away the diff at the end (set length of dest to src) bound = sourceSplit.SplitInfos.Length; using var destStream = new FileStream(destSplit.File.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); destStream.SetLength(source.Length); } else { // they are the same length w/e bound = sourceSplit.SplitInfos.Length; } // loop to bound, check each split for equality and copy over if needed Parallel.For(0, bound, (i) => { SplitInfo infoSource = sourceSplit.SplitInfos[i]; SplitInfo infoDest = destSplit.SplitInfos[i]; using var sourceStream = new FileStream(sourceSplit.File.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); using var destStream = new FileStream(destSplit.File.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); sourceStream.Position = infoSource.StartIndex; destStream.Position = infoSource.StartIndex; // if they are not the same length or not equal copy from src if (infoSource.Length != infoDest.Length || !FastCompare.Equals(sourceStream, destStream, infoSource.Length)) { sourceStream.Position = infoSource.StartIndex; destStream.Position = infoSource.StartIndex; StreamCopy(sourceStream, destStream, infoSource.Length, onWrite); } else { onWrite?.Invoke(infoSource.Length); } }); }
public static bool FastEquals <T>(this T[] left, T[] right) where T : unmanaged => FastCompare.Equals(left, right);