需要帮助编写此代码的测试用例

时间:2019-01-21 10:10:59

标签: python testing

如何在不使用任何外部库的情况下为此代码编写测试用例?

Python程序可以反转输入字符串,同时将特殊字符保留在同一位置

    #Returns true if x is an alphabetic character, false otherwise
    def isAlphabet(x):
        return x.isalpha()

    def reverse_string(input_str):
        LIST = toList(input_str)

        #Initialize left and right pointers
        r = len(LIST) - 1
        l = 0

        #Traverse LIST from both ends until 'l' and 'r'
        while l < r:


            #Ignore special characters
            if not isAlphabet(LIST[l]):
                l += 1
            elif not isAlphabet(LIST[r]):
                r -= 1

            #Both LIST[l] and LIST[r] are not special 
            else:
                LIST[l], LIST[r] = swap(LIST[l], LIST[r])
                l += 1
                r -= 1

        return toString(LIST)

    # Utility functions 
    def toList(input_str): 
        List = [] 
        for i in input_str: 
            List.append(i) 
        return List

    def toString(List): 
        return ''.join(List) 

    def swap(a, b): 
        return b, a 

    # Driver code 
    input_str = "adfc_#sin*"
    print "Input string: " + input_str
    input_str = reverse_string(input_str) 
    print "Output string: " + input_str


    input_str = "hon()lo&"
    print "Input string: " + input_str
    input_str = reverse_string(input_str) 
    print "Output string: " + input_str

1 个答案:

答案 0 :(得分:0)

尝试一下:

input_str = "1234#as"
wanted_output = "sa#4321"

cur_output = reverse_string(input_str)

print("Input: ", input_str, "Output OK?", cur_output==wanted_output)

或这种方式:

assert cur_output==wanted_output