Skip to content

Instantly share code, notes, and snippets.

@vderyagin
Created February 16, 2013 05:44
Show Gist options
  • Save vderyagin/4965705 to your computer and use it in GitHub Desktop.
Save vderyagin/4965705 to your computer and use it in GitHub Desktop.
package int_palindrome
func isPalindrome(n int) bool {
reverse := 0
for t := n; t > 0; t /= 10 {
reverse *= 10
reverse += t % 10
}
return reverse == n
}
package int_palindrome
import "testing"
func TestZero(t *testing.T) {
if !isPalindrome(0) {
t.Error("zero is a palindrome")
}
}
func TestOne(t *testing.T) {
if !isPalindrome(1) {
t.Error("1 is a palindrome")
}
}
func TestLongerPalindromes(t *testing.T) {
if n := 11; !isPalindrome(n) {
t.Errorf("%d is a palindrome", n)
}
if n := 12344321; !isPalindrome(n) {
t.Errorf("%d is a palindrome", n)
}
}
func TestNonPalindromes(t *testing.T) {
if n := 12; isPalindrome(n) {
t.Errorf("%d is not a palindrome", n)
}
if n := 312344321; isPalindrome(n) {
t.Errorf("%d is not a palindrome", n)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment