Skip to content

Commit

Permalink
feat: truncate too long test case (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 authored Nov 19, 2023
1 parent 4b40d26 commit 796c13a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
21 changes: 11 additions & 10 deletions leetcode/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/j178/leetgo/config"
"github.com/j178/leetgo/utils"
)

type UserStatus struct {
Expand Down Expand Up @@ -96,17 +97,17 @@ func (r *SubmitCheckResult) Display(q *QuestionData) string {
"\n%s%s%s%s%s%s\n",
config.FailedStyle.Render(" × Wrong Answer\n"),
fmt.Sprintf("\nPassed cases: %d/%d", r.TotalCorrect, r.TotalTestcases),
fmt.Sprintf("\nLast case: %s", strings.ReplaceAll(r.LastTestcase, "\n", "↩ ")),
fmt.Sprintf("\nOutput: %s", strings.ReplaceAll(r.CodeOutput, "\n", "↩ ")),
fmt.Sprintf("\nLast case: %s", utils.TruncateString(strings.ReplaceAll(r.LastTestcase, "\n", "↩ "), 100)),
fmt.Sprintf("\nOutput: %s", utils.TruncateString(strings.ReplaceAll(r.CodeOutput, "\n", "↩ "), 100)),
stdout,
fmt.Sprintf("\nExpected: %s", strings.ReplaceAll(r.ExpectedOutput, "\n", "↩ ")),
fmt.Sprintf("\nExpected: %s", utils.TruncateString(strings.ReplaceAll(r.ExpectedOutput, "\n", "↩ "), 100)),
)
case MemoryLimitExceeded, TimeLimitExceeded, OutputLimitExceeded:
return fmt.Sprintf(
"\n%s%s%s\n",
config.ErrorStyle.Render(fmt.Sprintf(" × %s\n", r.StatusMsg)),
fmt.Sprintf("\nPassed cases: %d/%d", r.TotalCorrect, r.TotalTestcases),
fmt.Sprintf("\nLast case: %s", r.LastTestcase),
fmt.Sprintf("\nLast case: %s", utils.TruncateString(r.LastTestcase, 100)),
)
case RuntimeError:
return fmt.Sprintf(
Expand Down Expand Up @@ -200,20 +201,20 @@ func (r *RunCheckResult) Display(q *QuestionData) string {
"\n%s%s%s%s%s%s\n",
config.PassedStyle.Render(fmt.Sprintf(" √ %s\n", r.StatusMsg)),
fmt.Sprintf("\nPassed cases: %s", formatCompare(r.CompareResult)),
fmt.Sprintf("\nInput: %s", strings.ReplaceAll(r.InputData, "\n", "↩ ")),
fmt.Sprintf("\nOutput: %s", strings.Join(r.CodeAnswer, "↩ ")),
fmt.Sprintf("\nInput: %s", utils.TruncateString(strings.ReplaceAll(r.InputData, "\n", "↩ "), 100)),
fmt.Sprintf("\nOutput: %s", utils.TruncateString(strings.Join(r.CodeAnswer, "↩ "), 100)),
stdout,
fmt.Sprintf("\nExpected: %s", strings.Join(r.ExpectedCodeAnswer, "↩ ")),
fmt.Sprintf("\nExpected: %s", utils.TruncateString(strings.Join(r.ExpectedCodeAnswer, "↩ "), 100)),
)
} else {
return fmt.Sprintf(
"\n%s%s%s%s%s%s\n",
config.ErrorStyle.Render("\n × Wrong Answer\n"),
fmt.Sprintf("\nPassed cases: %s", formatCompare(r.CompareResult)),
fmt.Sprintf("\nInput: %s", strings.ReplaceAll(r.InputData, "\n", "↩ ")),
fmt.Sprintf("\nOutput: %s", strings.Join(r.CodeAnswer, "↩ ")),
fmt.Sprintf("\nInput: %s", utils.TruncateString(strings.ReplaceAll(r.InputData, "\n", "↩ "), 100)),
fmt.Sprintf("\nOutput: %s", utils.TruncateString(strings.Join(r.CodeAnswer, "↩ "), 100)),
stdout,
fmt.Sprintf("\nExpected: %s", strings.Join(r.ExpectedCodeAnswer, "↩ ")),
fmt.Sprintf("\nExpected: %s", utils.TruncateString(strings.Join(r.ExpectedCodeAnswer, "↩ "), 100)),
)
}
case MemoryLimitExceeded, TimeLimitExceeded, OutputLimitExceeded:
Expand Down
14 changes: 14 additions & 0 deletions utils/str.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ func EnsureTrailingNewline(s string) string {
return s
}

// TruncateString shortens a string longer than n by replacing the middle part with "...<truncated>..."
func TruncateString(s string, n int) string {
if len(s) <= n {
return s
}
const l = len("...<truncated>...")
prefixLength := (n - l) / 2
suffixLength := n - prefixLength - l

truncated := s[:prefixLength] + "...<truncated>..." + s[len(s)-suffixLength:]

return truncated
}

func CamelToSnake(name string) string {
var snakeStrBuilder strings.Builder

Expand Down

0 comments on commit 796c13a

Please sign in to comment.