コード例 #1
0
ファイル: FastCompareTest.cs プロジェクト: steelden/fast-data
        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);
        }
コード例 #2
0
ファイル: PointerEqualsTests.cs プロジェクト: mztikk/RFReborn
        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)));
            }
        }
コード例 #3
0
ファイル: FastCompareTest.cs プロジェクト: steelden/fast-data
        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);
        }
コード例 #4
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));
         }
     }
 }
コード例 #5
0
        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));
                }
            }
        }
コード例 #6
0
        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));
                }
            }
        }
コード例 #7
0
 public void GetMacBytesTest()
 {
     byte[] bytes = WakeBroadcaster.GetMacBytes(MacString);
     Assert.IsTrue(FastCompare.Equals(s_macBytes, bytes));
 }
コード例 #8
0
 public void GetPacketTest()
 {
     byte[] packet = WakeBroadcaster.GetPacket(s_macBytes);
     Assert.IsTrue(FastCompare.Equals(s_wolPacket, packet));
 }
コード例 #9
0
 public static bool FastEquals <T>(this ReadOnlySpan <T> left, ReadOnlySpan <T> right) where T : unmanaged => FastCompare.Equals(left, right);
コード例 #10
0
 private static void AssertEqual(Stream left, Stream right, int len) => Assert.IsTrue(FastCompare.Equals(left, right, len));
コード例 #11
0
 private static void AssertDiff(Stream left, Stream right) => Assert.IsTrue(FastCompare.NotEquals(left, right));
コード例 #12
0
 private static async Task AssertEqualAsync(Stream left, Stream right) => Assert.IsTrue(await FastCompare.EqualsAsync(left, right));
コード例 #13
0
ファイル: SegmentCopy.cs プロジェクト: mztikk/RFReborn
    /// <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);
            }
        });
    }
コード例 #14
0
ファイル: ArrayExtensions.cs プロジェクト: mztikk/RFReborn
 public static bool FastEquals <T>(this T[] left, T[] right) where T : unmanaged => FastCompare.Equals(left, right);