|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | + |
| 3 | +namespace SqlServerSimulator.Storage; |
| 4 | + |
| 5 | +[TestClass] |
| 6 | +public class RowRoundTripTests |
| 7 | +{ |
| 8 | + [TestMethod] |
| 9 | + [DataRow(1)] |
| 10 | + [DataRow(0)] |
| 11 | + [DataRow(-1)] |
| 12 | + [DataRow(int.MaxValue)] |
| 13 | + [DataRow(int.MinValue)] |
| 14 | + public void SingleColumn_Int32_RoundTrips(int value) |
| 15 | + { |
| 16 | + var bytes = RowEncoder.EncodeRow([SqlType.Int32], [value]); |
| 17 | + var decoded = RowDecoder.DecodeRow([SqlType.Int32], bytes); |
| 18 | + AreEqual(1, decoded.Length); |
| 19 | + AreEqual(SqlValue.FromInt32(value), decoded[0]); |
| 20 | + } |
| 21 | + |
| 22 | + [TestMethod] |
| 23 | + public void SingleColumn_Null_RoundTrips() |
| 24 | + { |
| 25 | + var bytes = RowEncoder.EncodeRow([SqlType.Int32], [SqlValue.Null(SqlType.Int32)]); |
| 26 | + var decoded = RowDecoder.DecodeRow([SqlType.Int32], bytes); |
| 27 | + AreEqual(1, decoded.Length); |
| 28 | + IsTrue(decoded[0].IsNull); |
| 29 | + AreEqual(SqlType.Int32, decoded[0].Type); |
| 30 | + } |
| 31 | + |
| 32 | + [TestMethod] |
| 33 | + public void TwoColumns_BothPresent_RoundTrips() |
| 34 | + { |
| 35 | + SqlType[] schema = [SqlType.Int32, SqlType.Int32]; |
| 36 | + var decoded = RowDecoder.DecodeRow(schema, RowEncoder.EncodeRow(schema, [1, 2])); |
| 37 | + CollectionAssert.AreEqual(new SqlValue[] { 1, 2 }, decoded); |
| 38 | + } |
| 39 | + |
| 40 | + [TestMethod] |
| 41 | + public void TwoColumns_FirstNull_RoundTrips() |
| 42 | + { |
| 43 | + SqlType[] schema = [SqlType.Int32, SqlType.Int32]; |
| 44 | + SqlValue[] values = [SqlValue.Null(SqlType.Int32), 1]; |
| 45 | + var decoded = RowDecoder.DecodeRow(schema, RowEncoder.EncodeRow(schema, values)); |
| 46 | + CollectionAssert.AreEqual(values, decoded); |
| 47 | + } |
| 48 | + |
| 49 | + [TestMethod] |
| 50 | + public void TwoColumns_SecondNull_RoundTrips() |
| 51 | + { |
| 52 | + SqlType[] schema = [SqlType.Int32, SqlType.Int32]; |
| 53 | + SqlValue[] values = [1, SqlValue.Null(SqlType.Int32)]; |
| 54 | + var decoded = RowDecoder.DecodeRow(schema, RowEncoder.EncodeRow(schema, values)); |
| 55 | + CollectionAssert.AreEqual(values, decoded); |
| 56 | + } |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public void ThreeColumns_AllNull_RoundTrips() |
| 60 | + { |
| 61 | + SqlType[] schema = [SqlType.Int32, SqlType.Int32, SqlType.Int32]; |
| 62 | + SqlValue[] values = [SqlValue.Null(SqlType.Int32), SqlValue.Null(SqlType.Int32), SqlValue.Null(SqlType.Int32)]; |
| 63 | + var decoded = RowDecoder.DecodeRow(schema, RowEncoder.EncodeRow(schema, values)); |
| 64 | + CollectionAssert.AreEqual(values, decoded); |
| 65 | + } |
| 66 | + |
| 67 | + [TestMethod] |
| 68 | + public void EightColumns_BitmapByteBoundary_RoundTrips() |
| 69 | + { |
| 70 | + // Exactly 8 columns => 1 byte of NULL bitmap. Alternating null/value tests |
| 71 | + // the bit ordering within the byte (column i ↔ bit i mod 8). |
| 72 | + SqlType[] schema = [SqlType.Int32, SqlType.Int32, SqlType.Int32, SqlType.Int32, SqlType.Int32, SqlType.Int32, SqlType.Int32, SqlType.Int32]; |
| 73 | + var n = SqlValue.Null(SqlType.Int32); |
| 74 | + SqlValue[] values = [1, n, 2, n, 3, n, 4, n]; |
| 75 | + var decoded = RowDecoder.DecodeRow(schema, RowEncoder.EncodeRow(schema, values)); |
| 76 | + CollectionAssert.AreEqual(values, decoded); |
| 77 | + } |
| 78 | + |
| 79 | + [TestMethod] |
| 80 | + public void NineColumns_MultiByteBitmap_RoundTrips() |
| 81 | + { |
| 82 | + // 9 columns => 2 bytes of NULL bitmap; the 9th column lives in the second byte. |
| 83 | + SqlType[] schema = [SqlType.Int32, SqlType.Int32, SqlType.Int32, SqlType.Int32, SqlType.Int32, SqlType.Int32, SqlType.Int32, SqlType.Int32, SqlType.Int32]; |
| 84 | + SqlValue[] values = [10, 20, 30, 40, 50, 60, 70, 80, SqlValue.Null(SqlType.Int32)]; |
| 85 | + var decoded = RowDecoder.DecodeRow(schema, RowEncoder.EncodeRow(schema, values)); |
| 86 | + CollectionAssert.AreEqual(values, decoded); |
| 87 | + } |
| 88 | + |
| 89 | + [TestMethod] |
| 90 | + public void Encoder_RejectsEmptySchema() => |
| 91 | + Throws<ArgumentException>(() => RowEncoder.EncodeRow([], [])); |
| 92 | + |
| 93 | + [TestMethod] |
| 94 | + public void Encoder_RejectsSchemaValueLengthMismatch() => |
| 95 | + Throws<ArgumentException>(() => RowEncoder.EncodeRow([SqlType.Int32, SqlType.Int32], [1])); |
| 96 | + |
| 97 | + [TestMethod] |
| 98 | + public void EncodedSingleColumnRow_HasLength11() |
| 99 | + { |
| 100 | + // 4 (header) + 4 (one Int32) + 2 (column count) + 1 (NULL bitmap byte) = 11. |
| 101 | + AreEqual(11, RowEncoder.EncodeRow([SqlType.Int32], [42]).Length); |
| 102 | + AreEqual(11, RowEncoder.EncodeRow([SqlType.Int32], [SqlValue.Null(SqlType.Int32)]).Length); |
| 103 | + } |
| 104 | + |
| 105 | + [TestMethod] |
| 106 | + public void EncodedTwoColumnRow_HasLength15() |
| 107 | + { |
| 108 | + // 4 (header) + 8 (two Int32s) + 2 (column count) + 1 (NULL bitmap byte) = 15. |
| 109 | + AreEqual(15, RowEncoder.EncodeRow([SqlType.Int32, SqlType.Int32], [1, 2]).Length); |
| 110 | + } |
| 111 | + |
| 112 | + [TestMethod] |
| 113 | + public void EncodedSingleColumnRow_MatchesDocumentedLayout() |
| 114 | + { |
| 115 | + // Guards the byte-level layout described on RowEncoder.EncodeRow. |
| 116 | + // See RowEncoder's <remarks> for the public references that informed the |
| 117 | + // structural shape; the specific bit values are simulator-defined. |
| 118 | + var bytes = RowEncoder.EncodeRow([SqlType.Int32], [0x04030201]); |
| 119 | + AreEqual(0x10, bytes[0]); // TagA |
| 120 | + AreEqual(0x00, bytes[1]); // TagB |
| 121 | + AreEqual(8, BitConverter.ToUInt16(bytes, 2)); // fixed-length end offset |
| 122 | + CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x04 }, bytes[4..8]); // int LE |
| 123 | + AreEqual(1, BitConverter.ToUInt16(bytes, 8)); // column count |
| 124 | + AreEqual(0x00, bytes[10]); // NULL bitmap (not null) |
| 125 | + } |
| 126 | + |
| 127 | + [TestMethod] |
| 128 | + public void EncodedTwoColumnRow_MatchesDocumentedLayout() |
| 129 | + { |
| 130 | + var bytes = RowEncoder.EncodeRow([SqlType.Int32, SqlType.Int32], [0x04030201, 0x08070605]); |
| 131 | + AreEqual(0x10, bytes[0]); // TagA |
| 132 | + AreEqual(0x00, bytes[1]); // TagB |
| 133 | + AreEqual(12, BitConverter.ToUInt16(bytes, 2)); // fixed-length end offset (4 + 2*4) |
| 134 | + CollectionAssert.AreEqual(new byte[] { 0x01, 0x02, 0x03, 0x04 }, bytes[4..8]); // first int LE |
| 135 | + CollectionAssert.AreEqual(new byte[] { 0x05, 0x06, 0x07, 0x08 }, bytes[8..12]); // second int LE |
| 136 | + AreEqual(2, BitConverter.ToUInt16(bytes, 12)); // column count |
| 137 | + AreEqual(0x00, bytes[14]); // NULL bitmap |
| 138 | + } |
| 139 | + |
| 140 | + [TestMethod] |
| 141 | + public void EncodedNullRow_SetsNullBitmapBit0() |
| 142 | + { |
| 143 | + var bytes = RowEncoder.EncodeRow([SqlType.Int32], [SqlValue.Null(SqlType.Int32)]); |
| 144 | + AreEqual(0x01, bytes[10]); |
| 145 | + } |
| 146 | + |
| 147 | + [TestMethod] |
| 148 | + public void EncodedTwoColumnRow_SecondNullSetsBit1() |
| 149 | + { |
| 150 | + var bytes = RowEncoder.EncodeRow([SqlType.Int32, SqlType.Int32], [1, SqlValue.Null(SqlType.Int32)]); |
| 151 | + AreEqual(0x02, bytes[14]); |
| 152 | + } |
| 153 | + |
| 154 | + [TestMethod] |
| 155 | + public void Decoder_RejectsTruncatedRow() |
| 156 | + { |
| 157 | + var bytes = RowEncoder.EncodeRow([SqlType.Int32], [1]).AsSpan(0, 10).ToArray(); |
| 158 | + _ = Throws<InvalidDataException>(() => RowDecoder.DecodeRow([SqlType.Int32], bytes)); |
| 159 | + } |
| 160 | + |
| 161 | + [TestMethod] |
| 162 | + public void Decoder_RejectsBadTagA() |
| 163 | + { |
| 164 | + var bytes = RowEncoder.EncodeRow([SqlType.Int32], [1]); |
| 165 | + bytes[0] = 0x00; |
| 166 | + _ = Throws<InvalidDataException>(() => RowDecoder.DecodeRow([SqlType.Int32], bytes)); |
| 167 | + } |
| 168 | + |
| 169 | + [TestMethod] |
| 170 | + public void Decoder_RejectsColumnCountMismatch() |
| 171 | + { |
| 172 | + // The decoder cross-checks the declared column count against the schema length. |
| 173 | + var bytes = RowEncoder.EncodeRow([SqlType.Int32], [1]); |
| 174 | + bytes[8] = 0x02; |
| 175 | + _ = Throws<InvalidDataException>(() => RowDecoder.DecodeRow([SqlType.Int32], bytes)); |
| 176 | + } |
| 177 | + |
| 178 | + [TestMethod] |
| 179 | + public void Decoder_RejectsFixedEndMismatch() |
| 180 | + { |
| 181 | + // Fixed-length end offset is derived from the schema; tampering should be rejected. |
| 182 | + var bytes = RowEncoder.EncodeRow([SqlType.Int32], [1]); |
| 183 | + bytes[2] = 0x09; |
| 184 | + _ = Throws<InvalidDataException>(() => RowDecoder.DecodeRow([SqlType.Int32], bytes)); |
| 185 | + } |
| 186 | +} |
0 commit comments