TestCStrings.py revision e2172fd8191f1f6493a4388f7082bd2f2f43d67f
1"""
2Tests that C strings work as expected in expressions
3"""
4import lldb
5from lldbtest import *
6import lldbutil
7
8class CStringsTestCase(TestBase):
9
10    mydir = os.path.join("lang", "c", "strings")
11
12    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
13    @dsym_test
14    def test_with_dsym_and_run_command(self):
15        """Tests that C strings work as expected in expressions"""
16        self.buildDsym()
17        self.static_method_commands()
18
19    @dwarf_test
20    def test_with_dwarf_and_run_command(self):
21        """Tests that C strings work as expected in expressions"""
22        self.buildDwarf()
23        self.static_method_commands()
24
25    def setUp(self):
26        TestBase.setUp(self)
27
28    def set_breakpoint(self, line):
29        lldbutil.run_break_set_by_file_and_line (self, "main.c", line, num_expected_locations=1, loc_exact=True)
30
31    def static_method_commands(self):
32        """Tests that C strings work as expected in expressions"""
33        self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
34
35        self.set_breakpoint(line_number('main.c', '// breakpoint 1'))
36
37        self.runCmd("process launch", RUN_SUCCEEDED)
38
39        self.expect("expression -- a[2]",
40                    patterns = ["\((const )?char\) \$0 = 'c'"])
41
42        self.expect("expression -- z[2]",
43                    startstr = "(const char) $1 = 'x'")
44
45        # On Linux, the expression below will test GNU indirect function calls.
46        self.expect("expression -- (int)strlen(\"hello\")",
47                    startstr = "(int) $2 = 5")
48
49        self.expect("expression -- \"world\"[2]",
50                    startstr = "(const char) $3 = 'r'")
51
52        self.expect("expression -- \"\"[0]",
53                    startstr = "(const char) $4 = '\\0'")
54
55        self.expect("expr --raw -- \"hello\"",
56            substrs = ['[0] = \'h\'',
57                       '[5] = \'\\0\''])
58
59        self.expect("p \"hello\"",
60            substrs = ['[6]) $', 'hello'])
61
62        self.expect("p (char*)\"hello\"",
63                    substrs = ['(char *) $', ' = 0x',
64                               'hello'])
65
66        self.expect("p (int)strlen(\"\")",
67                    substrs = ['(int) $', ' = 0'])
68
69if __name__ == '__main__':
70    import atexit
71    lldb.SBDebugger.Initialize()
72    atexit.register(lambda: lldb.SBDebugger.Terminate())
73    unittest2.main()
74