~ubuntu-branches/ubuntu/saucy/db/saucy-proposed

« back to all changes in this revision

Viewing changes to test/scr024/src/com/sleepycat/bind/tuple/test/TupleOrderingTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2010-11-05 15:02:09 UTC
  • mfrom: (13.1.12 sid)
  • Revision ID: james.westby@ubuntu.com-20101105150209-ppvyn0619pu014xo
Tags: 5.1.19-1ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - Pass --build/--host to configure to support cross-building, and don't
    override CC.
  - Disable the Java build when cross-building, for now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 2002, 2010 Oracle and/or its affiliates.  All rights reserved.
5
 
 *
6
 
 * $Id$
7
 
 */
8
 
 
9
 
package com.sleepycat.bind.tuple.test;
10
 
 
11
 
import junit.framework.Test;
12
 
import junit.framework.TestCase;
13
 
import junit.framework.TestSuite;
14
 
 
15
 
import com.sleepycat.bind.tuple.TupleOutput;
16
 
import com.sleepycat.util.test.SharedTestUtils;
17
 
 
18
 
/**
19
 
 * @author Mark Hayes
20
 
 */
21
 
public class TupleOrderingTest extends TestCase {
22
 
 
23
 
    private TupleOutput out;
24
 
    private byte[] prevBuf;
25
 
 
26
 
    public static void main(String[] args) {
27
 
        junit.framework.TestResult tr =
28
 
            junit.textui.TestRunner.run(suite());
29
 
        if (tr.errorCount() > 0 ||
30
 
            tr.failureCount() > 0) {
31
 
            System.exit(1);
32
 
        } else {
33
 
            System.exit(0);
34
 
        }
35
 
    }
36
 
 
37
 
    public static Test suite() {
38
 
        TestSuite suite = new TestSuite(TupleOrderingTest.class);
39
 
        return suite;
40
 
    }
41
 
 
42
 
    public TupleOrderingTest(String name) {
43
 
 
44
 
        super(name);
45
 
    }
46
 
 
47
 
    @Override
48
 
    public void setUp() {
49
 
 
50
 
        SharedTestUtils.printTestName("TupleOrderingTest." + getName());
51
 
        out = new TupleOutput();
52
 
        prevBuf = null;
53
 
    }
54
 
 
55
 
    @Override
56
 
    public void tearDown() {
57
 
 
58
 
        /* Ensure that GC can cleanup. */
59
 
        out = null;
60
 
        prevBuf = null;
61
 
    }
62
 
 
63
 
    /**
64
 
     * Each tuple written must be strictly less than (by comparison of bytes)
65
 
     * the tuple written just before it.  The check() method compares bytes
66
 
     * just written to those written before the previous call to check().
67
 
     */
68
 
    private void check() {
69
 
 
70
 
        check(-1);
71
 
    }
72
 
 
73
 
