xref: /llvm-project/lldb/test/API/lang/c/set_values/TestSetValues.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""Test settings and readings of program variables."""
2
3
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class SetValuesTestCase(TestBase):
11    def setUp(self):
12        # Call super's setUp().
13        TestBase.setUp(self)
14        # Find the line numbers to break inside main().
15        self.line1 = line_number("main.c", "// Set break point #1.")
16        self.line2 = line_number("main.c", "// Set break point #2.")
17        self.line3 = line_number("main.c", "// Set break point #3.")
18        self.line4 = line_number("main.c", "// Set break point #4.")
19        self.line5 = line_number("main.c", "// Set break point #5.")
20
21    def test(self):
22        """Test settings and readings of program variables."""
23        self.build()
24        exe = self.getBuildArtifact("a.out")
25        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
26
27        # Set breakpoints on several places to set program variables.
28        lldbutil.run_break_set_by_file_and_line(
29            self, "main.c", self.line1, num_expected_locations=1, loc_exact=True
30        )
31
32        lldbutil.run_break_set_by_file_and_line(
33            self, "main.c", self.line2, num_expected_locations=1, loc_exact=True
34        )
35
36        lldbutil.run_break_set_by_file_and_line(
37            self, "main.c", self.line3, num_expected_locations=1, loc_exact=True
38        )
39
40        lldbutil.run_break_set_by_file_and_line(
41            self, "main.c", self.line4, num_expected_locations=1, loc_exact=True
42        )
43
44        lldbutil.run_break_set_by_file_and_line(
45            self, "main.c", self.line5, num_expected_locations=1, loc_exact=True
46        )
47
48        self.runCmd("run", RUN_SUCCEEDED)
49
50        # The stop reason of the thread should be breakpoint.
51        self.expect(
52            "thread list",
53            STOPPED_DUE_TO_BREAKPOINT,
54            substrs=["stopped", "stop reason = breakpoint"],
55        )
56
57        # The breakpoint should have a hit count of 1.
58        lldbutil.check_breakpoint(self, bpno=1, expected_hit_count=1)
59
60        # main.c:15
61        # Check that 'frame variable --show-types' displays the correct data
62        # type and value.
63        self.expect(
64            "frame variable --show-types",
65            VARIABLES_DISPLAYED_CORRECTLY,
66            startstr="(char) i = 'a'",
67        )
68
69        # Now set variable 'i' and check that it is correctly displayed.
70        self.runCmd("expression i = 'b'")
71        self.expect(
72            "frame variable --show-types",
73            VARIABLES_DISPLAYED_CORRECTLY,
74            startstr="(char) i = 'b'",
75        )
76
77        self.runCmd("continue")
78
79        # main.c:36
80        # Check that 'frame variable --show-types' displays the correct data
81        # type and value.
82        self.expect(
83            "frame variable --show-types",
84            VARIABLES_DISPLAYED_CORRECTLY,
85            patterns=["\((short unsigned int|unsigned short)\) i = 33"],
86        )
87
88        # Now set variable 'i' and check that it is correctly displayed.
89        self.runCmd("expression i = 333")
90        self.expect(
91            "frame variable --show-types",
92            VARIABLES_DISPLAYED_CORRECTLY,
93            patterns=["\((short unsigned int|unsigned short)\) i = 333"],
94        )
95
96        self.runCmd("continue")
97
98        # main.c:57
99        # Check that 'frame variable --show-types' displays the correct data
100        # type and value.
101        self.expect(
102            "frame variable --show-types",
103            VARIABLES_DISPLAYED_CORRECTLY,
104            startstr="(long) i = 33",
105        )
106
107        # Now set variable 'i' and check that it is correctly displayed.
108        self.runCmd("expression i = 33333")
109        self.expect(
110            "frame variable --show-types",
111            VARIABLES_DISPLAYED_CORRECTLY,
112            startstr="(long) i = 33333",
113        )
114
115        self.runCmd("continue")
116
117        # main.c:78
118        # Check that 'frame variable --show-types' displays the correct data
119        # type and value.
120        self.expect(
121            "frame variable --show-types",
122            VARIABLES_DISPLAYED_CORRECTLY,
123            startstr="(double) i = 2.25",
124        )
125
126        # Now set variable 'i' and check that it is correctly displayed.
127        self.runCmd("expression i = 1.5")
128        self.expect(
129            "frame variable --show-types",
130            VARIABLES_DISPLAYED_CORRECTLY,
131            startstr="(double) i = 1.5",
132        )
133
134        self.runCmd("continue")
135
136        # main.c:85
137        # Check that 'frame variable --show-types' displays the correct data
138        # type and value.
139        self.expect(
140            "frame variable --show-types",
141            VARIABLES_DISPLAYED_CORRECTLY,
142            startstr="(long double) i = 2.25",
143        )
144
145        # Now set variable 'i' and check that it is correctly displayed.
146        self.runCmd("expression i = 1.5")
147        self.expect(
148            "frame variable --show-types",
149            VARIABLES_DISPLAYED_CORRECTLY,
150            startstr="(long double) i = 1.5",
151        )
152