<commit_before>"""
 mbed CMSIS-DAP debugger
 Copyright (c) 2015 ARM Limited

 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 __future__ import print_function

import argparse, os, sys
from time import sleep, time
from random import randrange
import math
import struct
import traceback
import argparse

parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)

import pyOCD
from pyOCD.board import MbedBoard
from pyOCD.utility.conversion import float32beToU32be
from pyOCD.flash.flash import Flash
from pyOCD.flash.flash_builder import FlashBuilder
from test_util import Test, TestResult

addr = 0
size = 0

board = None

import logging

class FlashTestResult(TestResult):
    def __init__(self):
        super(FlashTestResult, self).__init__(None, None, None)
        self.chip_erase_rate_erased = None
        self.page_erase_rate_same = None
        self.page_erase_rate = None
        self.analyze = None
        self.analyze_rate = None
        self.chip_erase_rate = None

class FlashTest(Test):
    def __init__(self):
        super(FlashTest, self).__init__("Flash Test", flash_test)

    def print_perf_info(self, result_list, output_file=None):
        result_list = filter(lambda x: isinstance(x, FlashTestResult), result_list)

        print("\r\n\r\n------ Analyzer Performance ------", file=output_file)
        perf_format_str = "{:<10}{:<12}{:<18}{:<18}"
        print(perf_format_str.format("Target", "Analyzer", "Rate", "Time"),
              file=output_file)
        print("", file=output_file)
        for result in result_list:
            if result.passed:
                analyze_rate = "%f KB/s" % (result.analyze_rate / float(1000))
                analyze_time = "%s s" % result.analyze_time
            else:
                analyze_rate = "Fail"
                analyze_time = "Fail"
            print(perf_format_str.format(result.board.target_type,
                                         result.analyze, analyze_rate,
                                         analyze_time),
                  file=output_file)
        print("", file=output_file)

        print("\r\n\r\n------ Test Rate ------", file=output_file)
        rate_format_str = "{:<10}{:<20}{:<20}{:<20}"
        print(rate_format_str.format("Target", "Chip Erase", "Page Erase",
                                     "Page Erase (Same data)"),
              file=output_file)
        print("", file=output_file)
        for result in result_list:
            if result.passed:
                chip_erase_rate = "%f KB/s" % (result.chip_erase_rate / float(1000))
                page_erase_rate = "%f KB/s" % (result.page_erase_rate / float(1000))
                page_erase_rate_same = "%f KB/s" % (result.page_erase_rate_same / float(1000))
            else:
                chip_erase_rate = "Fail"
                page_erase_rate = "Fail"
                page_erase_rate_same = "Fail"
            print(rate_format_str.format(result.board.target_type,
                                         chip_erase_rate, page_erase_rate,
                                         page_erase_rate_same),
                  file=output_file)
        print("", file=output_file)

    def run(self, board):
        try:
            result = self.test_function(board.getUniqueID())
        except Exception as e:
            result = FlashTestResult()
            result.passed = False
            print("Exception %s when testing board %s" % (e, board.getUniqueID()))
            traceback.print_exc(file=sys.stdout)
        result.board = board
        result.test = self
        return result


def same(d1, d2):
    if len(d1) != len(d2):
        return False
    for i in range(len(d1)):
        if d1[i] != d2[i]:
            return False
    return True


def flash_test(board_id):
    with MbedBoard.chooseBoard(board_id=board_id, frequency=1000000) as board:
        target_type = board.getTargetType()

        test_clock = 10000000
        if target_type == "nrf51":
            # Override clock since 10MHz is too fast
            test_clock = 1000000

        memory_map = board.target.getMemoryMap()
        ram_regions = [region for region in memory_map if region.type == 'ram']
        ram_region = ram_regions[0]
        rom_region = memory_map.getBootMemory()

        ram_start = ram_region.start
        ram_size = ram_region.length
        rom_start = rom_region.start
        rom_size = rom_region.length

        target = board.target
        link = board.link
        flash = board.flash

        link.set_clock(test_clock)
        link.set_deferred_transfer(True)

        test_pass_count = 0
        test_count = 0
        result = FlashTestResult()

        def print_progress(progress):
            assert progress >= 0.0
            assert progress <= 1.0
            assert (progress == 0 and print_progress.prev_progress == 1.0) or (progress >= print_progress.prev_progress)

            # Reset state on 0.0
            if progress == 0.0:<commit_msg>Extend flash test to cover consecutive regions<commit_after>"""
 mbed CMSIS-DAP debugger
 Copyright (c) 2015 ARM Limited

 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 __future__ import print_function