    private void check(int dataIndex) {
74
 
 
75
 
        byte[] buf = new byte[out.size()];
76
 
        System.arraycopy(out.getBufferBytes(), out.getBufferOffset(),
77
 
                         buf, 0, buf.length);
78
 
        if (prevBuf != null) {
79
 
            int errOffset = -1;
80
 
            int len = Math.min(prevBuf.length,  buf.length);
81
 
            boolean areEqual = true;
82
 
            for (int i = 0; i < len; i += 1) {
83
 
                int val1 = prevBuf[i] & 0xFF;
84
 
                int val2 = buf[i] & 0xFF;
85
 
                if (val1 < val2) {
86
 
                    areEqual = false;
87
 
                    break;
88
 
                } else if (val1 > val2) {
89
 
                    errOffset = i;
90
 
                    break;
91
 
                }
92
 
            }
93
 
            if (areEqual) {
94
 
                if (prevBuf.length < buf.length) {
95
 
                    areEqual = false;
96
 
                } else if (prevBuf.length > buf.length) {
97
 
                    areEqual = false;
98
 
                    errOffset = buf.length + 1;
99
 
                }
100
 
            }
101
 
            if (errOffset != -1 || areEqual) {
102
 
                StringBuffer msg = new StringBuffer();
103
 
                if (errOffset != -1) {
104
 
                    msg.append("Left >= right at byte offset " + errOffset);
105
 
                } else if (areEqual) {
106
 
                    msg.append("Bytes are equal");
107
 
                } else {
108
 
                    throw new IllegalStateException();
109
 
                }
110
 
                msg.append("\nLeft hex bytes: ");
111
 
                for (int i = 0; i < prevBuf.length; i += 1) {
112
 
                    msg.append(' ');
113
 
                    int val = prevBuf[i] & 0xFF;
114
 
                    if ((val & 0xF0) == 0) {
115
 
                        msg.append('0');
116
 
                    }
117
 
                    msg.append(Integer.toHexString(val));
118
 
                }
119
 
                msg.append("\nRight hex bytes:");
120
 
                for (int i = 0; i < buf.length; i += 1) {
121
 
                    msg.append(' ');
122
 
                    int val = buf[i] & 0xFF;
123
 
                    if ((val & 0xF0) == 0) {
124
 
                        msg.append('0');
125
 
                    }
126
 
                    msg.append(Integer.toHexString(val));
127
 
                }
128
 
                if (dataIndex >= 0) {
129
 
                    msg.append("\nData index: " + dataIndex);
130
 
                }
131
 
                fail(msg.toString());
132
 
            }
133
 
        }
134
 
        prevBuf = buf;
135
 
        out.reset();
136
 
    }
137
 
 
138
 
    private void reset() {
139
 
 
140
 
        prevBuf = null;
141
 
        out.reset();
142
 
    }
143
 
 
144
 
    public void testString() {
145
 
 
146
 
        final String[] DATA = {
147
 
            "", "\u0001", "\u0002",
148
 
            "A", "a", "ab", "b", "bb", "bba",
149
 
            "c", "c\u0001", "d",
150
 
            new String(new char[] { 0x7F }),
151
 
            new String(new char[] { 0x7F, 0 }),
152
 
            new String(new char[] { 0xFF }),
153
 
            new String(new char[] { Character.MAX_VALUE }),
154
 
        };
155
 
        for (int i = 0; i < DATA.length; i += 1) {
156
 
            out.writeString(DATA[i]);
157
 
            check(i);
158
 
        }
159
 
        reset();
160
 
        out.writeString("a");
161
 
        check();
162
 
        out.writeString("a");
163
 
        out.writeString("");
164
 
        check();
165
 
        out.writeString("a");
166
 
        out.writeString("");
167
 
        out.writeString("a");
168
 
        check();
169
 
        out.writeString("a");
170
 
        out.writeString("b");
171
 
        check();
172
 
        out.writeString("aa");
173
 
        check();
174
 
        out.writeString("b");
175
 
        check();
176
 
    }
177
 
 
178
 
    public void testFixedString() {
179
 
 
180
 
        final char[][] DATA = {
181
 
            {}, {'a'}, {'a', 'b'}, {'b'}, {'b', 'b'}, {0x7F}, {0xFF},
182
 
        };
183
 
        for (int i = 0; i < DATA.length; i += 1) {
184
 
            out.writeString(DATA[i]);
185
 
            check(i);
186
 
        }
187
 
    }
188
 
 
189
 
    public void testChars() {
190
 
 
191
 
        final char[][] DATA = {
192
 
            {}, {0}, {'a'}, {'a', 0}, {'a', 'b'}, {'b'}, {'b', 'b'},
193
 
            {0x7F}, {0x7F, 0}, {0xFF}, {0xFF, 0},
194
 
        };
195
 
        for (int i = 0; i < DATA.length; i += 1) {
196
 
            out.writeChars(DATA[i]);
197
 
            check(i);
198
 
        }
199
 
    }
200
 
 
201
 
