public void GetEncodedLength() { int[] sizes = { 0, 115, 1999, 14510, 116384, 1681115 }; NboLengthEncoder encoder; for (int i = 0; i < sizes.Length; i++) { encoder = NboLengthEncoder.GetInstance(sizes[i]); Assert.IsTrue(encoder.EncodedLength == ((i / 2) + 1)); } }
public void MaximumLength() { int[] sizes = { 0, 115, 1999, 14510, 116384, 1681115 }; int[] expectedSizes = { 255, 255, 65535, 65535, 16777215, 16777215 }; NboLengthEncoder encoder; for (int i = 0; i < sizes.Length; i++) { encoder = NboLengthEncoder.GetInstance(sizes[i]); Assert.IsTrue(encoder.MaximumLength == expectedSizes[i]); } }
public void GetInstance() { NboLengthEncoder encoder; int[] sizes = { 0, 115, 1999, 14510, 116384, 1681115 }; for (int i = 0; i < sizes.Length; i++) { encoder = NboLengthEncoder.GetInstance(sizes[i]); Assert.IsTrue(encoder == NboLengthEncoder.GetInstance(sizes[i])); } try { encoder = NboLengthEncoder.GetInstance(16777216); Assert.Fail(); } catch (ArgumentOutOfRangeException e) { Assert.IsTrue(e.ParamName == "maximumLength"); } }
public void Encode() { FormatterContext formatterContext = new FormatterContext(FormatterContext.DefaultBufferSize); int[] sizes = { 0, 115, 1999, 14510, 116384, 1681115 }; byte[][] expectedData = { new byte[] { 0 }, new byte[] { 0x73 }, new byte[] {7, 0xCF }, new byte[] {0x38, 0xAE }, new byte[] {1, 0xC6, 0xA0 }, new byte[] {0x19, 0xA6, 0xDB } }; NboLengthEncoder encoder; byte[] data; for (int i = 0; i < sizes.Length; i++) { encoder = NboLengthEncoder.GetInstance(sizes[i]); encoder.Encode(sizes[i], ref formatterContext); data = formatterContext.GetData(); Assert.IsTrue(data.Length == expectedData[i].Length); for (int j = expectedData[i].Length - 1; j >= 0; j--) { Assert.IsTrue(data[j] == expectedData[i][j]); } formatterContext.Clear(); } encoder = NboLengthEncoder.GetInstance(65535); try { encoder.Encode(65536, ref formatterContext); Assert.Fail(); } catch (ArgumentOutOfRangeException e) { Assert.IsTrue(e.ParamName == "length"); } }
public void Decode() { ParserContext parserContext = new ParserContext(ParserContext.DefaultBufferSize); NboLengthEncoder encoder; int[] sizes = { 0, 115, 1999, 14510, 116384, 1681115 }; int length; int i; parserContext.Write(new byte[] { 0, 0x73, 7, 0xCF, 0x38, 0xAE, 1, 0xC6, 0xA0, 0x19, 0xA6, 0xDB }); parserContext.Write("Some data"); for (i = 0; i < sizes.Length; i++) { encoder = NboLengthEncoder.GetInstance(sizes[i]); length = encoder.Decode(ref parserContext); Assert.IsTrue(length == sizes[i]); } }