1"""
2Test some expressions involving STL data types.
3"""
4
5
6
7import unittest2
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class STLTestCase(TestBase):
15
16    mydir = TestBase.compute_mydir(__file__)
17
18    def setUp(self):
19        # Call super's setUp().
20        TestBase.setUp(self)
21        # Find the line number to break inside main().
22        self.source = 'main.cpp'
23        self.line = line_number(
24            self.source, '// Set break point at this line.')
25
26    @skipIf
27    @expectedFailureAll(bugnumber="llvm.org/PR36713")
28    def test(self):
29        """Test some expressions involving STL data types."""
30        self.build()
31        exe = self.getBuildArtifact("a.out")
32
33        # The following two lines, if uncommented, will enable loggings.
34        #self.ci.HandleCommand("log enable -f /tmp/lldb.log lldb default", res)
35        # self.assertTrue(res.Succeeded())
36
37        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
38
39        lldbutil.run_break_set_by_file_and_line(
40            self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
41
42        self.runCmd("run", RUN_SUCCEEDED)
43
44        # Stop at 'std::string hello_world ("Hello World!");'.
45        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
46                    substrs=['main.cpp:%d' % self.line,
47                             'stop reason = breakpoint'])
48
49        # The breakpoint should have a hit count of 1.
50        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
51                    substrs=[' resolved, hit count = 1'])
52
53        # Now try some expressions....
54
55        self.runCmd(
56            'expr for (int i = 0; i < hello_world.length(); ++i) { (void)printf("%c\\n", hello_world[i]); }')
57
58        self.expect('expr associative_array.size()',
59                    substrs=[' = 3'])
60        self.expect('expr associative_array.count(hello_world)',
61                    substrs=[' = 1'])
62        self.expect('expr associative_array[hello_world]',
63                    substrs=[' = 1'])
64        self.expect('expr associative_array["hello"]',
65                    substrs=[' = 2'])
66
67    @expectedFailureAll(
68        compiler="icc",
69        bugnumber="ICC (13.1, 14-beta) do not emit DW_TAG_template_type_parameter.")
70    @add_test_categories(['pyapi'])
71    def test_SBType_template_aspects(self):
72        """Test APIs for getting template arguments from an SBType."""
73        self.build()
74        exe = self.getBuildArtifact("a.out")
75
76        # Create a target by the debugger.
77        target = self.dbg.CreateTarget(exe)
78        self.assertTrue(target, VALID_TARGET)
79
80        # Create the breakpoint inside function 'main'.
81        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
82        self.assertTrue(breakpoint, VALID_BREAKPOINT)
83
84        # Now launch the process, and do not stop at entry point.
85        process = target.LaunchSimple(
86            None, None, self.get_process_working_directory())
87        self.assertTrue(process, PROCESS_IS_VALID)
88
89        # Get Frame #0.
90        self.assertEquals(process.GetState(), lldb.eStateStopped)
91        thread = lldbutil.get_stopped_thread(
92            process, lldb.eStopReasonBreakpoint)
93        self.assertTrue(
94            thread.IsValid(),
95            "There should be a thread stopped due to breakpoint condition")
96        frame0 = thread.GetFrameAtIndex(0)
97
98        # Get the type for variable 'associative_array'.
99        associative_array = frame0.FindVariable('associative_array')
100        self.DebugSBValue(associative_array)
101        self.assertTrue(associative_array, VALID_VARIABLE)
102        map_type = associative_array.GetType()
103        self.DebugSBType(map_type)
104        self.assertTrue(map_type, VALID_TYPE)
105        num_template_args = map_type.GetNumberOfTemplateArguments()
106        self.assertTrue(num_template_args > 0)
107
108        # We expect the template arguments to contain at least 'string' and
109        # 'int'.
110        expected_types = {'string': False, 'int': False}
111        for i in range(num_template_args):
112            t = map_type.GetTemplateArgumentType(i)
113            self.DebugSBType(t)
114            self.assertTrue(t, VALID_TYPE)
115            name = t.GetName()
116            if 'string' in name:
117                expected_types['string'] = True
118            elif 'int' == name:
119                expected_types['int'] = True
120
121        # Check that both entries of the dictionary have 'True' as the value.
122        self.assertTrue(all(expected_types.values()))
123