1"""
2Test LLDB's data formatter for libcxx's std::deque.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class LibcxxDequeDataFormatterTestCase(TestBase):
12    def check_numbers(self, var_name):
13        self.expect(
14            "frame variable " + var_name,
15            substrs=[
16                var_name + " = size=7",
17                "[0] = 1",
18                "[1] = 12",
19                "[2] = 123",
20                "[3] = 1234",
21                "[4] = 12345",
22                "[5] = 123456",
23                "[6] = 1234567",
24                "}",
25            ],
26        )
27
28        self.expect_expr(
29            var_name,
30            result_summary="size=7",
31            result_children=[
32                ValueCheck(value="1"),
33                ValueCheck(value="12"),
34                ValueCheck(value="123"),
35                ValueCheck(value="1234"),
36                ValueCheck(value="12345"),
37                ValueCheck(value="123456"),
38                ValueCheck(value="1234567"),
39            ],
40        )
41
42    @add_test_categories(["libc++"])
43    def test_with_run_command(self):
44        """Test basic formatting of std::deque"""
45        self.build()
46        (self.target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
47            self, "break here", lldb.SBFileSpec("main.cpp", False)
48        )
49
50        self.expect("frame variable numbers", substrs=["numbers = size=0"])
51
52        lldbutil.continue_to_breakpoint(process, bkpt)
53
54        # first value added
55        self.expect(
56            "frame variable numbers", substrs=["numbers = size=1", "[0] = 1", "}"]
57        )
58
59        # add remaining values
60        lldbutil.continue_to_breakpoint(process, bkpt)
61
62        self.check_numbers("numbers")
63
64        # clear out the deque
65        lldbutil.continue_to_breakpoint(process, bkpt)
66
67        self.expect("frame variable numbers", substrs=["numbers = size=0"])
68
69    @add_test_categories(["libc++"])
70    def test_ref_and_ptr(self):
71        """Test formatting of std::deque& and std::deque*"""
72        self.build()
73        (self.target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
74            self, "stop here", lldb.SBFileSpec("main.cpp", False)
75        )
76
77        # The reference should display the same was as the value did
78        self.check_numbers("ref")
79
80        # The pointer should just show the right number of elements:
81        self.expect("frame variable ptr", substrs=["ptr =", " size=7"])
82        self.expect("expression ptr", substrs=["$", "size=7"])
83