mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-08-15 11:02:17 +00:00
good good study, day day up!
This commit is contained in:
21
vendor/gopl.io/ch11/word1/word.go
generated
vendored
Normal file
21
vendor/gopl.io/ch11/word1/word.go
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 303.
|
||||
//!+
|
||||
|
||||
// Package word provides utilities for word games.
|
||||
package word
|
||||
|
||||
// IsPalindrome reports whether s reads the same forward and backward.
|
||||
// (Our first attempt.)
|
||||
func IsPalindrome(s string) bool {
|
||||
for i := range s {
|
||||
if s[i] != s[len(s)-1-i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
//!-
|
43
vendor/gopl.io/ch11/word1/word_test.go
generated
vendored
Normal file
43
vendor/gopl.io/ch11/word1/word_test.go
generated
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
//!+test
|
||||
package word
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPalindrome(t *testing.T) {
|
||||
if !IsPalindrome("detartrated") {
|
||||
t.Error(`IsPalindrome("detartrated") = false`)
|
||||
}
|
||||
if !IsPalindrome("kayak") {
|
||||
t.Error(`IsPalindrome("kayak") = false`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNonPalindrome(t *testing.T) {
|
||||
if IsPalindrome("palindrome") {
|
||||
t.Error(`IsPalindrome("palindrome") = true`)
|
||||
}
|
||||
}
|
||||
|
||||
//!-test
|
||||
|
||||
// The tests below are expected to fail.
|
||||
// See package gopl.io/ch11/word2 for the fix.
|
||||
|
||||
//!+more
|
||||
func TestFrenchPalindrome(t *testing.T) {
|
||||
if !IsPalindrome("été") {
|
||||
t.Error(`IsPalindrome("été") = false`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanalPalindrome(t *testing.T) {
|
||||
input := "A man, a plan, a canal: Panama"
|
||||
if !IsPalindrome(input) {
|
||||
t.Errorf(`IsPalindrome(%q) = false`, input)
|
||||
}
|
||||
}
|
||||
|
||||
//!-more
|
Reference in New Issue
Block a user