summaryrefslogtreecommitdiff
path: root/test/test_pid.cc
blob: e79eb1e83dc19517d48b4684841414ade49c1e94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */

/*
 * test_pid.cc — Self-tests for PID allocator
 *
 * Tests: alloc, free, re-use, exhaustion guard, reserved PIDs.
 */

#include <lume/selftest.h>
#include <lume/pid.h>

void selftest_pid()
{
    /* Create a local PidAllocator for isolated testing */
    PidAllocator pa;
    pa.init();

    /* ---- PID 0 is reserved (idle/swapper) ---- */
    st_begin("pid: PID 0 is reserved");
    {
        /* First alloc should return 1 (PID 0 reserved for idle) */
        int32 first = pa.alloc();
        ST_ASSERT(first >= 1);
    }
    st_pass();

    /* ---- Sequential allocation returns unique PIDs ---- */
    st_begin("pid: sequential allocation returns unique PIDs");
    {
        PidAllocator pa2;
        pa2.init();

        int32 p1 = pa2.alloc();
        int32 p2 = pa2.alloc();
        int32 p3 = pa2.alloc();
        ST_ASSERT(p1 >= 1);
        ST_ASSERT(p2 >= 1);
        ST_ASSERT(p3 >= 1);
        ST_ASSERT_NE(p1, p2);
        ST_ASSERT_NE(p2, p3);
        ST_ASSERT_NE(p1, p3);
    }
    st_pass();

    /* ---- Freed PID can be reused ---- */
    st_begin("pid: freed PID can be reused");
    {
        PidAllocator pa3;
        pa3.init();

        /* Allocate all PIDs from 2..N */
        constexpr int kAllocCount = 60;
        int32 pids[kAllocCount];
        for (int i = 0; i < kAllocCount; i++) {
            pids[i] = pa3.alloc();
            ST_ASSERT(pids[i] >= 1);
        }

        /* Free one in the middle */
        int32 freed_pid = pids[kAllocCount / 2];
        pa3.free(freed_pid);

        /* Allocate again — should eventually get the freed PID */
        bool found = false;
        for (int i = 0; i < kAllocCount; i++) {
            int32 p = pa3.alloc();
            if (p == freed_pid) { found = true; break; }
            pa3.free(p);
        }
        ST_ASSERT(found);
    }
    st_pass();

    /* ---- free invalid PID is safe ---- */
    st_begin("pid: free invalid PID is no-op");
    {
        PidAllocator pa4;
        pa4.init();

        /* These should not crash */
        pa4.free(-1);
        pa4.free(kMaxPid);
        pa4.free(kMaxPid + 100);

        /* Allocator should still work — PID 0 still reserved */
        int32 p = pa4.alloc();
        ST_ASSERT(p >= 1);
    }
    st_pass();

    /* ---- Monotonic allocation before wrap ---- */
    st_begin("pid: monotonic allocation");
    {
        PidAllocator pa5;
        pa5.init();

        int32 prev = pa5.alloc();
        for (int i = 0; i < 10; i++) {
            int32 next = pa5.alloc();
            ST_ASSERT(next > prev);
            prev = next;
        }
    }
    st_pass();
}