    public void testBytes() {
202
 
 
203
 
        final char[][] DATA = {
204
 
            {}, {0}, {'a'}, {'a', 0}, {'a', 'b'}, {'b'}, {'b', 'b'},
205
 
            {0x7F}, {0xFF},
206
 
        };
207
 
        for (int i = 0; i < DATA.length; i += 1) {
208
 
            out.writeBytes(DATA[i]);
209
 
            check(i);
210
 
        }
211
 
    }
212
 
 
213
 
    public void testBoolean() {
214
 
 
215
 
        final boolean[] DATA = {
216
 
            false, true
217
 
        };
218
 
        for (int i = 0; i < DATA.length; i += 1) {
219
 
            out.writeBoolean(DATA[i]);
220
 
            check(i);
221
 
        }
222
 
    }
223
 
 
224
 
    public void testUnsignedByte() {
225
 
 
226
 
        final int[] DATA = {
227
 
            0, 1, 0x7F, 0xFF
228
 
        };
229
 
        for (int i = 0; i < DATA.length; i += 1) {
230
 
            out.writeUnsignedByte(DATA[i]);
231
 
            check(i);
232
 
        }
233
 
    }
234
 
 
235
 
    public void testUnsignedShort() {
236
 
 
237
 
        final int[] DATA = {
238
 
            0, 1, 0xFE, 0xFF, 0x800, 0x7FFF, 0xFFFF
239
 
        };
240
 
        for (int i = 0; i < DATA.length; i += 1) {
241
 
            out.writeUnsignedShort(DATA[i]);
242
 
            check(i);
243
 
        }
244
 
    }
245
 
 
246
 
    public void testUnsignedInt() {
247
 
 
248
 
        final long[] DATA = {
249
 
            0, 1, 0xFE, 0xFF, 0x800, 0x7FFF, 0xFFFF, 0x80000,
250
 
            0x7FFFFFFF, 0x80000000, 0xFFFFFFFF
251
 
        };
252
 
        for (int i = 0; i < DATA.length; i += 1) {
253
 
            out.writeUnsignedInt(DATA[i]);
254
 
            check(i);
255
 
        }
256
 
    }
257
 
 
258
 
    public void testByte() {
259
 
 
260
 
        final byte[] DATA = {
261
 
            Byte.MIN_VALUE, Byte.MIN_VALUE + 1,
262
 
            -1, 0, 1,
263
 
            Byte.MAX_VALUE - 1, Byte.MAX_VALUE,
264
 
        };
265
 
        for (int i = 0; i < DATA.length; i += 1) {
266
 
            out.writeByte(DATA[i]);
267
 
            check(i);
268
 
        }
269
 
    }
270
 
 
271
 
    public void testShort() {
272
 
 
273
 
        final short[] DATA = {
274
 
            Short.MIN_VALUE, Short.MIN_VALUE + 1,
275
 
            Byte.MIN_VALUE, Byte.MIN_VALUE + 1,
276
 
            -1, 0, 1,
277
 
            Byte.MAX_VALUE - 1, Byte.MAX_VALUE,
278
 
            Short.MAX_VALUE - 1, Short.MAX_VALUE,
279
 
        };
280
 
        for (int i = 0; i < DATA.length; i += 1) {
281
 
            out.writeShort(DATA[i]);
282
 
            check(i);
283
 
        }
284
 
    }
285
 
 
286
 
    public void testInt() {
287
 
 
288
 
        final int[] DATA = {
289
 
            Integer.MIN_VALUE, Integer.MIN_VALUE + 1,
290
 
            Short.MIN_VALUE, Short.MIN_VALUE + 1,
291
 
            Byte.MIN_VALUE, Byte.MIN_VALUE + 1,
292
 
            -1, 0, 1,
293
 
            Byte.MAX_VALUE - 1, Byte.MAX_VALUE,
294
 
            Short.MAX_VALUE - 1, Short.MAX_VALUE,
295
 
            Integer.MAX_VALUE - 1, Integer.MAX_VALUE,
296
 
        };
297
 
        for (int i = 0; i < DATA.length; i += 1) {
298
 
            out.writeInt(DATA[i]);
299
 
            check(i);
300
 
        }
301
 
    }
302
 
 
303
 
