diff --git a/problems/simplify-path/simplify_path.go b/problems/simplify-path/simplify_path.go index d397e1d5e..8c8237982 100644 --- a/problems/simplify-path/simplify_path.go +++ b/problems/simplify-path/simplify_path.go @@ -1 +1,7 @@ package simplify_path + +import "path/filepath" + +func simplifyPath(path string) string { + return filepath.Clean(path) +} diff --git a/problems/simplify-path/simplify_path_test.go b/problems/simplify-path/simplify_path_test.go index d397e1d5e..911379e94 100644 --- a/problems/simplify-path/simplify_path_test.go +++ b/problems/simplify-path/simplify_path_test.go @@ -1 +1,43 @@ package simplify_path + +import "testing" + +type caseType struct { + input string + expected string +} + +func TestSimplifyPath(t *testing.T) { + tests := [...]caseType{ + { + input: "/home/", + expected: "/home", + }, + { + input: "/../", + expected: "/", + }, + { + input: "/home//foo/", + expected: "/home/foo", + }, + { + input: "/a/./b/../../c/", + expected: "/c", + }, + { + input: "/a/../../b/../c//.//", + expected: "/c", + }, + { + input: "/a//b////c/d//././/..", + expected: "/a/b/c", + }, + } + for _, tc := range tests { + output := simplifyPath(tc.input) + if output != tc.expected { + t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected) + } + } +}