Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0e32dc9d authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Implementing app standby quotas for alarms"

parents 938843a3 47ca6fc6
Loading
Loading
Loading
Loading
+165 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * 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 android.util;

import com.android.internal.util.ArrayUtils;
import com.android.internal.util.GrowingArrayUtils;

import libcore.util.EmptyArray;

import java.util.NoSuchElementException;

/**
 * A lightweight implementation for a queue with long values.
 * Additionally supports getting an element with a specified position from the head of the queue.
 * The queue grows in size if needed to accommodate new elements.
 *
 * @hide
 */
public class LongArrayQueue {

    private long[] mValues;
    private int mSize;
    private int mHead;
    private int mTail;

    /**
     * Initializes a queue with the given starting capacity.
     *
     * @param initialCapacity the capacity.
     */
    public LongArrayQueue(int initialCapacity) {
        if (initialCapacity == 0) {
            mValues = EmptyArray.LONG;
        } else {
            mValues = ArrayUtils.newUnpaddedLongArray(initialCapacity);
        }
        mSize = 0;
        mHead = mTail = 0;
    }

    /**
     * Initializes a queue with default starting capacity.
     */
    public LongArrayQueue() {
        this(16);
    }

    private void grow() {
        if (mSize < mValues.length) {
            throw new IllegalStateException("Queue not full yet!");
        }
        final int newSize = GrowingArrayUtils.growSize(mSize);
        final long[] newArray = ArrayUtils.newUnpaddedLongArray(newSize);
        final int r = mValues.length - mHead; // Number of elements on and to the right of head.
        System.arraycopy(mValues, mHead, newArray, 0, r);
        System.arraycopy(mValues, 0, newArray, r, mHead);
        mValues = newArray;
        mHead = 0;
        mTail = mSize;
    }

    /**
     * Returns the number of elements in the queue.
     */
    public int size() {
        return mSize;
    }

    /**
     * Removes all elements from this queue.
     */
    public void clear() {
        mSize = 0;
        mHead = mTail = 0;
    }

    /**
     * Adds a value to the tail of the queue.
     *
     * @param value the value to be added.
     */
    public void addLast(long value) {
        if (mSize == mValues.length) {
            grow();
        }
        mValues[mTail] = value;
        mTail = (mTail + 1) % mValues.length;
        mSize++;
    }

    /**
     * Removes an element from the head of the queue.
     *
     * @return the element at the head of the queue.
     * @throws NoSuchElementException if the queue is empty.
     */
    public long removeFirst() {
        if (mSize == 0) {
            throw new NoSuchElementException("Queue is empty!");
        }
        final long ret = mValues[mHead];
        mHead = (mHead + 1) % mValues.length;
        mSize--;
        return ret;
    }

    /**
     * Returns the element at the given position from the head of the queue, where 0 represents the
     * head of the queue.
     *
     * @param position the position from the head of the queue.
     * @return the element found at the given position.
     * @throws IndexOutOfBoundsException if {@code position} < {@code 0} or
     *                                   {@code position} >= {@link #size()}
     */
    public long get(int position) {
        if (position < 0 || position >= mSize) {
            throw new IndexOutOfBoundsException("Index " + position
                    + " not valid for a queue of size " + mSize);
        }
        final int index = (mHead + position) % mValues.length;
        return mValues[index];
    }

    /**
     * Returns the element at the head of the queue, without removing it.
     *
     * @return the element at the head of the queue.
     * @throws NoSuchElementException if the queue is empty
     */
    public long peekFirst() {
        if (mSize == 0) {
            throw new NoSuchElementException("Queue is empty!");
        }
        return mValues[mHead];
    }

    /**
     * Returns the element at the tail of the queue.
     *
     * @return the element at the tail of the queue.
     * @throws NoSuchElementException if the queue is empty.
     */
    public long peekLast() {
        if (mSize == 0) {
            throw new NoSuchElementException("Queue is empty!");
        }
        final int index = (mTail == 0) ? mValues.length - 1 : mTail - 1;
        return mValues[index];
    }
}
+238 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * 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 android.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.fail;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.NoSuchElementException;

/**
 * Internal tests for {@link LongArrayQueue}.
 */
@SmallTest
@RunWith(AndroidJUnit4.class)
public class LongArrayQueueTest {

    private LongArrayQueue mQueueUnderTest;

    @Before
    public void setUp() {
        mQueueUnderTest = new LongArrayQueue();
    }

    @Test
    public void removeFirstOnEmptyQueue() {
        try {
            mQueueUnderTest.removeFirst();
            fail("removeFirst() succeeded on an empty queue!");
        } catch (NoSuchElementException e) {
        }
        mQueueUnderTest.addLast(5);
        mQueueUnderTest.removeFirst();
        try {
            mQueueUnderTest.removeFirst();
            fail("removeFirst() succeeded on an empty queue!");
        } catch (NoSuchElementException e) {
        }
    }

    @Test
    public void addLastRemoveFirstFifo() {
        mQueueUnderTest.addLast(1);
        assertEquals(1, mQueueUnderTest.removeFirst());
        int n = 890;
        int removes = 0;
        for (int i = 0; i < n; i++) {
            mQueueUnderTest.addLast(i);
            if ((i % 3) == 0) {
                assertEquals(removes++, mQueueUnderTest.removeFirst());
            }
        }
        while (removes < n) {
            assertEquals(removes++, mQueueUnderTest.removeFirst());
        }
    }