    public void testLong() {
304
 
 
305
 
        final long[] DATA = {
306
 
            Long.MIN_VALUE, Long.MIN_VALUE + 1,
307
 
            Integer.MIN_VALUE, Integer.MIN_VALUE + 1,
308
 
            Short.MIN_VALUE, Short.MIN_VALUE + 1,
309
 
            Byte.MIN_VALUE, Byte.MIN_VALUE + 1,
310
 
            -1, 0, 1,
311
 
            Byte.MAX_VALUE - 1, Byte.MAX_VALUE,
312
 
            Short.MAX_VALUE - 1, Short.MAX_VALUE,
313
 
            Integer.MAX_VALUE - 1, Integer.MAX_VALUE,
314
 
            Long.MAX_VALUE - 1, Long.MAX_VALUE,
315
 
        };
316
 
        for (int i = 0; i < DATA.length; i += 1) {
317
 
            out.writeLong(DATA[i]);
318
 
            check(i);
319
 
        }
320
 
    }
321
 
 
322
 
    public void testFloat() {
323
 
 
324
 
        // Only positive floats and doubles are ordered deterministically
325
 
 
326
 
        final float[] DATA = {
327
 
            0, Float.MIN_VALUE, 2 * Float.MIN_VALUE,
328
 
            (float) 0.01, (float) 0.02, (float) 0.99,
329
 
            1, (float) 1.01, (float) 1.02, (float) 1.99,
330
 
            Byte.MAX_VALUE - 1, Byte.MAX_VALUE,
331
 
            Short.MAX_VALUE - 1, Short.MAX_VALUE,
332
 
            Integer.MAX_VALUE,
333
 
            Long.MAX_VALUE / 2, Long.MAX_VALUE,
334
 
            Float.MAX_VALUE,
335
 
            Float.POSITIVE_INFINITY,
336
 
            Float.NaN,
337
 
        };
338
 
        for (int i = 0; i < DATA.length; i += 1) {
339
 
            out.writeFloat(DATA[i]);
340
 
            check(i);
341
 
        }
342
 
    }
343
 
 
344
 
    public void testDouble() {
345
 
 
346
 
        // Only positive floats and doubles are ordered deterministically
347
 
 
348
 
        final double[] DATA = {
349
 
            0, Double.MIN_VALUE, 2 * Double.MIN_VALUE,
350
 
            0.001, 0.002, 0.999,
351
 
            1, 1.001, 1.002, 1.999,
352
 
            Byte.MAX_VALUE - 1, Byte.MAX_VALUE,
353
 
            Short.MAX_VALUE - 1, Short.MAX_VALUE,
354
 
            Integer.MAX_VALUE - 1, Integer.MAX_VALUE,
355
 
            Long.MAX_VALUE / 2, Long.MAX_VALUE,
356
 
            Float.MAX_VALUE, Double.MAX_VALUE,
357
 
            Double.POSITIVE_INFINITY,
358
 
            Double.NaN,
359
 
        };
360
 
        for (int i = 0; i < DATA.length; i += 1) {
361
 
            out.writeDouble(DATA[i]);
362
 
            check(i);
363
 
        }
364
 
    }
365
 
 
366
 
