diff --git a/README.md b/README.md index fd8293229..28937efbd 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,13 @@ ## Project chosen -Name: +Name: algorithms -URL: +URL: (https://github.com/keon/algorithms) -Number of lines of code and the tool used to count it: +Number of lines of code and the tool used to count it: (TODO), counted using lizard -Programming language: +Programming language: Python ## Coverage measurement @@ -56,6 +56,44 @@ Programming language: + + +#### + +An enhanced existing test + +Old coverage:
+![old coverage result (24%)](image-2.png) + +Diff (LHS = new code, RHS = old code):
+![LHS: new code, RHS: old code](image.png) + +New coverage:
+![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) + +#### + +An new test. (before, `insertion_sort` was not present in any test) + +Diff (LHS: new code, RHS: old code):
+(New test)
+![LHS: new code, RHS: old code](image-5.png)
+(Changes in imports)
+![LHS: new code, RHS: old code](image-6.png) + +Old coverage:
+![Old coverage result (4%)](image-3.png) + +New coverage:
+![alt text](image-4.png) + + + ### Overall diff --git a/algorithms/sort/insertion_sort.py b/algorithms/sort/insertion_sort.py index 06c86228d..79e2bbd20 100644 --- a/algorithms/sort/insertion_sort.py +++ b/algorithms/sort/insertion_sort.py @@ -1,3 +1,10 @@ +branch_coverage = { + "simulation": False, + "for": False, + "while": False, + "simulation-nested": False, +} + def insertion_sort(arr, simulation=False): """ Insertion Sort Complexity: O(n^2) @@ -5,13 +12,16 @@ def insertion_sort(arr, simulation=False): 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 @@ -19,7 +29,14 @@ def insertion_sort(arr, simulation=False): 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'}") + diff --git a/algorithms/unix/path/simplify_path.py b/algorithms/unix/path/simplify_path.py index 8880fc0c6..7cf773cb0 100644 --- a/algorithms/unix/path/simplify_path.py +++ b/algorithms/unix/path/simplify_path.py @@ -15,6 +15,12 @@ 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) @@ -22,8 +28,17 @@ def simplify_path_v1(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'}") \ No newline at end of file diff --git a/image-1.png b/image-1.png new file mode 100644 index 000000000..7071337a2 Binary files /dev/null and b/image-1.png differ diff --git a/image-2.png b/image-2.png new file mode 100644 index 000000000..77702129e Binary files /dev/null and b/image-2.png differ diff --git a/image-3.png b/image-3.png new file mode 100644 index 000000000..61fc9d074 Binary files /dev/null and b/image-3.png differ diff --git a/image-4.png b/image-4.png new file mode 100644 index 000000000..9b5c60694 Binary files /dev/null and b/image-4.png differ diff --git a/image-5.png b/image-5.png new file mode 100644 index 000000000..e65d38265 Binary files /dev/null and b/image-5.png differ diff --git a/image-6.png b/image-6.png new file mode 100644 index 000000000..78045907d Binary files /dev/null and b/image-6.png differ diff --git a/image.png b/image.png new file mode 100644 index 000000000..2a9ba91c1 Binary files /dev/null and b/image.png differ diff --git a/tests/test_unix.py b/tests/test_unix.py index 3cafba98f..e53881ef1 100644 --- a/tests/test_unix.py +++ b/tests/test_unix.py @@ -7,6 +7,9 @@ import os import unittest +from algorithms.unix import ( + print_coverage +) class TestUnixPath(unittest.TestCase): def test_join_with_slash(self): @@ -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() +