Created
January 8, 2012 05:53
-
-
Save shekibobo/1577401 to your computer and use it in GitHub Desktop.
Testing for TheRubyGame Palindrome
This file contains 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
#!/usr/bin/env ruby | |
class Palindrome | |
return string if string.length == 1 | |
l = "" | |
string.size.times do |i| | |
break if string.size - i <= l.size | |
c = -1 | |
while (a = string.rindex(string[i], c)) != i || a - i > l.size do | |
b = string[i..a] | |
l = b if b == b.reverse && b.size > l.size | |
c = a - 1 | |
end | |
end | |
l | |
end | |
end |
This file contains 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
#!/usr/bin/env ruby | |
require_relative "palindrome" | |
require "test/unit" | |
require "benchmark" | |
class TestKeygen < Test::Unit::TestCase | |
def setup | |
@string = "abacdfgdcabaypqqpy" | |
@long_string = "abacdfgdcabaypqqpyabacdgfdcasjdkfljaioskdke" | |
@superlong_string = "ybd" * 33 + @string | |
end | |
def test_single_letter_palindrome | |
assert_equal "a", Palindrome.find_longest_palindrome("a") | |
end | |
def test_find_longest_palindrome | |
assert_equal "ypqqpy", Palindrome.find_longest_palindrome(@string) | |
end | |
def test_find_longest_palindrome_reverse | |
assert_equal "ypqqpy", Palindrome.find_longest_palindrome(@string.reverse) | |
end | |
def test_find_longest_palindrome_long | |
assert_equal s = @string + @string.reverse, Palindrome.find_longest_palindrome(s) | |
end | |
def test_find_longest_palindrome_longer | |
assert_equal "acdfgdcabaypqqpyabacdgfdca", Palindrome.find_longest_palindrome(@long_string) | |
end | |
def test_find_longest_palindrome_shorter | |
assert_equal "ypqqpy", Palindrome.find_longest_palindrome(@string.reverse + "y") | |
end | |
def test_find_longest_palindrome_shorter_reverse | |
assert_equal "ypqqpy", Palindrome.find_longest_palindrome((@string.reverse + "y").reverse) | |
end | |
def test_find_longest_palindrome_superlong | |
assert_equal "ypqqpy", Palindrome.find_longest_palindrome(@superlong_string) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment