# Path: skoolkit/snapshot.py
# def get_snapshot(fname, page=None):
#     """
#     Read a snapshot file and produce a 65536-element list of byte values.
# 
#     :param fname: The snapshot filename.
#     :param page: The page number to map to addresses 49152-65535 (C000-FFFF).
#                  This is relevant only when reading a 128K snapshot file.
#     :return: A 65536-element list of byte values.
#     """
#     if not can_read(fname):
#         raise SnapshotError("{}: Unknown file type".format(fname))
#     data = read_bin_file(fname)
#     ext = fname[-4:].lower()
#     if ext == '.sna':
#         ram = _read_sna(data, page)
#     elif ext == '.z80':
#         ram = _read_z80(data, page)
#     elif ext == '.szx':
#         ram = _read_szx(data, page)
#     if len(ram) != 49152:
#         raise SnapshotError("RAM size is {0}".format(len(ram)))
#     mem = [0] * 16384
#     mem.extend(ram)
#     return mem
# 
# def make_z80_ram_block(data, page):
#     block = []
#     prev_b = None
#     count = 0
#     for b in data:
#         if b == prev_b or prev_b is None:
#             prev_b = b
#             if count < 255:
#                 count += 1
#                 continue
#         if count > 4 or (count > 1 and prev_b == 237):
#             block.extend((237, 237, count, prev_b))
#         elif prev_b == 237:
#             block.extend((237, b))
#             prev_b = None
#             count = 0
#             continue
#         else:
#             block.extend((prev_b,) * count)
#         prev_b = b
#         count = 1
#     if count > 4 or (count > 1 and prev_b == 237):
#         block.extend((237, 237, count, prev_b))
#     else:
#         block.extend((prev_b,) * count)
#     length = len(block)
#     return [length % 256, length // 256, page] + block
# 
# class SnapshotError(SkoolKitError):
#     """Raised when an error occurs while reading a snapshot file."""


from skoolkittest import SkoolKitTestCase
from skoolkit.snapshot import get_snapshot, make_z80_ram_block, SnapshotError
        page7 = [(n + 41) & 255 for n in range(16384)]
        config = [0, 0] # PC
        config.append(3) # Port 7ffd (page 3 mapped to 49152-65535)
        config.append(0) # TR-DOS ROM not paged
        sna = header + page5 + page2 + page3 + config + page0 + page1 + page4 + page6 + page7
        tmp_sna = self.write_bin_file(sna, suffix='.sna')
        snapshot = get_snapshot(tmp_sna, 5)
        ram = snapshot[16384:]
        self.assertEqual(len(ram), 49152)
        self.assertEqual(ram, page5 + page2 + page5)

class Z80Test(SnapshotTest):
    def _test_z80(self, exp_ram, version, compress, machine_id=0, modify=False, out_7ffd=0, pages={}, page=None):
        model, tmp_z80 = self.write_z80(exp_ram, version, compress, machine_id, modify, out_7ffd, pages)
        snapshot = get_snapshot(tmp_z80, page)
        self._check_ram(snapshot[16384:], exp_ram, model, out_7ffd, pages, page)

    def test_z80v1(self):
        exp_ram = [n & 255 for n in range(49152)]
        self._test_z80(exp_ram, 1, False)

    def test_z80v1_compressed(self):
        exp_ram = [0] * 261 + [1] * 10 + [2] * 4 + [237, 4] + [237] * 2
        exp_ram += [128] * (49152 - len(exp_ram))
        self._test_z80(exp_ram, 1, True)

    def test_z80v2_16k_compressed(self):
        exp_ram = [(n + 7) & 255 for n in range(49152)]
        self._test_z80(exp_ram, 2, True, modify=True)


# Path: skoolkit/snapshot.py
# def get_snapshot(fname, page=None):
#     """
#     Read a snapshot file and produce a 65536-element list of byte values.
# 
#     :param fname: The snapshot filename.
#     :param page: The page number to map to addresses 49152-65535 (C000-FFFF).
#                  This is relevant only when reading a 128K snapshot file.
#     :return: A 65536-element list of byte values.
#     """
#     if not can_read(fname):
#         raise SnapshotError("{}: Unknown file type".format(fname))
#     data = read_bin_file(fname)
#     ext = fname[-4:].lower()
#     if ext == '.sna':
#         ram = _read_sna(data, page)
#     elif ext == '.z80':
#         ram = _read_z80(data, page)
#     elif ext == '.szx':
#         ram = _read_szx(data, page)
#     if len(ram) != 49152:
#         raise SnapshotError("RAM size is {0}".format(len(ram)))
#     mem = [0] * 16384
#     mem.extend(ram)
#     return mem
# 
# def make_z80_ram_block(data, page):
#     block = []
#     prev_b = None
#     count = 0
#     for b in data:
#         if b == prev_b or prev_b is None:
#             prev_b = b
#             if count < 255:
#                 count += 1
#                 continue
#         if count > 4 or (count > 1 and prev_b == 237):
#             block.extend((237, 237, count, prev_b))
#         elif prev_b == 237:
#             block.extend((237, b))
#             prev_b = None
#             count = 0
#             continue
#         else:
#             block.extend((prev_b,) * count)
#         prev_b = b
#         count = 1
#     if count > 4 or (count > 1 and prev_b == 237):
#         block.extend((237, 237, count, prev_b))
#     else:
#         block.extend((prev_b,) * count)
#     length = len(block)
#     return [length % 256, length // 256, page] + block
# 
# class SnapshotError(SkoolKitError):
#     """Raised when an error occurs while reading a snapshot file."""


# Path: tests/test_snapshot.py
from skoolkittest import SkoolKitTestCase
from skoolkit.snapshot import get_snapshot, make_z80_ram_block, SnapshotError

        page7 = [(n + 41) & 255 for n in range(16384)]
        config = [0, 0] # PC
        config.append(3) # Port 7ffd (page 3 mapped to 49152-65535)
        config.append(0) # TR-DOS ROM not paged
        sna = header + page5 + page2 + page3 + config + page0 + page1 + page4 + page6 + page7
        tmp_sna = self.write_bin_file(sna, suffix='.sna')
        snapshot = get_snapshot(tmp_sna, 5)
        ram = snapshot[16384:]
        self.assertEqual(len(ram), 49152)
        self.assertEqual(ram, page5 + page2 + page5)

class Z80Test(SnapshotTest):
    def _test_z80(self, exp_ram, version, compress, machine_id=0, modify=False, out_7ffd=0, pages={}, page=None):
        model, tmp_z80 = self.write_z80(exp_ram, version, compress, machine_id, modify, out_7ffd, pages)
        snapshot = get_snapshot(tmp_z80, page)
        self._check_ram(snapshot[16384:], exp_ram, model, out_7ffd, pages, page)

    def test_z80v1(self):
        exp_ram = [n & 255 for n in range(49152)]
        self._test_z80(exp_ram, 1, False)

    def test_z80v1_compressed(self):
        exp_ram = [0] * 261 + [1] * 10 + [2] * 4 + [237, 4] + [237] * 2
        exp_ram += [128] * (49152 - len(exp_ram))
        self._test_z80(exp_ram, 1, True)

    def test_z80v2_16k_compressed(self):
        exp_ram = [(n + 7) & 255 for n in range(49152)]
        self._test_z80(exp_ram, 2, True, modify=True)


    def test_z80v2_48k(self):