Exemplo n.º 1
0
 public async Task ComputeHashAsync_SupportsCancellation()
 {
     using (CancellationTokenSource cancellationSource = new CancellationTokenSource(100))
         using (PositionValueStream stream = new SlowPositionValueStream(10000))
             using (HashAlgorithm hash = new SummingTestHashAlgorithm())
             {
                 await Assert.ThrowsAnyAsync <OperationCanceledException>(
                     () => hash.ComputeHashAsync(stream, cancellationSource.Token));
             }
 }
Exemplo n.º 2
0
 public void ComputeHashAsync_RequiresStream()
 {
     using (HashAlgorithm hash = new SummingTestHashAlgorithm())
     {
         AssertExtensions.Throws <ArgumentNullException>(
             "inputStream",
             () =>
         {
             // Not returning or awaiting the Task, it never got created.
             hash.ComputeHashAsync(null);
         });
     }
 }
Exemplo n.º 3
0
        public void ComputeHashAsync_Disposed()
        {
            using (PositionValueStream stream = new SlowPositionValueStream(10000))
                using (HashAlgorithm hash = new SummingTestHashAlgorithm())
                {
                    hash.Dispose();

                    Assert.Throws <ObjectDisposedException>(
                        () =>
                    {
                        // Not returning or awaiting the Task, it never got created.
                        hash.ComputeHashAsync(stream);
                    });
                }
        }
Exemplo n.º 4
0
        public void SpanMethodsUsed_NotOverridden_ArrayMethodsInvoked()
        {
            byte[] input = Enumerable.Range(0, 1024).Select(i => (byte)i).ToArray();
            byte[] output;
            int    bytesWritten;

            var testAlgorithm = new SummingTestHashAlgorithm();

            output = new byte[sizeof(long) - 1];
            Assert.False(testAlgorithm.TryComputeHash(input, output, out bytesWritten));
            Assert.Equal(0, bytesWritten);
            Assert.Equal <byte>(new byte[sizeof(long) - 1], output);

            output = new byte[sizeof(long)];
            Assert.True(testAlgorithm.TryComputeHash(input, output, out bytesWritten));
            Assert.Equal(sizeof(long), bytesWritten);
            Assert.Equal(input.Sum(b => (long)b), BitConverter.ToInt64(output, 0));
        }
Exemplo n.º 5
0
        public async Task VerifyComputeHashAsync(int size)
        {
            int fullCycles = size / 256;
            int partial    = size % 256;
            // SUM(0..255) is 32640
            const long CycleSum = 32640L;

            // The formula for additive sum IS n*(n+1)/2, but size is a count and the first n is 0,
            // which happens to turn it into (n-1) * n / 2, aka n * (n - 1) / 2.
            long expectedSum = CycleSum * fullCycles + (partial * (partial - 1) / 2);

            using (PositionValueStream stream = new PositionValueStream(size))
                using (HashAlgorithm hash = new SummingTestHashAlgorithm())
                {
                    byte[] result = await hash.ComputeHashAsync(stream);

                    byte[] expected = BitConverter.GetBytes(expectedSum);

                    Assert.Equal(expected, result);
                }
        }