Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21478 rajender 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
 
17
package com.android.volley.toolbox;
18
 
19
import org.junit.Test;
20
 
21
import static org.junit.Assert.assertNotSame;
22
import static org.junit.Assert.assertSame;
23
import static org.junit.Assert.assertTrue;
24
 
25
public class ByteArrayPoolTest {
26
    @Test public void reusesBuffer() {
27
        ByteArrayPool pool = new ByteArrayPool(32);
28
 
29
        byte[] buf1 = pool.getBuf(16);
30
        byte[] buf2 = pool.getBuf(16);
31
 
32
        pool.returnBuf(buf1);
33
        pool.returnBuf(buf2);
34
 
35
        byte[] buf3 = pool.getBuf(16);
36
        byte[] buf4 = pool.getBuf(16);
37
        assertTrue(buf3 == buf1 || buf3 == buf2);
38
        assertTrue(buf4 == buf1 || buf4 == buf2);
39
        assertTrue(buf3 != buf4);
40
    }
41
 
42
    @Test public void obeysSizeLimit() {
43
        ByteArrayPool pool = new ByteArrayPool(32);
44
 
45
        byte[] buf1 = pool.getBuf(16);
46
        byte[] buf2 = pool.getBuf(16);
47
        byte[] buf3 = pool.getBuf(16);
48
 
49
        pool.returnBuf(buf1);
50
        pool.returnBuf(buf2);
51
        pool.returnBuf(buf3);
52
 
53
        byte[] buf4 = pool.getBuf(16);
54
        byte[] buf5 = pool.getBuf(16);
55
        byte[] buf6 = pool.getBuf(16);
56
 
57
        assertTrue(buf4 == buf2 || buf4 == buf3);
58
        assertTrue(buf5 == buf2 || buf5 == buf3);
59
        assertTrue(buf4 != buf5);
60
        assertTrue(buf6 != buf1 && buf6 != buf2 && buf6 != buf3);
61
    }
62
 
63
    @Test public void returnsBufferWithRightSize() {
64
        ByteArrayPool pool = new ByteArrayPool(32);
65
 
66
        byte[] buf1 = pool.getBuf(16);
67
        pool.returnBuf(buf1);
68
 
69
        byte[] buf2 = pool.getBuf(17);
70
        assertNotSame(buf2, buf1);
71
 
72
        byte[] buf3 = pool.getBuf(15);
73
        assertSame(buf3, buf1);
74
    }
75
}