File: CompressedNumberTest.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (229 lines) | stat: -rw-r--r-- 7,619 bytes parent folder | download | duplicates (4)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*

   Derby - Class org.apache.derbyTesting.unitTests.junit.CompressedNumberTest

   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.derbyTesting.unitTests.junit;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.derby.iapi.services.io.ArrayInputStream;
import org.apache.derby.iapi.services.io.ArrayOutputStream;
import org.apache.derby.iapi.services.io.CompressedNumber;

import junit.framework.TestCase;

/**
 * Test case for CompressedNumber.
 */
public class CompressedNumberTest extends TestCase {
    private static byte[] holder = new byte[8];
    private static  ArrayOutputStream aos = new ArrayOutputStream(holder);
    private static  DataOutput out = new DataOutputStream(aos);

    private static  ArrayInputStream ais = new ArrayInputStream(holder);
    private static  DataInput in = new DataInputStream(ais);
    private static  InputStream in_stream = ais;

    /**
     * Public constructor required for running test as stand alone JUnit.
     *
     * @param name
     *            name to present this test case.
     */
    public CompressedNumberTest(String name) {
        super(name);
    }

    public void testLong() throws IOException{
        long[] dataToTest = {0, 1, 2,
                0x3fff - 2, 0x3fff - 1,
                0x3fff,
                0x3fff + 1, 0x3fff + 2,
                0x3fffffff - 4, 0x3fffffff - 3, 0x3fffffff - 2, 0x3fffffff - 1,
                0x3fffffff,
                0x3fffffff + 1, 0x3fffffff + 2, 0x3fffffff + 3, 0x3fffffff + 4,
                0x70000000 - 2, 0x70000000 - 1,
                0x70000000,
                0x70000000 + 1, 0x70000000 + 2,
                Long.MAX_VALUE - 2, Long.MAX_VALUE - 1,
                Long.MAX_VALUE,
                };
        int[] length = {2, 2, 2,
                2, 2,
                2,
                4, 4,
                4, 4, 4, 4,
                4,
                8, 8, 8, 8,
                8, 8,
                8,
                8, 8,
                8, 8,
                8,
        };

        for(int i = 0; i < dataToTest.length; i++){
            checkLong(dataToTest[i], length[i]);
        }
    }

    public void testLongWidely() throws IOException{
        for (long l = 0; l < 0xf0000; l++){
            if(l <= 0x3fff)
                checkLong(l, 2);
            else if(l <= 0x3fffffff)
                checkLong(l, 4);
            else
                checkLong(l, 8);
        }
    }

    public void testInt() throws IOException{
        int[] dataToTest = {0, 1, 2,
                0x3f - 4, 0x3f - 3, 0x3f - 2, 0x3f - 1,
                0x3f,
                0x3f + 1, 0x3f + 2, 0x3f + 3, 0x3f + 4,
                0x3f80 - 4, 0x3f80 - 3, 0x3f80 - 2, 0x3f80 - 1,
                0x3f80,
                0x3f80 + 1, 0x3f80 + 2, 0x3f80 + 3, 0x3f80 + 4,
                0x3fff - 4, 0x3fff - 3, 0x3fff - 2, 0x3fff - 1,
                0x3fff,
                0x3fff + 1, 0x3fff + 2, 0x3fff + 3, 0x3fff + 4,
                Integer.MAX_VALUE - 4, Integer.MAX_VALUE - 3,
                Integer.MAX_VALUE - 2, Integer.MAX_VALUE - 1,
                Integer.MAX_VALUE,
                };
        int[] length = { 1, 1, 1,
                1, 1, 1, 1,
                1,
                2, 2, 2, 2,
                2, 2, 2, 2,
                2,
                2, 2, 2, 2,
                2, 2, 2, 2,
                2,
                4, 4, 4, 4,
                4, 4,
                4, 4,
                4,
        };

        for(int i = 0; i < dataToTest.length; i++){
            checkInt(dataToTest[i], length[i]);
        }
    }

    public void testIntWidely() throws IOException{
        for (int i = 0; i < 0xf0000; i++){
            if(i <= 0x3f)
                checkInt(i, 1);
            else if(i <= 0x3fff)
                checkInt(i, 2);
            else
                checkInt(i, 4);
        }

         //takes 30 minutes to run.
//         for (int i = 0; i < Integer.MAX_VALUE; i++) {
//             if(i <= 0x3f)
//                 checkInt(i, 1);
//             else if(i <= 0x3fff)
//                 checkInt(i, 2);
//             else
//                 checkInt(i, 4);
//         }
    }

    /**
     * Check whether CompressedNumber can work well on integer passed.
     *
     * @param i
     *            the integer to be checked.
     * @param expectedLength
     *            the length expected of i after compressed.
     * @throws IOException
     */
    private void checkInt(int i, int expectedLength) throws IOException {
        aos.setPosition(0);
        int length = CompressedNumber.writeInt(out, i);
        assertEquals("Invalid length after compressed", expectedLength, length);

        assertEquals("MISMATCH written bytes", length, aos.getPosition());

        assertEquals("MISMATCH sizeInt() bytes", length,
                     CompressedNumber.sizeInt(i));

        ais.setPosition(0);
        assertEquals("MISMATCH value readInt(DataInput)", i,
                     CompressedNumber.readInt(in));

        ais.setPosition(0);
        assertEquals("MISMATCH value readInt(DataInput)", i,
                     ais.readCompressedInt());

        ais.setPosition(0);
        assertEquals("MISMATCH value in readInt(InputStream)", i,
                     CompressedNumber.readInt(in_stream));

        assertEquals("MISMATCH frome readInt(byte[], offset)", i,
                     CompressedNumber.readInt(holder, 0));
    }


    /**
     * Check whether CompressedNumber can work well on long number passed.
     *
     * @param l
     *            the long number to be checked.
     * @param expectedLength
     *            the length expected of l after compressed.
     * @throws IOException
     */
    private void checkLong(long l, int expectedLength) throws IOException {
        aos.setPosition(0);
        int length = CompressedNumber.writeLong(out, l);
        assertEquals("Invalid length after compressed", expectedLength, length);

        assertEquals("MISMATCH written bytes", length, aos.getPosition());

        assertEquals("MISMATCH sizeLong() bytes", length,
                     CompressedNumber.sizeLong(l));

        assertEquals("MISMATCH in readLong(byte[], offset) value", l,
                     CompressedNumber.readLong(holder, 0));

        ais.setPosition(0);
        assertEquals("MISMATCH value in readLong(InputStream)", l,
                     CompressedNumber.readLong(in_stream));

        ais.setPosition(0);
        assertEquals("MISMATCH value in readLong(InputStream)", l, ais
                .readCompressedLong());

        ais.setPosition(0);
        assertEquals("MISMATCH value in readLong(DataInput)", l,
                     CompressedNumber.readLong(in));
    }
}