<commit_before># pyOCD debugger
# Copyright (c) 2016 Arm Limited
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pyocd.core.memory_map import (
    MemoryType,
    check_range,
    MemoryRange,
    MemoryRegion,
    MemoryMap,
    FlashRegion,
    RomRegion,
    RamRegion,
    DeviceRegion,
    DefaultFlashWeights
    )
import pytest
import logging

@pytest.fixture(scope='function')
def flash():
    return FlashRegion(start=0, length=1*1024, blocksize=0x100, name='flash', is_boot_memory=True)

@pytest.fixture(scope='function')
def rom():
    return RomRegion(start=0x1c000000, length=16*1024, name='rom')

@pytest.fixture(scope='function')
def ram1():
    return RamRegion(start=0x20000000, length=1*1024, name='ram')

@pytest.fixture(scope='function')
def ram2():
    return RamRegion(start=0x20000400, length=1*1024, name='ram2', is_cacheable=False)

@pytest.fixture(scope='function')
def ram_alias():
    return RamRegion(start=0x30000400, length=1*1024, name='ram2_alias', alias='ram2', is_cacheable=False)

@pytest.fixture(scope='function')
def memmap(flash, rom, ram1, ram2):
    return MemoryMap(flash, rom, ram1, ram2)

@pytest.fixture(scope='function')
def memmap2(flash, rom, ram1, ram2, ram_alias):
    return MemoryMap(flash, rom, ram1, ram2, ram_alias)

class TestCheckRange:
    def test_1(self):
        assert check_range(0, end=0x1ff) == (0, 0x1ff)

    def test_2(self):
        assert check_range(0, length=0x200) == (0, 0x1ff)

    def test_3(self):
        with pytest.raises(AssertionError):
            check_range(None, end=100)

    def test_4(self):
        with pytest.raises(AssertionError):
            check_range(0x100, end=None)

    def test_5(self):
        with pytest.raises(AssertionError):
            check_range(0x100, length=None)

class TestRange:
    def test_empty_range_1(self):
        range = MemoryRange(start=0x1000, length=0)
        assert range.start == 0x1000
        assert range.end == 0xfff
        assert range.length == 0
    
    def test_empty_range_2(self):
        range = MemoryRange(start=0x1000, end=0xfff)
        assert range.start == 0x1000
        assert range.end == 0xfff
        assert range.length == 0
    
    def test_eq(self):
        assert MemoryRange(0, length=1000) == MemoryRange(0, length=1000)
    
    def test_lt(self):
        assert MemoryRange(0, length=1000) < MemoryRange(1000, length=1000)
    
    def test_gt(self):
        assert MemoryRange(1000, length=1000) > MemoryRange(0, length=1000)
    
    def test_sort(self, ram1, ram2, flash, rom):
        regionList = [ram2, rom, flash, ram1]
        sortedRegionList = sorted(regionList)
        assert sortedRegionList == [flash, rom, ram1, ram2]
    
    def test_inplace_sort(self, ram1, ram2, flash, rom):
        regionList = [ram2, rom, flash, ram1]
        regionList.sort()
        assert regionList == [flash, rom, ram1, ram2]

class TestHash:
    def test_range_neq(self):
        a = MemoryRange(0, 0x1000)
        b = MemoryRange(10, 20)
        assert hash(a) != hash(b)

    def test_range_eq(self):
        a = MemoryRange(0, 0x1000)
        b = MemoryRange(0, 0x1000)
        assert hash(a) == hash(b)

    def test_range_w_region_neq(self, ram1, rom):
        a = MemoryRange(0, 0x1000, region=ram1)
        b = MemoryRange(0x5000, length=200, region=rom)
        assert hash(a) != hash(b)
        
        a = MemoryRange(0, 0x1000, region=ram1)
        b = MemoryRange(0, 0x1000, region=rom)
        assert hash(a) != hash(b)

    def test_range_eq(self, ram1, rom):
        a = MemoryRange(0, 0x1000, region=ram1)
        b = MemoryRange(0, 0x1000, region=ram1)
        assert hash(a) == hash(b)

# MemoryRegion test cases.
class TestMemoryRegion:
    def test_empty_region_1(self):
        with pytest.raises(AssertionError):
            rgn = MemoryRegion(start=0x1000, length=0)
    
    def test_empty_region_2(self):
        with pytest.raises(AssertionError):
            rgn = MemoryRegion(start=0x1000, end=0xfff)
    
    def test_default_name(self):
        rgn = RamRegion(start=0x1000, end=0x1fff)
        assert rgn.name == 'ram'

        rgn = RomRegion(start=0x1000, end=0x1fff)
        assert rgn.name == 'rom'

        rgn = FlashRegion(start=0x1000, end=0x1fff)
        assert rgn.name == 'flash'

        rgn = DeviceRegion(start=0x1000, end=0x1fff)
        assert rgn.name == 'device'
    
    def test_flash_attrs(self, flash):
        assert flash.type == MemoryType.FLASH
        assert flash.start == 0
        assert flash.end == 0x3ff
        assert flash.length == 0x400
        assert flash.blocksize == 0x100
        assert flash.page_size == 0x100
        assert flash.phrase_size == 0x100<commit_msg>Fixed memory map unit test.<commit_after># pyOCD debugger
# Copyright (c) 2016 Arm Limited
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pyocd.core.memory_map import (
    MemoryType,
    check_range,
    MemoryRange,
    MemoryRegion,
    MemoryMap,
    FlashRegion,
    RomRegion,
    RamRegion,
    DeviceRegion,
    DefaultFlashWeights
    )
