|
| 1 | +using Source.Common.Bitbuffers; |
| 2 | + |
| 3 | +using System.Numerics; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +using static Source.Common.CoordSize; |
| 7 | + |
| 8 | +namespace Source.Common.Tests; |
| 9 | + |
| 10 | +public class BitBufferTests |
| 11 | +{ |
| 12 | + const int BufferBytes = 16; |
| 13 | + const byte Marker = 0x5A; |
| 14 | + |
| 15 | + static uint Mask(int bits) => bits == 32 ? uint.MaxValue : (1u << bits) - 1; |
| 16 | + |
| 17 | + static string Written(bf_write writer, byte[] data) => Convert.ToHexString(data, 0, BitBuffer.BitByte(writer.BitsWritten)); |
| 18 | + |
| 19 | + static int SetBits(byte[] data) => data.Sum(b => BitOperations.PopCount(b)); |
| 20 | + |
| 21 | + static bf_read StringThenMarker(string text) { |
| 22 | + byte[] data = new byte[64]; |
| 23 | + bf_write writer = new(data, data.Length); |
| 24 | + writer.WriteString(text); |
| 25 | + writer.WriteByte(Marker); |
| 26 | + return new bf_read(data, data.Length); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void ReadUBitLongReadsWhatWasWrittenAtEveryOffsetAndWidth() { |
| 31 | + Random random = new(1234); |
| 32 | + byte[] data = new byte[BufferBytes]; |
| 33 | + |
| 34 | + for (int offset = 0; offset < 32; offset++) { |
| 35 | + for (int bits = 1; bits <= 32; bits++) { |
| 36 | + uint value = (uint)random.NextInt64() & Mask(bits); |
| 37 | + |
| 38 | + Array.Clear(data); |
| 39 | + bf_write writer = new(data, data.Length); |
| 40 | + writer.Seek(offset); |
| 41 | + writer.WriteUBitLong(value, bits); |
| 42 | + |
| 43 | + bf_read reader = new(data, data.Length); |
| 44 | + reader.Seek(offset); |
| 45 | + Assert.Equal(value, reader.ReadUBitLong(bits)); |
| 46 | + Assert.Equal(offset + bits, reader.BitsRead); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void ReadUBitLongOnlyReturnsTheRequestedBits() { |
| 53 | + byte[] data = new byte[BufferBytes]; |
| 54 | + Array.Fill(data, (byte)0xFF); |
| 55 | + |
| 56 | + for (int offset = 0; offset < 32; offset++) { |
| 57 | + for (int bits = 1; bits <= 32; bits++) { |
| 58 | + bf_read reader = new(data, data.Length); |
| 59 | + reader.Seek(offset); |
| 60 | + Assert.Equal(Mask(bits), reader.ReadUBitLong(bits)); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void ReadUBitLongPastTheEndOverflows() { |
| 67 | + byte[] data = new byte[4]; |
| 68 | + bf_read reader = new(data, data.Length); |
| 69 | + reader.ReadUBitLong(20); |
| 70 | + |
| 71 | + Assert.Equal(0u, reader.ReadUBitLong(13)); |
| 72 | + Assert.True(reader.Overflowed); |
| 73 | + Assert.Equal(32, reader.BitsRead); |
| 74 | + } |
| 75 | + |
| 76 | + [Theory] |
| 77 | + [InlineData(0x12345678u, 0x12345678u, false)] |
| 78 | + [InlineData(0x12345678u, 0x12345679u, true)] |
| 79 | + [InlineData(0x80000000u, 0x00000000u, true)] |
| 80 | + public void CompareBitsReportsDifferenceAndAdvancesBothReaders(uint first, uint second, bool different) { |
| 81 | + for (int offset = 0; offset < 32; offset++) { |
| 82 | + byte[] data1 = new byte[BufferBytes]; |
| 83 | + byte[] data2 = new byte[BufferBytes]; |
| 84 | + |
| 85 | + bf_write writer1 = new(data1, data1.Length); |
| 86 | + writer1.Seek(offset); |
| 87 | + writer1.WriteUBitLong(first, 32); |
| 88 | + bf_write writer2 = new(data2, data2.Length); |
| 89 | + writer2.Seek(offset); |
| 90 | + writer2.WriteUBitLong(second, 32); |
| 91 | + |
| 92 | + bf_read reader1 = new(data1, data1.Length); |
| 93 | + bf_read reader2 = new(data2, data2.Length); |
| 94 | + reader1.Seek(offset); |
| 95 | + reader2.Seek(offset); |
| 96 | + |
| 97 | + Assert.Equal(different, reader1.CompareBits(reader2, 32)); |
| 98 | + Assert.Equal(offset + 32, reader1.BitsRead); |
| 99 | + Assert.Equal(offset + 32, reader2.BitsRead); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [Theory] |
| 104 | + [InlineData(0, 1)] |
| 105 | + [InlineData(3, 7)] |
| 106 | + [InlineData(13, 19)] |
| 107 | + [InlineData(29, 9)] |
| 108 | + [InlineData(31, 31)] |
| 109 | + public void WritesOnlyTouchTheRequestedBits(int offset, int bits) { |
| 110 | + Action<bf_write>[] writes = [ |
| 111 | + w => w.WriteUBitLong(uint.MaxValue, bits, false), |
| 112 | + w => w.WriteSBitLong(-1, bits), |
| 113 | + w => w.WriteBits(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, bits) |
| 114 | + ]; |
| 115 | + |
| 116 | + foreach (var write in writes) { |
| 117 | + byte[] data = new byte[BufferBytes]; |
| 118 | + bf_write writer = new(data, data.Length); |
| 119 | + writer.Seek(offset); |
| 120 | + write(writer); |
| 121 | + |
| 122 | + Assert.Equal(offset + bits, writer.BitsWritten); |
| 123 | + Assert.Equal(bits, SetBits(data)); |
| 124 | + bf_read reader = new(data, data.Length); |
| 125 | + reader.Seek(offset); |
| 126 | + Assert.Equal(Mask(bits), reader.ReadUBitLong(bits)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + [Fact] |
| 131 | + public void NamedReaderConstructorDefaultsToTheWholeBuffer() { |
| 132 | + byte[] data = [0x78, 0x56, 0x34, 0x12, 0, 0, 0, 0]; |
| 133 | + bf_read reader = new("named", data, data.Length); |
| 134 | + |
| 135 | + Assert.Equal((nint)64, reader.MaxBits); |
| 136 | + Assert.Equal(0x12345678u, reader.ReadUBitLong(32)); |
| 137 | + Assert.False(reader.Overflowed); |
| 138 | + } |
| 139 | + |
| 140 | + [Theory] |
| 141 | + [InlineData(0x0123456789ABCDEFL)] |
| 142 | + [InlineData(-1L)] |
| 143 | + [InlineData(long.MinValue)] |
| 144 | + public void ReadLongLongReadsTwoDwords(long value) { |
| 145 | + byte[] data = new byte[BufferBytes]; |
| 146 | + bf_write writer = new(data, data.Length); |
| 147 | + writer.Seek(3); |
| 148 | + writer.WriteLongLong(value); |
| 149 | + |
| 150 | + bf_read reader = new(data, data.Length); |
| 151 | + reader.Seek(3); |
| 152 | + Assert.Equal(value, reader.ReadLongLong()); |
| 153 | + Assert.Equal(67, reader.BitsRead); |
| 154 | + Assert.False(reader.Overflowed); |
| 155 | + } |
| 156 | + |
| 157 | + [Fact] |
| 158 | + public void TruncatedStringReadsSkipTheRestOfTheString() { |
| 159 | + bf_read reader = StringThenMarker("hello world"); |
| 160 | + Assert.False(reader.ReadString(out string? str, 5)); |
| 161 | + Assert.Equal("hell", str); |
| 162 | + Assert.Equal(Marker, reader.ReadByte()); |
| 163 | + |
| 164 | + reader = StringThenMarker("hello world"); |
| 165 | + Assert.Equal("hell", reader.ReadString(5)); |
| 166 | + Assert.Equal(Marker, reader.ReadByte()); |
| 167 | + |
| 168 | + reader = StringThenMarker("hello world"); |
| 169 | + char[] chars = new char[5]; |
| 170 | + Assert.Equal(4, reader.ReadString(chars.AsSpan())); |
| 171 | + Assert.Equal("hell\0", new string(chars)); |
| 172 | + Assert.Equal(Marker, reader.ReadByte()); |
| 173 | + |
| 174 | + reader = StringThenMarker("hello world"); |
| 175 | + byte[] bytes = new byte[5]; |
| 176 | + Assert.Equal(5, reader.ReadString(bytes.AsSpan())); |
| 177 | + Assert.Equal("hello", Encoding.ASCII.GetString(bytes)); |
| 178 | + Assert.Equal(Marker, reader.ReadByte()); |
| 179 | + } |
| 180 | + |
| 181 | + [Theory] |
| 182 | + [InlineData(1f)] |
| 183 | + [InlineData(-0.5f)] |
| 184 | + [InlineData(-3.5f)] |
| 185 | + [InlineData(16384f)] |
| 186 | + public void ReadBitCoordBitsReadsTheWholeCoord(float value) { |
| 187 | + byte[] data = new byte[BufferBytes]; |
| 188 | + bf_write writer = new(data, data.Length); |
| 189 | + writer.WriteBitCoord(value); |
| 190 | + |
| 191 | + bf_read raw = new(data, data.Length); |
| 192 | + uint expected = raw.ReadUBitLong(writer.BitsWritten); |
| 193 | + bf_read reader = new(data, data.Length); |
| 194 | + Assert.Equal(expected, reader.ReadBitCoordBits()); |
| 195 | + Assert.Equal(writer.BitsWritten, reader.BitsRead); |
| 196 | + } |
| 197 | + |
| 198 | + [Theory] |
| 199 | + [InlineData(-1)] |
| 200 | + [InlineData(int.MinValue)] |
| 201 | + public void SingleBitWritesTreatNegativeValuesAsSet(int value) { |
| 202 | + byte[] data = new byte[4]; |
| 203 | + bf_write writer = new(data, data.Length); |
| 204 | + writer.WriteOneBit(value); |
| 205 | + writer.WriteOneBitNoCheck(value); |
| 206 | + writer.WriteOneBitAt(5, value); |
| 207 | + |
| 208 | + Assert.Equal(0x23, data[0]); |
| 209 | + } |
| 210 | + |
| 211 | + [Fact] |
| 212 | + public void WriteStringReturnsWhetherItFit() { |
| 213 | + byte[] data = new byte[8]; |
| 214 | + Assert.True(new bf_write(data, data.Length).WriteString("hello")); |
| 215 | + Assert.False(new bf_write(data, 4).WriteString("hello")); |
| 216 | + } |
| 217 | + |
| 218 | + [Theory] |
| 219 | + [InlineData("é", "E900")] |
| 220 | + [InlineData("a´ÿ", "61B4FF00")] |
| 221 | + [InlineData("\0abc", "00")] |
| 222 | + public void WriteStringWritesBytesLikeGMod(string text, string expected) { |
| 223 | + byte[] data = new byte[BufferBytes]; |
| 224 | + bf_write writer = new(data, data.Length); |
| 225 | + writer.WriteString(text); |
| 226 | + Assert.Equal(expected, Written(writer, data)); |
| 227 | + } |
| 228 | + |
| 229 | + [Fact] |
| 230 | + public void StringReadsDoNotSignExtendHighBytes() { |
| 231 | + byte[] data = [0x61, 0xB4, 0xFF, 0x00]; |
| 232 | + char[] chars = new char[8]; |
| 233 | + Assert.True(new bf_read(data, data.Length).ReadString(chars.AsSpan(), false, out int count)); |
| 234 | + Assert.Equal("a´ÿ", new string(chars, 0, count)); |
| 235 | + Assert.Equal("a´ÿ", new bf_read(data, data.Length).ReadAndAllocateString()); |
| 236 | + } |
| 237 | + |
| 238 | + [Theory] |
| 239 | + [InlineData(5u, 0x3B201403u)] |
| 240 | + [InlineData(320u, 0x3E201403u)] |
| 241 | + [InlineData(1280u, 0x3F201403u)] |
| 242 | + public void ReadBitNormalRoundsLikeGMod(uint fraction, uint expectedBits) { |
| 243 | + byte[] data = new byte[BufferBytes]; |
| 244 | + bf_write writer = new(data, data.Length); |
| 245 | + writer.WriteOneBit(0); |
| 246 | + writer.WriteUBitLong(fraction, NORMAL_FRACTIONAL_BITS); |
| 247 | + |
| 248 | + float value = new bf_read(data, data.Length).ReadBitNormal(); |
| 249 | + Assert.Equal(expectedBits, BitConverter.SingleToUInt32Bits(value)); |
| 250 | + } |
| 251 | + |
| 252 | + [Fact] |
| 253 | + public void WriteBitNormalKeepsThePositiveSignAtNegativeFloatResolution() { |
| 254 | + byte[] data = new byte[BufferBytes]; |
| 255 | + bf_write writer = new(data, data.Length); |
| 256 | + writer.WriteBitNormal(-(1f / NORMAL_DENOMINATOR)); |
| 257 | + Assert.Equal("0200", Written(writer, data)); |
| 258 | + } |
| 259 | + |
| 260 | + [Theory] |
| 261 | + [InlineData(float.NaN, "FDFF01")] |
| 262 | + [InlineData(float.NegativeInfinity, "FDFF01")] |
| 263 | + [InlineData(3e9f, "F9FF01")] |
| 264 | + public void WriteBitCoordEncodesOutOfRangeValuesLikeGMod(float value, string expected) { |
| 265 | + byte[] data = new byte[BufferBytes]; |
| 266 | + bf_write writer = new(data, data.Length); |
| 267 | + writer.WriteBitCoord(value); |
| 268 | + Assert.Equal(expected, Written(writer, data)); |
| 269 | + } |
| 270 | + |
| 271 | + [Theory] |
| 272 | + [InlineData(float.NaN, false, false, "FFFF07")] |
| 273 | + [InlineData(float.NegativeInfinity, true, true, "FF3F")] |
| 274 | + [InlineData(3e9f, false, true, "FBFF01")] |
| 275 | + public void WriteBitCoordMPEncodesOutOfRangeValuesLikeGMod(float value, bool integral, bool lowPrecision, string expected) { |
| 276 | + byte[] data = new byte[BufferBytes]; |
| 277 | + bf_write writer = new(data, data.Length); |
| 278 | + writer.WriteBitCoordMP(value, integral, lowPrecision); |
| 279 | + Assert.Equal(expected, Written(writer, data)); |
| 280 | + } |
| 281 | + |
| 282 | + [Theory] |
| 283 | + [InlineData(float.NaN, "FF0F")] |
| 284 | + [InlineData(float.NegativeInfinity, "FF0F")] |
| 285 | + [InlineData(-1e10f, "FF0F")] |
| 286 | + public void WriteBitNormalEncodesOutOfRangeValuesLikeGMod(float value, string expected) { |
| 287 | + byte[] data = new byte[BufferBytes]; |
| 288 | + bf_write writer = new(data, data.Length); |
| 289 | + writer.WriteBitNormal(value); |
| 290 | + Assert.Equal(expected, Written(writer, data)); |
| 291 | + } |
| 292 | + |
| 293 | + [Fact] |
| 294 | + public void WriteBitVec3CoordEncodesOutOfRangeValuesLikeGMod() { |
| 295 | + byte[] data = new byte[BufferBytes]; |
| 296 | + bf_write writer = new(data, data.Length); |
| 297 | + writer.WriteBitVec3Coord(new Vector3(float.NaN, float.NegativeInfinity, 1e10f)); |
| 298 | + Assert.Equal(54, writer.BitsWritten); |
| 299 | + Assert.Equal("EFFFDFFF3FFF3F", Written(writer, data)); |
| 300 | + } |
| 301 | + |
| 302 | + [Theory] |
| 303 | + [InlineData(-258.23f, 31, 607081600u)] |
| 304 | + [InlineData(558.71f, 28, 148168928u)] |
| 305 | + [InlineData(-604.07f, 21, 675341u)] |
| 306 | + [InlineData(678.54f, 31, 0u)] |
| 307 | + public void WriteBitAngleEncodesLikeGMod(float angle, int bits, uint expected) { |
| 308 | + byte[] data = new byte[BufferBytes]; |
| 309 | + bf_write writer = new(data, data.Length); |
| 310 | + writer.WriteBitAngle(angle, bits); |
| 311 | + Assert.Equal(expected, new bf_read(data, data.Length).ReadUBitLong(bits)); |
| 312 | + } |
| 313 | + |
| 314 | + [Fact] |
| 315 | + public void ReadBitsAlignsTheOutputBeforeReadingDwords() { |
| 316 | + byte[] data = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0, 0, 0, 0]; |
| 317 | + bf_read reader = new(data, 8); |
| 318 | + reader.Seek(4); |
| 319 | + byte[] output = new byte[12]; |
| 320 | + Array.Fill(output, (byte)0xCC); |
| 321 | + |
| 322 | + reader.ReadBits(output.AsSpan(1), 72); |
| 323 | + |
| 324 | + Assert.True(reader.Overflowed); |
| 325 | + Assert.Equal(64, reader.BitsRead); |
| 326 | + Assert.Equal("CC213243546576870000CCCC", Convert.ToHexString(output)); |
| 327 | + } |
| 328 | +} |
0 commit comments