62 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System.Buffers.Binary;
 | 
						|
using Common;
 | 
						|
 | 
						|
namespace server.test;
 | 
						|
 | 
						|
public class CommonTest
 | 
						|
{
 | 
						|
    [Fact]
 | 
						|
    public void ReverseBytesTest()
 | 
						|
    {
 | 
						|
        var rnd = new Random();
 | 
						|
        var bytesLen = 8;
 | 
						|
        var bytesArray = new byte[bytesLen];
 | 
						|
        rnd.NextBytes(bytesArray);
 | 
						|
 | 
						|
        var rev2Bytes = new byte[] {
 | 
						|
            bytesArray[1],
 | 
						|
            bytesArray[0],
 | 
						|
            bytesArray[3],
 | 
						|
            bytesArray[2],
 | 
						|
            bytesArray[5],
 | 
						|
            bytesArray[4],
 | 
						|
            bytesArray[7],
 | 
						|
            bytesArray[6],
 | 
						|
        };
 | 
						|
        Assert.Equal(Number.ReverseBytes(bytesArray, 2).Value, rev2Bytes);
 | 
						|
 | 
						|
        var rev4Bytes = new byte[] {
 | 
						|
            bytesArray[3],
 | 
						|
            bytesArray[2],
 | 
						|
            bytesArray[1],
 | 
						|
            bytesArray[0],
 | 
						|
            bytesArray[7],
 | 
						|
            bytesArray[6],
 | 
						|
            bytesArray[5],
 | 
						|
            bytesArray[4],
 | 
						|
        };
 | 
						|
        Assert.Equal(Number.ReverseBytes(bytesArray, 4).Value, rev4Bytes);
 | 
						|
    }
 | 
						|
 | 
						|
    [Fact]
 | 
						|
    public void ToBitTest()
 | 
						|
    {
 | 
						|
        Assert.Equal(Number.ToBit(0xFF, 3).Value, true);
 | 
						|
        Assert.Equal(Number.ToBit(0x00, 3).Value, false);
 | 
						|
        Assert.Equal(Number.ToBit(0xAA, 3).Value, true);
 | 
						|
        Assert.Equal(Number.ToBit(0xAA, 2).Value, false);
 | 
						|
    }
 | 
						|
 | 
						|
    [Fact]
 | 
						|
    public void ReverseBits()
 | 
						|
    {
 | 
						|
        Assert.Equal(BinaryPrimitives.ReverseEndianness((byte)0xA0), (byte)0x50);
 | 
						|
 | 
						|
        var bytesSrc = new byte[] { 0xAB, 0x00, 0x00, 0x01 };
 | 
						|
        var bytes = new byte[4];
 | 
						|
        for (int i = 0; i < 4; i++)
 | 
						|
            bytes[i] = BinaryPrimitives.ReverseEndianness(bytesSrc[i]);
 | 
						|
        Assert.Equal(bytesSrc, new byte[] { 0xD5, 0x00, 0x00, 0x80 });
 | 
						|
    }
 | 
						|
}
 |