|
| 1 | +using System; |
| 2 | +namespace MLAPI.Tests.NetworkingManagerComponents.Binary |
| 3 | +{ |
| 4 | + using MLAPI.NetworkingManagerComponents.Binary; |
| 5 | + using NUnit.Framework; |
| 6 | + using static MLAPI.NetworkingManagerComponents.Binary.BitStream; |
| 7 | + |
| 8 | + [TestFixture] |
| 9 | + public class BitStreamTest |
| 10 | + { |
| 11 | + |
| 12 | + [Test] |
| 13 | + public void TestEmptyStream() |
| 14 | + { |
| 15 | + BitStream bitStream = new BitStream(new byte[100]); |
| 16 | + // ideally an empty stream should take no space |
| 17 | + Assert.That(bitStream.Length, Is.EqualTo(100)); |
| 18 | + } |
| 19 | + |
| 20 | + [Test] |
| 21 | + public void TestBool() |
| 22 | + { |
| 23 | + BitStream bitStream = new BitStream(new byte[100]); |
| 24 | + bitStream.WriteBit(true); |
| 25 | + |
| 26 | + // we only wrote 1 bit, so the size should be as small as possible |
| 27 | + // which is 1 byte, regardless of how big the buffer is |
| 28 | + Assert.That(bitStream.Length, Is.EqualTo(100)); |
| 29 | + } |
| 30 | + |
| 31 | + [Test] |
| 32 | + public void TestGrow() |
| 33 | + { |
| 34 | + // stream should not grow when given a buffer |
| 35 | + BitStream bitStream = new BitStream(new byte[0]); |
| 36 | + Assert.That( |
| 37 | + () => { bitStream.WriteInt64(long.MaxValue); }, |
| 38 | + Throws.TypeOf<CapacityException>()); |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public void TestInOutBool() |
| 43 | + { |
| 44 | + byte[] buffer = new byte[100]; |
| 45 | + |
| 46 | + BitStream outStream = new BitStream(buffer); |
| 47 | + outStream.WriteBit(true); |
| 48 | + outStream.WriteBit(false); |
| 49 | + outStream.WriteBit(true); |
| 50 | + |
| 51 | + |
| 52 | + // the bit should now be stored in the buffer, lets see if it comes out |
| 53 | + |
| 54 | + BitStream inStream = new BitStream(buffer); |
| 55 | + |
| 56 | + Assert.That(inStream.ReadBit(), Is.True); |
| 57 | + Assert.That(inStream.ReadBit(), Is.False); |
| 58 | + Assert.That(inStream.ReadBit(), Is.True); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + [Test] |
| 63 | + public void TestInOutPacked64Bit() |
| 64 | + { |
| 65 | + byte[] buffer = new byte[100]; |
| 66 | + |
| 67 | + long someNumber = 1469598103934656037; |
| 68 | + |
| 69 | + |
| 70 | + BitStream outStream = new BitStream(buffer); |
| 71 | + outStream.WriteInt64Packed(someNumber); |
| 72 | + |
| 73 | + |
| 74 | + // the bit should now be stored in the buffer, lets see if it comes out |
| 75 | + |
| 76 | + BitStream inStream = new BitStream(buffer); |
| 77 | + long result = inStream.ReadInt64Packed(); |
| 78 | + |
| 79 | + Assert.That(result, Is.EqualTo(someNumber)); |
| 80 | + } |
| 81 | + |
| 82 | + [Test] |
| 83 | + public void TestInOutBytes() |
| 84 | + { |
| 85 | + byte[] buffer = new byte[100]; |
| 86 | + |
| 87 | + byte someNumber = 0xff; |
| 88 | + |
| 89 | + |
| 90 | + BitStream outStream = new BitStream(buffer); |
| 91 | + outStream.WriteByte(someNumber); |
| 92 | + |
| 93 | + |
| 94 | + // the bit should now be stored in the buffer, lets see if it comes out |
| 95 | + |
| 96 | + BitStream inStream = new BitStream(buffer); |
| 97 | + Assert.That(inStream.ReadByte(), Is.EqualTo(someNumber)); |
| 98 | + |
| 99 | + // wtf this is standard behaviour |
| 100 | + //Assert.Fail("Read byte should return byte, but it returns int"); |
| 101 | + } |
| 102 | + |
| 103 | + [Test] |
| 104 | + public void TestInOutInt16() |
| 105 | + { |
| 106 | + byte[] buffer = new byte[100]; |
| 107 | + |
| 108 | + short someNumber = 23223; |
| 109 | + |
| 110 | + |
| 111 | + BitStream outStream = new BitStream(buffer); |
| 112 | + outStream.WriteInt16(someNumber); |
| 113 | + |
| 114 | + |
| 115 | + // the bit should now be stored in the buffer, lets see if it comes out |
| 116 | + |
| 117 | + BitStream inStream = new BitStream(buffer); |
| 118 | + short result = inStream.ReadInt16(); |
| 119 | + |
| 120 | + Assert.That(result, Is.EqualTo(someNumber)); |
| 121 | + } |
| 122 | + |
| 123 | + [Test] |
| 124 | + public void TestInOutInt32() |
| 125 | + { |
| 126 | + byte[] buffer = new byte[100]; |
| 127 | + |
| 128 | + int someNumber = 23234223; |
| 129 | + |
| 130 | + |
| 131 | + BitStream outStream = new BitStream(buffer); |
| 132 | + outStream.WriteInt32(someNumber); |
| 133 | + |
| 134 | + |
| 135 | + // the bit should now be stored in the buffer, lets see if it comes out |
| 136 | + |
| 137 | + BitStream inStream = new BitStream(buffer); |
| 138 | + int result = inStream.ReadInt32(); |
| 139 | + |
| 140 | + Assert.That(result, Is.EqualTo(someNumber)); |
| 141 | + } |
| 142 | + |
| 143 | + [Test] |
| 144 | + public void TestInOutMultiple() |
| 145 | + { |
| 146 | + byte[] buffer = new byte[100]; |
| 147 | + |
| 148 | + short someNumber = -12423; |
| 149 | + short someNumber2 = 9322; |
| 150 | + |
| 151 | + BitStream outStream = new BitStream(buffer); |
| 152 | + outStream.WriteInt16(someNumber); |
| 153 | + outStream.WriteInt16(someNumber2); |
| 154 | + |
| 155 | + |
| 156 | + // the bit should now be stored in the buffer, lets see if it comes out |
| 157 | + |
| 158 | + BitStream inStream = new BitStream(buffer); |
| 159 | + short result = inStream.ReadInt16(); |
| 160 | + short result2 = inStream.ReadInt16(); |
| 161 | + |
| 162 | + Assert.That(result, Is.EqualTo(someNumber)); |
| 163 | + Assert.That(result2, Is.EqualTo(someNumber2)); |
| 164 | + } |
| 165 | + |
| 166 | + [Test] |
| 167 | + public void TestLength() |
| 168 | + { |
| 169 | + BitStream inStream = new BitStream(4); |
| 170 | + Assert.That(inStream.Length, Is.EqualTo(0)); |
| 171 | + inStream.WriteByte(1); |
| 172 | + Assert.That(inStream.Length, Is.EqualTo(1)); |
| 173 | + inStream.WriteByte(2); |
| 174 | + Assert.That(inStream.Length, Is.EqualTo(2)); |
| 175 | + inStream.WriteByte(3); |
| 176 | + Assert.That(inStream.Length, Is.EqualTo(3)); |
| 177 | + inStream.WriteByte(4); |
| 178 | + Assert.That(inStream.Length, Is.EqualTo(4)); |
| 179 | + } |
| 180 | + |
| 181 | + [Test] |
| 182 | + public void TestCapacityGrowth() |
| 183 | + { |
| 184 | + BitStream inStream = new BitStream(4); |
| 185 | + Assert.That(inStream.Capacity, Is.EqualTo(4)); |
| 186 | + |
| 187 | + inStream.WriteByte(1); |
| 188 | + inStream.WriteByte(2); |
| 189 | + inStream.WriteByte(3); |
| 190 | + inStream.WriteByte(4); |
| 191 | + inStream.WriteByte(5); |
| 192 | + |
| 193 | + // buffer should grow and the reported length |
| 194 | + // should not waste any space |
| 195 | + // note MemoryStream makes a distinction between Length and Capacity |
| 196 | + Assert.That(inStream.Length, Is.EqualTo(5)); |
| 197 | + Assert.That(inStream.Capacity, Is.GreaterThanOrEqualTo(5)); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments