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

时间:2018-01-16 18:53:13

标签: go

修改if j == len(remark)if j == len(remark) && z > 0

后显示的代码出现紧急错误

错误是:

--- 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文件:

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文件:

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文件:

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",
        "\nDoes this cryogenic chamber make me look fat?\nno",
        "Whatever.",
    },
    {
        "starting with whitespace",
        "         hmmmmmmm...",
        "Whatever.",
    },
    {
        "ending with whitespace",
        "Okay if like my  spacebar  quite a bit?   ",
        "Sure.",
    },
    {
        "other whitespace",
        "\n\r \t",
        "Fine. Be that way!",
    },
    {
        "non-question ending with whitespace",
        "This is a statement ending with whitespace      ",
        "Whatever.",
    },
}

1 个答案:

答案 0 :(得分:0)

您的一个测试用例是:

DATADIR=
for d in \
    ${with_pari_datadir} \
    ${LIBPARI_PREFIX:+${LIBPARI_PREFIX}/share/pari} \
    ${datadir_expanded}/pari \
    /usr/local/share/pari \
    /usr/share/pari
do
  AS_IF([test -r "$[]d/pari.desc"], [DATADIR="$[]d"; break])
done

AS_IF([test x = "x$DATADIR"], [AC_MSG_ERROR(["Could not identify PARI data directory"])])
AC_SUBST([DATADIR])

这会调用{ "silence", "", "Fine. Be that way!", }, ,表示Hey("")

恐慌发生在L23:

len(remark) == 0

使用提供的输入,变为if remark[len(remark)-1] == '?' { ,这肯定超出范围并导致恐慌。

您应该清理输入,包括处理空字符串。