/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.operator;

import org.testng.annotations.Test;

import java.util.ArrayDeque;
import java.util.Deque;

import static io.airlift.slice.SizeOf.sizeOfByteArray;
import static io.airlift.slice.SizeOf.sizeOfIntArray;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertThrows;

public class TestSimpleArrayAllocator
{
    @Test
    public void testNewLease()
    {
        SimpleArrayAllocator allocator = new SimpleArrayAllocator(10);

        Deque<int[]> intArrayList = new ArrayDeque<>();
        for (int i = 0; i < 5; i++) {
            intArrayList.push(allocator.borrowIntArray(10));
        }
        assertEquals(allocator.getBorrowedArrayCount(), 5);
        assertEquals(allocator.getEstimatedSizeInBytes(), sizeOfIntArray(10) * 5);

        Deque<byte[]> byteArrayList = new ArrayDeque<>();
        for (int i = 0; i < 5; i++) {
            byteArrayList.push(allocator.borrowByteArray(10));
        }
        assertEquals(allocator.getBorrowedArrayCount(), 10);
        assertEquals(allocator.getEstimatedSizeInBytes(), (sizeOfIntArray(10) + sizeOfByteArray(10)) * 5);

        for (int i = 0; i < 5; i++) {
            allocator.returnArray(intArrayList.pop());
        }
        assertEquals(allocator.getBorrowedArrayCount(), 5);
        assertEquals(allocator.getEstimatedSizeInBytes(), (sizeOfIntArray(10) + sizeOfByteArray(10)) * 5);

        for (int i = 0; i < 5; i++) {
            allocator.returnArray(byteArrayList.pop());
        }
        assertEquals(allocator.getBorrowedArrayCount(), 0);
        assertEquals(allocator.getEstimatedSizeInBytes(), (sizeOfIntArray(10) + sizeOfByteArray(10)) * 5);

        int[] intBorrowed = allocator.borrowIntArray(101);
        assertEquals(allocator.getEstimatedSizeInBytes(), sizeOfIntArray(101) + sizeOfByteArray(10) * 5);
        allocator.returnArray(intBorrowed);
        assertEquals(allocator.getEstimatedSizeInBytes(), sizeOfIntArray(101) + sizeOfByteArray(10) * 5);

        byte[] byteBorrowed = allocator.borrowByteArray(101);
        assertEquals(allocator.getEstimatedSizeInBytes(), sizeOfIntArray(101) + sizeOfByteArray(101));
        allocator.returnArray(byteBorrowed);
        assertEquals(allocator.getEstimatedSizeInBytes(), sizeOfIntArray(101) + sizeOfByteArray(101));
    }

    @Test
    public void testOverAllocateLeases()
    {
        SimpleArrayAllocator allocator = new SimpleArrayAllocator(2);

        allocator.borrowIntArray(10);
        allocator.borrowIntArray(10);
        assertThrows(IllegalStateException.class, () -> allocator.borrowIntArray(10));
    }

    @Test
    public void testReturnArrayNotBorrowed()
    {
        SimpleArrayAllocator allocator = new SimpleArrayAllocator(2);

        int[] array = allocator.borrowIntArray(10);
        allocator.returnArray(array);
        assertThrows(IllegalArgumentException.class, () -> allocator.returnArray(array));
    }
}

@@ -594,16 +594,72 @@
 rator;%0A%0A
+import com.facebook.presto.common.block.ArrayAllocator;%0A
 import o
@@ -686,16 +686,16 @@
 s.Test;%0A
-
 %0Aimport 
@@ -1030,38 +1030,32 @@
 )%0A    %7B%0A        
-Simple
 ArrayAllocator a
@@ -1095,16 +1095,734 @@
 or(10);%0A
+        testNewLease(allocator);%0A    %7D%0A%0A    @Test%0A    public void testOverAllocateLeases()%0A    %7B%0A        SimpleArrayAllocator allocator = new SimpleArrayAllocator(2);%0A%0A        allocator.borrowIntArray(10);%0A        allocator.borrowIntArray(10);%0A        assertThrows(IllegalStateException.class, () -%3E allocator.borrowIntArray(10));%0A    %7D%0A%0A    @Test%0A    public void testReturnArrayNotBorrowed()%0A    %7B%0A        SimpleArrayAllocator allocator = new SimpleArrayAllocator(2);%0A%0A        int%5B%5D array = allocator.borrowIntArray(10);%0A        allocator.returnArray(array);%0A        assertThrows(IllegalArgumentException.class, () -%3E allocator.returnArray(array));%0A    %7D%0A%0A    private void testNewLease(ArrayAllocator allocator)%0A    %7B
 %0A       
@@ -3642,624 +3642,7 @@
    %7D
-%0A%0A    @Test%0A    public void testOverAllocateLeases()%0A    %7B%0A        SimpleArrayAllocator allocator = new SimpleArrayAllocator(2);%0A%0A        allocator.borrowIntArray(10);%0A        allocator.borrowIntArray(10);%0A        assertThrows(IllegalStateException.class, () -%3E allocator.borrowIntArray(10));%0A    %7D%0A%0A    @Test%0A    public void testReturnArrayNotBorrowed()%0A    %7B%0A        SimpleArrayAllocator allocator = new SimpleArrayAllocator(2);%0A%0A        int%5B%5D array = allocator.borrowIntArray(10);%0A        allocator.returnArray(array);%0A        assertThrows(IllegalArgumentException.class, () -%3E allocator.returnArray(array));%0A    %7D
 %0A%7D%0A

Refactor TestSimpleArrayAllocator