/// <exception cref="System.IO.IOException"/> public virtual void TestSerializeAndDeserializeNull() { bool gotException = false; try { new EnumSetWritable <TestEnumSetWritable.TestEnumSet>(null); } catch (RuntimeException) { gotException = true; } Assert.True("Instantiation of empty EnumSetWritable with no element type class " + "provided should throw exception", gotException); EnumSetWritable <TestEnumSetWritable.TestEnumSet> nullFlagWritable = new EnumSetWritable <TestEnumSetWritable.TestEnumSet>(null, typeof(TestEnumSetWritable.TestEnumSet)); DataOutputBuffer @out = new DataOutputBuffer(); ObjectWritable.WriteObject(@out, nullFlagWritable, nullFlagWritable.GetType(), null ); DataInputBuffer @in = new DataInputBuffer(); @in.Reset(@out.GetData(), @out.GetLength()); EnumSet <TestEnumSetWritable.TestEnumSet> read = ((EnumSetWritable <TestEnumSetWritable.TestEnumSet >)ObjectWritable.ReadObject(@in, null)).Get(); Assert.Equal(read, null); }
public virtual void TestOldFormat() { //Make sure we still correctly write the old format if desired. //Write the data array with old ObjectWritable API //which will set allowCompactArrays false. ObjectWritable.WriteObject(@out, i, i.GetType(), null); //Get ready to read it back @in.Reset(@out.GetData(), @out.GetLength()); //Read the int[] object as written by ObjectWritable, but //"going around" ObjectWritable string className = UTF8.ReadString(@in); Assert.Equal("The int[] written by ObjectWritable as a non-compact array " + "was not labelled as an array of int", i.GetType().FullName, className); int length = @in.ReadInt(); Assert.Equal("The int[] written by ObjectWritable as a non-compact array " + "was not expected length", i.Length, length); int[] readValue = new int[length]; try { for (int i = 0; i < length; i++) { readValue[i] = (int)((int)ObjectWritable.ReadObject(@in, null)); } } catch (Exception e) { Fail("The int[] written by ObjectWritable as a non-compact array " + "was corrupted. Failed to correctly read int[] of length " + length + ". Got exception:\n" + StringUtils.StringifyException(e)); } Assert.True("The int[] written by ObjectWritable as a non-compact array " + "was corrupted.", Arrays.Equals(i, readValue)); }
public virtual void TestMany() { //Write a big set of data, one of each primitive type array foreach (object x in bigSet) { //write each test object two ways //First, transparently via ObjectWritable ObjectWritable.WriteObject(@out, x, x.GetType(), null, true); //Second, explicitly via ArrayPrimitiveWritable (new ArrayPrimitiveWritable(x)).Write(@out); } //Now read the data back in @in.Reset(@out.GetData(), @out.GetLength()); for (int x_1 = 0; x_1 < resultSet.Length;) { //First, transparently resultSet[x_1++] = ObjectWritable.ReadObject(@in, null); //Second, explicitly ArrayPrimitiveWritable apw = new ArrayPrimitiveWritable(); apw.ReadFields(@in); resultSet[x_1++] = apw.Get(); } //validate data structures and values Assert.Equal(expectedResultSet.Length, resultSet.Length); for (int x_2 = 0; x_2 < resultSet.Length; x_2++) { Assert.Equal("ComponentType of array " + x_2, expectedResultSet [x_2].GetType().GetElementType(), resultSet[x_2].GetType().GetElementType()); } Assert.True("In and Out arrays didn't match values", Arrays.DeepEquals (expectedResultSet, resultSet)); }
/// <summary> /// Write a protobuf to a buffer 'numProtos' times, and then /// read them back, making sure all data comes through correctly. /// </summary> /// <exception cref="System.IO.IOException"/> private void DoTest(int numProtos) { Configuration conf = new Configuration(); DataOutputBuffer @out = new DataOutputBuffer(); // Write numProtos protobufs to the buffer Message[] sent = new Message[numProtos]; for (int i = 0; i < numProtos; i++) { // Construct a test protocol buffer using one of the // protos that ships with the protobuf library Message testProto = ((DescriptorProtos.EnumValueDescriptorProto)DescriptorProtos.EnumValueDescriptorProto .NewBuilder().SetName("test" + i).SetNumber(i).Build()); ObjectWritable.WriteObject(@out, testProto, typeof(DescriptorProtos.EnumValueDescriptorProto ), conf); sent[i] = testProto; } // Read back the data DataInputBuffer @in = new DataInputBuffer(); @in.Reset(@out.GetData(), @out.GetLength()); for (int i_1 = 0; i_1 < numProtos; i_1++) { Message received = (Message)ObjectWritable.ReadObject(@in, conf); Assert.Equal(sent[i_1], received); } }
/// <exception cref="System.IO.IOException"/> public virtual void TestSerializeAndDeserializeNonEmpty() { DataOutputBuffer @out = new DataOutputBuffer(); ObjectWritable.WriteObject(@out, nonEmptyFlagWritable, nonEmptyFlagWritable.GetType (), null); DataInputBuffer @in = new DataInputBuffer(); @in.Reset(@out.GetData(), @out.GetLength()); EnumSet <TestEnumSetWritable.TestEnumSet> read = ((EnumSetWritable <TestEnumSetWritable.TestEnumSet >)ObjectWritable.ReadObject(@in, null)).Get(); Assert.Equal(read, nonEmptyFlag); }