aboutsummaryrefslogtreecommitdiff
path: root/generator/src/test/java/com/google/archivepatcher/generator/bsdiff/SuffixSorterTestBase.java
blob: cb72d8ff6afb44e73473af9e3a6241d0111fa2bf (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.archivepatcher.generator.bsdiff;

import static org.junit.Assert.fail;

import java.io.IOException;
import java.util.Random;
import org.junit.Assert;
import org.junit.Test;

/**
 * Base class for suffix sorter tests with common tests for a suffix sorter algorithm.
 */
public abstract class SuffixSorterTestBase {

  public abstract SuffixSorter getSuffixSorter();

  @Test
  public void suffixSortEmptyDataTest() throws Exception {
    checkSuffixSort( new int[] {0}, new byte[] {});
  }

  @Test
  public void suffixSortShortDataTest() throws Exception {
    checkSuffixSort(new int[] {1, 0}, new byte[] {23});
    checkSuffixSort(new int[] {2, 1, 0}, new byte[] {23, 20});
    checkSuffixSort(new int[] {2, 0, 1}, new byte[] {0, 127});
    checkSuffixSort(new int[] {2, 1, 0}, new byte[] {42, 42});
  }

  private void checkSuffixSort(int[] expectedSuffixArray, byte[] inputBytes) throws Exception {
    RandomAccessObject input = new RandomAccessObject.RandomAccessByteArrayObject(inputBytes);
    RandomAccessObject groupArray = getSuffixSorter().suffixSort(input);

    assertSorted(groupArray, input);
    Assert.assertArrayEquals(expectedSuffixArray, randomAccessObjectToIntArray(groupArray));
  }

  @Test
  public void suffixSortLongDataTest() throws Exception {
    RandomAccessObject groupArrayRO = getSuffixSorter().suffixSort(BsDiffTestData.LONG_DATA_99_RO);

    assertSorted(groupArrayRO, BsDiffTestData.LONG_DATA_99_RO);

    Assert.assertArrayEquals(
        BsDiffTestData.QUICK_SUFFIX_SORT_TEST_GA_CONTROL,
        randomAccessObjectToIntArray(groupArrayRO));
  }

  @Test
  public void suffixSortVeryLongDataTest() throws Exception {
    RandomAccessObject groupArray2RO =
        getSuffixSorter().suffixSort(BsDiffTestData.LONGER_DATA_349_RO);

    assertSorted(groupArray2RO, BsDiffTestData.LONGER_DATA_349_RO);

    Assert.assertArrayEquals(
        BsDiffTestData.QUICK_SUFFIX_SORT_TEST_IA_CONTROL,
        randomAccessObjectToIntArray(groupArray2RO));
  }

  @Test
  public void testRandom() throws Exception {
    Random rand = new Random(1123458);
    for (int i = 1; i <= 10; i++) {
      RandomAccessObject input = generateRandom(rand, i * 10000);
      RandomAccessObject suffixArray = getSuffixSorter().suffixSort(input);

      assertSorted(suffixArray, input);
    }
  }

  private static RandomAccessObject generateRandom(Random rand, int length) {
    byte[] bytes = new byte[length];
    rand.nextBytes(bytes);
    return new RandomAccessObject.RandomAccessByteArrayObject(bytes);
  }

  protected static RandomAccessObject intArrayToRandomAccessObject(final int[] array)
      throws Exception {
    RandomAccessObject ret =
        new RandomAccessObject.RandomAccessByteArrayObject(new byte[array.length * 4]);
    ret.seekToIntAligned(0);

    for (int element : array) {
      ret.writeInt(element);
    }

    return ret;
  }

  protected static boolean intArrayEqualsRandomAccessObject(
      int[] array, RandomAccessObject randomAccessObject) throws Exception {
    randomAccessObject.seekToIntAligned(0);

    for (int element : array) {
      if (element != randomAccessObject.readInt()) {
        return false;
      }
    }

    return true;
  }

  protected static int[] randomAccessObjectToIntArray(RandomAccessObject randomAccessObject)
      throws Exception {
    int[] ret = new int[(int) (randomAccessObject.length() / 4)];
    randomAccessObject.seekToIntAligned(0);

    for (int i = 0; i < ret.length; i++) {
      ret[i] = randomAccessObject.readInt();
    }

    return ret;
  }

  private static boolean checkSuffixLessThanOrEqual(
      RandomAccessObject input, int index1, int index2) throws Exception {
    while (true) {
      if (index1 == input.length()) {
        return true;
      }
      input.seek(index1);
      int unsignedByte1 = input.readUnsignedByte();
      input.seek(index2);
      int unsignedByte2 = input.readUnsignedByte();
      if (unsignedByte1 < unsignedByte2) {
        return true;
      }
      if (unsignedByte1 > unsignedByte2) {
        return false;
      }
      index1++;
      index2++;
    }
  }

  private static void assertSorted(RandomAccessObject suffixArray, RandomAccessObject input)
      throws Exception {
    for (int i = 0; i < input.length(); i++) {
      suffixArray.seekToIntAligned(i);
      int index1 = suffixArray.readInt();
      suffixArray.seekToIntAligned(i+1);
      int index2 = suffixArray.readInt();
      if (!checkSuffixLessThanOrEqual(input, index1, index2)) {
        fail();
      }
    }
  }
}