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

Commit 9e0bfd82 authored by AI test gen's avatar AI test gen Committed by Zhuoyao Zhang
Browse files

Add test for siftUp behavior in MessageHeap add method

Adds a focused unit test to verify that when a new message with the smallest 'when' value is added to the heap, it is correctly moved to the top.

This explicitly validates the behavior of the private `siftUp` method as it is used by the public `add` method, ensuring the heap property is maintained upon insertion.


Please help fill out the survey for feedback: https://docs.google.com/forms/d/e/1FAIpQLSeKFKpHImCAqZIa_OR801cw72HQUreM2oGM25C3mKKT2tBFnw/viewform?usp=pp_url&entry.1586624956=ag/35397295

Original Change: ag/35088928 (Note tests are based on the original CL but may add coverage beyond the specific changes to improve overall code health)

Test: ATP tests passed http://go/forrest-run/L39500030017397747
Bug: 431235865
Flag: TEST_ONLY

Change-Id: Ia09ac28c64cc56face55adb96890e7626765e46f
parent 06dac6d4
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));
    }
}