|
| 1 | +// This source code is dual-licensed under the Apache License, version |
| 2 | +// 2.0, and the Mozilla Public License, version 1.1. |
| 3 | +// |
| 4 | +// The APL v2.0: |
| 5 | +// |
| 6 | +//--------------------------------------------------------------------------- |
| 7 | +// Copyright (C) 2007-2013 VMware, Inc. |
| 8 | +// |
| 9 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +// you may not use this file except in compliance with the License. |
| 11 | +// You may obtain a copy of the License at |
| 12 | +// |
| 13 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +// |
| 15 | +// Unless required by applicable law or agreed to in writing, software |
| 16 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +// See the License for the specific language governing permissions and |
| 19 | +// limitations under the License. |
| 20 | +//--------------------------------------------------------------------------- |
| 21 | +// |
| 22 | +// The MPL v1.1: |
| 23 | +// |
| 24 | +//--------------------------------------------------------------------------- |
| 25 | +// The contents of this file are subject to the Mozilla Public License |
| 26 | +// Version 1.1 (the "License"); you may not use this file except in |
| 27 | +// compliance with the License. You may obtain a copy of the License |
| 28 | +// at http://www.mozilla.org/MPL/ |
| 29 | +// |
| 30 | +// Software distributed under the License is distributed on an "AS IS" |
| 31 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| 32 | +// the License for the specific language governing rights and |
| 33 | +// limitations under the License. |
| 34 | +// |
| 35 | +// The Original Code is RabbitMQ. |
| 36 | +// |
| 37 | +// The Initial Developer of the Original Code is VMware, Inc. |
| 38 | +// Copyright (c) 2013-2013 VMware, Inc. All rights reserved. |
| 39 | +//--------------------------------------------------------------------------- |
| 40 | + |
| 41 | +using System; |
| 42 | +using System.Collections.Generic; |
| 43 | +using RabbitMQ.Util; |
| 44 | +using NUnit.Framework; |
| 45 | + |
| 46 | +namespace RabbitMQ.Client.Unit |
| 47 | +{ |
| 48 | + [TestFixture] |
| 49 | + public class TestIntAllocator |
| 50 | + { |
| 51 | + [Test] |
| 52 | + public void TestRandomAllocation() |
| 53 | + { |
| 54 | + int repeatCount = 10000; |
| 55 | + int range = 100; |
| 56 | + IList<int> allocated = new List<int>(); |
| 57 | + IntAllocator intAllocator = new IntAllocator(0, range); |
| 58 | + Random rand = new Random(); |
| 59 | + while (repeatCount-- > 0) |
| 60 | + { |
| 61 | + if (rand.Next(2) == 0) |
| 62 | + { |
| 63 | + int a = intAllocator.Allocate(); |
| 64 | + if (a > -1) |
| 65 | + { |
| 66 | + Assert.False(allocated.Contains(a)); |
| 67 | + allocated.Add(a); |
| 68 | + } |
| 69 | + } |
| 70 | + else if (allocated.Count > 0) |
| 71 | + { |
| 72 | + int a = allocated[0]; |
| 73 | + intAllocator.Free(a); |
| 74 | + allocated.RemoveAt(0); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public void TestAllocateAll() |
| 81 | + { |
| 82 | + int range = 100; |
| 83 | + IList<int> allocated = new List<int>(); |
| 84 | + IntAllocator intAllocator = new IntAllocator(0, range); |
| 85 | + for (int i=0; i <= range; i++) |
| 86 | + { |
| 87 | + int a = intAllocator.Allocate(); |
| 88 | + Assert.AreNotEqual(-1, a); |
| 89 | + Assert.False(allocated.Contains(a)); |
| 90 | + allocated.Add(a); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
0 commit comments