# Path: tests/conftest.py
# def setup_debugger(debugger, binary, lines, on_state_change=None,
#                    startup_info=None, cont=True, wait=True):
#     assert debugger.load_binary("src/{}".format(binary))
# 
#     test_exception = []
# 
#     if not isinstance(lines, Iterable):
#         lines = [lines]
# 
#     for line in lines:
#         assert debugger.breakpoint_manager.add_breakpoint(
#             "src/{}.cpp".format(binary), line)
# 
#     if on_state_change:
#         def on_stop(state, data):
#             if state == ProcessState.Stopped:
#                 try:
#                     on_state_change()
#                     if cont:
#                         debugger.exec_continue()
#                 except Exception as exc:
#                     test_exception.append(traceback.format_exc(exc))
#                     debugger.quit_program()
# 
#         debugger.on_process_state_changed.subscribe(on_stop)
# 
#     assert debugger.launch(startup_info)
# 
#     if wait:
#         debugger.wait_for_exit()
# 
#     if len(test_exception) > 0:
#         pytest.fail(test_exception[0], pytrace=False)


import copy
from tests.conftest import setup_debugger
# -*- coding: utf-8 -*-

"""
This test depends on particular implementation of glibc (that does not
allocate memory on it's own for the given program (src/test_alloc.cpp),
so it may not work elsewhere.
"""



TEST_FILE = "test_alloc"
TEST_LINE = 8


def test_alloc(debugger):
    heap = []
    debugger.heap_manager.on_heap_change.subscribe(lambda new_heap:
                                                   heap.append(
                                                       copy.copy(new_heap)))

    def test_alloc_cb():
        assert len(heap[0]) == 1
        assert heap[0][0].size == 1024
        assert len(heap[1]) == 0


# Path: tests/conftest.py
# def setup_debugger(debugger, binary, lines, on_state_change=None,
#                    startup_info=None, cont=True, wait=True):
#     assert debugger.load_binary("src/{}".format(binary))
# 
#     test_exception = []
# 
#     if not isinstance(lines, Iterable):
#         lines = [lines]
# 
#     for line in lines:
#         assert debugger.breakpoint_manager.add_breakpoint(
#             "src/{}.cpp".format(binary), line)
# 
#     if on_state_change:
#         def on_stop(state, data):
#             if state == ProcessState.Stopped:
#                 try:
#                     on_state_change()
#                     if cont:
#                         debugger.exec_continue()
#                 except Exception as exc:
#                     test_exception.append(traceback.format_exc(exc))
#                     debugger.quit_program()
# 
#         debugger.on_process_state_changed.subscribe(on_stop)
# 
#     assert debugger.launch(startup_info)
# 
#     if wait:
#         debugger.wait_for_exit()
# 
#     if len(test_exception) > 0:
#         pytest.fail(test_exception[0], pytrace=False)


# Path: tests/test_mi_alloc.py
import copy
from tests.conftest import setup_debugger

# -*- coding: utf-8 -*-

"""
This test depends on particular implementation of glibc (that does not
allocate memory on it's own for the given program (src/test_alloc.cpp),
so it may not work elsewhere.
"""



TEST_FILE = "test_alloc"
TEST_LINE = 8


def test_alloc(debugger):
    heap = []
    debugger.heap_manager.on_heap_change.subscribe(lambda new_heap:
                                                   heap.append(
                                                       copy.copy(new_heap)))

    def test_alloc_cb():
        assert len(heap[0]) == 1
        assert heap[0][0].size == 1024
        assert len(heap[1]) == 0


    setup_debugger(debugger, TEST_FILE, TEST_LINE, test_alloc_cb)