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