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