|
| 1 | +using System; |
| 2 | + |
| 3 | +using Force.Blazer; |
| 4 | +using Force.Blazer.Algorithms; |
| 5 | +using Force.Blazer.Helpers; |
| 6 | +using Force.Blazer.Native; |
| 7 | + |
| 8 | +using NUnit.Framework; |
| 9 | + |
| 10 | +namespace Blazer.Net.Tests |
| 11 | +{ |
| 12 | + [TestFixture] |
| 13 | + public class DataArrayTests |
| 14 | + { |
| 15 | + [Test] |
| 16 | + public void Stream_Encode_Decode_Should_Not_Resize_Array() |
| 17 | + { |
| 18 | + var bufferIn = new byte[] { 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; |
| 19 | + var compr = StreamEncoder.CompressData(bufferIn); |
| 20 | + var bufferOut = new byte[bufferIn.Length]; |
| 21 | + var cnt = StreamDecoder.DecompressBlockExternal(compr, 0, compr.Length, ref bufferOut, 0, bufferOut.Length, false); |
| 22 | + Assert.That(cnt, Is.EqualTo(bufferIn.Length)); |
| 23 | + CollectionAssert.AreEqual(bufferIn, bufferOut); |
| 24 | + } |
| 25 | + |
| 26 | + [Test] |
| 27 | + [TestCase(typeof(BlockEncoder), typeof(BlockDecoder))] |
| 28 | + [TestCase(typeof(BlockEncoderNative), typeof(BlockDecoderNative))] |
| 29 | + public void Block_Encode_Decode_Should_Not_Resize_Array(Type encoderType, Type decoderType) |
| 30 | + { |
| 31 | + // ensuring native is inited |
| 32 | + NativeHelper.SetNativeImplementation(true); |
| 33 | + var bufferIn = new byte[] { 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; |
| 34 | + var encoder = (BlockEncoder)Activator.CreateInstance(encoderType); |
| 35 | + encoder.Init(bufferIn.Length); |
| 36 | + var compr = new byte[bufferIn.Length]; |
| 37 | + var comprCnt = encoder.CompressBlock(bufferIn, 0, bufferIn.Length, compr, 0, true); |
| 38 | + Console.WriteLine(comprCnt); |
| 39 | + |
| 40 | + var bufferOut = new byte[bufferIn.Length]; |
| 41 | + var decoder = (BlockDecoder)Activator.CreateInstance(decoderType); |
| 42 | + decoder.Init(bufferIn.Length); |
| 43 | + var cnt = decoder.DecompressBlock(compr, 0, comprCnt, bufferOut, 0, bufferOut.Length, true); |
| 44 | + Assert.That(cnt, Is.EqualTo(bufferIn.Length)); |
| 45 | + CollectionAssert.AreEqual(bufferIn, bufferOut); |
| 46 | + } |
| 47 | + |
| 48 | + [Test] |
| 49 | + [TestCase(typeof(StreamEncoder), typeof(StreamDecoder))] |
| 50 | + [TestCase(typeof(StreamEncoderNative), typeof(StreamDecoderNative))] |
| 51 | + [TestCase(typeof(StreamEncoderHigh), typeof(StreamDecoder))] |
| 52 | + public void Stream_Encode_Decode_Should_Not_Resize_Array(Type encoderType, Type decoderType) |
| 53 | + { |
| 54 | + // ensuring native is inited |
| 55 | + NativeHelper.SetNativeImplementation(true); |
| 56 | + var bufferIn = new byte[] { 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; |
| 57 | + var encoder = (StreamEncoder)Activator.CreateInstance(encoderType); |
| 58 | + encoder.Init(bufferIn.Length); |
| 59 | + var compr = new byte[bufferIn.Length]; |
| 60 | + var comprCnt = encoder.CompressBlock(bufferIn, 0, bufferIn.Length, 0, compr, 0); |
| 61 | + Console.WriteLine(comprCnt); |
| 62 | + |
| 63 | + var bufferOut = new byte[bufferIn.Length]; |
| 64 | + var decoder = (StreamDecoder)Activator.CreateInstance(decoderType); |
| 65 | + decoder.Init(bufferIn.Length); |
| 66 | + var cnt = decoder.DecompressBlock(compr, 0, comprCnt, bufferOut, 0, bufferOut.Length); |
| 67 | + Assert.That(cnt, Is.EqualTo(bufferIn.Length)); |
| 68 | + CollectionAssert.AreEqual(bufferIn, bufferOut); |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + [TestCase(BlazerAlgorithm.NoCompress)] |
| 73 | + [TestCase(BlazerAlgorithm.Block)] |
| 74 | + [TestCase(BlazerAlgorithm.Stream)] |
| 75 | + public void DataArrayCompressionHelper_Should_Encode_Decode(BlazerAlgorithm algorithm) |
| 76 | + { |
| 77 | + var bufferIn = new byte[] { 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; |
| 78 | + var arr = DataArrayCompressorHelper.CompressDataToArray(bufferIn, EncoderDecoderFactory.GetEncoder(algorithm)); |
| 79 | + var bufferOut = DataArrayCompressorHelper.DecompressDataArray(arr, EncoderDecoderFactory.GetDecoder(algorithm)); |
| 80 | + CollectionAssert.AreEqual(bufferIn, bufferOut); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments