File: test_llist.py

package info (click to toggle)
drgn 0.0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 5,956 kB
  • sloc: ansic: 49,446; python: 45,054; awk: 423; makefile: 339; sh: 114
file content (114 lines) | stat: -rw-r--r-- 3,835 bytes parent folder | download | duplicates (2)
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
107
108
109
110
111
112
113
114
# Copyright (c) 2022, Oracle and/or its affiliates.
# SPDX-License-Identifier: LGPL-2.1-or-later

from drgn import NULL
from drgn.helpers.linux.llist import (
    llist_empty,
    llist_first_entry,
    llist_first_entry_or_null,
    llist_for_each,
    llist_for_each_entry,
    llist_is_singular,
    llist_next_entry,
)
from tests.linux_kernel import LinuxKernelTestCase, skip_unless_have_test_kmod


@skip_unless_have_test_kmod
class TestLlist(LinuxKernelTestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.empty = cls.prog["drgn_test_empty_llist"].address_of_()
        cls.full = cls.prog["drgn_test_full_llist"].address_of_()
        cls.entries = cls.prog["drgn_test_llist_entries"]
        cls.num_entries = 3
        cls.singular = cls.prog["drgn_test_singular_llist"].address_of_()
        cls.singular_entry = cls.prog["drgn_test_singular_llist_entry"].address_of_()
        cls.singular_node = cls.singular_entry.node.address_of_()

    def node(self, n):
        return self.entries[n].node.address_of_()

    def entry(self, n):
        return self.entries[n].address_of_()

    def test_llist_empty(self):
        self.assertTrue(llist_empty(self.empty))
        self.assertFalse(llist_empty(self.full))
        self.assertFalse(llist_empty(self.singular))

    def test_llist_is_singular(self):
        self.assertFalse(llist_is_singular(self.empty))
        self.assertFalse(llist_is_singular(self.full))
        self.assertTrue(llist_is_singular(self.singular))

    def test_llist_first_entry(self):
        self.assertEqual(
            llist_first_entry(self.full, "struct drgn_test_llist_entry", "node"),
            self.entry(2),
        )
        self.assertEqual(
            llist_first_entry(self.singular, "struct drgn_test_llist_entry", "node"),
            self.singular_entry,
        )

    def test_llist_first_entry_or_null(self):
        self.assertEqual(
            llist_first_entry_or_null(
                self.empty, "struct drgn_test_llist_entry", "node"
            ),
            NULL(self.prog, "struct drgn_test_llist_entry *"),
        )
        self.assertEqual(
            llist_first_entry_or_null(
                self.full, "struct drgn_test_llist_entry", "node"
            ),
            self.entry(2),
        )
        self.assertEqual(
            llist_first_entry_or_null(
                self.singular, "struct drgn_test_llist_entry", "node"
            ),
            self.singular_entry,
        )

    def test_llist_next_entry(self):
        for i in reversed(range(1, self.num_entries)):
            self.assertEqual(llist_next_entry(self.entry(i), "node"), self.entry(i - 1))

    def test_llist_for_each(self):
        self.assertEqual(list(llist_for_each(self.empty.first)), [])
        self.assertEqual(
            list(llist_for_each(self.full.first)),
            [self.node(i) for i in reversed(range(self.num_entries))],
        )
        self.assertEqual(
            list(llist_for_each(self.singular.first)), [self.singular_node]
        )

    def test_llist_for_each_entry(self):
        self.assertEqual(
            list(
                llist_for_each_entry(
                    "struct drgn_test_llist_entry", self.empty.first, "node"
                )
            ),
            [],
        )
        self.assertEqual(
            list(
                llist_for_each_entry(
                    "struct drgn_test_llist_entry", self.full.first, "node"
                )
            ),
            [self.entry(i) for i in reversed(range(self.num_entries))],
        )
        self.assertEqual(
            list(
                llist_for_each_entry(
                    "struct drgn_test_llist_entry", self.singular.first, "node"
                )
            ),
            [self.singular_entry],
        )