import argparse, os, sys
from time import sleep, time
from random import randrange
import math
import struct
import traceback
import argparse

parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)

import pyOCD
from pyOCD.board import MbedBoard
from pyOCD.utility.conversion import float32beToU32be
from pyOCD.flash.flash import Flash
from pyOCD.flash.flash_builder import FlashBuilder
from test_util import Test, TestResult

addr = 0
size = 0

board = None

import logging

class FlashTestResult(TestResult):
    def __init__(self):
        super(FlashTestResult, self).__init__(None, None, None)
        self.chip_erase_rate_erased = None
        self.page_erase_rate_same = None
        self.page_erase_rate = None
        self.analyze = None
        self.analyze_rate = None
        self.chip_erase_rate = None

class FlashTest(Test):
    def __init__(self):
        super(FlashTest, self).__init__("Flash Test", flash_test)

    def print_perf_info(self, result_list, output_file=None):
        result_list = filter(lambda x: isinstance(x, FlashTestResult), result_list)

        print("\r\n\r\n------ Analyzer Performance ------", file=output_file)
        perf_format_str = "{:<10}{:<12}{:<18}{:<18}"
        print(perf_format_str.format("Target", "Analyzer", "Rate", "Time"),
              file=output_file)
        print("", file=output_file)
        for result in result_list:
            if result.passed:
                analyze_rate = "%f KB/s" % (result.analyze_rate / float(1000))
                analyze_time = "%s s" % result.analyze_time
            else:
                analyze_rate = "Fail"
                analyze_time = "Fail"
            print(perf_format_str.format(result.board.target_type,
                                         result.analyze, analyze_rate,
                                         analyze_time),
                  file=output_file)
        print("", file=output_file)

        print("\r\n\r\n------ Test Rate ------", file=output_file)
        rate_format_str = "{:<10}{:<20}{:<20}{:<20}"
        print(rate_format_str.format("Target", "Chip Erase", "Page Erase",
                                     "Page Erase (Same data)"),
              file=output_file)
        print("", file=output_file)
        for result in result_list:
            if result.passed:
                chip_erase_rate = "%f KB/s" % (result.chip_erase_rate / float(1000))
                page_erase_rate = "%f KB/s" % (result.page_erase_rate / float(1000))
                page_erase_rate_same = "%f KB/s" % (result.page_erase_rate_same / float(1000))
            else:
                chip_erase_rate = "Fail"
                page_erase_rate = "Fail"
                page_erase_rate_same = "Fail"
            print(rate_format_str.format(result.board.target_type,
                                         chip_erase_rate, page_erase_rate,
                                         page_erase_rate_same),
                  file=output_file)
        print("", file=output_file)

    def run(self, board):
        try:
            result = self.test_function(board.getUniqueID())
        except Exception as e:
            result = FlashTestResult()
            result.passed = False
            print("Exception %s when testing board %s" % (e, board.getUniqueID()))
            traceback.print_exc(file=sys.stdout)
        result.board = board
        result.test = self
        return result


def same(d1, d2):
    if len(d1) != len(d2):
        return False
    for i in range(len(d1)):
        if d1[i] != d2[i]:
            return False
    return True


def flash_test(board_id):
    with MbedBoard.chooseBoard(board_id=board_id, frequency=1000000) as board:
        target_type = board.getTargetType()

        test_clock = 10000000
        if target_type == "nrf51":
            # Override clock since 10MHz is too fast
            test_clock = 1000000

        memory_map = board.target.getMemoryMap()
        ram_regions = [region for region in memory_map if region.type == 'ram']
        ram_region = ram_regions[0]

        ram_start = ram_region.start
        ram_size = ram_region.length

        # Grab boot flash and any regions coming immediately after
        rom_region = memory_map.getBootMemory()
        rom_start = rom_region.start
        rom_size = rom_region.length
        for region in memory_map:
            if region.isFlash and (region.start == rom_start + rom_size):
                rom_size += region.length

        target = board.target
        link = board.link
        flash = board.flash

        link.set_clock(test_clock)
        link.set_deferred_transfer(True)

        test_pass_count = 0
        test_count = 0
        result = FlashTestResult()

        def print_progress(progress):
            assert progress >= 0.0
            assert progress <= 1.0
            assert (progress == 0 and print_progress.prev_progress == 1.0) or (progress >= print_progress.prev_progress)

            # Reset state on 0.0
            if progress == 0.0:
{'fraction_non_alphanumeric': 0.09481915933528837, 'fraction_numerical': 0.016706656002843687, 'mean_word_length': 2.21175799086758, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7276486', 'n_tokens_mistral': 3362, 'n_tokens_neox': 3182, 'n_words': 846}