1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.volley.toolbox;
18
19import android.test.AndroidTestCase;
20
21public class ByteArrayPoolTest extends AndroidTestCase {
22    public void testReusesBuffer() {
23        ByteArrayPool pool = new ByteArrayPool(32);
24
25        byte[] buf1 = pool.getBuf(16);
26        byte[] buf2 = pool.getBuf(16);
27
28        pool.returnBuf(buf1);
29        pool.returnBuf(buf2);
30
31        byte[] buf3 = pool.getBuf(16);
32        byte[] buf4 = pool.getBuf(16);
33        assertTrue(buf3 == buf1 || buf3 == buf2);
34        assertTrue(buf4 == buf1 || buf4 == buf2);
35        assertTrue(buf3 != buf4);
36    }
37
38    public void testObeysSizeLimit() {
39        ByteArrayPool pool = new ByteArrayPool(32);
40
41        byte[] buf1 = pool.getBuf(16);
42        byte[] buf2 = pool.getBuf(16);
43        byte[] buf3 = pool.getBuf(16);
44
45        pool.returnBuf(buf1);
46        pool.returnBuf(buf2);
47        pool.returnBuf(buf3);
48
49        byte[] buf4 = pool.getBuf(16);
50        byte[] buf5 = pool.getBuf(16);
51        byte[] buf6 = pool.getBuf(16);
52
53        assertTrue(buf4 == buf2 || buf4 == buf3);
54        assertTrue(buf5 == buf2 || buf5 == buf3);
55        assertTrue(buf4 != buf5);
56        assertTrue(buf6 != buf1 && buf6 != buf2 && buf6 != buf3);
57    }
58
59    public void testReturnsBufferWithRightSize() {
60        ByteArrayPool pool = new ByteArrayPool(32);
61
62        byte[] buf1 = pool.getBuf(16);
63        pool.returnBuf(buf1);
64
65        byte[] buf2 = pool.getBuf(17);
66        assertNotSame(buf2, buf1);
67
68        byte[] buf3 = pool.getBuf(15);
69        assertSame(buf3, buf1);
70    }
71}
72