Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

## Project chosen

Name: <TODO>
Name: algorithms

URL: <TODO>
URL: (https://github.com/keon/algorithms)

Number of lines of code and the tool used to count it: <TODO>
Number of lines of code and the tool used to count it: (TODO), counted using lizard

Programming language: <TODO>
Programming language: Python

## Coverage measurement

Expand Down Expand Up @@ -56,6 +56,44 @@ Programming language: <TODO>

<Provide the same kind of information provided for Test 1>

<Ayman Errahmouni>

#### <Test "test_simplify_path">

An enhanced existing test

Old coverage:<br>
![old coverage result (24%)](image-2.png)

Diff (LHS = new code, RHS = old code):<br>
![LHS: new code, RHS: old code](image.png)

New coverage:<br>
![new coverage result (100%)](image-1.png)

The coverage was improved because certain cases that could happen in file paths (e.g. the "." directory, empty path) were not tested for.
By added additional tests that use such cases, the coverage improved.

The test was also faulty on windows (i guess linux was assumed), so i added support for that in the test. (It now passes on Windows 10 too)

#### <Test "test_actual_insertion_sort">

An new test. (before, `insertion_sort` was not present in any test)

Diff (LHS: new code, RHS: old code):<br>
(New test)<br>
![LHS: new code, RHS: old code](image-5.png)<br>
(Changes in imports)<br>
![LHS: new code, RHS: old code](image-6.png)

Old coverage:<br>
![Old coverage result (4%)](image-3.png)

New coverage:<br>
![alt text](image-4.png)

<State the coverage improvement with a number and elaborate on why the coverage is improved>

### Overall

<Provide a screenshot of the old coverage results by running an existing tool (the same as you already showed above)>
Expand Down
17 changes: 17 additions & 0 deletions algorithms/sort/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
branch_coverage = {
"simulation": False,
"for": False,
"while": False,
"simulation-nested": False,
}

def insertion_sort(arr, simulation=False):
""" Insertion Sort
Complexity: O(n^2)
"""

iteration = 0
if simulation:
branch_coverage["simulation"] = True
print("iteration",iteration,":",*arr)

for i in range(len(arr)):
branch_coverage["for"] = True
cursor = arr[i]
pos = i

while pos > 0 and arr[pos - 1] > cursor:
branch_coverage["while"] = True
# Swap the number down the list
arr[pos] = arr[pos - 1]
pos = pos - 1
# Break and do the final swap
arr[pos] = cursor

if simulation:
branch_coverage["simulation-nested"] = True
iteration = iteration + 1
print("iteration",iteration,":",*arr)

return arr

def print_coverage():
print("branch coverage for `insertion_sort`:")
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")

15 changes: 15 additions & 0 deletions algorithms/unix/path/simplify_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,30 @@
Reference: https://leetcode.com/problems/simplify-path/description/
"""

branch_coverage = {
"for": False,
"if": False,
"elif": False,
}

import os
def simplify_path_v1(path):
return os.path.abspath(path)

def simplify_path_v2(path):
stack, tokens = [], path.split("/")
for token in tokens:
branch_coverage["for"] = True
if token == ".." and stack:
branch_coverage["if"] = True
stack.pop()
elif token != ".." and token != "." and token:
branch_coverage["elif"] = True
stack.append(token)

return "/" + "/".join(stack)

def print_coverage():
print("branch coverage for `simplify_path_v2`:")
for branch, hit in branch_coverage.items():
print(f"{branch} was {'hit' if hit else 'not hit'}")
Binary file added image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 24 additions & 2 deletions tests/test_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import os
import unittest

from algorithms.unix import (
print_coverage
)

class TestUnixPath(unittest.TestCase):
def test_join_with_slash(self):
Expand Down Expand Up @@ -42,7 +45,26 @@ def test_split(self):
self.assertEqual("test.py", expect_result[1])

def test_simplify_path(self):
self.assertEqual("/", simplify_path_v1("/../"))
self.assertEqual("/home/foo", simplify_path_v1("/home//foo/"))
root = None
pathsep = None
drive = None
if os.name == 'nt':
root = "" # Assumed to be ran on the C drive
pathsep = "\\"
drive = "C:\\"
elif os.name == 'posix':
root = "/"
pathsep = "/"
drive = ""

self.assertEqual(drive + root, simplify_path_v1("/../"))
self.assertEqual(drive + root + pathsep.join(["home", "foo"]), simplify_path_v1("/home//foo/"))

self.assertEqual("/", simplify_path_v2("."))
self.assertEqual("/", simplify_path_v2("/../"))
self.assertEqual("/home/foo", simplify_path_v2("/home//foo/"))
self.assertEqual("/", simplify_path_v2(""))
self.assertEqual("/", simplify_path_v2("/home/../"))

print_coverage()