Skip to content

Commit 4da741e

Browse files
author
Steve Powell
committed
Added IntAllocatorTests which exhibits stack overflow on original.
1 parent 0a72538 commit 4da741e

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// The contents of this file are subject to the Mozilla Public License
2+
// Version 1.1 (the "License"); you may not use this file except in
3+
// compliance with the License. You may obtain a copy of the License
4+
// at http://www.mozilla.org/MPL/
5+
//
6+
// Software distributed under the License is distributed on an "AS IS"
7+
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
8+
// the License for the specific language governing rights and
9+
// limitations under the License.
10+
//
11+
// The Original Code is RabbitMQ.
12+
//
13+
// The Initial Developer of the Original Code is VMware, Inc.
14+
// Copyright (c) 2011 VMware, Inc. All rights reserved.
15+
//
16+
17+
package com.rabbitmq.utility;
18+
19+
import java.util.HashSet;
20+
import java.util.Iterator;
21+
import java.util.Random;
22+
import java.util.Set;
23+
24+
import junit.framework.TestCase;
25+
26+
public class IntAllocatorTests extends TestCase {
27+
28+
private static final int TEST_ITERATIONS = 50000;
29+
private static final int HI_RANGE = 100000;
30+
private static final int LO_RANGE = 100;
31+
private IntAllocator intAllocator = new IntAllocator(LO_RANGE, HI_RANGE);
32+
33+
private Random rand = new Random();
34+
35+
public void testReserveAndFree() throws Exception {
36+
Set<Integer> set = new HashSet<Integer>();
37+
for (int i = 0; i < TEST_ITERATIONS; ++i) {
38+
int trial = getTrial();
39+
if (set.contains(trial)) {
40+
intAllocator.free(trial);
41+
set.remove(trial);
42+
} else {
43+
assertTrue("Did not reserve free integer " + trial,
44+
intAllocator.reserve(trial));
45+
set.add(trial);
46+
}
47+
}
48+
49+
for (int trial : set) {
50+
assertFalse("Integer " + trial + " not allocated!",
51+
intAllocator.reserve(trial));
52+
}
53+
}
54+
55+
public void testAllocateAndFree() throws Exception {
56+
Set<Integer> set = new HashSet<Integer>();
57+
for (int i=0; i < TEST_ITERATIONS; ++i) {
58+
if (getBool()) {
59+
int trial = intAllocator.allocate();
60+
assertFalse("Already allocated " + trial, set.contains(trial));
61+
set.add(trial);
62+
} else {
63+
if (!set.isEmpty()) {
64+
int trial = extract(set);
65+
assertFalse("Allocator agreed to reserve " + trial,
66+
intAllocator.reserve(trial));
67+
intAllocator.free(trial);
68+
}
69+
}
70+
}
71+
72+
for (int trial : set) {
73+
assertFalse("Integer " + trial + " should be allocated!",
74+
intAllocator.reserve(trial));
75+
}
76+
}
77+
78+
private static int extract(Set<Integer> set) {
79+
Iterator<Integer> iter = set.iterator();
80+
int trial = iter.next();
81+
iter.remove();
82+
return trial;
83+
}
84+
85+
private int getTrial() {
86+
return rand.nextInt(HI_RANGE-LO_RANGE+1) + LO_RANGE;
87+
}
88+
89+
private boolean getBool() {
90+
return rand.nextBoolean();
91+
}
92+
}

0 commit comments

Comments
 (0)