Created
February 16, 2013 05:44
-
-
Save vderyagin/4965705 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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