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