|
1 | 1 | using System;
|
2 | 2 | using System.Linq;
|
3 | 3 | using System.Text;
|
| 4 | +using System.Threading; |
4 | 5 | using NUnit.Framework;
|
5 | 6 |
|
6 | 7 | namespace ZstdNet.Tests
|
@@ -184,6 +185,34 @@ public void Compress_canRead_fromArraySegment([Values(false, true)] bool useDict
|
184 | 185 | CollectionAssert.AreEqual(segment, decompressed);
|
185 | 186 | }
|
186 | 187 |
|
| 188 | + [Test] |
| 189 | + public void CompressAndDecompress_workCorrectly_spans([Values(false, true)] bool useDictionary) |
| 190 | + { |
| 191 | + var buffer = GenerateSample(); |
| 192 | + |
| 193 | + var data = new ReadOnlySpan<byte>(buffer, 1, buffer.Length - 1); |
| 194 | + var dict = useDictionary ? BuildDictionary() : null; |
| 195 | + |
| 196 | + Span<byte> compressed = stackalloc byte[Compressor.GetCompressBound(data.Length)]; |
| 197 | + using(var options = new CompressionOptions(dict)) |
| 198 | + using(var compressor = new Compressor(options)) |
| 199 | + { |
| 200 | + var size = compressor.Wrap(data, compressed); |
| 201 | + compressed = compressed.Slice(0, size); |
| 202 | + } |
| 203 | + |
| 204 | + Span<byte> decompressed = stackalloc byte[data.Length + 1]; |
| 205 | + using(var options = new DecompressionOptions(dict)) |
| 206 | + using(var decompressor = new Decompressor(options)) |
| 207 | + { |
| 208 | + var size = decompressor.Unwrap(compressed, decompressed); |
| 209 | + Assert.AreEqual(data.Length, size); |
| 210 | + decompressed = decompressed.Slice(0, size); |
| 211 | + } |
| 212 | + |
| 213 | + CollectionAssert.AreEqual(data.ToArray(), decompressed.ToArray()); |
| 214 | + } |
| 215 | + |
187 | 216 | [Test]
|
188 | 217 | public void Decompress_canRead_fromArraySegment([Values(false, true)] bool useDictionary)
|
189 | 218 | {
|
@@ -364,16 +393,38 @@ public void CompressAndDecompress_workCorrectly_ifDifferentInstancesRunInDiffere
|
364 | 393 | });
|
365 | 394 | }
|
366 | 395 |
|
| 396 | + [Test, Explicit("stress")] |
| 397 | + public void CompressAndDecompress_workCorrectly_stress([Values(false, true)] bool useDictionary) |
| 398 | + { |
| 399 | + long i = 0L; |
| 400 | + var data = GenerateBuffer(65536); |
| 401 | + var dict = useDictionary ? BuildDictionary() : null; |
| 402 | + using(var compressionOptions = new CompressionOptions(dict)) |
| 403 | + using(var decompressionOptions = new DecompressionOptions(dict)) |
| 404 | + Enumerable.Range(0, 10000) |
| 405 | + .AsParallel().WithDegreeOfParallelism(100) |
| 406 | + .ForAll(_ => |
| 407 | + { |
| 408 | + using(var compressor = new Compressor(compressionOptions)) |
| 409 | + using(var decompressor = new Decompressor(decompressionOptions)) |
| 410 | + { |
| 411 | + var decompressed = decompressor.Unwrap(compressor.Wrap(data)); |
| 412 | + if(Interlocked.Increment(ref i) % 100 == 0) |
| 413 | + GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true); |
| 414 | + CollectionAssert.AreEqual(data, decompressed); |
| 415 | + } |
| 416 | + }); |
| 417 | + } |
| 418 | + |
367 | 419 | private static byte[] BuildDictionary()
|
368 | 420 | {
|
369 |
| - return DictBuilder.TrainFromBuffer(Enumerable.Range(0, 5).Select(_ => GenerateSample()).ToArray(), 1024); |
| 421 | + return DictBuilder.TrainFromBuffer(Enumerable.Range(0, 8).Select(_ => GenerateSample()).ToArray(), 1024); |
370 | 422 | }
|
371 | 423 |
|
372 | 424 | private static byte[] GenerateSample()
|
373 | 425 | {
|
374 | 426 | return Enumerable.Range(0, 10)
|
375 |
| - .SelectMany(_ => Encoding.ASCII.GetBytes(string.Format("['a': 'constant_field', 'b': '{0}', 'c': {1}, 'd': '{2} constant field']", |
376 |
| - Random.Next(), Random.Next(), Random.Next(1) == 1 ? "almost" : "sometimes"))) |
| 427 | + .SelectMany(_ => Encoding.ASCII.GetBytes($"['a': 'constant_field', 'b': '{Random.Next()}', 'c': {Random.Next()}, 'd': '{(Random.Next(1) == 1 ? "almost" : "sometimes")} constant field']")) |
377 | 428 | .ToArray();
|
378 | 429 | }
|
379 | 430 |
|
|
0 commit comments