def test_replace_all():
    input_string = '// user-defined function to check prime number \n\
            int checkPrimeNumber(int n) \n\
            { \n\
                int j, flag = 1; \n\
                for(j=DVA; j <= n/DVA; ++j) \n\
                { \n\
                    if (n%j == 0) \n\
                    { \n\
                        flag =0; \n\
                         break; \n\
                    } \n\
                 } \n\
            return flag;\n\
            }'
    tool = PreprocessorTool(input_string)
    tool.replace_all('DVA', '2')
    valid_string = '// user-defined function to check prime number \n\
            int checkPrimeNumber(int n) \n\
            { \n\
                int j, flag = 1; \n\
                for(j=2; j <= n/2; ++j) \n\
                { \n\
                    if (n%j == 0) \n\
                    { \n\
                        flag =0; \n\
                         break; \n\
                    } \n\
                 } \n\
            return flag;\n\
            }'
    assert tool.c_code == valid_string
def replace_all(c_code, replace_what, replace_to):
    """
    Replace all 'replace_what' entries with 'replace_to' (only where it is needed)
    :param c_code: C code in which replace is taking place
    :param replace_what: string that should be replaced
    :param replace_to: string that should be placed instead of 'replace_what'
    :return: C code (string)
    """
    preprocessor_tool = PreprocessorTool(c_code)
    define_string = '#define ' + replace_what + ' ' + replace_to
    preprocessor_tool.remove_first(define_string)
    preprocessor_tool.replace_all(replace_what, replace_to)
    return preprocessor_tool.c_code
def test_2_remove_first():
    input_string = '#define KAZAN_CODE 843'
    tool = PreprocessorTool(input_string)
    tool.remove_first('#define KAZAN_CODE 843')
    assert tool.c_code == ''
def test_1_remove_first():
    input_string = 'Removing first entry'
    tool = PreprocessorTool(input_string)
    tool.remove_first('first')
    assert tool.c_code == 'Removing  entry'
def test_2_get_next_char():
    input_string = ''
    tool = PreprocessorTool(input_string)
    assert tool.get_next_char() == '_EOF'
def test_skip():
    input_string = '       iterator should point to 6th char (char before non space)'
    tool = PreprocessorTool(input_string)
    tool.skip()
    assert tool.iterator == 6
def test_1_get_next_char():
    input_string = 'It\'s 3AM and I\'m writing this test'
    tool = PreprocessorTool(input_string)
    assert tool.get_next_char() == 'I'
def test_set_iterator():
    input_string = 'Useless string, but it is needed to create an object'
    tool = PreprocessorTool(input_string)
    tool.set_iterator(2)
    assert tool.iterator == 2
def test_find_all():
    input_string = 'This bad boy can find so many "a" in it (4)'
    tool = PreprocessorTool(input_string)
    assert tool.find_all('a') == [6, 14, 26, 31]
def test_find():
    input_string = 'Do I really need to test built-in features?'
    tool = PreprocessorTool(input_string)
    assert tool.find('really') == 5
def scan_for_define(c_code):
    """
    Scans for '#define' tokens and what string should be replaced in input C code
    :param c_code: C code in which scan is taking place
    :return: dictionary with following format: {what_should_be_replaced: on_what_should_be_replaced}
    """
    replacing = {}
    preprocessor_tool = PreprocessorTool(c_code)
    define_indexes = preprocessor_tool.find_all("#define ")
    for define_index in define_indexes:
        preprocessor_tool.set_iterator(define_index + 7)
        preprocessor_tool.skip()
        replace_what = ''
        current_char = preprocessor_tool.get_next_char()
        if current_char == '_EOF':
            return replacing
        while current_char != ' ' and current_char != '\n':
            replace_what += current_char
            current_char = preprocessor_tool.get_next_char()
            if current_char == '_EOF':
                return replacing
        preprocessor_tool.skip()
        replace_to = ''
        current_char = preprocessor_tool.get_next_char()
        if current_char == '_EOF':
            return replacing
        while current_char != ' ' and current_char != '\n':
            replace_to += current_char
            current_char = preprocessor_tool.get_next_char()
            if current_char == '_EOF':
                replacing[replace_what] = replace_to
                return replacing
        replacing[replace_what] = replace_to
    return replacing