-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
func head(input string, n int) string { | ||
lines := splitLines(input) | ||
if n > len(lines) { | ||
n = len(lines) | ||
} | ||
lineEnding := "\n" | ||
if runtime.GOOS == "windows" { | ||
lineEnding = "\r\n" | ||
} | ||
return strings.Join(lines[:n], lineEnding) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"runtime" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestHead(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
n int | ||
expected string | ||
}{ | ||
{"line1\nline2\nline3\nline4\nline5\n", 2, "line1\nline2"}, | ||
{"line1\nline2\nline3\nline4\nline5\n", 0, ""}, | ||
{"line1\nline2\nline3\nline4\nline5\n", 5, "line1\nline2\nline3\nline4\nline5"}, | ||
{"line1\nline2\nline3\nline4\nline5\n", 10, "line1\nline2\nline3\nline4\nline5"}, | ||
{"line1\nline2\nline3\nline4\nline5", 3, "line1\nline2\nline3"}, | ||
{"", 3, ""}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.input, func(t *testing.T) { | ||
// Determine the expected line ending based on the operating system | ||
lineEnding := "\n" | ||
if runtime.GOOS == "windows" { | ||
lineEnding = "\r\n" | ||
} | ||
|
||
// Replace expected line endings with the appropriate ones for the OS | ||
expected := strings.ReplaceAll(tt.expected, "\n", lineEnding) | ||
|
||
result := head(tt.input, tt.n) | ||
if result != expected { | ||
t.Errorf("head(%d) = %q; want %q", tt.n, result, expected) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
func tail(input string, n int) string { | ||
lines := splitLines(input) | ||
if n > len(lines) { | ||
n = len(lines) | ||
} | ||
lineEnding := "\n" | ||
if runtime.GOOS == "windows" { | ||
lineEnding = "\r\n" | ||
} | ||
return strings.Join(lines[len(lines)-n:], lineEnding) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"runtime" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestTail(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
n int | ||
expected string | ||
}{ | ||
{"line1\nline2\nline3\nline4\nline5\n", 2, "line4\nline5"}, | ||
{"line1\nline2\nline3\nline4\nline5\n", 0, ""}, | ||
{"line1\nline2\nline3\nline4\nline5\n", 5, "line1\nline2\nline3\nline4\nline5"}, | ||
{"line1\nline2\nline3\nline4\nline5\n", 10, "line1\nline2\nline3\nline4\nline5"}, | ||
{"line1\nline2\nline3\nline4\nline5", 3, "line3\nline4\nline5"}, | ||
{"", 3, ""}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.input, func(t *testing.T) { | ||
// Determine the expected line ending based on the operating system | ||
lineEnding := "\n" | ||
if runtime.GOOS == "windows" { | ||
lineEnding = "\r\n" | ||
} | ||
|
||
// Replace expected line endings with the appropriate ones for the OS | ||
expected := strings.ReplaceAll(tt.expected, "\n", lineEnding) | ||
|
||
result := tail(tt.input, tt.n) | ||
if result != expected { | ||
t.Errorf("tail(%d) = %q; want %q", tt.n, result, expected) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters