# palindromestest.py
# Lillian Lee (LJL2), Walker White (wmw2)
# Mar 19, 2014

"""Test cases for palindromes recursion"""

import palindromes
import random
import cornelltest


def test_palindrome_fns():
    """test ispalindrome, ispalindrome2, ispalindrome_loosely"""

    # All functions should return the same result on these test cases, so run
    # them on the exact same test cases.
    for fn in [palindromes.ispalindrome]:
        print ' testing ' + fn.__name__

        # one test case inspired by www.youtube.com/watch?v=JUQDzj6R3p4?
        for test_s in ['', 'B', 'BB', 'BoB',
                       'ablewasiereisawelba', 'amanaplanacanalpanama',
                       'rats live on no evil star']:
            cornelltest.assert_true(fn(test_s))

        for test_s in ['BA', 'BOA', 'BABA', 'ablewasiereisawelbaa',
                        'rats live on not evil star']:
            cornelltest.assert_true(not fn(test_s))

    test_s = 'amanaplana canalpanama' # extra space = not official palindrome
    cornelltest.assert_true(not palindromes.ispalindrome(test_s))
    print ' finished palindrome tests'


def complement(x):
    """Returns complement of character x, assumed to represent a
    base pair"""

    return palindromes.comp[palindromes.base.index(x)]


def testhairpin():
    """Test is5Ahairpin"""

    # Test things that are hairpins
    candidate = 'A'*5
    hps = [candidate, 'GUAAAAAAC']
    for i in range(20):
        b = random.choice(palindromes.base) # random base
        candidate = b + candidate + complement(b)
        hps.append(candidate)

    for candidate in hps:
        # print candidate
        cornelltest.assert_true(palindromes.is5Ahairpin(candidate))

    # Test things that aren't hairpins
    nhps = hps[:]
    for i in range(len(nhps)):
        # Change known hairpins into non-hairpins
        j = random.randrange(0, len(hps[i])-1)
        # Change the jth character of the ith string
        #print nhps[i], i, j
        nhps[i] = nhps[i][:j] + complement(nhps[i][j]) + nhps[i][j:]
        #print '-> ' + nhps[i]

    nhps.extend(['', 'A', 'G', 'GGG', 'AGCU', 'AAAA', 'GAAAC'])
    nhps.extend(['AAAAAC', 'UAAAAA', 'GUAAAAAACC', 'GUAAAAAGC'])

    for candidate in nhps:
        #print candidate
        cornelltest.assert_true(not palindromes.is5Ahairpin(candidate))

    print ' finished rna tests'


if __name__ == '__main__':
    test_palindrome_fns()
    testhairpin()
    print 'all test cases for palindromes passed'