1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util;
19
20 import static org.junit.Assert.assertArrayEquals;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.fail;
23
24 import java.math.BigDecimal;
25 import java.util.Arrays;
26 import java.util.Collections;
27
28 import org.apache.hadoop.hbase.testclassification.SmallTests;
29 import org.junit.Test;
30 import org.junit.experimental.categories.Category;
31
32 @Category(SmallTests.class)
33 public class TestOrderedBytes {
34
35
36 static final Long[] I_VALS =
37 { 0L, 1L, 10L, 99L, 100L, 1234L, 9999L, 10000L, 10001L, 12345L, 123450L, Long.MAX_VALUE,
38 -1L, -10L, -99L, -100L, -123L, -999L, -10000L, -10001L, -12345L, -123450L, Long.MIN_VALUE };
39 static final int[] I_LENGTHS =
40 { 1, 2, 2, 2, 2, 3, 3, 2, 4, 4, 4, 11, 2, 2, 2, 2, 3, 3, 2, 4, 4, 4, 11 };
41
42
43 static final Double[] D_VALS =
44 { 0.0, 0.00123, 0.0123, 0.123, 1.0, 10.0, 12.345, 99.0, 99.01, 99.0001, 100.0, 100.01,
45 100.1, 1234.0, 1234.5, 9999.0, 9999.000001, 9999.000009, 9999.00001, 9999.00009,
46 9999.000099, 9999.0001, 9999.001, 9999.01, 9999.1, 10000.0, 10001.0, 12345.0, 123450.0,
47 Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NaN, Double.MAX_VALUE,
48 -0.00123, -0.0123, -0.123, -1.0, -10.0, -12.345, -99.0, -99.01, -99.0001, -100.0, -100.01,
49 -100.1, -1234.0, -1234.5, -9999.0, -9999.000001, -9999.000009, -9999.00001, -9999.00009,
50 -9999.000099, -9999.0001, -9999.001, -9999.01, -9999.1, -10000.0, -10001.0, -12345.0,
51 -123450.0 };
52 static final int[] D_LENGTHS =
53 { 1, 4, 4, 4, 2, 2, 4, 2, 3, 4, 2, 4,
54 4, 3, 4, 3, 6, 6, 6, 6,
55 6, 5, 5, 4, 4, 2, 4, 4, 4,
56 1, 1, 1, 11,
57 4, 4, 4, 2, 2, 4, 2, 3, 4, 2, 4,
58 4, 3, 4, 3, 6, 6, 6, 6,
59 6, 5, 5, 4, 4, 2, 4, 4,
60 4 };
61
62
63 static final BigDecimal[] BD_VALS =
64 { null, BigDecimal.valueOf(Long.MAX_VALUE), BigDecimal.valueOf(Long.MIN_VALUE),
65 BigDecimal.valueOf(Double.MAX_VALUE), BigDecimal.valueOf(Double.MIN_VALUE),
66 BigDecimal.valueOf(Long.MAX_VALUE).multiply(BigDecimal.valueOf(100)) };
67 static final int[] BD_LENGTHS =
68 { 1, 11, 11, 11, 4, 12 };
69
70
71
72
73 static final double MIN_EPSILON = 0.000001;
74
75
76
77
78 @Test
79 public void testVerifyTestIntegrity() {
80 for (int i = 0; i < I_VALS.length; i++) {
81 for (int d = 0; d < D_VALS.length; d++) {
82 if (Math.abs(I_VALS[i] - D_VALS[d]) < MIN_EPSILON) {
83 assertEquals(
84 "Test inconsistency detected: expected lengths for " + I_VALS[i] + " do not match.",
85 I_LENGTHS[i], D_LENGTHS[d]);
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 @Test
111 public void testVaruint64Boundaries() {
112 long vals[] =
113 { 239L, 240L, 2286L, 2287L, 67822L, 67823L, 16777214L, 16777215L, 4294967294L, 4294967295L,
114 1099511627774L, 1099511627775L, 281474976710654L, 281474976710655L, 72057594037927934L,
115 72057594037927935L, Long.MAX_VALUE - 1, Long.MAX_VALUE, Long.MIN_VALUE + 1,
116 Long.MIN_VALUE, -2L, -1L };
117 int lens[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9, 9, 9 };
118 assertEquals("Broken test!", vals.length, lens.length);
119
120
121
122
123
124 for (boolean comp : new boolean[] { true, false }) {
125 for (int i = 0; i < vals.length; i++) {
126
127 byte[] a = new byte[lens[i] + 2];
128 PositionedByteRange buf = new SimplePositionedMutableByteRange(a, 1, lens[i]);
129
130
131 assertEquals("Surprising return value.",
132 lens[i], OrderedBytes.putVaruint64(buf, vals[i], comp));
133 assertEquals("Surprising serialized length.", lens[i], buf.getPosition());
134 assertEquals("Buffer underflow.", 0, a[0]);
135 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
136
137
138 buf.setPosition(0);
139 assertEquals("Surprising return value.",
140 lens[i], OrderedBytes.skipVaruint64(buf, comp));
141 assertEquals("Did not skip enough bytes.", lens[i], buf.getPosition());
142
143
144 buf.setPosition(0);
145 assertEquals("Deserialization failed.", vals[i], OrderedBytes.getVaruint64(buf, comp));
146 assertEquals("Did not consume enough bytes.", lens[i], buf.getPosition());
147 }
148 }
149 }
150
151
152
153
154
155 @Test
156 public void testNumericInt() {
157
158
159
160
161 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
162 for (int i = 0; i < I_VALS.length; i++) {
163
164 byte[] a = new byte[I_LENGTHS[i] + 3];
165 PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, I_LENGTHS[i] + 1);
166 buf1.setPosition(1);
167
168
169 assertEquals("Surprising return value.",
170 I_LENGTHS[i], OrderedBytes.encodeNumeric(buf1, I_VALS[i], ord));
171 assertEquals("Broken test: serialization did not consume entire buffer.",
172 buf1.getLength(), buf1.getPosition());
173 assertEquals("Surprising serialized length.", I_LENGTHS[i], buf1.getPosition() - 1);
174 assertEquals("Buffer underflow.", 0, a[0]);
175 assertEquals("Buffer underflow.", 0, a[1]);
176 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
177
178
179 buf1.setPosition(1);
180 assertEquals("Surprising return value.", I_LENGTHS[i], OrderedBytes.skip(buf1));
181 assertEquals("Did not skip enough bytes.", I_LENGTHS[i], buf1.getPosition() - 1);
182
183
184 buf1.setPosition(1);
185 assertEquals("Deserialization failed.",
186 I_VALS[i].longValue(), OrderedBytes.decodeNumericAsLong(buf1));
187 assertEquals("Did not consume enough bytes.", I_LENGTHS[i], buf1.getPosition() - 1);
188 }
189 }
190
191
192
193
194 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
195 byte[][] encoded = new byte[I_VALS.length][];
196 PositionedByteRange pbr = new SimplePositionedMutableByteRange();
197 for (int i = 0; i < I_VALS.length; i++) {
198 encoded[i] = new byte[I_LENGTHS[i]];
199 OrderedBytes.encodeNumeric(pbr.set(encoded[i]), I_VALS[i], ord);
200 }
201
202 Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
203 Long[] sortedVals = Arrays.copyOf(I_VALS, I_VALS.length);
204 if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
205 else Arrays.sort(sortedVals, Collections.reverseOrder());
206
207 for (int i = 0; i < sortedVals.length; i++) {
208 pbr.set(encoded[i]);
209 long decoded = OrderedBytes.decodeNumericAsLong(pbr);
210 assertEquals(
211 String.format(
212 "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
213 sortedVals[i], decoded, ord),
214 sortedVals[i].longValue(), decoded);
215 }
216 }
217 }
218
219
220
221
222 @Test
223 public void testNumericReal() {
224
225
226
227
228 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
229 for (int i = 0; i < D_VALS.length; i++) {
230
231 byte[] a = new byte[D_LENGTHS[i] + 3];
232 PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, D_LENGTHS[i] + 1);
233 buf1.setPosition(1);
234
235
236 assertEquals("Surprising return value.",
237 D_LENGTHS[i], OrderedBytes.encodeNumeric(buf1, D_VALS[i], ord));
238 assertEquals("Broken test: serialization did not consume entire buffer.",
239 buf1.getLength(), buf1.getPosition());
240 assertEquals("Surprising serialized length.", D_LENGTHS[i], buf1.getPosition() - 1);
241 assertEquals("Buffer underflow.", 0, a[0]);
242 assertEquals("Buffer underflow.", 0, a[1]);
243 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
244
245
246 buf1.setPosition(1);
247 assertEquals("Surprising return value.", D_LENGTHS[i], OrderedBytes.skip(buf1));
248 assertEquals("Did not skip enough bytes.", D_LENGTHS[i], buf1.getPosition() - 1);
249
250
251 buf1.setPosition(1);
252 assertEquals("Deserialization failed.",
253 D_VALS[i].doubleValue(), OrderedBytes.decodeNumericAsDouble(buf1), MIN_EPSILON);
254 assertEquals("Did not consume enough bytes.", D_LENGTHS[i], buf1.getPosition() - 1);
255 }
256 }
257
258
259
260
261 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
262 byte[][] encoded = new byte[D_VALS.length][];
263 PositionedByteRange pbr = new SimplePositionedMutableByteRange();
264 for (int i = 0; i < D_VALS.length; i++) {
265 encoded[i] = new byte[D_LENGTHS[i]];
266 OrderedBytes.encodeNumeric(pbr.set(encoded[i]), D_VALS[i], ord);
267 }
268
269 Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
270 Double[] sortedVals = Arrays.copyOf(D_VALS, D_VALS.length);
271 if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
272 else Arrays.sort(sortedVals, Collections.reverseOrder());
273
274 for (int i = 0; i < sortedVals.length; i++) {
275 pbr.set(encoded[i]);
276 double decoded = OrderedBytes.decodeNumericAsDouble(pbr);
277 assertEquals(
278 String.format(
279 "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
280 sortedVals[i], decoded, ord),
281 sortedVals[i].doubleValue(), decoded, MIN_EPSILON);
282 }
283 }
284 }
285
286
287
288
289 @Test
290 public void testNumericOther() {
291
292
293
294
295 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
296 for (int i = 0; i < BD_VALS.length; i++) {
297
298 byte[] a = new byte[BD_LENGTHS[i] + 3];
299 PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, BD_LENGTHS[i] + 1);
300 buf1.setPosition(1);
301
302
303 assertEquals("Surprising return value.",
304 BD_LENGTHS[i], OrderedBytes.encodeNumeric(buf1, BD_VALS[i], ord));
305 assertEquals("Broken test: serialization did not consume entire buffer.",
306 buf1.getLength(), buf1.getPosition());
307 assertEquals("Surprising serialized length.", BD_LENGTHS[i], buf1.getPosition() - 1);
308 assertEquals("Buffer underflow.", 0, a[0]);
309 assertEquals("Buffer underflow.", 0, a[1]);
310 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
311
312
313 buf1.setPosition(1);
314 assertEquals("Surprising return value.", BD_LENGTHS[i], OrderedBytes.skip(buf1));
315 assertEquals("Did not skip enough bytes.", BD_LENGTHS[i], buf1.getPosition() - 1);
316
317
318 buf1.setPosition(1);
319 BigDecimal decoded = OrderedBytes.decodeNumericAsBigDecimal(buf1);
320 if (null == BD_VALS[i]) {
321 assertEquals(BD_VALS[i], decoded);
322 } else {
323 assertEquals("Deserialization failed.", 0, BD_VALS[i].compareTo(decoded));
324 }
325 assertEquals("Did not consume enough bytes.", BD_LENGTHS[i], buf1.getPosition() - 1);
326 }
327 }
328 }
329
330
331
332
333 @Test
334 public void testNumericIntRealCompatibility() {
335 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
336 for (int i = 0; i < I_VALS.length; i++) {
337
338 PositionedByteRange pbri = new SimplePositionedMutableByteRange(I_LENGTHS[i]);
339 PositionedByteRange pbrr = new SimplePositionedMutableByteRange(I_LENGTHS[i]);
340 OrderedBytes.encodeNumeric(pbri, I_VALS[i], ord);
341 OrderedBytes.encodeNumeric(pbrr, I_VALS[i], ord);
342 assertArrayEquals("Integer and real encodings differ.", pbri.getBytes(), pbrr.getBytes());
343 pbri.setPosition(0);
344 pbrr.setPosition(0);
345 assertEquals((long) I_VALS[i], OrderedBytes.decodeNumericAsLong(pbri));
346 assertEquals((long) I_VALS[i], (long) OrderedBytes.decodeNumericAsDouble(pbrr));
347
348
349 BigDecimal bd = BigDecimal.valueOf(I_VALS[i]);
350 PositionedByteRange pbrbd = new SimplePositionedMutableByteRange(I_LENGTHS[i]);
351 OrderedBytes.encodeNumeric(pbrbd, bd, ord);
352 assertArrayEquals("Integer and BigDecimal encodings differ.",
353 pbri.getBytes(), pbrbd.getBytes());
354 pbri.setPosition(0);
355 assertEquals("Value not preserved when decoding as Long",
356 0, bd.compareTo(BigDecimal.valueOf(OrderedBytes.decodeNumericAsLong(pbri))));
357 }
358 }
359 }
360
361
362
363
364 @Test
365 public void testInt8() {
366 Byte[] vals =
367 { Byte.MIN_VALUE, Byte.MIN_VALUE / 2, 0, Byte.MAX_VALUE / 2, Byte.MAX_VALUE };
368
369
370
371
372
373 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
374 for (int i = 0; i < vals.length; i++) {
375
376 byte[] a = new byte[2 + 3];
377 PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 2 + 1);
378 buf1.setPosition(1);
379
380
381 assertEquals("Surprising return value.",
382 2, OrderedBytes.encodeInt8(buf1, vals[i], ord));
383 assertEquals("Broken test: serialization did not consume entire buffer.",
384 buf1.getLength(), buf1.getPosition());
385 assertEquals("Surprising serialized length.", 2, buf1.getPosition() - 1);
386 assertEquals("Buffer underflow.", 0, a[0]);
387 assertEquals("Buffer underflow.", 0, a[1]);
388 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
389
390
391 buf1.setPosition(1);
392 assertEquals("Surprising return value.", 2, OrderedBytes.skip(buf1));
393 assertEquals("Did not skip enough bytes.", 2, buf1.getPosition() - 1);
394
395
396 buf1.setPosition(1);
397 assertEquals("Deserialization failed.",
398 vals[i].byteValue(), OrderedBytes.decodeInt8(buf1));
399 assertEquals("Did not consume enough bytes.", 2, buf1.getPosition() - 1);
400 }
401 }
402
403
404
405
406 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
407 byte[][] encoded = new byte[vals.length][2];
408 PositionedByteRange pbr = new SimplePositionedMutableByteRange();
409 for (int i = 0; i < vals.length; i++) {
410 OrderedBytes.encodeInt8(pbr.set(encoded[i]), vals[i], ord);
411 }
412
413 Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
414 Byte[] sortedVals = Arrays.copyOf(vals, vals.length);
415 if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
416 else Arrays.sort(sortedVals, Collections.reverseOrder());
417
418 for (int i = 0; i < sortedVals.length; i++) {
419 int decoded = OrderedBytes.decodeInt8(pbr.set(encoded[i]));
420 assertEquals(
421 String.format(
422 "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
423 sortedVals[i], decoded, ord),
424 sortedVals[i].byteValue(), decoded);
425 }
426 }
427 }
428
429
430
431
432 @Test
433 public void testInt16() {
434 Short[] vals =
435 { Short.MIN_VALUE, Short.MIN_VALUE / 2, 0, Short.MAX_VALUE / 2, Short.MAX_VALUE };
436
437
438
439
440
441 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
442 for (int i = 0; i < vals.length; i++) {
443
444 byte[] a = new byte[3 + 3];
445 PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 3 + 1);
446 buf1.setPosition(1);
447
448
449 assertEquals("Surprising return value.",
450 3, OrderedBytes.encodeInt16(buf1, vals[i], ord));
451 assertEquals("Broken test: serialization did not consume entire buffer.",
452 buf1.getLength(), buf1.getPosition());
453 assertEquals("Surprising serialized length.", 3, buf1.getPosition() - 1);
454 assertEquals("Buffer underflow.", 0, a[0]);
455 assertEquals("Buffer underflow.", 0, a[1]);
456 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
457
458
459 buf1.setPosition(1);
460 assertEquals("Surprising return value.", 3, OrderedBytes.skip(buf1));
461 assertEquals("Did not skip enough bytes.", 3, buf1.getPosition() - 1);
462
463
464 buf1.setPosition(1);
465 assertEquals("Deserialization failed.",
466 vals[i].shortValue(), OrderedBytes.decodeInt16(buf1));
467 assertEquals("Did not consume enough bytes.", 3, buf1.getPosition() - 1);
468 }
469 }
470
471
472
473
474 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
475 byte[][] encoded = new byte[vals.length][3];
476 PositionedByteRange pbr = new SimplePositionedMutableByteRange();
477 for (int i = 0; i < vals.length; i++) {
478 OrderedBytes.encodeInt16(pbr.set(encoded[i]), vals[i], ord);
479 }
480
481 Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
482 Short[] sortedVals = Arrays.copyOf(vals, vals.length);
483 if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
484 else Arrays.sort(sortedVals, Collections.reverseOrder());
485
486 for (int i = 0; i < sortedVals.length; i++) {
487 int decoded = OrderedBytes.decodeInt16(pbr.set(encoded[i]));
488 assertEquals(
489 String.format(
490 "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
491 sortedVals[i], decoded, ord),
492 sortedVals[i].shortValue(), decoded);
493 }
494 }
495 }
496
497
498
499
500 @Test
501 public void testInt32() {
502 Integer[] vals =
503 { Integer.MIN_VALUE, Integer.MIN_VALUE / 2, 0, Integer.MAX_VALUE / 2, Integer.MAX_VALUE };
504
505
506
507
508
509 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
510 for (int i = 0; i < vals.length; i++) {
511
512 byte[] a = new byte[5 + 3];
513 PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 5 + 1);
514 buf1.setPosition(1);
515
516
517 assertEquals("Surprising return value.",
518 5, OrderedBytes.encodeInt32(buf1, vals[i], ord));
519 assertEquals("Broken test: serialization did not consume entire buffer.",
520 buf1.getLength(), buf1.getPosition());
521 assertEquals("Surprising serialized length.", 5, buf1.getPosition() - 1);
522 assertEquals("Buffer underflow.", 0, a[0]);
523 assertEquals("Buffer underflow.", 0, a[1]);
524 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
525
526
527 buf1.setPosition(1);
528 assertEquals("Surprising return value.", 5, OrderedBytes.skip(buf1));
529 assertEquals("Did not skip enough bytes.", 5, buf1.getPosition() - 1);
530
531
532 buf1.setPosition(1);
533 assertEquals("Deserialization failed.",
534 vals[i].intValue(), OrderedBytes.decodeInt32(buf1));
535 assertEquals("Did not consume enough bytes.", 5, buf1.getPosition() - 1);
536 }
537 }
538
539
540
541
542 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
543 byte[][] encoded = new byte[vals.length][5];
544 PositionedByteRange pbr = new SimplePositionedMutableByteRange();
545 for (int i = 0; i < vals.length; i++) {
546 OrderedBytes.encodeInt32(pbr.set(encoded[i]), vals[i], ord);
547 }
548
549 Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
550 Integer[] sortedVals = Arrays.copyOf(vals, vals.length);
551 if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
552 else Arrays.sort(sortedVals, Collections.reverseOrder());
553
554 for (int i = 0; i < sortedVals.length; i++) {
555 int decoded = OrderedBytes.decodeInt32(pbr.set(encoded[i]));
556 assertEquals(
557 String.format(
558 "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
559 sortedVals[i], decoded, ord),
560 sortedVals[i].intValue(), decoded);
561 }
562 }
563 }
564
565
566
567
568 @Test
569 public void testInt64() {
570 Long[] vals = { Long.MIN_VALUE, Long.MIN_VALUE / 2, 0L, Long.MAX_VALUE / 2, Long.MAX_VALUE };
571
572
573
574
575
576 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
577 for (int i = 0; i < vals.length; i++) {
578
579 byte[] a = new byte[9 + 3];
580 PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 9 + 1);
581 buf1.setPosition(1);
582
583
584 assertEquals("Surprising return value.",
585 9, OrderedBytes.encodeInt64(buf1, vals[i], ord));
586 assertEquals("Broken test: serialization did not consume entire buffer.",
587 buf1.getLength(), buf1.getPosition());
588 assertEquals("Surprising serialized length.", 9, buf1.getPosition() - 1);
589 assertEquals("Buffer underflow.", 0, a[0]);
590 assertEquals("Buffer underflow.", 0, a[1]);
591 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
592
593
594 buf1.setPosition(1);
595 assertEquals("Surprising return value.", 9, OrderedBytes.skip(buf1));
596 assertEquals("Did not skip enough bytes.", 9, buf1.getPosition() - 1);
597
598
599 buf1.setPosition(1);
600 assertEquals("Deserialization failed.",
601 vals[i].longValue(), OrderedBytes.decodeInt64(buf1));
602 assertEquals("Did not consume enough bytes.", 9, buf1.getPosition() - 1);
603 }
604 }
605
606
607
608
609 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
610 byte[][] encoded = new byte[vals.length][9];
611 PositionedByteRange pbr = new SimplePositionedMutableByteRange();
612 for (int i = 0; i < vals.length; i++) {
613 OrderedBytes.encodeInt64(pbr.set(encoded[i]), vals[i], ord);
614 }
615
616 Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
617 Long[] sortedVals = Arrays.copyOf(vals, vals.length);
618 if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
619 else Arrays.sort(sortedVals, Collections.reverseOrder());
620
621 for (int i = 0; i < sortedVals.length; i++) {
622 long decoded = OrderedBytes.decodeInt64(pbr.set(encoded[i]));
623 assertEquals(
624 String.format(
625 "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
626 sortedVals[i], decoded, ord),
627 sortedVals[i].longValue(), decoded);
628 }
629 }
630 }
631
632
633
634
635 @Test
636 public void testFloat32() {
637 Float[] vals =
638 { Float.MIN_VALUE, Float.MIN_VALUE + 1.0f, 0.0f, Float.MAX_VALUE / 2.0f, Float.MAX_VALUE };
639
640
641
642
643
644 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
645 for (int i = 0; i < vals.length; i++) {
646
647 byte[] a = new byte[5 + 3];
648 PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 5 + 1);
649 buf1.setPosition(1);
650
651
652 assertEquals("Surprising return value.",
653 5, OrderedBytes.encodeFloat32(buf1, vals[i], ord));
654 assertEquals("Broken test: serialization did not consume entire buffer.",
655 buf1.getLength(), buf1.getPosition());
656 assertEquals("Surprising serialized length.", 5, buf1.getPosition() - 1);
657 assertEquals("Buffer underflow.", 0, a[0]);
658 assertEquals("Buffer underflow.", 0, a[1]);
659 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
660
661
662 buf1.setPosition(1);
663 assertEquals("Surprising return value.", 5, OrderedBytes.skip(buf1));
664 assertEquals("Did not skip enough bytes.", 5, buf1.getPosition() - 1);
665
666
667 buf1.setPosition(1);
668 assertEquals("Deserialization failed.",
669 Float.floatToIntBits(vals[i].floatValue()),
670 Float.floatToIntBits(OrderedBytes.decodeFloat32(buf1)));
671 assertEquals("Did not consume enough bytes.", 5, buf1.getPosition() - 1);
672 }
673 }
674
675
676
677
678 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
679 byte[][] encoded = new byte[vals.length][5];
680 PositionedByteRange pbr = new SimplePositionedMutableByteRange();
681 for (int i = 0; i < vals.length; i++) {
682 OrderedBytes.encodeFloat32(pbr.set(encoded[i]), vals[i], ord);
683 }
684
685 Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
686 Float[] sortedVals = Arrays.copyOf(vals, vals.length);
687 if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
688 else Arrays.sort(sortedVals, Collections.reverseOrder());
689
690 for (int i = 0; i < sortedVals.length; i++) {
691 float decoded = OrderedBytes.decodeFloat32(pbr.set(encoded[i]));
692 assertEquals(
693 String.format(
694 "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
695 sortedVals[i], decoded, ord),
696 Float.floatToIntBits(sortedVals[i].floatValue()),
697 Float.floatToIntBits(decoded));
698 }
699 }
700 }
701
702
703
704
705 @Test
706 public void testFloat64() {
707 Double[] vals =
708 { Double.MIN_VALUE, Double.MIN_VALUE + 1.0, 0.0, Double.MAX_VALUE / 2.0, Double.MAX_VALUE };
709
710
711
712
713
714 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
715 for (int i = 0; i < vals.length; i++) {
716
717 byte[] a = new byte[9 + 3];
718 PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, 9 + 1);
719 buf1.setPosition(1);
720
721
722 assertEquals("Surprising return value.",
723 9, OrderedBytes.encodeFloat64(buf1, vals[i], ord));
724 assertEquals("Broken test: serialization did not consume entire buffer.",
725 buf1.getLength(), buf1.getPosition());
726 assertEquals("Surprising serialized length.", 9, buf1.getPosition() - 1);
727 assertEquals("Buffer underflow.", 0, a[0]);
728 assertEquals("Buffer underflow.", 0, a[1]);
729 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
730
731
732 buf1.setPosition(1);
733 assertEquals("Surprising return value.", 9, OrderedBytes.skip(buf1));
734 assertEquals("Did not skip enough bytes.", 9, buf1.getPosition() - 1);
735
736
737 buf1.setPosition(1);
738 assertEquals("Deserialization failed.",
739 Double.doubleToLongBits(vals[i].doubleValue()),
740 Double.doubleToLongBits(OrderedBytes.decodeFloat64(buf1)));
741 assertEquals("Did not consume enough bytes.", 9, buf1.getPosition() - 1);
742 }
743 }
744
745
746
747
748 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
749 byte[][] encoded = new byte[vals.length][9];
750 PositionedByteRange pbr = new SimplePositionedMutableByteRange();
751 for (int i = 0; i < vals.length; i++) {
752 OrderedBytes.encodeFloat64(pbr.set(encoded[i]), vals[i], ord);
753 }
754
755 Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
756 Double[] sortedVals = Arrays.copyOf(vals, vals.length);
757 if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
758 else Arrays.sort(sortedVals, Collections.reverseOrder());
759
760 for (int i = 0; i < sortedVals.length; i++) {
761 double decoded = OrderedBytes.decodeFloat64(pbr.set(encoded[i]));
762 assertEquals(
763 String.format(
764 "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
765 sortedVals[i], decoded, ord),
766 Double.doubleToLongBits(sortedVals[i].doubleValue()),
767 Double.doubleToLongBits(decoded));
768 }
769 }
770 }
771
772
773
774
775 @Test
776 public void testString() {
777 String[] vals = { "foo", "baaaar", "bazz" };
778 int expectedLengths[] = { 5, 8, 6 };
779
780
781
782
783
784 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
785 for (int i = 0; i < vals.length; i++) {
786
787 byte[] a = new byte[expectedLengths[i] + 3];
788 PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1,
789 expectedLengths[i] + 1);
790 buf1.setPosition(1);
791
792
793 assertEquals("Surprising return value.",
794 expectedLengths[i], OrderedBytes.encodeString(buf1, vals[i], ord));
795 assertEquals("Broken test: serialization did not consume entire buffer.",
796 buf1.getLength(), buf1.getPosition());
797 assertEquals("Surprising serialized length.", expectedLengths[i], buf1.getPosition() - 1);
798 assertEquals("Buffer underflow.", 0, a[0]);
799 assertEquals("Buffer underflow.", 0, a[1]);
800 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
801
802
803 buf1.setPosition(1);
804 assertEquals("Surprising return value.", expectedLengths[i], OrderedBytes.skip(buf1));
805 assertEquals("Did not skip enough bytes.", expectedLengths[i], buf1.getPosition() - 1);
806
807
808 buf1.setPosition(1);
809 assertEquals("Deserialization failed.", vals[i], OrderedBytes.decodeString(buf1));
810 assertEquals("Did not consume enough bytes.", expectedLengths[i], buf1.getPosition() - 1);
811 }
812 }
813
814
815
816
817 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
818 byte[][] encoded = new byte[vals.length][];
819 PositionedByteRange pbr = new SimplePositionedMutableByteRange();
820 for (int i = 0; i < vals.length; i++) {
821 encoded[i] = new byte[expectedLengths[i]];
822 OrderedBytes.encodeString(pbr.set(encoded[i]), vals[i], ord);
823 }
824
825 Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
826 String[] sortedVals = Arrays.copyOf(vals, vals.length);
827 if (ord == Order.ASCENDING) Arrays.sort(sortedVals);
828 else Arrays.sort(sortedVals, Collections.reverseOrder());
829
830 for (int i = 0; i < sortedVals.length; i++) {
831 pbr.set(encoded[i]);
832 String decoded = OrderedBytes.decodeString(pbr);
833 assertEquals(
834 String.format(
835 "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
836 sortedVals[i], decoded, ord),
837 sortedVals[i], decoded);
838 }
839 }
840 }
841
842 @Test(expected = IllegalArgumentException.class)
843 public void testStringNoNullChars() {
844 PositionedByteRange buff = new SimplePositionedMutableByteRange(3);
845 OrderedBytes.encodeString(buff, "\u0000", Order.ASCENDING);
846 }
847
848
849
850
851
852 @Test
853 public void testBlobVarLencodedLength() {
854 int[][] values = {
855
856
857
858 { 1, 3 }, { 2, 4 }, { 3, 5 }, { 4, 6 },
859 { 5, 7 }, { 6, 8 }, { 7, 9 }, { 8, 11 }
860 };
861
862 for (int[] pair : values) {
863 assertEquals(pair[1], OrderedBytes.blobVarEncodedLength(pair[0]));
864 assertEquals(pair[0], OrderedBytes.blobVarDecodedLength(pair[1]));
865 }
866 }
867
868
869
870
871 @Test
872 public void testBlobVar() {
873 byte[][] vals =
874 { Bytes.toBytes(""),
875 Bytes.toBytes("foo"),
876 Bytes.toBytes("foobarbazbub"),
877 { (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa,
878 (byte) 0xaa,
879 { (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa,
880 (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa },
881 { (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa,
882 (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa,
883 (byte) 0xaa, (byte) 0xaa,
884 { (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55,
885 (byte) 0x55,
886 { (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55,
887 (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55 },
888 { (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55,
889 (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55,
890 (byte) 0x55, (byte) 0x55,
891 Bytes.toBytes("1"),
892 Bytes.toBytes("22"),
893 Bytes.toBytes("333"),
894 Bytes.toBytes("4444"),
895 Bytes.toBytes("55555"),
896 Bytes.toBytes("666666"),
897 Bytes.toBytes("7777777"),
898 Bytes.toBytes("88888888")
899 };
900
901
902
903
904
905 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
906 for (byte[] val : vals) {
907
908 int expectedLen = OrderedBytes.blobVarEncodedLength(val.length);
909 byte[] a = new byte[expectedLen + 3];
910 PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, expectedLen + 1);
911 buf1.setPosition(1);
912
913
914 assertEquals("Surprising return value.",
915 expectedLen, OrderedBytes.encodeBlobVar(buf1, val, ord));
916 assertEquals("Broken test: serialization did not consume entire buffer.",
917 buf1.getLength(), buf1.getPosition());
918 assertEquals("Surprising serialized length.", expectedLen, buf1.getPosition() - 1);
919 assertEquals("Buffer underflow.", 0, a[0]);
920 assertEquals("Buffer underflow.", 0, a[1]);
921 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
922
923
924 buf1.setPosition(1);
925 assertEquals("Surprising return value.", expectedLen, OrderedBytes.skip(buf1));
926 assertEquals("Did not skip enough bytes.", expectedLen, buf1.getPosition() - 1);
927
928
929 buf1.setPosition(1);
930 assertArrayEquals("Deserialization failed.", val, OrderedBytes.decodeBlobVar(buf1));
931 assertEquals("Did not consume enough bytes.", expectedLen, buf1.getPosition() - 1);
932 }
933 }
934
935
936
937
938 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
939 byte[][] encoded = new byte[vals.length][];
940 PositionedByteRange pbr = new SimplePositionedMutableByteRange();
941 for (int i = 0; i < vals.length; i++) {
942 encoded[i] = new byte[OrderedBytes.blobVarEncodedLength(vals[i].length)];
943 OrderedBytes.encodeBlobVar(pbr.set(encoded[i]), vals[i], ord);
944 }
945
946 Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
947 byte[][] sortedVals = Arrays.copyOf(vals, vals.length);
948 if (ord == Order.ASCENDING) Arrays.sort(sortedVals, Bytes.BYTES_COMPARATOR);
949 else Arrays.sort(sortedVals, Collections.reverseOrder(Bytes.BYTES_COMPARATOR));
950
951 for (int i = 0; i < sortedVals.length; i++) {
952 pbr.set(encoded[i]);
953 byte[] decoded = OrderedBytes.decodeBlobVar(pbr);
954 assertArrayEquals(
955 String.format(
956 "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
957 Arrays.toString(sortedVals[i]), Arrays.toString(decoded), ord),
958 sortedVals[i], decoded);
959 }
960 }
961 }
962
963
964
965
966 @Test
967 public void testBlobCopy() {
968 byte[][] vals =
969 { Bytes.toBytes(""),
970 Bytes.toBytes("foo"),
971 Bytes.toBytes("foobarbazbub"),
972 { (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa,
973 (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa },
974 { (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55,
975 (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55, (byte) 0x55 },
976 };
977
978
979
980
981
982 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
983 for (byte[] val : vals) {
984
985 int expectedLen = val.length + (Order.ASCENDING == ord ? 1 : 2);
986 byte[] a = new byte[expectedLen + 3];
987 PositionedByteRange buf1 = new SimplePositionedMutableByteRange(a, 1, expectedLen + 1);
988 buf1.setPosition(1);
989
990
991 assertEquals("Surprising return value.",
992 expectedLen, OrderedBytes.encodeBlobCopy(buf1, val, ord));
993 assertEquals("Broken test: serialization did not consume entire buffer.",
994 buf1.getLength(), buf1.getPosition());
995 assertEquals("Surprising serialized length.", expectedLen, buf1.getPosition() - 1);
996 assertEquals("Buffer underflow.", 0, a[0]);
997 assertEquals("Buffer underflow.", 0, a[1]);
998 assertEquals("Buffer overflow.", 0, a[a.length - 1]);
999
1000
1001 buf1.setPosition(1);
1002 assertEquals("Surprising return value.", expectedLen, OrderedBytes.skip(buf1));
1003 assertEquals("Did not skip enough bytes.", expectedLen, buf1.getPosition() - 1);
1004
1005
1006 buf1.setPosition(1);
1007 assertArrayEquals("Deserialization failed.", val, OrderedBytes.decodeBlobCopy(buf1));
1008 assertEquals("Did not consume enough bytes.", expectedLen, buf1.getPosition() - 1);
1009 }
1010 }
1011
1012
1013
1014
1015 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
1016 byte[][] encoded = new byte[vals.length][];
1017 PositionedByteRange pbr = new SimplePositionedMutableByteRange();
1018 for (int i = 0; i < vals.length; i++) {
1019 encoded[i] = new byte[vals[i].length + (Order.ASCENDING == ord ? 1 : 2)];
1020 OrderedBytes.encodeBlobCopy(pbr.set(encoded[i]), vals[i], ord);
1021 }
1022
1023 Arrays.sort(encoded, Bytes.BYTES_COMPARATOR);
1024 byte[][] sortedVals = Arrays.copyOf(vals, vals.length);
1025 if (ord == Order.ASCENDING) Arrays.sort(sortedVals, Bytes.BYTES_COMPARATOR);
1026 else Arrays.sort(sortedVals, Collections.reverseOrder(Bytes.BYTES_COMPARATOR));
1027
1028 for (int i = 0; i < sortedVals.length; i++) {
1029 pbr.set(encoded[i]);
1030 byte[] decoded = OrderedBytes.decodeBlobCopy(pbr);
1031 assertArrayEquals(
1032 String.format(
1033 "Encoded representations do not preserve natural order: <%s>, <%s>, %s",
1034 Arrays.toString(sortedVals[i]), Arrays.toString(decoded), ord),
1035 sortedVals[i], decoded);
1036 }
1037 }
1038
1039
1040
1041
1042 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
1043 byte[] a = new byte[3 + (Order.ASCENDING == ord ? 1 : 2) + 2];
1044 PositionedByteRange buf =
1045 new SimplePositionedMutableByteRange(a, 1, 3 + (Order.ASCENDING == ord ? 1 : 2));
1046 OrderedBytes.encodeBlobCopy(buf, Bytes.toBytes("foobarbaz"), 3, 3, ord);
1047 buf.setPosition(0);
1048 assertArrayEquals(Bytes.toBytes("bar"), OrderedBytes.decodeBlobCopy(buf));
1049 }
1050 }
1051
1052
1053
1054
1055 @Test(expected = IllegalArgumentException.class)
1056 public void testBlobCopyNoZeroBytes() {
1057 byte[] val = { 0x01, 0x02, 0x00, 0x03 };
1058
1059 byte[] ascExpected = { 0x38, 0x01, 0x02, 0x00, 0x03 };
1060 PositionedByteRange buf = new SimplePositionedMutableByteRange(val.length + 1);
1061 OrderedBytes.encodeBlobCopy(buf, val, Order.ASCENDING);
1062 assertArrayEquals(ascExpected, buf.getBytes());
1063 buf.set(val.length + 2);
1064 OrderedBytes.encodeBlobCopy(buf, val, Order.DESCENDING);
1065 fail("test should never get here.");
1066 }
1067
1068
1069
1070
1071 @Test
1072 public void testSkip() {
1073 BigDecimal longMax = BigDecimal.valueOf(Long.MAX_VALUE);
1074 double negInf = Double.NEGATIVE_INFINITY;
1075 BigDecimal negLarge = longMax.multiply(longMax).negate();
1076 BigDecimal negMed = new BigDecimal("-10.0");
1077 BigDecimal negSmall = new BigDecimal("-0.0010");
1078 long zero = 0l;
1079 BigDecimal posSmall = negSmall.negate();
1080 BigDecimal posMed = negMed.negate();
1081 BigDecimal posLarge = negLarge.negate();
1082 double posInf = Double.POSITIVE_INFINITY;
1083 double nan = Double.NaN;
1084 byte int8 = 100;
1085 short int16 = 100;
1086 int int32 = 100;
1087 long int64 = 100l;
1088 float float32 = 100.0f;
1089 double float64 = 100.0d;
1090 String text = "hello world.";
1091 byte[] blobVar = Bytes.toBytes("foo");
1092 byte[] blobCopy = Bytes.toBytes("bar");
1093
1094 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
1095 PositionedByteRange buff = new SimplePositionedMutableByteRange(30);
1096 int o;
1097 o = OrderedBytes.encodeNull(buff, ord);
1098 buff.setPosition(0);
1099 assertEquals(o, OrderedBytes.skip(buff));
1100
1101 buff.setPosition(0);
1102 o = OrderedBytes.encodeNumeric(buff, negInf, ord);
1103 buff.setPosition(0);
1104 assertEquals(o, OrderedBytes.skip(buff));
1105
1106 buff.setPosition(0);
1107 o = OrderedBytes.encodeNumeric(buff, negLarge, ord);
1108 buff.setPosition(0);
1109 assertEquals(o, OrderedBytes.skip(buff));
1110
1111 buff.setPosition(0);
1112 o = OrderedBytes.encodeNumeric(buff, negMed, ord);
1113 buff.setPosition(0);
1114 assertEquals(o, OrderedBytes.skip(buff));
1115
1116 buff.setPosition(0);
1117 o = OrderedBytes.encodeNumeric(buff, negSmall, ord);
1118 buff.setPosition(0);
1119 assertEquals(o, OrderedBytes.skip(buff));
1120
1121 buff.setPosition(0);
1122 o = OrderedBytes.encodeNumeric(buff, zero, ord);
1123 buff.setPosition(0);
1124 assertEquals(o, OrderedBytes.skip(buff));
1125
1126 buff.setPosition(0);
1127 o = OrderedBytes.encodeNumeric(buff, posSmall, ord);
1128 buff.setPosition(0);
1129 assertEquals(o, OrderedBytes.skip(buff));
1130
1131 buff.setPosition(0);
1132 o = OrderedBytes.encodeNumeric(buff, posMed, ord);
1133 buff.setPosition(0);
1134 assertEquals(o, OrderedBytes.skip(buff));
1135
1136 buff.setPosition(0);
1137 o = OrderedBytes.encodeNumeric(buff, posLarge, ord);
1138 buff.setPosition(0);
1139 assertEquals(o, OrderedBytes.skip(buff));
1140
1141 buff.setPosition(0);
1142 o = OrderedBytes.encodeNumeric(buff, posInf, ord);
1143 buff.setPosition(0);
1144 assertEquals(o, OrderedBytes.skip(buff));
1145
1146 buff.setPosition(0);
1147 o = OrderedBytes.encodeNumeric(buff, nan, ord);
1148 buff.setPosition(0);
1149 assertEquals(o, OrderedBytes.skip(buff));
1150
1151 buff.setPosition(0);
1152 o = OrderedBytes.encodeInt8(buff, int8, ord);
1153 buff.setPosition(0);
1154 assertEquals(o, OrderedBytes.skip(buff));
1155
1156 buff.setPosition(0);
1157 o = OrderedBytes.encodeInt16(buff, int16, ord);
1158 buff.setPosition(0);
1159 assertEquals(o, OrderedBytes.skip(buff));
1160
1161 buff.setPosition(0);
1162 o = OrderedBytes.encodeInt32(buff, int32, ord);
1163 buff.setPosition(0);
1164 assertEquals(o, OrderedBytes.skip(buff));
1165
1166 buff.setPosition(0);
1167 o = OrderedBytes.encodeInt64(buff, int64, ord);
1168 buff.setPosition(0);
1169 assertEquals(o, OrderedBytes.skip(buff));
1170
1171 buff.setPosition(0);
1172 o = OrderedBytes.encodeFloat32(buff, float32, ord);
1173 buff.setPosition(0);
1174 assertEquals(o, OrderedBytes.skip(buff));
1175
1176 buff.setPosition(0);
1177 o = OrderedBytes.encodeFloat64(buff, float64, ord);
1178 buff.setPosition(0);
1179 assertEquals(o, OrderedBytes.skip(buff));
1180
1181 buff.setPosition(0);
1182 o = OrderedBytes.encodeString(buff, text, ord);
1183 buff.setPosition(0);
1184 assertEquals(o, OrderedBytes.skip(buff));
1185
1186 buff.setPosition(0);
1187 o = OrderedBytes.encodeBlobVar(buff, blobVar, ord);
1188 buff.setPosition(0);
1189 assertEquals(o, OrderedBytes.skip(buff));
1190
1191
1192 buff.set(blobCopy.length + (Order.ASCENDING == ord ? 1 : 2));
1193 o = OrderedBytes.encodeBlobCopy(buff, blobCopy, ord);
1194 buff.setPosition(0);
1195 assertEquals(o, OrderedBytes.skip(buff));
1196 }
1197 }
1198
1199
1200
1201
1202 @Test
1203 public void testEncodedValueCheck() {
1204 BigDecimal longMax = BigDecimal.valueOf(Long.MAX_VALUE);
1205 double negInf = Double.NEGATIVE_INFINITY;
1206 BigDecimal negLarge = longMax.multiply(longMax).negate();
1207 BigDecimal negMed = new BigDecimal("-10.0");
1208 BigDecimal negSmall = new BigDecimal("-0.0010");
1209 long zero = 0l;
1210 BigDecimal posSmall = negSmall.negate();
1211 BigDecimal posMed = negMed.negate();
1212 BigDecimal posLarge = negLarge.negate();
1213 double posInf = Double.POSITIVE_INFINITY;
1214 double nan = Double.NaN;
1215 byte int8 = 100;
1216 short int16 = 100;
1217 int int32 = 100;
1218 long int64 = 100l;
1219 float float32 = 100.0f;
1220 double float64 = 100.0d;
1221 String text = "hello world.";
1222 byte[] blobVar = Bytes.toBytes("foo");
1223
1224 int cnt = 0;
1225 PositionedByteRange buff = new SimplePositionedMutableByteRange(1024);
1226 for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
1227 int o;
1228 o = OrderedBytes.encodeNull(buff, ord); cnt++;
1229 o = OrderedBytes.encodeNumeric(buff, negInf, ord); cnt++;
1230 o = OrderedBytes.encodeNumeric(buff, negLarge, ord); cnt++;
1231 o = OrderedBytes.encodeNumeric(buff, negMed, ord); cnt++;
1232 o = OrderedBytes.encodeNumeric(buff, negSmall, ord); cnt++;
1233 o = OrderedBytes.encodeNumeric(buff, zero, ord); cnt++;
1234 o = OrderedBytes.encodeNumeric(buff, posSmall, ord); cnt++;
1235 o = OrderedBytes.encodeNumeric(buff, posMed, ord); cnt++;
1236 o = OrderedBytes.encodeNumeric(buff, posLarge, ord); cnt++;
1237 o = OrderedBytes.encodeNumeric(buff, posInf, ord); cnt++;
1238 o = OrderedBytes.encodeNumeric(buff, nan, ord); cnt++;
1239 o = OrderedBytes.encodeInt8(buff, int8, ord); cnt++;
1240 o = OrderedBytes.encodeInt16(buff, int16, ord); cnt++;
1241 o = OrderedBytes.encodeInt32(buff, int32, ord); cnt++;
1242 o = OrderedBytes.encodeInt64(buff, int64, ord); cnt++;
1243 o = OrderedBytes.encodeFloat32(buff, float32, ord); cnt++;
1244 o = OrderedBytes.encodeFloat64(buff, float64, ord); cnt++;
1245 o = OrderedBytes.encodeString(buff, text, ord); cnt++;
1246 o = OrderedBytes.encodeBlobVar(buff, blobVar, ord); cnt++;
1247 }
1248
1249 buff.setPosition(0);
1250 assertEquals(OrderedBytes.length(buff), cnt);
1251 for (int i = 0; i < cnt; i++) {
1252 assertEquals(true, OrderedBytes.isEncodedValue(buff));
1253 OrderedBytes.skip(buff);
1254 }
1255 }
1256 }