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
7package org.brotli.dec;
8
9import static org.junit.Assert.assertArrayEquals;
10import static org.junit.Assert.assertEquals;
11
12import java.nio.ByteBuffer;
13import org.junit.Test;
14import org.junit.runner.RunWith;
15import org.junit.runners.JUnit4;
16
17/**
18 * Tests for {@link Transform}.
19 */
20@RunWith(JUnit4.class)
21public class TransformTest {
22
23  private static long crc64(byte[] data) {
24    long crc = -1;
25    for (int i = 0; i < data.length; ++i) {
26      long c = (crc ^ (long) (data[i] & 0xFF)) & 0xFF;
27      for (int k = 0; k < 8; k++) {
28        c = (c >>> 1) ^ (-(c & 1L) & -3932672073523589310L);
29      }
30      crc = c ^ (crc >>> 8);
31    }
32    return ~crc;
33  }
34
35  @Test
36  public void testTrimAll() {
37    byte[] output = new byte[0];
38    byte[] input = {119, 111, 114, 100}; // "word"
39    Transform.transformDictionaryWord(
40        output, 0, ByteBuffer.wrap(input), 0, input.length, 39);
41    byte[] expectedOutput = new byte[0];
42    assertArrayEquals(expectedOutput, output);
43  }
44
45  @Test
46  public void testCapitalize() {
47    byte[] output = new byte[6];
48    byte[] input = {113, -61, -90, -32, -92, -86}; // "qæप"
49    Transform.transformDictionaryWord(
50      output, 0, ByteBuffer.wrap(input), 0, input.length, 44);
51    byte[] expectedOutput = {81, -61, -122, -32, -92, -81}; // "QÆय"
52    assertArrayEquals(expectedOutput, output);
53  }
54
55  @Test
56  public void testAllTransforms() {
57    /* This string allows to apply all transforms: head and tail cutting, capitalization and
58       turning to upper case; all results will be mutually different. */
59    // "o123456789abcdef"
60    byte[] testWord = {111, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102};
61    byte[] output = new byte[2259];
62    int offset = 0;
63    for (int i = 0; i < Transform.NUM_TRANSFORMS; ++i) {
64      offset += Transform.transformDictionaryWord(
65          output, offset, ByteBuffer.wrap(testWord), 0, testWord.length, i);
66      output[offset++] = -1;
67    }
68    assertEquals(output.length, offset);
69    assertEquals(8929191060211225186L, crc64(output));
70  }
71}
72