dougai8673
2018-01-16 18:53
采纳率: 100%
浏览 66
已采纳

紧急:运行时错误:索引超出范围[已恢复]

In my code panic error shown after Modify if j == len(remark) to if j == len(remark) && z > 0

Error is :

--- FAIL: TestHey (0.00s)
panic: runtime error: index out of range [recovered]
        panic: runtime error: index out of range

goroutine 5 [running]:
testing.tRunner.func1(0xc04207a0f0)
        C:/Go/src/testing/testing.go:711 +0x2d9
panic(0x526700, 0x5f57c0)
        C:/Go/src/runtime/panic.go:491 +0x291
_/C_/Exercism/Go/bob.Hey(0x0, 0x0, 0x54bda2, 0x5)
        C:/Exercism/Go/bob/bob.go:23 +0x26a
_/C_/Exercism/Go/bob.TestHey(0xc04207a0f0)
        C:/Exercism/Go/bob/bob_test.go:8 +0xd3
testing.tRunner(0xc04207a0f0, 0x555e98)
        C:/Go/src/testing/testing.go:746 +0xd7
created by testing.(*T).Run
        C:/Go/src/testing/testing.go:789 +0x2e5
exit status 2
FAIL    _/C_/Exercism/Go/bob    0.072s

bob.go file :

package bob

// Hey should have a comment documenting it.
func Hey(remark string) string {
    j := 0
    z := 0
    for i := 0; i < len(remark); i++ {
        te := remark[i]
        if te < 'a' || te > 'z' {
            j++
        }
        if te >= 'A' && te <= 'Z' {
            z++
        }
    }
    if j == len(remark) && z > 0 {
        if remark[len(remark)-1] == '?' {
            return "Calm down, I know what I'm doing!"
        } else {
            return "Whoa, chill out!"
        }
    }
    if remark[len(remark)-1] == '?' {
        return "Sure."
    }
    j = 0
    for i := 0; i < len(remark); i++ {
        if string(remark[i]) == string(9) || string(remark[i]) == string(10) || string(remark[i]) == string(13) || string(remark[i]) == string(32) {
            j++
        }
    }
    if j == len(remark) {
        return "Fine. Be that way!"
    }
    return "Whatever."
}

bob_test file:

package bob

import "testing"
import "fmt"

func TestHey(t *testing.T) {
    for _, tt := range testCases {
        actual := Hey(tt.input)
        if actual != tt.expected {
            msg := `
    ALICE (%s): %q
    BOB: %s

    Expected Bob to respond: %s`
            t.Fatalf(msg, tt.description, tt.input, actual, tt.expected)
        }
    }
}

func BenchmarkHey(b *testing.B) {
    for _, tt := range testCases {
        for i := 0; i < b.N; i++ {
            Hey(tt.input)
            fmt.Println(i)
        }
    }
}

cases_test file:

package bob

// Source: exercism/problem-specifications
// Commit: 6dc2014 bob: apply "input" policy
// Problem Specifications Version: 1.2.0

var testCases = []struct {
    description string
    input       string
    expected    string
}{
    {
        "stating something",
        "Tom-ay-to, tom-aaaah-to.",
        "Whatever.",
    },
    {
        "shouting",
        "WATCH OUT!",
        "Whoa, chill out!",
    },
    {
        "shouting gibberish",
        "FCECDFCAAB",
        "Whoa, chill out!",
    },
    {
        "asking a question",
        "Does this cryogenic chamber make me look fat?",
        "Sure.",
    },
    {
        "asking a numeric question",
        "You are, what, like 15?",
        "Sure.",
    },
    {
        "asking gibberish",
        "fffbbcbeab?",
        "Sure.",
    },
    {
        "talking forcefully",
        "Let's go make out behind the gym!",
        "Whatever.",
    },
    {
        "using acronyms in regular speech",
        "It's OK if you don't want to go to the DMV.",
        "Whatever.",
    },
    {
        "forceful question",
        "WHAT THE HELL WERE YOU THINKING?",
        "Calm down, I know what I'm doing!",
    },
    {
        "shouting numbers",
        "1, 2, 3 GO!",
        "Whoa, chill out!",
    },
    {
        "only numbers",
        "1, 2, 3",
        "Whatever.",
    },
    {
        "question with only numbers",
        "4?",
        "Sure.",
    },
    {
        "shouting with special characters",
        "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!",
        "Whoa, chill out!",
    },
    {
        "shouting with no exclamation mark",
        "I HATE YOU",
        "Whoa, chill out!",
    },
    {
        "statement containing question mark",
        "Ending with ? means a question.",
        "Whatever.",
    },
    {
        "non-letters with question",
        ":) ?",
        "Sure.",
    },
    {
        "prattling on",
        "Wait! Hang on. Are you going to be OK?",
        "Sure.",
    },
    {
        "silence",
        "",
        "Fine. Be that way!",
    },
    {
        "prolonged silence",
        "          ",
        "Fine. Be that way!",
    },
    {
        "alternate silence",
        "\t\t\t\t\t\t\t\t\t\t",
        "Fine. Be that way!",
    },
    {
        "multiple line question",
        "
Does this cryogenic chamber make me look fat?
no",
        "Whatever.",
    },
    {
        "starting with whitespace",
        "         hmmmmmmm...",
        "Whatever.",
    },
    {
        "ending with whitespace",
        "Okay if like my  spacebar  quite a bit?   ",
        "Sure.",
    },
    {
        "other whitespace",
        "
 \t",
        "Fine. Be that way!",
    },
    {
        "non-question ending with whitespace",
        "This is a statement ending with whitespace      ",
        "Whatever.",
    },
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dps69208 2018-01-16 20:34
    已采纳

    One of your test cases is:

    {
        "silence",
        "",
        "Fine. Be that way!",
    },
    

    This calls Hey(""), meaning that len(remark) == 0.

    The panic occurs at L23:

    if remark[len(remark)-1] == '?' {
    

    With the provided input, that turns into remark[-1] which is definitely out of range and causes a panic.

    You should sanitize your inputs which includes handling empty strings.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题