Example #1
0
    static void ByteBufferPoolTest()
    {
        for (int i = 0; i < ByteBufferPool.sPool.Length; ++i)
        {
            var p = ByteBufferPool.sPool[i];
            Debug.Assert(p.Count == ByteBufferPool.initialArraySize);
            for (int j = 0; j < p.Count; ++j)
            {
                Debug.Assert(p[j].Data.Length == 1 << (i + ByteBufferPool.minSizePOT));
            }
        }

        int min = 1 << ByteBufferPool.minSizePOT;
        List <ByteBuffer> buffers = new List <ByteBuffer>();

        buffers.Add(ByteBufferPool.Alloc(0));
        buffers.Add(ByteBufferPool.Alloc(1));
        buffers.Add(ByteBufferPool.Alloc(min - 1));
        buffers.Add(ByteBufferPool.Alloc(min));
        buffers.Add(ByteBufferPool.Alloc(min + 1));

        var pool_64 = ByteBufferPool.sPool[ByteBufferPool.sIndexLookup[1 << ByteBufferPool.minSizePOT]];

        Debug.Assert(ByteBufferPool.AvailableSlots(pool_64) == 0);
        var pool_128 = ByteBufferPool.sPool[ByteBufferPool.sIndexLookup[1 << (ByteBufferPool.minSizePOT + 1)]];

        Debug.Assert(ByteBufferPool.AvailableSlots(pool_128) == ByteBufferPool.initialArraySize - 1);

        buffers.Add(ByteBufferPool.Alloc(min));
        Debug.Assert(ByteBufferPool.AvailableSlots(pool_64) == ByteBufferPool.initialArraySize - 1);

        for (int i = 0; i < buffers.Count; ++i)
        {
            ByteBuffer bb = buffers[i];
            ByteBufferPool.Dealloc(ref bb);
            Debug.Assert(null == bb);
        }
        Debug.Assert(ByteBufferPool.AvailableSlots(pool_64) == ByteBufferPool.initialArraySize * 2);
        Debug.Assert(ByteBufferPool.AvailableSlots(pool_128) == ByteBufferPool.initialArraySize);

        try
        {
            ByteBufferPool.Alloc((1 << ByteBufferPool.maxSizePOT) + 1);
            Debug.Assert(false);
        } catch (ArgumentOutOfRangeException) { }

        ByteBuffer wrongsize = new ByteBuffer(new byte[123]);

        ByteBufferPool.Dealloc(ref wrongsize);
        Debug.Assert(null != wrongsize);

        ByteBuffer exceed = new ByteBuffer(new byte[1 << ByteBufferPool.minSizePOT]);

        ByteBufferPool.Dealloc(ref exceed);
        Debug.Assert(null != exceed);

        Console.WriteLine("ByteBuffer Pool Checked");
    }