Implemented Medium, UnsignedMedium (#241) (#244)

Motivation:
In network programming the int24/uint24 is required in some cases

Modifications:
- added MediumUtil class to DotNetty.Common that provides ToMediumInt and ToUnsignedMediumInt
- added GetMedium, GetUnsignedMedium, SetMedium, ReadMedium, ReadUnsignedMedium, WriteUnsignedMedium, WriteMedium to IByteBuffer and their implementation in various buffers
- added SwapMedium to ByteBufferUtil

Result:
- int24/uint24 support
This commit is contained in:
ScarletKuro 2017-05-12 21:47:14 +03:00 коммит произвёл Max Gortman
Родитель 95f79e1cd2
Коммит ee07830122
14 изменённых файлов: 577 добавлений и 0 удалений

Просмотреть файл

@ -57,6 +57,7 @@ namespace DotNetty.Buffers
{
throw new IndexOutOfRangeException($"ReaderIndex: {readerIndex} (expected: 0 <= readerIndex <= writerIndex({this.WriterIndex})");
}
this.ReaderIndex = readerIndex;
return this;
}
@ -276,6 +277,19 @@ namespace DotNetty.Buffers
}
}
public virtual int GetMedium(int index)
{
this.CheckIndex(index, 3);
return this._GetMedium(index);
}
public virtual int GetUnsignedMedium(int index)
{
return this.GetMedium(index).ToUnsignedMediumInt();
}
protected abstract int _GetMedium(int index);
public virtual int GetInt(int index)
{
this.CheckIndex(index, 4);
@ -386,6 +400,15 @@ namespace DotNetty.Buffers
protected abstract void _SetLong(int index, long value);
public virtual IByteBuffer SetMedium(int index, int value)
{
this.CheckIndex(index, 3);
this._SetMedium(index, value);
return this;
}
protected abstract void _SetMedium(int index, int value);
public virtual IByteBuffer SetChar(int index, char value)
{
this.SetShort(index, value);
@ -467,6 +490,17 @@ namespace DotNetty.Buffers
return v;
}
public virtual int ReadMedium()
{
this.CheckReadableBytes(3);
int v = this._GetMedium(this.ReaderIndex);
this.ReaderIndex += 3;
return v;
}
public virtual int ReadUnsignedMedium()
{
return this.ReadMedium().ToUnsignedMediumInt();
}
public virtual uint ReadUnsignedInt()
{
unchecked
@ -588,6 +622,21 @@ namespace DotNetty.Buffers
return this;
}
public IByteBuffer WriteUnsignedMedium(int value)
{
this.WriteMedium(value.ToUnsignedMediumInt());
return this;
}
public virtual IByteBuffer WriteMedium(int value)
{
this.EnsureAccessible();
this.EnsureWritable(3);
this._SetMedium(this.WriterIndex, value);
this.WriterIndex += 3;
return this;
}
public virtual IByteBuffer WriteInt(int value)
{
this.EnsureAccessible();

Просмотреть файл

@ -117,6 +117,18 @@ namespace DotNetty.Buffers
return base.GetByte(index);
}
public override int GetMedium(int index)
{
this.RecordLeakNonRefCountingOperation();
return base.GetMedium(index);
}
public override int GetUnsignedMedium(int index)
{
this.RecordLeakNonRefCountingOperation();
return base.GetUnsignedMedium(index);
}
public override short GetShort(int index)
{
this.RecordLeakNonRefCountingOperation();
@ -214,6 +226,12 @@ namespace DotNetty.Buffers
return base.SetByte(index, value);
}
public override IByteBuffer SetMedium(int index, int value)
{
this.RecordLeakNonRefCountingOperation();
return base.SetMedium(index, value);
}
public override IByteBuffer SetShort(int index, int value)
{
this.RecordLeakNonRefCountingOperation();
@ -318,6 +336,18 @@ namespace DotNetty.Buffers
return base.ReadUnsignedShort();
}
public override int ReadMedium()
{
this.RecordLeakNonRefCountingOperation();
return base.ReadMedium();
}
public override int ReadUnsignedMedium()
{
this.RecordLeakNonRefCountingOperation();
return base.ReadUnsignedMedium();
}
public override int ReadInt()
{
this.RecordLeakNonRefCountingOperation();
@ -427,6 +457,12 @@ namespace DotNetty.Buffers
return base.WriteInt(value);
}
public override IByteBuffer WriteMedium(int value)
{
this.RecordLeakNonRefCountingOperation();
return base.WriteMedium(value);
}
public override IByteBuffer WriteLong(long value)
{
this.RecordLeakNonRefCountingOperation();

Просмотреть файл

@ -382,6 +382,15 @@ namespace DotNetty.Buffers
/// </summary>
public static short SwapShort(short value) => (short)(((value & 0xFF) << 8) | (value >> 8) & 0xFF);
/// <summary>
/// Toggles the endianness of the specified 24-bit medium integer.
/// </summary>
public static int SwapMedium(int value)
{
int swapped = value << 16 & 0xff0000 | value & 0xff00 | value.RightUShift(16) & 0xff;
return swapped.ToMediumInt();
}
/// <summary>
/// Read the given amount of bytes into a new {@link ByteBuf} that is allocated from the {@link ByteBufAllocator}.
/// </summary>

Просмотреть файл

@ -702,6 +702,23 @@ namespace DotNetty.Buffers
}
}
protected override int _GetMedium(int index)
{
ComponentEntry c = this.FindComponent(index);
if (index + 3 <= c.EndOffset)
{
return c.Buffer.GetMedium(index - c.Offset);
}
else if (this.Order == ByteOrder.BigEndian)
{
return this._GetShort(index) << 8 | this._GetByte(index + 2);
}
else
{
return (ushort)this._GetShort(index) | (sbyte)this._GetByte(index + 2) << 16;
}
}
protected override long _GetLong(int index)
{
ComponentEntry c = this.FindComponent(index);
@ -818,6 +835,25 @@ namespace DotNetty.Buffers
}
}
protected override void _SetMedium(int index, int value)
{
ComponentEntry c = this.FindComponent(index);
if (index + 3 <= c.EndOffset)
{
c.Buffer.SetMedium(index - c.Offset, value);
}
else if (this.Order == ByteOrder.BigEndian)
{
this._SetShort(index, (short)(value >> 8));
this._SetByte(index + 2, (byte)value);
}
else
{
this._SetShort(index, (short)value);
this._SetByte(index + 2, (byte)(value.RightUShift(16)));
}
}
protected override void _SetInt(int index, int value)
{
ComponentEntry c = this.FindComponent(index);

Просмотреть файл

@ -41,6 +41,10 @@ namespace DotNetty.Buffers
protected override int _GetInt(int index) => this.buffer.GetInt(index);
public override int GetMedium(int index) => this._GetMedium(index);
protected override int _GetMedium(int index) => this.buffer.GetMedium(index);
public override long GetLong(int index) => this._GetLong(index);
protected override long _GetLong(int index) => this.buffer.GetLong(index);
@ -93,6 +97,14 @@ namespace DotNetty.Buffers
protected override void _SetInt(int index, int value) => this.buffer.SetInt(index, value);
public override IByteBuffer SetMedium(int index, int value)
{
this._SetMedium(index, value);
return this;
}
protected override void _SetMedium(int index, int value) => this.buffer.SetMedium(index, value);
public override IByteBuffer SetLong(int index, long value)
{
this._SetLong(index, value);

Просмотреть файл

@ -158,6 +158,16 @@ namespace DotNetty.Buffers
throw new IndexOutOfRangeException();
}
public int GetMedium(int index)
{
throw new IndexOutOfRangeException();
}
public int GetUnsignedMedium(int index)
{
throw new IndexOutOfRangeException();
}
public IByteBuffer GetBytes(int index, IByteBuffer destination) => this.CheckIndex(index, destination.WritableBytes);
public IByteBuffer GetBytes(int index, IByteBuffer destination, int length) => this.CheckIndex(index, length);
@ -195,11 +205,17 @@ namespace DotNetty.Buffers
throw new IndexOutOfRangeException();
}
public IByteBuffer SetUnsignedInt(int index, uint value)
{
throw new IndexOutOfRangeException();
}
public IByteBuffer SetMedium(int index, int value)
{
throw new IndexOutOfRangeException();
}
public IByteBuffer SetLong(int index, long value)
{
throw new IndexOutOfRangeException();
@ -269,6 +285,16 @@ namespace DotNetty.Buffers
throw new IndexOutOfRangeException();
}
public int ReadMedium()
{
throw new IndexOutOfRangeException();
}
public int ReadUnsignedMedium()
{
throw new IndexOutOfRangeException();
}
public char ReadChar()
{
throw new IndexOutOfRangeException();
@ -330,6 +356,16 @@ namespace DotNetty.Buffers
throw new IndexOutOfRangeException();
}
public IByteBuffer WriteUnsignedMedium(int value)
{
throw new IndexOutOfRangeException();
}
public IByteBuffer WriteMedium(int value)
{
throw new IndexOutOfRangeException();
}
public IByteBuffer WriteChar(char value)
{
throw new IndexOutOfRangeException();

Просмотреть файл

@ -259,6 +259,28 @@ namespace DotNetty.Buffers
/// </exception>
long GetLong(int index);
/// <summary>
/// Gets a 24-bit medium integer at the specified absolute index in this buffer.
/// This method does not modify <see cref="ReaderIndex" /> or <see cref="WriterIndex" />
/// of this buffer.
/// </summary>
/// <exception cref="IndexOutOfRangeException">
/// if the specified <see cref="index" /> is less than <c>0</c> or
/// <c>index + 3</c> greater than <see cref="Capacity" />
/// </exception>
int GetMedium(int index);
/// <summary>
/// Gets an unsigned 24-bit medium integer at the specified absolute index in this buffer.
/// This method does not modify <see cref="ReaderIndex" /> or <see cref="WriterIndex" />
/// of this buffer.
/// </summary>
/// <exception cref="IndexOutOfRangeException">
/// if the specified <see cref="index" /> is less than <c>0</c> or
/// <c>index + 3</c> greater than <see cref="Capacity" />
/// </exception>
int GetUnsignedMedium(int index);
/// <summary>
/// Gets a char at the specified absolute <see cref="index" /> in this buffer.
/// This method does not modify <see cref="ReaderIndex" /> or <see cref="WriterIndex" />
@ -409,6 +431,17 @@ namespace DotNetty.Buffers
/// </exception>
IByteBuffer SetUnsignedInt(int index, uint value);
/// <summary>
/// Sets the specified 24-bit medium integer at the specified absolute <see cref="index" /> in this buffer.
/// Note that the most significant byte is ignored in the specified value.
/// This method does not directly modify <see cref="ReaderIndex" /> or <see cref="WriterIndex" /> of this buffer.
/// </summary>
/// <exception cref="IndexOutOfRangeException">
/// if the specified <see cref="index" /> is less than <c>0</c> or
/// <c>index + 3</c> greater than <see cref="Capacity" />
/// </exception>
IByteBuffer SetMedium(int index, int value);
/// <summary>
/// Sets the specified long integer at the specified absolute <see cref="index" /> in this buffer.
/// This method does not directly modify <see cref="ReaderIndex" /> or <see cref="WriterIndex" /> of this buffer.
@ -527,6 +560,20 @@ namespace DotNetty.Buffers
/// <exception cref="IndexOutOfRangeException">if <see cref="ReadableBytes" /> is less than <c>2</c></exception>
short ReadShort();
/// <summary>
/// Gets a 24-bit medium integer at the current <see cref="ReaderIndex" /> and increases the <see cref="ReaderIndex" />
/// by <c>3</c> in this buffer.
/// </summary>
/// <exception cref="IndexOutOfRangeException">if <see cref="ReadableBytes" /> is less than <c>3</c></exception>
int ReadMedium();
/// <summary>
/// Gets an unsigned 24-bit medium integer at the current <see cref="ReaderIndex" /> and increases the <see cref="ReaderIndex" />
/// by <c>3</c> in this buffer.
/// </summary>
/// <exception cref="IndexOutOfRangeException">if <see cref="ReadableBytes" /> is less than <c>3</c></exception>
int ReadUnsignedMedium();
/// <summary>
/// Gets an unsigned short at the current <see cref="ReaderIndex" /> and increases the <see cref="ReaderIndex" />
/// by <c>2</c> in this buffer.
@ -619,6 +666,10 @@ namespace DotNetty.Buffers
IByteBuffer WriteDouble(double value);
IByteBuffer WriteUnsignedMedium(int value);
IByteBuffer WriteMedium(int value);
IByteBuffer WriteBytes(IByteBuffer src);
IByteBuffer WriteBytes(IByteBuffer src, int length);

Просмотреть файл

@ -59,6 +59,14 @@ namespace DotNetty.Buffers
(long)this.Memory[index + 7] & 0xff;
}
protected override int _GetMedium(int index)
{
index = this.Idx(index);
return (sbyte)this.Memory[index] << 16 |
this.Memory[index + 1] << 8 |
this.Memory[index + 2];
}
public override IByteBuffer GetBytes(int index, IByteBuffer dst, int dstIndex, int length)
{
this.CheckDstIndex(index, length, dstIndex, dst.Capacity);
@ -105,6 +113,14 @@ namespace DotNetty.Buffers
this.Memory[index + 3] = (byte)value;
}
protected override void _SetMedium(int index, int value)
{
index = this.Idx(index);
this.Memory[index] = (byte)value.RightUShift(16);
this.Memory[index + 1] = (byte)value.RightUShift(8);
this.Memory[index + 2] = (byte)value;
}
protected override void _SetLong(int index, long value)
{
index = this.Idx(index);

Просмотреть файл

@ -84,6 +84,8 @@ namespace DotNetty.Buffers
protected override long _GetLong(int index) => this.buffer.GetLong(index + this.adjustment);
protected override int _GetMedium(int index) => this.buffer.GetMedium(index + this.adjustment);
public override IByteBuffer Duplicate()
{
IByteBuffer duplicate = this.buffer.Slice(this.adjustment, this.length);
@ -142,6 +144,8 @@ namespace DotNetty.Buffers
protected override void _SetLong(int index, long value) => this.buffer.SetLong(index + this.adjustment, value);
protected override void _SetMedium(int index, int value) => this.buffer.SetMedium(index + this.adjustment, value);
public override IByteBuffer SetBytes(int index, byte[] src, int srcIndex, int length)
{
this.CheckIndex(index, length);

Просмотреть файл

@ -186,6 +186,10 @@ namespace DotNetty.Buffers
public long GetLong(int index) => ByteBufferUtil.SwapLong(this.buf.GetLong(index));
public int GetMedium(int index) => ByteBufferUtil.SwapMedium(this.buf.GetMedium(index));
public int GetUnsignedMedium(int index) => this.GetMedium(index).ToUnsignedMediumInt();
public char GetChar(int index) => (char)this.GetShort(index);
public double GetDouble(int index) => BitConverter.Int64BitsToDouble(this.GetLong(index));
@ -279,6 +283,12 @@ namespace DotNetty.Buffers
return this;
}
public IByteBuffer SetMedium(int index, int value)
{
this.buf.SetMedium(index, ByteBufferUtil.SwapMedium(value));
return this;
}
public IByteBuffer SetChar(int index, char value)
{
this.SetShort(index, (short)value);
@ -349,6 +359,10 @@ namespace DotNetty.Buffers
public long ReadLong() => ByteBufferUtil.SwapLong(this.buf.ReadLong());
public int ReadMedium() => ByteBufferUtil.SwapMedium(this.buf.ReadMedium());
public int ReadUnsignedMedium() => this.ReadMedium().ToUnsignedMediumInt();
public char ReadChar() => (char)this.ReadShort();
public double ReadDouble() => BitConverter.Int64BitsToDouble(this.ReadLong());
@ -448,6 +462,18 @@ namespace DotNetty.Buffers
return this;
}
public IByteBuffer WriteUnsignedMedium(int value)
{
this.buf.WriteMedium(ByteBufferUtil.SwapMedium(value.ToUnsignedMediumInt()));
return this;
}
public IByteBuffer WriteMedium(int value)
{
this.buf.WriteMedium(ByteBufferUtil.SwapMedium(value));
return this;
}
public IByteBuffer WriteChar(char value)
{
this.WriteShort(value);

Просмотреть файл

@ -3,6 +3,7 @@
namespace DotNetty.Buffers
{
using DotNetty.Common.Utilities;
using System;
using System.Diagnostics.Contracts;
using System.IO;
@ -217,6 +218,19 @@ namespace DotNetty.Buffers
return this._GetLong(index);
}
public override int GetMedium(int index)
{
this.EnsureAccessible();
return this._GetMedium(index);
}
protected override int _GetMedium(int index)
{
return (sbyte)this.array[index] << 16 |
this.array[index + 1] << 8 |
this.array[index + 2];
}
protected override long _GetLong(int index)
{
unchecked
@ -258,6 +272,24 @@ namespace DotNetty.Buffers
}
}
public override IByteBuffer SetMedium(int index, int value)
{
this.EnsureAccessible();
this._SetMedium(index, value);
return this;
}
protected override void _SetMedium(int index, int value)
{
unchecked
{
uint unsignedValue = (uint)value;
this.array[index] = (byte)(unsignedValue >> 16);
this.array[index + 1] = (byte)(unsignedValue >> 8);
this.array[index + 2] = (byte)value;
}
}
public override IByteBuffer SetInt(int index, int value)
{
this.EnsureAccessible();

Просмотреть файл

@ -137,6 +137,10 @@ namespace DotNetty.Buffers
public virtual long GetLong(int index) => this.Buf.GetLong(index);
public virtual int GetMedium(int index) => this.Buf.GetMedium(index);
public virtual int GetUnsignedMedium(int index) => this.Buf.GetUnsignedMedium(index);
public virtual char GetChar(int index) => this.Buf.GetChar(index);
// todo: port: complete
@ -209,6 +213,12 @@ namespace DotNetty.Buffers
return this;
}
public virtual IByteBuffer SetMedium(int index, int value)
{
this.Buf.SetMedium(index, value);
return this;
}
public virtual IByteBuffer SetUnsignedInt(int index, uint value) => this.Buf.SetUnsignedInt(index, value);
public virtual IByteBuffer SetLong(int index, long value)
@ -289,6 +299,10 @@ namespace DotNetty.Buffers
public virtual long ReadLong() => this.Buf.ReadLong();
public virtual int ReadMedium() => this.Buf.ReadMedium();
public virtual int ReadUnsignedMedium() => this.Buf.ReadUnsignedMedium();
public virtual char ReadChar() => this.Buf.ReadChar();
// todo: port: complete
@ -387,6 +401,14 @@ namespace DotNetty.Buffers
return this;
}
public virtual IByteBuffer WriteUnsignedMedium(int value) => this.Buf.WriteUnsignedMedium(value);
public virtual IByteBuffer WriteMedium(int value)
{
this.Buf.WriteMedium(value);
return this;
}
// todo: port: complete
//public virtual IByteBuffer WriteFloat(float value)
//{

Просмотреть файл

@ -0,0 +1,35 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace DotNetty.Common.Utilities
{
using System;
public static class MediumUtil
{
//High byte bit-mask used when a 24-bit integer is stored within a 32-bit integer.
const uint MediumBitMask = 0xff000000;
public static int ToMediumInt(this int value)
{
// Check bit 23, the sign bit in a signed 24-bit integer
if ((value & 0x00800000) > 0)
{
// If the sign-bit is set, this number will be negative - set all high-byte bits (keeps 32-bit number in 24-bit range)
value |= unchecked((int)MediumBitMask);
}
else
{
// If the sign-bit is not set, this number will be positive - clear all high-byte bits (keeps 32-bit number in 24-bit range)
value &= ~unchecked((int)MediumBitMask);
}
return value;
}
public static int ToUnsignedMediumInt(this int value)
{
return (int)((uint)value & ~MediumBitMask);
}
}
}

Просмотреть файл

@ -146,6 +146,12 @@ namespace DotNetty.Buffers.Tests
[Fact]
public void GetLongBoundaryCheck1() => Assert.Throws<IndexOutOfRangeException>(() => this.buffer.GetLong(-1));
[Fact]
public void GetMediumBoundaryCheck1() => Assert.Throws<IndexOutOfRangeException>(() => this.buffer.GetMedium(-1));
[Fact]
public void GetMediumBoundaryCheck2() => Assert.Throws<IndexOutOfRangeException>(() => this.buffer.GetMedium(this.buffer.Capacity -2));
[Fact]
public void GetLongBoundaryCheck2() => Assert.Throws<IndexOutOfRangeException>(() => this.buffer.GetLong(this.buffer.Capacity - 7));
@ -241,6 +247,78 @@ namespace DotNetty.Buffers.Tests
}
}
[Fact]
public void TestRandomMediumAccess() => this.TestRandomMediumAccess(true);
[Fact]
public void TestRandomMediumLeAccess() => this.TestRandomMediumAccess(false);
public void TestRandomMediumAccess(bool testBigEndian)
{
for (int i = 0; i < this.buffer.Capacity - 2; i += 3)
{
int value = this.random.Next();
if (testBigEndian)
{
this.buffer.SetMedium(i, value);
}
else
{
this.buffer.WithOrder(ByteOrder.LittleEndian).SetMedium(i, value);
}
}
this.random = new Random(this.seed);
for (int i = 0; i < this.buffer.Capacity - 2; i += 3)
{
int value = this.random.Next() << 8 >> 8;
if (testBigEndian)
{
Assert.Equal(value, this.buffer.GetMedium(i));
}
else
{
Assert.Equal(value, this.buffer.WithOrder(ByteOrder.LittleEndian).GetMedium(i));
}
}
}
[Fact]
public void TestRandomUnsignedMediumAccess() => this.TestRandomUnsignedMediumAccess(true);
[Fact]
public void TestRandomUnsignedMediumLeAccess() => this.TestRandomUnsignedMediumAccess(false);
public void TestRandomUnsignedMediumAccess(bool testBigEndian)
{
for (int i = 0; i < this.buffer.Capacity - 2; i += 3)
{
int value = this.random.Next();
if (testBigEndian)
{
this.buffer.SetMedium(i, value);
}
else
{
this.buffer.WithOrder(ByteOrder.LittleEndian).SetMedium(i, value);
}
}
this.random = new Random(this.seed);
for (int i = 0; i < this.buffer.Capacity - 2; i += 3)
{
int value = this.random.Next().ToUnsignedMediumInt();
if (testBigEndian)
{
Assert.Equal(value, this.buffer.GetUnsignedMedium(i));
}
else
{
Assert.Equal(value, this.buffer.WithOrder(ByteOrder.LittleEndian).GetUnsignedMedium(i));
}
}
}
[Fact]
public void TestRandomShortAccess() => this.TestRandomShortAccess(true);
@ -569,6 +647,105 @@ namespace DotNetty.Buffers.Tests
Assert.False(this.buffer.IsWritable());
}
[Fact]
public void TestSequentialMediumAccess() => this.TestSequentialMediumAccess(true);
[Fact]
public void TestSequentialMediumLeAccess() => this.TestSequentialMediumAccess(false);
void TestSequentialMediumAccess(bool testBigEndian)
{
this.buffer.SetWriterIndex(0);
for (int i = 0; i < this.buffer.Capacity / 3 * 3; i += 3)
{
int value = this.random.Next();
Assert.Equal(i, this.buffer.WriterIndex);
Assert.True(this.buffer.IsWritable());
if (testBigEndian)
{
this.buffer.WriteMedium(value);
}
else
{
this.buffer.WithOrder(ByteOrder.LittleEndian).WriteMedium(value);
}
}
Assert.Equal(0, this.buffer.ReaderIndex);
Assert.Equal(this.buffer.Capacity / 3 * 3, this.buffer.WriterIndex);
Assert.Equal(this.buffer.Capacity % 3, this.buffer.WritableBytes);
this.random = new Random(this.seed);
for (int i = 0; i < this.buffer.Capacity / 3 * 3; i += 3)
{
int value = this.random.Next() << 8 >> 8;
Assert.Equal(i, this.buffer.ReaderIndex);
Assert.True(this.buffer.IsReadable());
if (testBigEndian)
{
Assert.Equal(value, this.buffer.ReadMedium());
}
else
{
Assert.Equal(value, this.buffer.WithOrder(ByteOrder.LittleEndian).ReadMedium());
}
}
Assert.Equal(this.buffer.Capacity / 3 * 3, this.buffer.ReaderIndex);
Assert.Equal(this.buffer.Capacity / 3 * 3, this.buffer.WriterIndex);
Assert.Equal(0, this.buffer.ReadableBytes);
Assert.Equal(this.buffer.Capacity % 3, this.buffer.WritableBytes);
}
[Fact]
public void TestSequentialUnsignedMediumAccess() => this.TestSequentialUnsignedMediumAccess(true);
[Fact]
public void TestSequentialUnsignedMediumLeAccess() => this.TestSequentialUnsignedMediumAccess(false);
void TestSequentialUnsignedMediumAccess(bool testBigEndian)
{
this.buffer.SetWriterIndex(0);
for (int i = 0; i < this.buffer.Capacity / 3 * 3; i += 3)
{
int value = this.random.Next();
Assert.Equal(i, this.buffer.WriterIndex);
Assert.True(this.buffer.IsWritable());
if (testBigEndian)
{
this.buffer.WriteUnsignedMedium(value);
}
else
{
this.buffer.WithOrder(ByteOrder.LittleEndian).WriteUnsignedMedium(value);
}
}
Assert.Equal(0, this.buffer.ReaderIndex);
Assert.Equal(this.buffer.Capacity / 3 * 3, this.buffer.WriterIndex);
Assert.Equal(this.buffer.Capacity % 3, this.buffer.WritableBytes);
this.random = new Random(this.seed);
for (int i = 0; i < this.buffer.Capacity / 3 * 3; i += 3)
{
int value = this.random.Next().ToUnsignedMediumInt();
Assert.Equal(i, this.buffer.ReaderIndex);
Assert.True(this.buffer.IsReadable());
if (testBigEndian)
{
Assert.Equal(value, this.buffer.ReadUnsignedMedium());
}
else
{
Assert.Equal(value, this.buffer.WithOrder(ByteOrder.LittleEndian).ReadUnsignedMedium());
}
}
Assert.Equal(this.buffer.Capacity / 3 * 3, this.buffer.ReaderIndex);
Assert.Equal(this.buffer.Capacity / 3 * 3, this.buffer.WriterIndex);
Assert.Equal(0, this.buffer.ReadableBytes);
Assert.Equal(this.buffer.Capacity % 3, this.buffer.WritableBytes);
}
[Fact]
public void TestSequentialIntAccess() => this.TestSequentialIntAccess(true);
@ -1776,6 +1953,18 @@ namespace DotNetty.Buffers.Tests
[Fact]
public void TestGetByteAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().GetByte(0));
[Fact]
public void TestGetMediumAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().GetMedium(0));
[Fact]
public void TestGetMediumLeAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().WithOrder(ByteOrder.LittleEndian).GetMedium(0));
[Fact]
public void TestGetUnsignedMediumAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().GetUnsignedMedium(0));
[Fact]
public void TestGetUnsignedMediumLeAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().WithOrder(ByteOrder.LittleEndian).GetUnsignedMedium(0));
[Fact]
public void TestGetShortAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().GetShort(0));
@ -1836,6 +2025,12 @@ namespace DotNetty.Buffers.Tests
[Fact]
public void TestSetShortAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().SetShort(0, 1));
[Fact]
public void TestSetMediumAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().SetMedium(0, 1));
[Fact]
public void TestSetMediumLeAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().WithOrder(ByteOrder.LittleEndian).SetMedium(0, 1));
[Fact]
public void TestSetShortLeAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().WithOrder(ByteOrder.LittleEndian).SetShort(0, 1));
@ -1884,6 +2079,18 @@ namespace DotNetty.Buffers.Tests
[Fact]
public void TestReadShortLeAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().WithOrder(ByteOrder.LittleEndian).ReadShort());
[Fact]
public void TestReadMediumAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().ReadMedium());
[Fact]
public void TestReadMediumLeAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().WithOrder(ByteOrder.LittleEndian).ReadMedium());
[Fact]
public void TestReadUnsignedMediumAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().ReadUnsignedMedium());
[Fact]
public void TestReadUnsignedMediumLeAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().WithOrder(ByteOrder.LittleEndian).ReadUnsignedMedium());
[Fact]
public void TestReadUnsignedShortAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().ReadUnsignedShort());
@ -1938,6 +2145,12 @@ namespace DotNetty.Buffers.Tests
[Fact]
public void TestWriteByteAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().WriteByte(1));
[Fact]
public void TestWriteMediumAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().WriteMedium(1));
[Fact]
public void TestWriteMediumLeAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().WithOrder(ByteOrder.LittleEndian).WriteMedium(1));
[Fact]
public void TestWriteShortAfterRelease() => Assert.Throws<IllegalReferenceCountException>(() => this.ReleasedBuffer().WriteShort(1));