ArrayFieldTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.internal.message.field;
00018 
00019 import static org.junit.Assert.assertArrayEquals;
00020 import static org.junit.Assert.assertEquals;
00021 
00022 import org.ros.internal.message.MessageBuffers;
00023 
00024 import org.jboss.netty.buffer.ChannelBuffer;
00025 import org.junit.Test;
00026 
00033 public class ArrayFieldTest {
00034 
00035   @Test
00036   public void testBooleanArrayFieldVariableSize() {
00037     BooleanArrayField field = BooleanArrayField.newVariable("foo", -1);
00038     boolean[] value = new boolean[] { true, false, true, false };
00039     field.setValue(value);
00040     assertEquals(PrimitiveFieldType.BOOL, field.getType());
00041     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00042     field.serialize(buffer);
00043     byte[] expected = new byte[] { 4, 0, 0, 0, 1, 0, 1, 0 };
00044     byte[] actual = new byte[buffer.readableBytes()];
00045     buffer.readBytes(actual);
00046     assertArrayEquals(expected, actual);
00047   }
00048 
00049   @Test
00050   public void testBooleanArrayFieldFixedSize() {
00051     BooleanArrayField field = BooleanArrayField.newVariable("foo", 4);
00052     field.setValue(new boolean[] { true, false, true, false });
00053     assertEquals(PrimitiveFieldType.BOOL, field.getType());
00054     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00055     field.serialize(buffer);
00056     byte[] expected = new byte[] { 1, 0, 1, 0 };
00057     byte[] actual = new byte[buffer.readableBytes()];
00058     buffer.readBytes(actual);
00059     assertArrayEquals(expected, actual);
00060   }
00061 
00062   @SuppressWarnings("deprecation")
00063   @Test
00064   public void testByteArrayFieldVariableSize() {
00065     testByteArrayFieldVariableSize(PrimitiveFieldType.INT8);
00066     testByteArrayFieldVariableSize(PrimitiveFieldType.BYTE);
00067     testByteArrayFieldVariableSize(PrimitiveFieldType.UINT8);
00068     testByteArrayFieldVariableSize(PrimitiveFieldType.CHAR);
00069   }
00070 
00071   private void testByteArrayFieldVariableSize(FieldType type) {
00072     ByteArrayField field = ByteArrayField.newVariable(type, "foo", -1);
00073     field.setValue(new byte[] { 1, 2, 3, 4 });
00074     assertEquals(type, field.getType());
00075     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00076     field.serialize(buffer);
00077     byte[] expected = new byte[] { 4, 0, 0, 0, 1, 2, 3, 4 };
00078     byte[] actual = new byte[buffer.readableBytes()];
00079     buffer.readBytes(actual);
00080     assertArrayEquals(expected, actual);
00081   }
00082 
00083   @SuppressWarnings("deprecation")
00084   @Test
00085   public void testByteArrayFieldFixedSize() {
00086     testByteArrayFieldFixedSize(PrimitiveFieldType.INT8);
00087     testByteArrayFieldFixedSize(PrimitiveFieldType.BYTE);
00088     testByteArrayFieldFixedSize(PrimitiveFieldType.UINT8);
00089     testByteArrayFieldFixedSize(PrimitiveFieldType.CHAR);
00090   }
00091 
00092   private void testByteArrayFieldFixedSize(FieldType type) {
00093     ByteArrayField field = ByteArrayField.newVariable(type, "foo", 4);
00094     field.setValue(new byte[] { 1, 2, 3, 4 });
00095     assertEquals(type, field.getType());
00096     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00097     field.serialize(buffer);
00098     byte[] expected = new byte[] { 1, 2, 3, 4 };
00099     byte[] actual = new byte[buffer.readableBytes()];
00100     buffer.readBytes(actual);
00101     assertArrayEquals(expected, actual);
00102   }
00103 
00104   @Test
00105   public void testDoubleArrayFieldVariableSize() {
00106     DoubleArrayField field = DoubleArrayField.newVariable("foo", -1);
00107     field.setValue(new double[] { 1, 2, 3, 4 });
00108     assertEquals(PrimitiveFieldType.FLOAT64, field.getType());
00109     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00110     field.serialize(buffer);
00111     byte[] expected =
00112         new byte[] { 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, -16, 63, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0,
00113             0, 8, 64, 0, 0, 0, 0, 0, 0, 16, 64 };
00114     byte[] actual = new byte[buffer.readableBytes()];
00115     buffer.readBytes(actual);
00116     assertArrayEquals(expected, actual);
00117   }
00118 
00119   @Test
00120   public void testDoubleArrayFieldFixedSize() {
00121     DoubleArrayField field = DoubleArrayField.newVariable("foo", 4);
00122     field.setValue(new double[] { 1, 2, 3, 4 });
00123     assertEquals(PrimitiveFieldType.FLOAT64, field.getType());
00124     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00125     field.serialize(buffer);
00126     byte[] expected =
00127         new byte[] { 0, 0, 0, 0, 0, 0, -16, 63, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 8, 64,
00128             0, 0, 0, 0, 0, 0, 16, 64 };
00129     byte[] actual = new byte[buffer.readableBytes()];
00130     buffer.readBytes(actual);
00131     assertArrayEquals(expected, actual);
00132   }
00133 
00134   @Test
00135   public void testFloatArrayFieldVariableSize() {
00136     FloatArrayField field = FloatArrayField.newVariable("foo", -1);
00137     field.setValue(new float[] { 1, 2, 3, 4 });
00138     assertEquals(PrimitiveFieldType.FLOAT32, field.getType());
00139     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00140     field.serialize(buffer);
00141     byte[] expected =
00142         new byte[] { 4, 0, 0, 0, 0, 0, -128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, -128, 64 };
00143     byte[] actual = new byte[buffer.readableBytes()];
00144     buffer.readBytes(actual);
00145     assertArrayEquals(expected, actual);
00146   }
00147 
00148   @Test
00149   public void testFloatArrayFieldFixedSize() {
00150     FloatArrayField field = FloatArrayField.newVariable("foo", 4);
00151     field.setValue(new float[] { 1, 2, 3, 4 });
00152     assertEquals(PrimitiveFieldType.FLOAT32, field.getType());
00153     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00154     field.serialize(buffer);
00155     byte[] expected = new byte[] { 0, 0, -128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, -128, 64 };
00156     byte[] actual = new byte[buffer.readableBytes()];
00157     buffer.readBytes(actual);
00158     assertArrayEquals(expected, actual);
00159   }
00160 
00161   @Test
00162   public void testIntegerArrayFieldVariableSize() {
00163     testIntegerArrayFieldVariableSize(PrimitiveFieldType.INT32);
00164     testIntegerArrayFieldVariableSize(PrimitiveFieldType.UINT32);
00165   }
00166 
00167   private void testIntegerArrayFieldVariableSize(FieldType type) {
00168     IntegerArrayField field = IntegerArrayField.newVariable(type, "foo", -1);
00169     field.setValue(new int[] { 1, 2, 3, 4 });
00170     assertEquals(type, field.getType());
00171     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00172     field.serialize(buffer);
00173     byte[] expected = new byte[] { 4, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0 };
00174     byte[] actual = new byte[buffer.readableBytes()];
00175     buffer.readBytes(actual);
00176     assertArrayEquals(expected, actual);
00177   }
00178 
00179   @Test
00180   public void testIntegerArrayFieldFixedSize() {
00181     testIntegerArrayFieldFixedSize(PrimitiveFieldType.INT32);
00182     testIntegerArrayFieldFixedSize(PrimitiveFieldType.UINT32);
00183   }
00184 
00185   private void testIntegerArrayFieldFixedSize(FieldType type) {
00186     IntegerArrayField field = IntegerArrayField.newVariable(type, "foo", 4);
00187     field.setValue(new int[] { 1, 2, 3, 4 });
00188     assertEquals(type, field.getType());
00189     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00190     field.serialize(buffer);
00191     byte[] expected = new byte[] { 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0 };
00192     byte[] actual = new byte[buffer.readableBytes()];
00193     buffer.readBytes(actual);
00194     assertArrayEquals(expected, actual);
00195   }
00196 
00197   @Test
00198   public void testLongArrayFieldVariableSize() {
00199     testLongArrayFieldVariableSize(PrimitiveFieldType.INT64);
00200     testLongArrayFieldVariableSize(PrimitiveFieldType.UINT64);
00201   }
00202 
00203   private void testLongArrayFieldVariableSize(FieldType type) {
00204     LongArrayField field = LongArrayField.newVariable(type, "foo", -1);
00205     field.setValue(new long[] { 1, 2, 3, 4 });
00206     assertEquals(type, field.getType());
00207     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00208     field.serialize(buffer);
00209     byte[] expected =
00210         new byte[] { 4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0,
00211             0, 0, 4, 0, 0, 0, 0, 0, 0, 0 };
00212     byte[] actual = new byte[buffer.readableBytes()];
00213     buffer.readBytes(actual);
00214     assertArrayEquals(expected, actual);
00215   }
00216 
00217   @Test
00218   public void testLongArrayFieldFixedSize() {
00219     testLongArrayFieldFixedSize(PrimitiveFieldType.INT64);
00220     testLongArrayFieldFixedSize(PrimitiveFieldType.UINT64);
00221   }
00222 
00223   private void testLongArrayFieldFixedSize(FieldType type) {
00224     LongArrayField field = LongArrayField.newVariable(type, "foo", 4);
00225     field.setValue(new long[] { 1, 2, 3, 4 });
00226     assertEquals(type, field.getType());
00227     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00228     field.serialize(buffer);
00229     byte[] expected =
00230         new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0,
00231             0, 0, 0, 0, 0, 0 };
00232     byte[] actual = new byte[buffer.readableBytes()];
00233     buffer.readBytes(actual);
00234     assertArrayEquals(expected, actual);
00235   }
00236 
00237   @Test
00238   public void testShortArrayFieldVariableSize() {
00239     testShortArrayFieldVariableSize(PrimitiveFieldType.INT16);
00240     testShortArrayFieldVariableSize(PrimitiveFieldType.UINT16);
00241   }
00242 
00243   private void testShortArrayFieldVariableSize(FieldType type) {
00244     ShortArrayField field = ShortArrayField.newVariable(type, "foo", -1);
00245     field.setValue(new short[] { 1, 2, 3, 4 });
00246     assertEquals(type, field.getType());
00247     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00248     field.serialize(buffer);
00249     byte[] expected = new byte[] { 4, 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0 };
00250     byte[] actual = new byte[buffer.readableBytes()];
00251     buffer.readBytes(actual);
00252     assertArrayEquals(expected, actual);
00253   }
00254 
00255   @Test
00256   public void testShortArrayFieldFixedSize() {
00257     testShortArrayFieldFixedSize(PrimitiveFieldType.INT16);
00258     testShortArrayFieldFixedSize(PrimitiveFieldType.UINT16);
00259   }
00260 
00261   private void testShortArrayFieldFixedSize(FieldType type) {
00262     ShortArrayField field = ShortArrayField.newVariable(type, "foo", 4);
00263     field.setValue(new short[] { 1, 2, 3, 4 });
00264     assertEquals(type, field.getType());
00265     ChannelBuffer buffer = MessageBuffers.dynamicBuffer();
00266     field.serialize(buffer);
00267     byte[] expected = new byte[] { 1, 0, 2, 0, 3, 0, 4, 0 };
00268     byte[] actual = new byte[buffer.readableBytes()];
00269     buffer.readBytes(actual);
00270     assertArrayEquals(expected, actual);
00271   }
00272 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49