|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | + |
| 4 | +using Force.Blazer.Algorithms; |
| 5 | +using Force.Blazer.Algorithms.Sampled; |
| 6 | + |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +namespace Blazer.Net.Tests |
| 10 | +{ |
| 11 | + [TestFixture(1)] |
| 12 | + [TestFixture(2)] |
| 13 | + [TestFixture(3)] |
| 14 | + public class SampledCompressionTests |
| 15 | + { |
| 16 | + private readonly Type _compressorType; |
| 17 | + |
| 18 | + private BaseSampledCompressor GetCompressor() |
| 19 | + { |
| 20 | + return (BaseSampledCompressor)Activator.CreateInstance(_compressorType); |
| 21 | + } |
| 22 | + |
| 23 | + public SampledCompressionTests(int type) |
| 24 | + { |
| 25 | + if (type == 1) _compressorType = typeof(StreamSampledCompressor); |
| 26 | + else if (type == 2) _compressorType = typeof(StreamHighSampledCompressor); |
| 27 | + else if (type == 3) _compressorType = typeof(BlockSampledCompressor); |
| 28 | + else throw new NotImplementedException(); |
| 29 | + } |
| 30 | + |
| 31 | + [Test] |
| 32 | + public void DataWithSample_Should_Be_Correctly_Encoded_Decoded() |
| 33 | + { |
| 34 | + var ssed = GetCompressor(); |
| 35 | + var sample = new byte[100]; |
| 36 | + sample[1] = 42; |
| 37 | + ssed.PrepareSample(sample, 0, sample.Length); |
| 38 | + |
| 39 | + var data1 = new byte[10]; |
| 40 | + data1[0] = 42; |
| 41 | + |
| 42 | + var data2 = new byte[10]; |
| 43 | + data2[0] = 12; |
| 44 | + |
| 45 | + var tmpOut = new byte[ssed.CalculateMaxCompressedBufferLength(10)]; |
| 46 | + var tmpOut2 = new byte[ssed.CalculateMaxCompressedBufferLength(10)]; |
| 47 | + |
| 48 | + var cntSampled = ssed.EncodeWithSample(data1, 0, data1.Length, tmpOut, 0); |
| 49 | + var cntUnsampled = StreamEncoder.CompressData(data1); |
| 50 | + // TODO: uncomment |
| 51 | + Assert.That(cntSampled, Is.LessThan(cntUnsampled.Length)); |
| 52 | + var cntSampledUnpacked = ssed.DecodeWithSample(tmpOut, 0, cntSampled, tmpOut2, 0); |
| 53 | + Assert.That(cntSampledUnpacked, Is.EqualTo(10)); |
| 54 | + CollectionAssert.AreEqual(data1, tmpOut2.Take(cntSampledUnpacked)); |
| 55 | + |
| 56 | + // checking that we can repeat without failure |
| 57 | + cntSampled = ssed.EncodeWithSample(data2, 0, data2.Length, tmpOut, 0); |
| 58 | + cntSampledUnpacked = ssed.DecodeWithSample(tmpOut, 0, cntSampled, tmpOut2, 0); |
| 59 | + Assert.That(cntSampledUnpacked, Is.EqualTo(10)); |
| 60 | + CollectionAssert.AreEqual(data2, tmpOut2.Take(cntSampledUnpacked)); |
| 61 | + } |
| 62 | + |
| 63 | + [Test] |
| 64 | + public void DataWithSample_Should_Be_Correctly_Encoded_Decoded_With_Offsets() |
| 65 | + { |
| 66 | + var ssed = GetCompressor(); |
| 67 | + var sample = new byte[100]; |
| 68 | + sample[1] = 42; |
| 69 | + ssed.PrepareSample(sample, 1, sample.Length - 1); |
| 70 | + |
| 71 | + var data1 = new byte[10]; |
| 72 | + data1[1] = 42; |
| 73 | + |
| 74 | + var data2 = new byte[9]; |
| 75 | + data1[0] = 42; |
| 76 | + |
| 77 | + var tmpOut = new byte[ssed.CalculateMaxCompressedBufferLength(10)]; |
| 78 | + var tmpOut2 = new byte[ssed.CalculateMaxCompressedBufferLength(10)]; |
| 79 | + |
| 80 | + var cntSampled1 = ssed.EncodeWithSample(data1, 1, data1.Length - 1, tmpOut, 1); |
| 81 | + var cntSampled2 = ssed.EncodeWithSample(data2, 0, data2.Length, tmpOut2, 0); |
| 82 | + Assert.That(cntSampled1, Is.EqualTo(cntSampled2)); |
| 83 | + var cntSampledUnpacked = ssed.DecodeWithSample(tmpOut, 1, cntSampled1, tmpOut2, 1); |
| 84 | + Assert.That(cntSampledUnpacked, Is.EqualTo(9)); |
| 85 | + CollectionAssert.AreEqual(data1.Skip(1), tmpOut2.Skip(1).Take(cntSampledUnpacked)); |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public void DataWithSample_Should_Be_Correctly_Encoded_Decoded_With_Simple_Interface() |
| 90 | + { |
| 91 | + var ssed = GetCompressor(); |
| 92 | + var sample = new byte[100]; |
| 93 | + sample[1] = 42; |
| 94 | + ssed.PrepareSample(sample, 1, sample.Length - 1); |
| 95 | + |
| 96 | + var data1 = new byte[10]; |
| 97 | + data1[1] = 42; |
| 98 | + |
| 99 | + var sampled1 = ssed.EncodeWithSample(data1); |
| 100 | + var sampledUnpacked = ssed.DecodeWithSample(sampled1); |
| 101 | + |
| 102 | + CollectionAssert.AreEqual(data1, sampledUnpacked); |
| 103 | + } |
| 104 | + |
| 105 | + [Test] |
| 106 | + public void Zero_Length_Should_not_Cause_Error() |
| 107 | + { |
| 108 | + var ssed = GetCompressor(); |
| 109 | + var sample = new byte[100]; |
| 110 | + sample[1] = 42; |
| 111 | + ssed.PrepareSample(sample, 1, sample.Length - 1); |
| 112 | + |
| 113 | + var data1 = new byte[0]; |
| 114 | + |
| 115 | + var sampled1 = ssed.EncodeWithSample(data1); |
| 116 | + Assert.That(sampled1.Length, Is.EqualTo(1)); |
| 117 | + var sampledUnpacked = ssed.DecodeWithSample(sampled1); |
| 118 | + |
| 119 | + CollectionAssert.AreEqual(data1, sampledUnpacked); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments