xref: /llvm-project/lldb/test/API/functionalities/data-formatter/parray/TestPrintArray.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Test lldb data formatter subsystem.
3"""
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class PrintArrayTestCase(TestBase):
13    def test_print_array(self):
14        """Test that expr -Z works"""
15        self.build()
16        self.printarray_data_formatter_commands()
17
18    def setUp(self):
19        # Call super's setUp().
20        TestBase.setUp(self)
21        # Find the line number to break at.
22        self.line = line_number("main.cpp", "break here")
23
24    def printarray_data_formatter_commands(self):
25        """Test that expr -Z works"""
26        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
27
28        lldbutil.run_break_set_by_file_and_line(
29            self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True
30        )
31
32        self.runCmd("run", RUN_SUCCEEDED)
33
34        # The stop reason of the thread should be breakpoint.
35        self.expect(
36            "thread list",
37            STOPPED_DUE_TO_BREAKPOINT,
38            substrs=["stopped", "stop reason = breakpoint"],
39        )
40
41        # This is the function to remove the custom formats in order to have a
42        # clean slate for the next test case.
43        def cleanup():
44            self.runCmd("type format clear", check=False)
45            self.runCmd("type summary clear", check=False)
46            self.runCmd("type synth clear", check=False)
47
48        # Execute the cleanup function during test case tear down.
49        self.addTearDownHook(cleanup)
50
51        self.expect(
52            "expr --element-count 3 -- data", substrs=["[0] = 1", "[1] = 3", "[2] = 5"]
53        )
54        self.expect("expr data", substrs=["int *", "$", "0x"])
55        self.expect(
56            "expr -f binary --element-count 0 -- data", substrs=["int *", "$", "0b"]
57        )
58        self.expect(
59            "expr -f hex --element-count 3 -- data",
60            substrs=["[0] = 0x", "1", "[1] = 0x", "3", "[2] = 0x", "5"],
61        )
62        self.expect(
63            "expr -f binary --element-count 2 -- data",
64            substrs=["int *", "$", "0x", "[0] = 0b", "1", "[1] = 0b", "11"],
65        )
66        self.expect("parray 3 data", substrs=["[0] = 1", "[1] = 3", "[2] = 5"])
67        self.expect(
68            "parray `1 + 1 + 1` data", substrs=["[0] = 1", "[1] = 3", "[2] = 5"]
69        )
70        self.expect("parray `data[1]` data", substrs=["[0] = 1", "[1] = 3", "[2] = 5"])
71        self.expect(
72            "parray/x 3 data",
73            substrs=["[0] = 0x", "1", "[1] = 0x", "3", "[2] = 0x", "5"],
74        )
75        self.expect(
76            "parray/x `data[1]` data",
77            substrs=["[0] = 0x", "1", "[1] = 0x", "3", "[2] = 0x", "5"],
78        )
79
80        # check error conditions
81        self.expect(
82            "expr --element-count 10 -- 123",
83            error=True,
84            substrs=[
85                "expression cannot be used with --element-count as it does not refer to a pointer"
86            ],
87        )
88        self.expect(
89            "expr --element-count 10 -- (void*)123",
90            error=True,
91            substrs=[
92                "expression cannot be used with --element-count as it refers to a pointer to void"
93            ],
94        )
95        self.expect("parray data", error=True, substrs=["invalid element count 'data'"])
96        self.expect(
97            "parray data data", error=True, substrs=["invalid element count 'data'"]
98        )
99        self.expect("parray", error=True, substrs=["Not enough arguments provided"])
100