blob: 62f433230f4601e21ad414a818479221647d9d10 [file] [log] [blame]
Johnny Chen377a8ed2010-07-28 21:24:31 +00001"""
Johnny Chen5fede4b2011-10-31 23:35:33 +00002Test some expressions involving STL data types.
Johnny Chen377a8ed2010-07-28 21:24:31 +00003"""
4
5import os, time
Johnny Chen73258832010-08-05 23:42:46 +00006import unittest2
Johnny Chen377a8ed2010-07-28 21:24:31 +00007import lldb
Johnny Chen15f247a2012-02-03 20:43:00 +00008import lldbutil
Johnny Chen17941842010-08-09 23:44:24 +00009from lldbtest import *
Johnny Chen377a8ed2010-07-28 21:24:31 +000010
Johnny Chencbb4be02010-09-01 19:59:58 +000011class STLTestCase(TestBase):
Johnny Chen377a8ed2010-07-28 21:24:31 +000012
Johnny Chen8d798eb2011-06-25 21:07:03 +000013 mydir = os.path.join("lang", "cpp", "stl")
Johnny Chen377a8ed2010-07-28 21:24:31 +000014
Johnny Chen9b547242011-11-14 18:33:39 +000015 # rdar://problem/10400981
Johnny Chen5fede4b2011-10-31 23:35:33 +000016 @unittest2.expectedFailure
Johnny Chen165a0792010-09-07 18:27:35 +000017 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chen24086bc2012-04-06 19:54:10 +000018 @dsym_test
Johnny Chen165a0792010-09-07 18:27:35 +000019 def test_with_dsym(self):
Johnny Chen5fede4b2011-10-31 23:35:33 +000020 """Test some expressions involving STL data types."""
Johnny Chen165a0792010-09-07 18:27:35 +000021 self.buildDsym()
Johnny Chen5fede4b2011-10-31 23:35:33 +000022 self.step_stl_exprs()
Johnny Chen165a0792010-09-07 18:27:35 +000023
Johnny Chen9b547242011-11-14 18:33:39 +000024 # rdar://problem/10400981
Johnny Chen5fede4b2011-10-31 23:35:33 +000025 @unittest2.expectedFailure
Johnny Chen24086bc2012-04-06 19:54:10 +000026 @dwarf_test
Johnny Chen165a0792010-09-07 18:27:35 +000027 def test_with_dwarf(self):
Johnny Chen5fede4b2011-10-31 23:35:33 +000028 """Test some expressions involving STL data types."""
Johnny Chen165a0792010-09-07 18:27:35 +000029 self.buildDwarf()
Johnny Chen5fede4b2011-10-31 23:35:33 +000030 self.step_stl_exprs()
Johnny Chen165a0792010-09-07 18:27:35 +000031
Johnny Chen15f247a2012-02-03 20:43:00 +000032 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000033 @dsym_test
Johnny Chen15f247a2012-02-03 20:43:00 +000034 def test_SBType_template_aspects_with_dsym(self):
Johnny Chen9dad8ae2012-02-03 20:50:56 +000035 """Test APIs for getting template arguments from an SBType."""
Johnny Chen15f247a2012-02-03 20:43:00 +000036 self.buildDsym()
37 self.sbtype_template_apis()
38
39 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000040 @dwarf_test
Johnny Chen15f247a2012-02-03 20:43:00 +000041 def test_SBType_template_aspects_with_dwarf(self):
Johnny Chen9dad8ae2012-02-03 20:50:56 +000042 """Test APIs for getting template arguments from an SBType."""
Johnny Chen15f247a2012-02-03 20:43:00 +000043 self.buildDwarf()
44 self.sbtype_template_apis()
45
Johnny Chen77ca1a42010-10-12 22:53:02 +000046 def setUp(self):
Johnny Chenaadcef52010-10-14 17:31:24 +000047 # Call super's setUp().
48 TestBase.setUp(self)
Johnny Chen77ca1a42010-10-12 22:53:02 +000049 # Find the line number to break inside main().
Johnny Chen15f247a2012-02-03 20:43:00 +000050 self.source = 'main.cpp'
51 self.line = line_number(self.source, '// Set break point at this line.')
Johnny Chen77ca1a42010-10-12 22:53:02 +000052
Johnny Chen5fede4b2011-10-31 23:35:33 +000053 def step_stl_exprs(self):
54 """Test some expressions involving STL data types."""
Johnny Chen377a8ed2010-07-28 21:24:31 +000055 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chenff3d01d2010-08-20 21:03:09 +000056
Johnny Chen377a8ed2010-07-28 21:24:31 +000057 # The following two lines, if uncommented, will enable loggings.
58 #self.ci.HandleCommand("log enable -f /tmp/lldb.log lldb default", res)
59 #self.assertTrue(res.Succeeded())
Johnny Chenff3d01d2010-08-20 21:03:09 +000060
61 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
Johnny Chen377a8ed2010-07-28 21:24:31 +000062
Johnny Chen78c0aeb2010-10-12 23:01:50 +000063 # rdar://problem/8543077
64 # test/stl: clang built binaries results in the breakpoint locations = 3,
65 # is this a problem with clang generated debug info?
Jim Ingham63dfc722012-09-22 00:05:11 +000066 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
Johnny Chen377a8ed2010-07-28 21:24:31 +000067
Johnny Chen5ee88192010-08-27 23:47:36 +000068 self.runCmd("run", RUN_SUCCEEDED)
Johnny Chen377a8ed2010-07-28 21:24:31 +000069
70 # Stop at 'std::string hello_world ("Hello World!");'.
Johnny Chenff3d01d2010-08-20 21:03:09 +000071 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Johnny Chen77ca1a42010-10-12 22:53:02 +000072 substrs = ['main.cpp:%d' % self.line,
Johnny Chenff3d01d2010-08-20 21:03:09 +000073 'stop reason = breakpoint'])
Johnny Chen377a8ed2010-07-28 21:24:31 +000074
75 # The breakpoint should have a hit count of 1.
Caroline Tice79042b32011-02-04 22:59:41 +000076 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
Johnny Chenff3d01d2010-08-20 21:03:09 +000077 substrs = [' resolved, hit count = 1'])
Johnny Chen377a8ed2010-07-28 21:24:31 +000078
Johnny Chen9a1e9af2011-10-31 23:28:52 +000079 # Now try some expressions....
Johnny Chen377a8ed2010-07-28 21:24:31 +000080
Johnny Chen9a1e9af2011-10-31 23:28:52 +000081 self.runCmd('expr for (int i = 0; i < hello_world.length(); ++i) { (void)printf("%c\\n", hello_world[i]); }')
82
Johnny Chen5fede4b2011-10-31 23:35:33 +000083 # rdar://problem/10373783
Johnny Chen9b547242011-11-14 18:33:39 +000084 # rdar://problem/10400981
Johnny Chen9a1e9af2011-10-31 23:28:52 +000085 self.expect('expr associative_array.size()',
86 substrs = [' = 3'])
87 self.expect('expr associative_array.count(hello_world)',
88 substrs = [' = 1'])
89 self.expect('expr associative_array[hello_world]',
90 substrs = [' = 1'])
91 self.expect('expr associative_array["hello"]',
92 substrs = [' = 2'])
Johnny Chen377a8ed2010-07-28 21:24:31 +000093
Johnny Chen15f247a2012-02-03 20:43:00 +000094 def sbtype_template_apis(self):
95 """Test APIs for getting template arguments from an SBType."""
96 exe = os.path.join(os.getcwd(), 'a.out')
97
98 # Create a target by the debugger.
99 target = self.dbg.CreateTarget(exe)
100 self.assertTrue(target, VALID_TARGET)
101
102 # Create the breakpoint inside function 'main'.
103 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
104 self.assertTrue(breakpoint, VALID_BREAKPOINT)
105
106 # Now launch the process, and do not stop at entry point.
107 process = target.LaunchSimple(None, None, os.getcwd())
108 self.assertTrue(process, PROCESS_IS_VALID)
109
110 # Get Frame #0.
111 self.assertTrue(process.GetState() == lldb.eStateStopped)
112 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
113 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
114 frame0 = thread.GetFrameAtIndex(0)
115
116 # Get the type for variable 'associative_array'.
117 associative_array = frame0.FindVariable('associative_array')
118 self.DebugSBValue(associative_array)
119 self.assertTrue(associative_array, VALID_VARIABLE)
120 map_type = associative_array.GetType()
121 self.DebugSBType(map_type)
122 self.assertTrue(map_type, VALID_TYPE)
123 num_template_args = map_type.GetNumberOfTemplateArguments()
124 self.assertTrue(num_template_args > 0)
125
126 # We expect the template arguments to contain at least 'string' and 'int'.
127 expected_types = { 'string': False, 'int': False }
128 for i in range(num_template_args):
129 t = map_type.GetTemplateArgumentType(i)
130 self.DebugSBType(t)
131 self.assertTrue(t, VALID_TYPE)
132 name = t.GetName()
133 if 'string' in name:
134 expected_types['string'] = True
135 elif 'int' == name:
136 expected_types['int'] = True
137
138 # Check that both entries of the dictionary have 'True' as the value.
139 self.assertTrue(all(expected_types.values()))
140
Johnny Chen377a8ed2010-07-28 21:24:31 +0000141
142if __name__ == '__main__':
Johnny Chena2124952010-08-05 21:23:45 +0000143 import atexit
Johnny Chen377a8ed2010-07-28 21:24:31 +0000144 lldb.SBDebugger.Initialize()
Johnny Chena2124952010-08-05 21:23:45 +0000145 atexit.register(lambda: lldb.SBDebugger.Terminate())
Johnny Chen73258832010-08-05 23:42:46 +0000146 unittest2.main()