Size: 5550 bytes.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
# cs/q/simplify_path/simplify_path_test_chatgpt.py
import unittest
from simplify_path import SimplifyPath


class TestSimplifyPath(unittest.TestCase):
    # Basic functionality tests
    def test_root_directory(self):
        self.assertEqual(SimplifyPath("/"), "/")

    def test_trailing_slash(self):
        self.assertEqual(SimplifyPath("/home/"), "/home")

    def test_multiple_trailing_slashes(self):
        self.assertEqual(SimplifyPath("/abc/def///"), "/abc/def")

    def test_multiple_consecutive_slashes(self):
        self.assertEqual(SimplifyPath("/home//foo/bar///baz"), "/home/foo/bar/baz")

    def test_only_multiple_slashes(self):
        self.assertEqual(SimplifyPath("//////"), "/")

    def test_single_current_dir_root(self):
        self.assertEqual(SimplifyPath("/./"), "/")

    def test_single_current_dir_in_path(self):
        self.assertEqual(SimplifyPath("/foo/./bar"), "/foo/bar")

    def test_multiple_current_dirs(self):
        self.assertEqual(SimplifyPath("/././foo/."), "/foo")

    # Parent directory (..) handling
    def test_single_parent_dir(self):
        self.assertEqual(SimplifyPath("/foo/bar/.."), "/foo")

    def test_single_parent_dir_with_trailing(self):
        self.assertEqual(SimplifyPath("/foo/bar/../"), "/foo")

    def test_multiple_parent_dirs(self):
        # Going up two levels from /foo/bar/baz
        self.assertEqual(SimplifyPath("/foo/bar/baz/../.."), "/foo")

    def test_parent_dir_at_root(self):
        self.assertEqual(SimplifyPath("/../"), "/")
        self.assertEqual(SimplifyPath("/.."), "/")

    def test_multiple_parent_dir_at_root(self):
        self.assertEqual(SimplifyPath("/../../.."), "/")

    def test_parent_then_directory(self):
        # Going up beyond root then into folder
        self.assertEqual(SimplifyPath("/../../folder"), "/folder")

    # Mixed current (.) and parent (..) references
    def test_mixed_current_and_parent(self):
        self.assertEqual(SimplifyPath("/a/./b/../c/"), "/a/c")

    def test_mixed_numbers_and_dirs(self):
        self.assertEqual(SimplifyPath("/1/2/../3/./4/../5"), "/1/3/5")

    def test_double_slash_and_dots_combined(self):
        self.assertEqual(SimplifyPath("/a//b/../..//c/."), "/c")

    def test_multiple_slashes_with_dot_name(self):
        # "///...//." -> multiple slashes compress, "..." is a directory, last "." removed
        self.assertEqual(SimplifyPath("///...//."), "/...")

    # Directory names with periods (not just "." or "..")
    def test_directory_named_triple_dots(self):
        # "..." is a valid directory name
        self.assertEqual(SimplifyPath("/.../"), "/...")

    def test_directory_named_four_dots(self):
        self.assertEqual(SimplifyPath("/...."), "/....")

    def test_directory_name_starts_with_dot(self):
        # .hidden is a valid name (not a current-dir marker)
        self.assertEqual(SimplifyPath("/.hidden"), "/.hidden")

    def test_hidden_directory_navigation(self):
        # .hidden treated as normal dir, then .. goes up to root
        self.assertEqual(SimplifyPath("/.hidden/../visible"), "/visible")

    def test_name_starts_with_double_dot(self):
        # "..hidden" should be treated as a regular name, not as parent reference
        self.assertEqual(SimplifyPath("/..hidden"), "/..hidden")

    def test_name_ends_with_double_dot(self):
        self.assertEqual(SimplifyPath("/hidden.."), "/hidden..")

    def test_remove_name_ending_with_double_dot(self):
        # "hidden.." is a dir name, ".." removes it
        self.assertEqual(SimplifyPath("/hidden../.."), "/")

    def test_name_with_double_dot_inside(self):
        # "..bar" is a normal name starting with ".."
        self.assertEqual(SimplifyPath("/foo/..bar/.."), "/foo")

    def test_name_with_dot_inside(self):
        # ".bar" is a normal name (starts with '.', more chars after)
        self.assertEqual(SimplifyPath("/foo/.bar/.."), "/foo")

    def test_name_with_dots_and_underscore(self):
        # Complex name with dots and underscores, followed by parent and current dir ops
        path = "/folder.name_/sub..dir/../file.txt/./"
        self.assertEqual(SimplifyPath(path), "/folder.name_/file.txt")

    # Stress tests for large inputs
    def test_max_length_single_segment(self):
        # Path of length 3000: "/" + 2999 "a"s
        path = "/" + "a" * 2999
        expected = path  # only one huge directory name, no changes expected
        self.assertEqual(SimplifyPath(path), expected)

    def test_max_segments_path(self):
        # Path with 1500 "a" directories: "/a/a/a/... (1500 times)"
        segments = ["a"] * 1500
        path = "/" + "/".join(segments)
        expected = "/" + "/".join(segments)  # same as path (already canonical)
        self.assertEqual(SimplifyPath(path), expected)

    def test_many_parent_operations(self):
        # 1000 parent directory operations from root
        ups = [".."] * 1000
        path = "/" + "/".join(ups)
        # Can't go above root, should remain "/"
        self.assertEqual(SimplifyPath(path), "/")

    def test_alternating_nested_operations(self):
        # Pattern: 10 "x" dirs then 10 "..", repeated 60 times (max length ~3000)
        parts = []
        for i in range(60):
            parts.extend(["x"] * 10)
            parts.extend([".."] * 10)
        path = "/" + "/".join(parts)
        # Each block of 10 "x" followed by 10 ".." cancels out, ending at root
        self.assertEqual(SimplifyPath(path), "/")


if __name__ == "__main__":
    unittest.main()
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 5ms.