怎么测试对象池
很久没发帖了,今天来水一个!
问题是这样的:我用特殊方法实现了一个对象池,并且通过了简单的测试。怎么才能非常完善的测试一个对象池是的实现否是正确的。
为了方便讨论,先贴一个标准的对象池的实现代码,大家可以假设这个实现有缺陷,对其进行测试。
using System;namespace TEST{ public sealed class ObjectPool<T> : IPool<T> where T : class { private readonly Func<T> _creater = null; private readonly Action<T> _rester = null; private readonly T[] _objs = null; private readonly int _capcity; private int _remainer; public ObjectPool(int capcity, Func<T> creater = null, Action<T> rester = null) { _capcity = _remainer = capcity; _creater = creater; _rester = rester; _objs = new T[capcity]; for (int i = 0; i < capcity; i++) _objs[i] = creater(); } public int Capcity { get { return _capcity; } } public int Remainer { get { return _remainer; } } public T Fetch() { if (_remainer <= 0) throw new Exception(); lock (_objs) { return _objs[--_remainer]; } } public void Store(T obj) { if (_remainer >= _capcity) throw new Exception(); if (_rester != null) _rester(obj); lock (_objs) { _objs[_remainer++] = obj; } } }}