• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.util;
2 
3 import java.util.*;
4 
5 import com.fasterxml.jackson.databind.BaseMapTest;
6 
7 public class TestObjectBuffer
8     extends BaseMapTest
9 {
10     /**
11      * First a test that treats results as plain old Object[]
12      */
testUntyped()13     public void testUntyped()
14     {
15         _testObjectBuffer(null);
16     }
17 
testTyped()18     public void testTyped()
19     {
20         _testObjectBuffer(Integer.class);
21     }
22 
23     /*
24     /**********************************************************
25     /* Helper methods
26     /**********************************************************
27      */
28 
_testObjectBuffer(Class<?> clz)29     private void _testObjectBuffer(Class<?> clz)
30     {
31         int[] SIZES = new int[] {
32             3, 19, 99, 1007, 19999, 99001
33         };
34 
35         // Let's loop separately for reused instance, new instance
36         for (int reuse = 0; reuse < 2; ++reuse) {
37             ObjectBuffer buf = (reuse == 0) ? null : new ObjectBuffer();
38 
39             // then distinct sizes
40             for (int sizeIndex = 0; sizeIndex < SIZES.length; ++sizeIndex) {
41                 int size = SIZES[sizeIndex];
42                 Random r = new Random(size);
43                 ObjectBuffer thisBuf = (buf == null) ? new ObjectBuffer() : buf;
44                 Object[] chunk = thisBuf.resetAndStart();
45                 int ix = 0;
46 
47                 for (int i = 0; i < size; ++i) {
48                     if (ix >= chunk.length) {
49                         chunk = thisBuf.appendCompletedChunk(chunk);
50                         ix = 0;
51                     }
52                     chunk[ix++] = Integer.valueOf(r.nextInt());
53                 }
54 
55                 Object[] result;
56 
57                 if (clz == null) {
58                     result = thisBuf.completeAndClearBuffer(chunk, ix);
59                 } else {
60                     result = thisBuf.completeAndClearBuffer(chunk, ix, clz);
61                 }
62                 assertEquals(size, result.length);
63 
64                 r = new Random(size);
65                 for (int i = 0; i < size; ++i) {
66                     assertEquals(r.nextInt(), ((Integer) result[i]).intValue());
67                 }
68             }
69         }
70     }
71 }
72