|
| 1 | +package com.fasterxml.jackson.dataformat.protobuf; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.fasterxml.jackson.databind.ObjectWriter; |
| 7 | +import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema; |
| 8 | +import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchemaLoader; |
| 9 | + |
| 10 | +public class WritePrimitiveArrayTest extends ProtobufTestBase |
| 11 | +{ |
| 12 | + final protected static String PROTOC_INT_ARRAY_SPARSE = "message Ints {\n" |
| 13 | + +" repeated sint32 values = 1;\n" |
| 14 | + +"}\n" |
| 15 | + ; |
| 16 | + |
| 17 | + final protected static String PROTOC_INT_ARRAY_PACKED = "message Ints {\n" |
| 18 | + +" repeated sint32 values = 1 [packed=true];\n" |
| 19 | + +"}\n" |
| 20 | + ; |
| 21 | + |
| 22 | + final protected static String PROTOC_INT32_ARRAY_SPARSE = "message Ints {\n" |
| 23 | + +" repeated fixed32 values = 1;\n" |
| 24 | + +"}\n" |
| 25 | + ; |
| 26 | + |
| 27 | + final protected static String PROTOC_INT32_ARRAY_PACKED = "message Ints {\n" |
| 28 | + +" repeated fixed32 values = 1 [packed=true];\n" |
| 29 | + +"}\n" |
| 30 | + ; |
| 31 | + |
| 32 | + final protected static String PROTOC_INT64_ARRAY_SPARSE = "message Longs {\n" |
| 33 | + +" repeated fixed64 values = 1;\n" |
| 34 | + +"}\n" |
| 35 | + ; |
| 36 | + |
| 37 | + final protected static String PROTOC_INT64_ARRAY_PACKED = "message Longs {\n" |
| 38 | + +" repeated fixed64 values = 1 [packed=true];\n" |
| 39 | + +"}\n" |
| 40 | + ; |
| 41 | + |
| 42 | + final protected static String PROTOC_DOUBLE_ARRAY_SPARSE = "message Doubles {\n" |
| 43 | + +" repeated double values = 1;\n" |
| 44 | + +"}\n" |
| 45 | + ; |
| 46 | + |
| 47 | + final protected static String PROTOC_DOUBLE_ARRAY_PACKED = "message Doubles {\n" |
| 48 | + +" repeated double values = 1 [packed=true];\n" |
| 49 | + +"}\n" |
| 50 | + ; |
| 51 | + |
| 52 | + static class IntArray { |
| 53 | + public int[] values; |
| 54 | + |
| 55 | + protected IntArray() { } |
| 56 | + public IntArray(int... v) { |
| 57 | + values = v; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + static class LongArray { |
| 62 | + public long[] values; |
| 63 | + |
| 64 | + protected LongArray() { } |
| 65 | + public LongArray(long... v) { |
| 66 | + values = v; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static class DoubleArray { |
| 71 | + public double[] values; |
| 72 | + |
| 73 | + protected DoubleArray() { } |
| 74 | + public DoubleArray(double... v) { |
| 75 | + values = v; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + final ObjectMapper MAPPER = new ObjectMapper(new ProtobufFactory()); |
| 80 | + |
| 81 | + public WritePrimitiveArrayTest() throws Exception { } |
| 82 | + |
| 83 | + /* |
| 84 | + /********************************************************** |
| 85 | + /* Test methods, int arrays |
| 86 | + /********************************************************** |
| 87 | + */ |
| 88 | + |
| 89 | + public void testVIntArraySparse() throws Exception |
| 90 | + { |
| 91 | + final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse(PROTOC_INT_ARRAY_SPARSE)); |
| 92 | + byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2)); |
| 93 | + // 3 x 2 bytes per value (typed tag, value) -> 6 |
| 94 | + assertEquals(6, bytes.length); |
| 95 | + assertEquals(0x8, bytes[0]); // zig-zagged vint (0) value, field 1 |
| 96 | + assertEquals(0x6, bytes[1]); // zig-zagged value for 3 |
| 97 | + assertEquals(0x8, bytes[2]); |
| 98 | + assertEquals(0x1, bytes[3]); // zig-zagged value for -1 |
| 99 | + assertEquals(0x8, bytes[4]); |
| 100 | + assertEquals(0x4, bytes[5]); // zig-zagged value for 2 |
| 101 | + } |
| 102 | + |
| 103 | + public void testVIntArrayPacked() throws Exception |
| 104 | + { |
| 105 | + final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse(PROTOC_INT_ARRAY_PACKED)); |
| 106 | + byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2)); |
| 107 | + // 1 byte for typed tag, 1 byte for length, 3 x 1 byte per value -> 5 |
| 108 | + assertEquals(5, bytes.length); |
| 109 | + assertEquals(0x8, bytes[0]); // zig-zagged vint (0) value, field 1 |
| 110 | + assertEquals(0x3, bytes[1]); // length for array, 3 bytes |
| 111 | + assertEquals(0x6, bytes[2]); // zig-zagged value for 3 |
| 112 | + assertEquals(0x1, bytes[3]); // zig-zagged value for -1 |
| 113 | + assertEquals(0x4, bytes[4]); // zig-zagged value for 2 |
| 114 | + } |
| 115 | + |
| 116 | + public void testInt32ArraySparse() throws Exception |
| 117 | + { |
| 118 | + final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse |
| 119 | + (PROTOC_INT32_ARRAY_SPARSE)); |
| 120 | + byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2)); |
| 121 | + // 3 x 5 bytes per value (typed tag, value) -> 18 |
| 122 | + assertEquals(15, bytes.length); |
| 123 | + } |
| 124 | + |
| 125 | + public void testInt32ArrayPacked() throws Exception |
| 126 | + { |
| 127 | + final ObjectWriter w = MAPPER.writer(ProtobufSchemaLoader.std.parse |
| 128 | + (PROTOC_INT32_ARRAY_PACKED)); |
| 129 | + byte[] bytes = w.writeValueAsBytes(new IntArray(3, -1, 2)); |
| 130 | + // 1 byte for typed tag, 1 byte for length, 3 x 4 byte per value -> 14 |
| 131 | + assertEquals(14, bytes.length); |
| 132 | + } |
| 133 | + |
| 134 | + /* |
| 135 | + /********************************************************** |
| 136 | + /* Test methods, long arrays |
| 137 | + /********************************************************** |
| 138 | + */ |
| 139 | + |
| 140 | + // // // First as ints: |
| 141 | + |
| 142 | + public void testIntAsLongArraySparse() throws Exception |
| 143 | + { |
| 144 | + ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_INT64_ARRAY_SPARSE); |
| 145 | + final ObjectWriter w = MAPPER.writer(schema); |
| 146 | + IntArray input = new IntArray(3, -1, -2); |
| 147 | + byte[] bytes = w.writeValueAsBytes(input); |
| 148 | + // 3 x 9 bytes per value (typed tag, value) -> 30 |
| 149 | + assertEquals(27, bytes.length); |
| 150 | + |
| 151 | + IntArray result = MAPPER.readerFor(IntArray.class).with(schema) |
| 152 | + .readValue(bytes); |
| 153 | + Assert.assertArrayEquals(input.values, result.values); |
| 154 | + } |
| 155 | + |
| 156 | + public void testIntAsLongArrayPacked() throws Exception |
| 157 | + { |
| 158 | + ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_INT64_ARRAY_PACKED); |
| 159 | + final ObjectWriter w = MAPPER.writer(schema); |
| 160 | + IntArray input = new IntArray(3, -1, -2); |
| 161 | + byte[] bytes = w.writeValueAsBytes(input); |
| 162 | + // 1 byte for typed tag, 1 byte for length, 3 x 8 byte per value -> 26 |
| 163 | + assertEquals(26, bytes.length); |
| 164 | + |
| 165 | + IntArray result = MAPPER.readerFor(IntArray.class).with(schema) |
| 166 | + .readValue(bytes); |
| 167 | + Assert.assertArrayEquals(input.values, result.values); |
| 168 | + } |
| 169 | + |
| 170 | + // // // But then as regular longs |
| 171 | + |
| 172 | + public void testLongArraySparse() throws Exception |
| 173 | + { |
| 174 | + ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_INT64_ARRAY_SPARSE); |
| 175 | + final ObjectWriter w = MAPPER.writer(schema); |
| 176 | + LongArray input = new LongArray(Integer.MAX_VALUE, -1, Long.MIN_VALUE); |
| 177 | + byte[] bytes = w.writeValueAsBytes(input); |
| 178 | + assertEquals(27, bytes.length); |
| 179 | + |
| 180 | + LongArray result = MAPPER.readerFor(LongArray.class).with(schema) |
| 181 | + .readValue(bytes); |
| 182 | + Assert.assertArrayEquals(input.values, result.values); |
| 183 | + } |
| 184 | + |
| 185 | + public void testLongArrayPacked() throws Exception |
| 186 | + { |
| 187 | + ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_INT64_ARRAY_PACKED); |
| 188 | + final ObjectWriter w = MAPPER.writer(schema); |
| 189 | + LongArray input = new LongArray(Integer.MIN_VALUE, -1, Long.MAX_VALUE); |
| 190 | + byte[] bytes = w.writeValueAsBytes(input); |
| 191 | + assertEquals(26, bytes.length); |
| 192 | + |
| 193 | + LongArray result = MAPPER.readerFor(LongArray.class).with(schema) |
| 194 | + .readValue(bytes); |
| 195 | + Assert.assertArrayEquals(input.values, result.values); |
| 196 | + } |
| 197 | + |
| 198 | + /* |
| 199 | + /********************************************************** |
| 200 | + /* Test methods, floating-point arrays |
| 201 | + /********************************************************** |
| 202 | + */ |
| 203 | + |
| 204 | + public void testDoubleArraySparse() throws Exception |
| 205 | + { |
| 206 | + ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_DOUBLE_ARRAY_SPARSE); |
| 207 | + final ObjectWriter w = MAPPER.writer(schema); |
| 208 | + DoubleArray input = new DoubleArray(0.25, -2.5, 1000.125); |
| 209 | + byte[] bytes = w.writeValueAsBytes(input); |
| 210 | + assertEquals(27, bytes.length); |
| 211 | + |
| 212 | + DoubleArray result = MAPPER.readerFor(DoubleArray.class).with(schema) |
| 213 | + .readValue(bytes); |
| 214 | + _assertEquals(input.values, result.values); |
| 215 | + } |
| 216 | + |
| 217 | + public void testDoubleArrayPacked() throws Exception |
| 218 | + { |
| 219 | + ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_DOUBLE_ARRAY_PACKED); |
| 220 | + final ObjectWriter w = MAPPER.writer(schema); |
| 221 | + DoubleArray input = new DoubleArray(-0.5, 89245.25, 0.625); |
| 222 | + byte[] bytes = w.writeValueAsBytes(input); |
| 223 | + assertEquals(26, bytes.length); |
| 224 | + |
| 225 | + DoubleArray result = MAPPER.readerFor(DoubleArray.class).with(schema) |
| 226 | + .readValue(bytes); |
| 227 | + _assertEquals(input.values, result.values); |
| 228 | + } |
| 229 | + |
| 230 | + private void _assertEquals(double[] exp, double[] act) |
| 231 | + { |
| 232 | + assertEquals(exp.length, act.length); |
| 233 | + for (int i = 0; i < exp.length; ++i) { |
| 234 | + // note: caller ensures it only uses values that reliably round-trip |
| 235 | + if (exp[i] != act[i]) { |
| 236 | + fail("Entry #"+i+" wrong: expected "+exp[i]+", got "+act[i]); |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments