aboutsummaryrefslogtreecommitdiff
path: root/java/org/brotli/dec/TransformTest.java
blob: 616ba3515dc914e4c3039439112e8e36bd3e19a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Copyright 2015 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

package org.brotli.dec;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.nio.ByteBuffer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Tests for {@link Transform}.
 */
@RunWith(JUnit4.class)
public class TransformTest {

  private static long crc64(byte[] data) {
    long crc = -1;
    for (int i = 0; i < data.length; ++i) {
      long c = (crc ^ (long) (data[i] & 0xFF)) & 0xFF;
      for (int k = 0; k < 8; k++) {
        c = (c >>> 1) ^ (-(c & 1L) & -3932672073523589310L);
      }
      crc = c ^ (crc >>> 8);
    }
    return ~crc;
  }

  @Test
  public void testTrimAll() {
    byte[] output = new byte[0];
    byte[] input = {119, 111, 114, 100}; // "word"
    Transform.transformDictionaryWord(
        output, 0, ByteBuffer.wrap(input), 0, input.length, 39);
    byte[] expectedOutput = new byte[0];
    assertArrayEquals(expectedOutput, output);
  }

  @Test
  public void testCapitalize() {
    byte[] output = new byte[6];
    byte[] input = {113, -61, -90, -32, -92, -86}; // "qæप"
    Transform.transformDictionaryWord(
      output, 0, ByteBuffer.wrap(input), 0, input.length, 44);
    byte[] expectedOutput = {81, -61, -122, -32, -92, -81}; // "QÆय"
    assertArrayEquals(expectedOutput, output);
  }

  @Test
  public void testAllTransforms() {
    /* This string allows to apply all transforms: head and tail cutting, capitalization and
       turning to upper case; all results will be mutually different. */
    // "o123456789abcdef"
    byte[] testWord = {111, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102};
    byte[] output = new byte[2259];
    int offset = 0;
    for (int i = 0; i < Transform.NUM_TRANSFORMS; ++i) {
      offset += Transform.transformDictionaryWord(
          output, offset, ByteBuffer.wrap(testWord), 0, testWord.length, i);
      output[offset++] = -1;
    }
    assertEquals(output.length, offset);
    assertEquals(8929191060211225186L, crc64(output));
  }
}