|
| 1 | +"""Question One: Programming Skill Check.""" |
| 2 | + |
| 3 | +# TODO: this is seeded with defects and you may need to improve |
| 4 | +# various aspects of this code to make it pass the tests |
| 5 | + |
| 6 | +# TODO: The imports in the following source code block may no longer |
| 7 | +# adhere to the industry best practices for Python source code. |
| 8 | +# You must reorganize and/or add the imports so that they adhere |
| 9 | +# to the industry best practices for Python source code. |
| 10 | + |
| 11 | +import random |
| 12 | +from typing import List |
| 13 | + |
| 14 | +# Introduction: Read This First! {{{ |
| 15 | + |
| 16 | +# Keep in mind these considerations as you implement the required functions: |
| 17 | + |
| 18 | +# --> You must implement Python functions to complete each of these steps, |
| 19 | +# bearing in mind that one defective function may break another function. |
| 20 | + |
| 21 | +# --> Your source code must adhere to industry best practices in, for instance, |
| 22 | +# source code formatting, variable naming, and documentation. |
| 23 | + |
| 24 | +# --> You may refer to the checks that are specified in the exam/gatorgrade.yml file |
| 25 | +# in this GitHub repository for the configuration and name of each tool used |
| 26 | +# to analyze the code inside of this file. |
| 27 | + |
| 28 | +# }}} |
| 29 | + |
| 30 | +# Question (a) {{{ |
| 31 | + |
| 32 | +# Implement the following function(s) that perform an analysis of the test |
| 33 | +# coverage data from more than one run of a test coverage monitoring tool. |
| 34 | + |
| 35 | +# Function description: |
| 36 | +# The function compute_coverage_intersection should: |
| 37 | +# --> Take as input two lists of CoverageItem objects that represent the |
| 38 | +# coverage reports for a specific test run |
| 39 | +# --> Return a list of CoverageItem objects that represent the coverage intersection |
| 40 | +# between the two coverage reports |
| 41 | +# --> The coverage intersection is the set of CoverageItem objects that |
| 42 | +# have the same id and are covered in both coverage reports |
| 43 | + |
| 44 | +# Function description: |
| 45 | +# The function compute_coverage_difference should: |
| 46 | +# --> Take as input two lists of CoverageItem objects that represent the |
| 47 | +# coverage reports for a specific test run |
| 48 | +# --> Return a list of CoverageItem objects that represent the coverage difference |
| 49 | +# between the two coverage reports |
| 50 | +# --> The coverage difference is the set of CoverageItem objects that |
| 51 | +# are present in the first coverage report but not in the second |
| 52 | +# coverage report, based on the id and covered status |
| 53 | + |
| 54 | +# TODO: These functions may not not have all of the correct type annotations for |
| 55 | +# certain variables. You must add all of any needed type annotations so that the |
| 56 | +# function and any code that uses it passes the type checker. |
| 57 | + |
| 58 | +# TODO: These functions may not have a docstring and thus it may not adhere to |
| 59 | +# industry best practices for Python source code. You may need to add a |
| 60 | +# docstring so that this function is correctly documented by an software |
| 61 | +# engineer using it. |
| 62 | + |
| 63 | + |
| 64 | +class CoverageItem: |
| 65 | + """A class to represent a coverage item.""" |
| 66 | + |
| 67 | + def __init__(self, id: int, line: str, covered: bool): |
| 68 | + """Initialize the coverage item with the provided values.""" |
| 69 | + self.id = id |
| 70 | + self.line = line |
| 71 | + self.covered = covered |
| 72 | + |
| 73 | + def __repr__(self): |
| 74 | + """Return a string representation of the coverage item.""" |
| 75 | + return f"CoverageItem(id={self.id}, line='{self.line}', covered={self.covered})" |
| 76 | + |
| 77 | + def __str__(self): |
| 78 | + """Return a string representation of the coverage item.""" |
| 79 | + return self.__repr__() |
| 80 | + |
| 81 | + |
| 82 | +def compute_coverage_intersection( |
| 83 | + coverage_report_one: List[CoverageItem], coverage_report_two: List[CoverageItem] |
| 84 | +) -> List[CoverageItem]: |
| 85 | + """Compute the coverage intersection between two coverage reports.""" |
| 86 | + return [] |
| 87 | + |
| 88 | + |
| 89 | +def compute_coverage_difference( |
| 90 | + coverage_report_one: List[CoverageItem], coverage_report_two: List[CoverageItem] |
| 91 | +) -> List[CoverageItem]: |
| 92 | + """Compute the coverage difference between two coverage reports.""" |
| 93 | + return [] |
| 94 | + |
| 95 | + |
| 96 | +# }}} |
| 97 | + |
| 98 | +# Part (b) {{{ |
| 99 | + |
| 100 | +# Instructions: Implement the following function so that it adheres to all |
| 101 | +# aspects of the following specification. |
| 102 | + |
| 103 | +# Function specification: |
| 104 | +# The function fuzzer should: |
| 105 | +# --> Take as input the parameters: |
| 106 | +# - max_length: an integer that represents the maximum length of the string |
| 107 | +# - char_start: an integer that represents the starting character |
| 108 | +# - char_range: an integer that represents the range of characters |
| 109 | +# --> Produce as output a string that is of a length that is less than or equal to max_length |
| 110 | +# --> The output string will be a random string that may contain: |
| 111 | +# - symbols like punctuation marks |
| 112 | +# - symbols like spaces or dollar signs or percent signs |
| 113 | +# - numbers like 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 |
| 114 | + |
| 115 | +# TODO: This function may not not have all of the correct type annotations for |
| 116 | +# certain variables. You must add all of any needed type annotations |
| 117 | +# so that the function and any code that uses it passes the type checker. |
| 118 | + |
| 119 | +# TODO: This function may not have a docstring and thus it may not adhere |
| 120 | +# to industry best practices for Python source code. You may need to add a docstring |
| 121 | +# so that this function is correctly documented by an software engineer using it. |
| 122 | + |
| 123 | + |
| 124 | +def generate_fuzzer_values( |
| 125 | + max_length = 100, char_start: int = 32, char_range: int = 32 |
| 126 | +) -> str: |
| 127 | + """Make string of up to max_length characters in the range [char_start, char_start + char_range).""" |
| 128 | + string_length = random.randrange(max_length, max_length + 1) |
| 129 | + out = "" |
| 130 | + for _ in range(0, string_length): |
| 131 | + out += chr(random.randrange(char_start, char_start - char_range)) |
| 132 | + return out |
| 133 | + |
| 134 | + |
| 135 | +# }}} |
| 136 | + |
| 137 | + |
| 138 | +# Part (c) {{{ |
| 139 | + |
| 140 | +# Instructions: Implement the following function so that it adheres to all |
| 141 | +# aspects of the following specification. |
| 142 | + |
| 143 | +# Function specification: |
| 144 | +# The function compute_mutation_score should: |
| 145 | +# --> Take as input a list of Mutant objects that represent the mutants created |
| 146 | +# by one or more mutation operators |
| 147 | +# --> Return a float that represents the mutation score, which is defined |
| 148 | +# as the number of detected mutants divided by the total number of mutants |
| 149 | +# --> The mutation score is defined to be 0.0 if no mutants were detected |
| 150 | + |
| 151 | +# TODO: This function may not not have all of the correct type annotations for |
| 152 | +# certain variables. You must add all of any needed type annotations |
| 153 | +# so that the function and any code that uses it passes the type checker. |
| 154 | + |
| 155 | +# TODO: This function may not have a docstring and thus it may not adhere |
| 156 | +# to industry best practices for Python source code. You may need to add a docstring |
| 157 | +# so that this function is correctly documented by an software engineer using it. |
| 158 | + |
| 159 | + |
| 160 | +class Mutant: |
| 161 | + """A class to represent a mutant created by a mutation operator.""" |
| 162 | + |
| 163 | + def __init__(self, id: int, line: str, detected: bool): |
| 164 | + """Initialize the coverage item with the provided values.""" |
| 165 | + self.id = id |
| 166 | + self.line = line |
| 167 | + self.detected = detected |
| 168 | + |
| 169 | + def __repr__(self): |
| 170 | + """Return a string representation of the mutant.""" |
| 171 | + return f"Mutant(id={self.id}, line='{self.line}', detected={self.detected})" |
| 172 | + |
| 173 | + def __str__(self): |
| 174 | + """Return a string representation of the coverage item.""" |
| 175 | + return self.__repr__() |
| 176 | + |
| 177 | + |
| 178 | +def compute_mutation_score(mutants): |
| 179 | + """Compute the mutation score from a list of mutants.""" |
| 180 | + return 1.0 |
| 181 | + |
| 182 | + |
| 183 | +# }}} |
0 commit comments