aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/tests/junit/org/apache/tools/bzip2/BlockSortTest.java
blob: 12b871ed635d4727bd204465867bcb3a2835666d (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
164
165
166
167
168
169
170
171
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.tools.bzip2;

import org.junit.Test;

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

public class BlockSortTest {

    private static final byte[] FIXTURE = { 0, 1, (byte) 252, (byte) 253, (byte) 255,
                                            (byte) 254, 3, 2, (byte) 128 };

    /*
      Burrows-Wheeler transform of fixture the manual way:

      * build the matrix

      0, 1, 252, 253, 255, 254, 3, 2, 128
      1, 252, 253, 255, 254, 3, 2, 128, 0
      252, 253, 255, 254, 3, 2, 128, 0, 1
      253, 255, 254, 3, 2, 128, 0, 1, 252
      255, 254, 3, 2, 128, 0, 1, 252, 253
      254, 3, 2, 128, 0, 1, 252, 253, 255
      3, 2, 128, 0, 1, 252, 253, 255, 254
      2, 128, 0, 1, 252, 253, 255, 254, 3
      128, 0, 1, 252, 253, 255, 254, 3, 2

      * sort it

      0, 1, 252, 253, 255, 254, 3, 2, 128
      1, 252, 253, 255, 254, 3, 2, 128, 0
      2, 128, 0, 1, 252, 253, 255, 254, 3
      3, 2, 128, 0, 1, 252, 253, 255, 254
      128, 0, 1, 252, 253, 255, 254, 3, 2
      252, 253, 255, 254, 3, 2, 128, 0, 1
      253, 255, 254, 3, 2, 128, 0, 1, 252
      254, 3, 2, 128, 0, 1, 252, 253, 255
      255, 254, 3, 2, 128, 0, 1, 252, 253

      * grab last column

      128, 0, 3, 254, 2, 1, 252, 255, 253

        and the original line has been 0
    */

    private static final byte[] FIXTURE_BWT = { (byte) 128, 0, 3, (byte) 254, 2, 1, 
                                                (byte) 252, (byte) 255, (byte) 253 };

    private static final int[] FIXTURE_SORTED = {
        0, 1, 7, 6, 8, 2, 3, 5, 4
    };

    private static final byte[] FIXTURE2 = { 
        'C', 'o', 'm', 'm', 'o', 'n', 's', ' ', 'C', 'o', 'm', 'p', 'r', 'e', 's', 's', 
    };

    private static final byte[] FIXTURE2_BWT = {
        's', 's', ' ', 'r', 'o', 'm', 'o', 'o', 'C', 'C', 'm', 'm', 'p', 'n', 's', 'e', 
    };

    @Test
    public void testSortFixture() {
        DS ds = setUpFixture();
        ds.s.blockSort(ds.data, FIXTURE.length - 1);
        assertFixtureSorted(ds.data);
        assertEquals(0, ds.data.origPtr);
    }

    @Test
    public void testSortFixtureMainSort() {
        DS ds = setUpFixture();
        ds.s.mainSort(ds.data, FIXTURE.length - 1);
        assertFixtureSorted(ds.data);
    }

    @Test
    public void testSortFixtureFallbackSort() {
        DS ds = setUpFixture();
        ds.s.fallbackSort(ds.data, FIXTURE.length - 1);
        assertFixtureSorted(ds.data);
    }

    @Test
    public void testSortFixture2() {
        DS ds = setUpFixture2();
        ds.s.blockSort(ds.data, FIXTURE2.length - 1);
        assertFixture2Sorted(ds.data);
        assertEquals(1, ds.data.origPtr);
    }

    @Test
    public void testSortFixture2MainSort() {
        DS ds = setUpFixture2();
        ds.s.mainSort(ds.data, FIXTURE2.length - 1);
        assertFixture2Sorted(ds.data);
    }

    @Test
    public void testSortFixture2FallbackSort() {
        DS ds = setUpFixture2();
        ds.s.fallbackSort(ds.data, FIXTURE2.length - 1);
        assertFixture2Sorted(ds.data);
    }

    @Test
    public void testFallbackSort() {
        CBZip2OutputStream.Data data = new CBZip2OutputStream.Data(1);
        BlockSort s = new BlockSort(data);
        int[] fmap = new int[FIXTURE.length];
        s.fallbackSort(fmap, FIXTURE, FIXTURE.length);
        assertArrayEquals(FIXTURE_SORTED, fmap);
    }

    private DS setUpFixture() {
        return setUpFixture(FIXTURE);
    }

    private void assertFixtureSorted(CBZip2OutputStream.Data data) {
        assertFixtureSorted(data, FIXTURE, FIXTURE_BWT);
    }

    private DS setUpFixture2() {
        return setUpFixture(FIXTURE2);
    }

    private void assertFixture2Sorted(CBZip2OutputStream.Data data) {
        assertFixtureSorted(data, FIXTURE2, FIXTURE2_BWT);
    }

    private DS setUpFixture(byte[] fixture) {
        CBZip2OutputStream.Data data = new CBZip2OutputStream.Data(1);
        System.arraycopy(fixture, 0, data.block, 1, fixture.length);
        return new DS(data, new BlockSort(data));
    }

    private void assertFixtureSorted(CBZip2OutputStream.Data data,
                                     byte[] fixture, byte[] fixtureBwt) {
        assertEquals(fixture[fixture.length - 1], data.block[0]);
        for (int i = 0; i < fixture.length; i++) {
            assertEquals(fixtureBwt[i], data.block[data.fmap[i]]);
        }
    }

    private static class DS {
        private final CBZip2OutputStream.Data data;
        private final BlockSort s;
        DS(CBZip2OutputStream.Data data, BlockSort s) {
            this.data = data;
            this.s = s;
        }
    }
}