1"""
2Test that global operators are found and evaluated.
3"""
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class TestCppGlobalOperators(TestBase):
11
12    def prepare_executable_and_get_frame(self):
13        self.build()
14
15        # Get main source file
16        src_file = "main.cpp"
17        src_file_spec = lldb.SBFileSpec(src_file)
18        self.assertTrue(src_file_spec.IsValid(), "Main source file")
19
20        # Get the path of the executable
21        exe_path = self.getBuildArtifact("a.out")
22
23        # Load the executable
24        target = self.dbg.CreateTarget(exe_path)
25        self.assertTrue(target.IsValid(), VALID_TARGET)
26
27        # Break on main function
28        main_breakpoint = target.BreakpointCreateBySourceRegex(
29            "// break here", src_file_spec)
30        self.assertTrue(
31            main_breakpoint.IsValid() and main_breakpoint.GetNumLocations() >= 1,
32            VALID_BREAKPOINT)
33
34        # Launch the process
35        args = None
36        env = None
37        process = target.LaunchSimple(
38            args, env, self.get_process_working_directory())
39        self.assertTrue(process.IsValid(), PROCESS_IS_VALID)
40
41        # Get the thread of the process
42        self.assertEqual(
43            process.GetState(), lldb.eStateStopped,
44            PROCESS_STOPPED)
45        thread = lldbutil.get_stopped_thread(
46            process, lldb.eStopReasonBreakpoint)
47
48        return thread.GetSelectedFrame()
49
50    def test_equals_operator(self):
51        frame = self.prepare_executable_and_get_frame()
52
53        test_result = frame.EvaluateExpression("operator==(s1, s2)")
54        self.assertTrue(
55            test_result.IsValid() and test_result.GetValue() == "false",
56            "operator==(s1, s2) = false")
57
58        test_result = frame.EvaluateExpression("operator==(s1, s3)")
59        self.assertTrue(
60            test_result.IsValid() and test_result.GetValue() == "true",
61            "operator==(s1, s3) = true")
62
63        test_result = frame.EvaluateExpression("operator==(s2, s3)")
64        self.assertTrue(
65            test_result.IsValid() and test_result.GetValue() == "false",
66            "operator==(s2, s3) = false")
67
68    def do_new_test(self, frame, expr, expected_value_name):
69        """Evaluate a new expression, and check its result"""
70
71        expected_value = frame.FindValue(
72            expected_value_name, lldb.eValueTypeVariableGlobal)
73        self.assertTrue(expected_value.IsValid())
74
75        expected_value_addr = expected_value.AddressOf()
76        self.assertTrue(expected_value_addr.IsValid())
77
78        got = frame.EvaluateExpression(expr)
79        self.assertTrue(got.IsValid())
80        self.assertEqual(
81            got.GetValueAsUnsigned(),
82            expected_value_addr.GetValueAsUnsigned())
83        got_type = got.GetType()
84        self.assertTrue(got_type.IsPointerType())
85        self.assertEqual(got_type.GetPointeeType().GetName(), "Struct")
86
87    def test_operator_new(self):
88        frame = self.prepare_executable_and_get_frame()
89
90        self.do_new_test(frame, "new Struct()", "global_new_buf")
91        self.do_new_test(frame, "new(new_tag) Struct()", "tagged_new_buf")
92