Ejemplo n.º 1
0
        public void TestSubset()
        {
            byte[] source = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            // We test for various subsets that overlap partially or fully with the different segments
            for (int sourceStart = 0; sourceStart < source.Length / 2; sourceStart++)
            {
                for (int sourceEnd = source.Length - 1; sourceEnd - sourceStart > 0; sourceEnd--)
                {
                    int sourceCount = sourceEnd - sourceStart + 1;
                    TransportPacket packet = new TransportPacket(source, sourceStart, sourceCount);
                    Assert.AreEqual(sourceCount, packet.Length);
                    Assert.AreEqual(1, ((IList<ArraySegment<byte>>)packet).Count);

                    for (int i = 0; i < 10; i++)
                    {
                        packet.Append(source, sourceStart, sourceCount);
                    }
                    Assert.AreEqual(sourceCount * 11, packet.Length);

                    const int subsetStart = 4;
                    int subsetCount = Math.Min(17, packet.Length - 2);

                    TransportPacket subset = packet.Subset(subsetStart, subsetCount);
                    Assert.AreEqual(subsetCount, subset.Length);
                    byte[] result = subset.ToArray();
                    Assert.AreEqual(subsetCount, result.Length);
                    for (int i = 0; i < result.Length; i++)
                    {
                        Assert.AreEqual(source[sourceStart +
                            ((subsetStart + i) % sourceCount)], result[i]);
                        Assert.AreEqual(source[sourceStart +
                            ((subsetStart + i) % sourceCount)], subset.ByteAt(i));
                        Assert.AreEqual(packet.ByteAt(subsetStart + i), subset.ByteAt(i));
                    }

                    // And ensure the subset has the same backing byte array is still referenced
                    // sourceEquivIndex = the equivalent index in source to subset[0]
                    int sourceEquivIndex = sourceStart + (subsetStart % sourceCount);
                    Assert.AreEqual(source[sourceEquivIndex], packet.ByteAt(subsetStart));
                    Assert.AreEqual(source[sourceEquivIndex], subset.ByteAt(0));
                    packet.Replace(subsetStart, new byte[] { 255 }, 0, 1);
                    Assert.AreEqual(255, packet.ByteAt(subsetStart));
                    Assert.AreEqual(255, subset.ByteAt(0));
                    packet.Replace(subsetStart, source, sourceEquivIndex, 1);
                    Assert.AreEqual(source[sourceEquivIndex], packet.ByteAt(subsetStart));
                    Assert.AreEqual(source[sourceEquivIndex], subset.ByteAt(0));

                    CheckNotDisposed(subset);

                    /// Ensure that disposing of the subset doesn't dispose the parent packet
                    for (int i = 0; i < packet.Length; i++)
                    {
                        Assert.AreEqual(source[sourceStart + (i % sourceCount)], packet.ByteAt(i));
                    }
                    CheckDisposed(packet);
                }
            }
            CheckForUndisposedSegments();
        }