    public void testSortedFloat() {
367
 
 
368
 
        final float[] DATA = {
369
 
            Float.NEGATIVE_INFINITY,
370
 
            (- Float.MAX_VALUE),
371
 
            Long.MIN_VALUE,
372
 
            Long.MIN_VALUE / 2,
373
 
            Integer.MIN_VALUE,
374
 
            Short.MIN_VALUE,
375
 
            Short.MIN_VALUE + 1,
376
 
            Byte.MIN_VALUE,
377
 
            Byte.MIN_VALUE + 1,
378
 
            (float) -1.99,
379
 
            (float) -1.02,
380
 
            (float) -1.01,
381
 
            -1,
382
 
            (float) -0.99,
383
 
            (float) -0.02,
384
 
            (float) -0.01,
385
 
            2 * (- Float.MIN_VALUE),
386
 
            (- Float.MIN_VALUE),
387
 
            0,
388
 
            Float.MIN_VALUE,
389
 
            2 * Float.MIN_VALUE,
390
 
            (float) 0.01,
391
 
            (float) 0.02,
392
 
            (float) 0.99,
393
 
            1,
394
 
            (float) 1.01,
395
 
            (float) 1.02,
396
 
            (float) 1.99,
397
 
            Byte.MAX_VALUE - 1,
398
 
            Byte.MAX_VALUE,
399
 
            Short.MAX_VALUE - 1,
400
 
            Short.MAX_VALUE,
401
 
            Integer.MAX_VALUE,
402
 
            Long.MAX_VALUE / 2,
403
 
            Long.MAX_VALUE,
404
 
            Float.MAX_VALUE,
405
 
            Float.POSITIVE_INFINITY,
406
 
            Float.NaN,
407
 
        };
408
 
        for (int i = 0; i < DATA.length; i += 1) {
409
 
            out.writeSortedFloat(DATA[i]);
410
 
            check(i);
411
 
        }
412
 
    }
413
 
 
414
 
    public void testSortedDouble() {
415
 
 
416
 
        final double[] DATA = {
417
 
            Double.NEGATIVE_INFINITY,
418
 
            (- Double.MAX_VALUE),
419
 
            (- Float.MAX_VALUE),
420
 
            Long.MIN_VALUE,
421
 
            Long.MIN_VALUE / 2,
422
 
            Integer.MIN_VALUE,
423
 
            Short.MIN_VALUE,
424
 
            Short.MIN_VALUE + 1,
425
 
            Byte.MIN_VALUE,
426
 
            Byte.MIN_VALUE + 1,
427
 
            -1.999,
428
 
            -1.002,
429
 
            -1.001,
430
 
            -1,
431
 
            -0.999,
432
 
            -0.002,
433
 
            -0.001,
434
 
            2 * (- Double.MIN_VALUE),
435
 
            (- Double.MIN_VALUE),
436
 
            0,
437
 
            Double.MIN_VALUE,
438
 
            2 * Double.MIN_VALUE,
439
 
            0.001,
440
 
            0.002,
441
 
            0.999,
442
 
            1,
443
 
            1.001,
444
 
            1.002,
445
 
            1.999,
446
 
            Byte.MAX_VALUE - 1,
447
 
            Byte.MAX_VALUE,
448
 
            Short.MAX_VALUE - 1,
449
 
            Short.MAX_VALUE,
450
 
            Integer.MAX_VALUE - 1,
451
 
            Integer.MAX_VALUE,
452
 
            Long.MAX_VALUE / 2,
453
 
            Long.MAX_VALUE,
454
 
            Float.MAX_VALUE,
455
 
            Double.MAX_VALUE,
456
 
            Double.POSITIVE_INFINITY,
457
 
            Double.NaN,
458
 
        };
459
 
        for (int i = 0; i < DATA.length; i += 1) {
460
 
            out.writeSortedDouble(DATA[i]);
461
 
            check(i);
462
 
        }
463
 
    }
464
 
 
465
 
    public void testPackedIntAndLong() {
466
 
        /* Only packed int/long values from 0 to 630 are ordered correctly */
467
 
        for (int i = 0; i <= 630; i += 1) {
468
 
            out.writePackedInt(i);
469
 
            check(i);
470
 
        }
471
 
        reset();
472
 
        for (int i = 0; i <= 630; i += 1) {
473
 
            out.writePackedLong(i);
474
 
            check(i);
475
 
        }
476
 
    }
477
 
}