import pytest
import logging

@pytest.fixture(scope='function')
def flash():
    return FlashRegion(start=0, length=1*1024, blocksize=0x100, name='flash', is_boot_memory=True)

@pytest.fixture(scope='function')
def rom():
    return RomRegion(start=0x1c000000, length=16*1024, name='rom')

@pytest.fixture(scope='function')
def ram1():
    return RamRegion(start=0x20000000, length=1*1024, name='ram')

@pytest.fixture(scope='function')
def ram2():
    return RamRegion(start=0x20000400, length=1*1024, name='ram2', is_cacheable=False)

@pytest.fixture(scope='function')
def ram_alias():
    return RamRegion(start=0x30000400, length=1*1024, name='ram2_alias', alias='ram2', is_cacheable=False)

@pytest.fixture(scope='function')
def memmap(flash, rom, ram1, ram2):
    return MemoryMap(flash, rom, ram1, ram2)

@pytest.fixture(scope='function')
def memmap2(flash, rom, ram1, ram2, ram_alias):
    return MemoryMap(flash, rom, ram1, ram2, ram_alias)

class TestCheckRange:
    def test_1(self):
        assert check_range(0, end=0x1ff) == (0, 0x1ff)

    def test_2(self):
        assert check_range(0, length=0x200) == (0, 0x1ff)

    def test_3(self):
        with pytest.raises(AssertionError):
            check_range(None, end=100)

    def test_4(self):
        with pytest.raises(AssertionError):
            check_range(0x100, end=None)

    def test_5(self):
        with pytest.raises(AssertionError):
            check_range(0x100, length=None)

class TestRange:
    def test_empty_range_1(self):
        range = MemoryRange(start=0x1000, length=0)
        assert range.start == 0x1000
        assert range.end == 0xfff
        assert range.length == 0
    
    def test_empty_range_2(self):
        range = MemoryRange(start=0x1000, end=0xfff)
        assert range.start == 0x1000
        assert range.end == 0xfff
        assert range.length == 0
    
    def test_eq(self):
        assert MemoryRange(0, length=1000) == MemoryRange(0, length=1000)
    
    def test_lt(self):
        assert MemoryRange(0, length=1000) < MemoryRange(1000, length=1000)
    
    def test_gt(self):
        assert MemoryRange(1000, length=1000) > MemoryRange(0, length=1000)
    
    def test_sort(self, ram1, ram2, flash, rom):
        regionList = [ram2, rom, flash, ram1]
        sortedRegionList = sorted(regionList)
        assert sortedRegionList == [flash, rom, ram1, ram2]
    
    def test_inplace_sort(self, ram1, ram2, flash, rom):
        regionList = [ram2, rom, flash, ram1]
        regionList.sort()
        assert regionList == [flash, rom, ram1, ram2]

class TestHash:
    def test_range_neq(self):
        a = MemoryRange(0, 0x1000)
        b = MemoryRange(10, 20)
        assert hash(a) != hash(b)

    def test_range_eq(self):
        a = MemoryRange(0, 0x1000)
        b = MemoryRange(0, 0x1000)
        assert hash(a) == hash(b)

    def test_range_w_region_neq(self, ram1, rom):
        a = MemoryRange(0, 0x1000, region=ram1)
        b = MemoryRange(0x5000, length=200, region=rom)
        assert hash(a) != hash(b)
        
        a = MemoryRange(0, 0x1000, region=ram1)
        b = MemoryRange(0, 0x1000, region=rom)
        assert hash(a) != hash(b)

    def test_range_eq(self, ram1, rom):
        a = MemoryRange(0, 0x1000, region=ram1)
        b = MemoryRange(0, 0x1000, region=ram1)
        assert hash(a) == hash(b)

# MemoryRegion test cases.
class TestMemoryRegion:
    def test_empty_region_1(self):
        with pytest.raises(AssertionError):
            rgn = MemoryRegion(start=0x1000, length=0)
    
    def test_empty_region_2(self):
        with pytest.raises(AssertionError):
            rgn = MemoryRegion(start=0x1000, end=0xfff)
    
    def test_default_name(self):
        rgn = RamRegion(start=0x1000, end=0x1fff)
        assert rgn.name == 'ram'

        rgn = RomRegion(start=0x1000, end=0x1fff)
        assert rgn.name == 'rom'

        rgn = FlashRegion(start=0x1000, end=0x1fff, blocksize=256)
        assert rgn.name == 'flash'

        rgn = DeviceRegion(start=0x1000, end=0x1fff)
        assert rgn.name == 'device'
    
    def test_block_sector_size(self):
        rgn = FlashRegion(start=0x1000, end=0x1fff, blocksize=256)
        assert rgn.blocksize == 256
        assert rgn.sector_size == 256

        rgn = FlashRegion(start=0x1000, end=0x1fff, sector_size=256)
        assert rgn.blocksize == 256
        assert rgn.sector_size == 256
    
    def test_flash_attrs(self, flash):
        assert flash.type == MemoryType.FLASH
        assert flash.start == 0
        assert flash.end == 0x3ff
        assert flash.length == 0x400
        assert flash.blocksize == 0x100
        assert flash.page_size == 0x100
        assert flash.phrase_size == 0x100
{'fraction_non_alphanumeric': 0.11233053582956747, 'fraction_numerical': 0.06280549663377294, 'mean_word_length': 3.1279025504377618, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3194338', 'n_tokens_mistral': 3986, 'n_tokens_neox': 3627, 'n_words': 876}