1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.arrow.algorithm.sort; 19 20 import static junit.framework.TestCase.assertEquals; 21 import static junit.framework.TestCase.assertTrue; 22 23 import org.apache.arrow.memory.BufferAllocator; 24 import org.apache.arrow.memory.RootAllocator; 25 import org.junit.After; 26 import org.junit.Before; 27 import org.junit.Test; 28 29 /** 30 * Test cases for {@link OffHeapIntStack}. 31 */ 32 public class TestOffHeapIntStack { 33 34 private BufferAllocator allocator; 35 36 @Before prepare()37 public void prepare() { 38 allocator = new RootAllocator(1024 * 1024); 39 } 40 41 @After shutdown()42 public void shutdown() { 43 allocator.close(); 44 } 45 46 @Test testPushPop()47 public void testPushPop() { 48 try (OffHeapIntStack stack = new OffHeapIntStack(allocator)) { 49 assertTrue(stack.isEmpty()); 50 51 final int elemCount = 500; 52 for (int i = 0; i < elemCount; i++) { 53 stack.push(i); 54 assertEquals(i, stack.getTop()); 55 } 56 57 assertEquals(elemCount, stack.getCount()); 58 59 for (int i = 0; i < elemCount; i++) { 60 assertEquals(elemCount - i - 1, stack.getTop()); 61 assertEquals(elemCount - i - 1, stack.pop()); 62 } 63 64 assertTrue(stack.isEmpty()); 65 } 66 } 67 } 68