1 /* Copyright 2015 Google Inc. All Rights Reserved.
2 
3 Distributed under MIT license.
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5 */
6 namespace Org.Brotli.Dec
7 {
8 	/// <summary>
9 	/// Tests for
10 	/// <see cref="Transform"/>
11 	/// .
12 	/// </summary>
13 	public class TransformTest
14 	{
Crc64(byte[] data)15 		private static long Crc64(byte[] data)
16 		{
17 			long crc = -1;
18 			for (int i = 0; i < data.Length; ++i)
19 			{
20 				long c = (crc ^ (long)(data[i] & unchecked((int)(0xFF)))) & unchecked((int)(0xFF));
21 				for (int k = 0; k < 8; k++)
22 				{
23 					c = ((long)(((ulong)c) >> 1)) ^ (-(c & 1L) & -3932672073523589310L);
24 				}
25 				crc = c ^ ((long)(((ulong)crc) >> 8));
26 			}
27 			return ~crc;
28 		}
29 
30 		[NUnit.Framework.Test]
TestTrimAll()31 		public virtual void TestTrimAll()
32 		{
33 			byte[] output = new byte[2];
34 			byte[] input = new byte[] { 119, 111, 114, 100 };
35 			// "word"
36 			Org.Brotli.Dec.Transform transform = new Org.Brotli.Dec.Transform("[", Org.Brotli.Dec.WordTransformType.OmitFirst5, "]");
37 			Org.Brotli.Dec.Transform.TransformDictionaryWord(output, 0, input, 0, input.Length, transform);
38 			byte[] expectedOutput = new byte[] { 91, 93 };
39 			// "[]"
40 			NUnit.Framework.Assert.AreEqual(expectedOutput, output);
41 		}
42 
43 		[NUnit.Framework.Test]
TestCapitalize()44 		public virtual void TestCapitalize()
45 		{
46 			byte[] output = new byte[8];
47 			byte[] input = new byte[] { 113, unchecked((byte)(-61)), unchecked((byte)(-90)), unchecked((byte)(-32)), unchecked((byte)(-92)), unchecked((byte)(-86)) };
48 			// "qæप"
49 			Org.Brotli.Dec.Transform transform = new Org.Brotli.Dec.Transform("[", Org.Brotli.Dec.WordTransformType.UppercaseAll, "]");
50 			Org.Brotli.Dec.Transform.TransformDictionaryWord(output, 0, input, 0, input.Length, transform);
51 			byte[] expectedOutput = new byte[] { 91, 81, unchecked((byte)(-61)), unchecked((byte)(-122)), unchecked((byte)(-32)), unchecked((byte)(-92)), unchecked((byte)(-81)), 93 };
52 			// "[QÆय]"
53 			NUnit.Framework.Assert.AreEqual(expectedOutput, output);
54 		}
55 
56 		[NUnit.Framework.Test]
TestAllTransforms()57 		public virtual void TestAllTransforms()
58 		{
59 			/* This string allows to apply all transforms: head and tail cutting, capitalization and
60 			turning to upper case; all results will be mutually different. */
61 			// "o123456789abcdef"
62 			byte[] testWord = new byte[] { 111, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 };
63 			byte[] output = new byte[2259];
64 			int offset = 0;
65 			for (int i = 0; i < Org.Brotli.Dec.Transform.Transforms.Length; ++i)
66 			{
67 				offset += Org.Brotli.Dec.Transform.TransformDictionaryWord(output, offset, testWord, 0, testWord.Length, Org.Brotli.Dec.Transform.Transforms[i]);
68 				output[offset++] = unchecked((byte)(-1));
69 			}
70 			NUnit.Framework.Assert.AreEqual(output.Length, offset);
71 			NUnit.Framework.Assert.AreEqual(8929191060211225186L, Crc64(output));
72 		}
73 	}
74 }
75