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

Commit b72d31c3 authored by Ang Li's avatar Ang Li Committed by Android (Google) Code Review
Browse files

Merge "Add test for siftUp behavior in MessageHeap add method" into main

parents c0bbfb05 9e0bfd82
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.os;

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

@@ -195,4 +196,33 @@ public final class MessageHeapTest {
        }
        assertTrue(verify(heap));
    }

    /**
     * Verifies that adding a message with a smaller 'when' value than the current minimum
     * correctly places it at the front of the heap. This directly tests the 'siftUp' operation
     * that is invoked within the add() method.
     */
    @Test
    public void add_siftsUpSmallerElementToFront() {
        MessageHeap heap = new MessageHeap();

        // Add a few messages to populate the heap.
        insertMessage(heap, 100);
        insertMessage(heap, 200);
        insertMessage(heap, 150);

        // At this point, the message with 'when' = 100 should be at the top.
        assertEquals(100, heap.peek().when);

        // Add a new message with the smallest 'when' value.
        // This should trigger a 'siftUp' operation to move it to the root.
        insertMessage(heap, 50);

        // Verify that the new smallest message is now at the top.
        assertEquals(50, heap.peek().when);
        assertEquals(4, heap.size());

        // Verify the entire heap is still valid and ordered correctly.
        assertTrue(verify(heap));
    }
}