    @Test
    public void peekFirstOnEmptyQueue() {
        try {
            mQueueUnderTest.peekFirst();
            fail("peekFirst() succeeded on an empty queue!");
        } catch (NoSuchElementException e) {
        }
        mQueueUnderTest.addLast(5);
        mQueueUnderTest.removeFirst();
        try {
            mQueueUnderTest.peekFirst();
            fail("peekFirst() succeeded on an empty queue!");
        } catch (NoSuchElementException e) {
        }
    }

    @Test
    public void peekFirstChanges() {
        mQueueUnderTest.addLast(1);
        assertEquals(1, mQueueUnderTest.peekFirst());
        mQueueUnderTest.addLast(2);
        mQueueUnderTest.addLast(3);
        mQueueUnderTest.addLast(4);
        // addLast() has no effect on peekFirst().
        assertEquals(1, mQueueUnderTest.peekFirst());
        mQueueUnderTest.removeFirst();
        mQueueUnderTest.removeFirst();
        assertEquals(3, mQueueUnderTest.peekFirst());
    }

    @Test
    public void peekLastOnEmptyQueue() {
        try {
            mQueueUnderTest.peekLast();
            fail("peekLast() succeeded on an empty queue!");
        } catch (NoSuchElementException e) {
        }
        mQueueUnderTest.addLast(5);
        mQueueUnderTest.removeFirst();
        try {
            mQueueUnderTest.peekLast();
            fail("peekLast() succeeded on an empty queue!");
        } catch (NoSuchElementException e) {
        }
    }

    @Test
    public void peekLastChanges() {
        mQueueUnderTest.addLast(1);
        assertEquals(1, mQueueUnderTest.peekLast());
        mQueueUnderTest.addLast(2);
        mQueueUnderTest.addLast(3);
        mQueueUnderTest.addLast(4);
        assertEquals(4, mQueueUnderTest.peekLast());
        mQueueUnderTest.removeFirst();
        mQueueUnderTest.removeFirst();
        // removeFirst() has no effect on peekLast().
        assertEquals(4, mQueueUnderTest.peekLast());
    }

    @Test
    public void peekFirstVsPeekLast() {
        mQueueUnderTest.addLast(2);
        assertEquals(mQueueUnderTest.peekFirst(), mQueueUnderTest.peekLast());
        mQueueUnderTest.addLast(3);
        assertNotEquals(mQueueUnderTest.peekFirst(), mQueueUnderTest.peekLast());
        mQueueUnderTest.removeFirst();
        assertEquals(mQueueUnderTest.peekFirst(), mQueueUnderTest.peekLast());
    }

    @Test
    public void peekFirstVsRemoveFirst() {
        int n = 25;
        for (int i = 0; i < n; i++) {
            mQueueUnderTest.addLast(i + 1);
        }
        for (int i = 0; i < n; i++) {
            long peekVal = mQueueUnderTest.peekFirst();
            assertEquals(peekVal, mQueueUnderTest.removeFirst());
        }
    }

    @Test
    public void sizeOfEmptyQueue() {
        assertEquals(0, mQueueUnderTest.size());
        mQueueUnderTest = new LongArrayQueue(1000);
        // capacity doesn't affect size.
        assertEquals(0, mQueueUnderTest.size());
    }

    @Test
    public void sizeAfterOperations() {
        final int added = 1200;
        for (int i = 0; i < added; i++) {
            mQueueUnderTest.addLast(i);
        }
        // each add increments the size by 1.
        assertEquals(added, mQueueUnderTest.size());
        mQueueUnderTest.peekLast();
        mQueueUnderTest.peekFirst();
        // peeks don't change the size.
        assertEquals(added, mQueueUnderTest.size());
        final int removed = 345;
        for (int i = 0; i < removed; i++) {
            mQueueUnderTest.removeFirst();
        }
        // each remove decrements the size by 1.
        assertEquals(added - removed, mQueueUnderTest.size());
        mQueueUnderTest.clear();
        // clear reduces the size to 0.
        assertEquals(0, mQueueUnderTest.size());
    }

    @Test
    public void getInvalidPositions() {
        try {
            mQueueUnderTest.get(0);
            fail("get(0) succeeded on an empty queue!");
        } catch (IndexOutOfBoundsException e) {
        }
        int n = 520;
        for (int i = 0; i < 2 * n; i++) {
            mQueueUnderTest.addLast(i + 1);
        }
        for (int i = 0; i < n; i++) {
            mQueueUnderTest.removeFirst();
        }
        try {
            mQueueUnderTest.get(-3);
            fail("get(-3) succeeded");
        } catch (IndexOutOfBoundsException e) {
        }
        assertEquals(n, mQueueUnderTest.size());
        try {
            mQueueUnderTest.get(n);
            fail("get(" + n + ") succeeded on a queue with " + n + " elements");
        } catch (IndexOutOfBoundsException e) {
        }
        try {
            mQueueUnderTest.get(n + 3);
            fail("get(" + (n + 3) + ") succeeded on a queue with " + n + " elements");
        } catch (IndexOutOfBoundsException e) {
        }
    }

    @Test
    public void getValidPositions() {
        int added = 423;
        int removed = 212;
        for (int i = 0; i < added; i++) {
            mQueueUnderTest.addLast(i);
        }
        for (int i = 0; i < removed; i++) {
            mQueueUnderTest.removeFirst();
        }
        for (int i = 0; i < (added - removed); i++) {
            assertEquals(removed + i, mQueueUnderTest.get(i));
        }
    }
}
+279 −81

File changed.

Preview size limit exceeded, changes collapsed.

+1 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
    "presubmit": [
        {
            "name": "FrameworksMockingServicesTests",
            "file_patterns": ["AlarmManagerService\\.java"],
            "options": [
                {
                  "include-annotation": "android.platform.test.annotations.Presubmit"
+291 −7

File changed.

Preview size limit exceeded, changes collapsed.