public void SimpleTest() { var rng0 = new CryptographicRandomGenerator(); // this sequence will throw if iterated over multiple times. ulong?[] vals = new ulong?[4096]; var seq = Enumerable.Range(0, vals.Length) .Select(i => { Assert.False(vals[i].HasValue); return((ulong)(vals[i] = rng0.NextUInt64())); }); using (var lazyBufferedSeq = new LazyBufferedEnumerable <ulong>(seq)) { ulong[] vals2 = new ulong[vals.Length]; // spawn tons of concurrent tasks, all but one of which stops before the end. Parallel.For(0, vals2.Length, i => { int j = 0; foreach (var val in lazyBufferedSeq.Take(i + 1)) { vals2[j++] = val; } }); Assert.All(vals, val => Assert.True(val.HasValue)); Assert.Equal(vals.Select(val => val.Value), vals2); } }
public void EqualityTests() { ulong[] data = new ulong[50124]; CryptographicRandomGenerator.FillBuffer(MemoryMarshal.AsBytes(data.AsSpan())); var arrMine = new HugeManagedArray <ulong>(data); // IEnumerable<T> Assert.Equal(data, arrMine); // simple indexing for (int i = 0; i < data.Length; i++) { Assert.Equal(data[i], arrMine[i]); } // pattern foreach int j = 0; foreach (ulong val in data) { Assert.Equal(val, arrMine[j++]); } Assert.Equal(j, arrMine.Length); }
public async Task ReadAsyncShouldThrottle(int throttleRateBytesPerSecond) { byte[] data = new byte[16384]; CryptographicRandomGenerator.FillBuffer(data); byte[] finalBuf = new byte[data.Length]; Stopwatch sw = Stopwatch.StartNew(); using (var outputStream = new MemoryStream()) { using (var readStream = new MemoryStream(data)) using (var bufferedStream = new BufferedStream(readStream, 1024)) using (var throttledStream = new ReadThrottledStream(bufferedStream, throttleRateBytesPerSecond)) { await throttledStream.CopyToAsync(outputStream); } finalBuf = outputStream.ToArray(); } sw.Stop(); double seconds = sw.ElapsedTicks / (double)Stopwatch.Frequency; this.outputHelper.WriteLine("It took {0} seconds to copy {1} bytes at a nominal {2} bytes per second throttle, for a real {3} bytes per second throttle.", seconds, data.Length, throttleRateBytesPerSecond, data.Length / seconds); }
public void CopyFromBitArrayShouldWork(int byteCount) { byte[] someStuff = CryptographicRandomGenerator.GetBuffer(byteCount); var ba = new BitArray(someStuff); var bl = new BitList(ba); for (int i = 0; i < ba.Length; ++i) { Assert.Equal(ba[i], bl[i]); } }
public void TestToReadableStream() { var data = CryptographicRandomGenerator.GetBuffer(3500).AsImmutableArrayDangerous(); using (var dst = new MemoryStream()) { using (var src = data.ToReadableStream()) { src.CopyTo(dst); } Assert.True(data.AsReadOnlySpan().SequenceEqual(dst.ToArray())); } }
public void CopyToShouldWork() { const int ByteCount = 1927; const int BoolCount = ByteCount * 8; const int ArrayOffset = 3871; byte[] someStuff = CryptographicRandomGenerator.GetBuffer(ByteCount); var bl = new BitList(); foreach (byte val in someStuff) { for (int j = 0; j < 8; ++j) { bl.Add((val & (1 << j)) > 0); } } bool[] vals = new bool[BoolCount + ArrayOffset]; ((IList <bool>)bl).CopyTo(vals, ArrayOffset); Assert.Equal(bl, vals.Skip(ArrayOffset)); }
public void ToBitArrayShouldWork() { const int ByteCount = 1927; byte[] someStuff = CryptographicRandomGenerator.GetBuffer(ByteCount); var bl = new BitList(); foreach (byte val in someStuff) { for (int j = 0; j < 8; ++j) { bl.Add((val & (1 << j)) > 0); } } var ba = bl.ToBitArray(); Assert.Equal(bl.Count, ba.Length); for (int i = 0; i < bl.Count; ++i) { Assert.Equal(bl[i], ba[i]); } }