⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testtokenize.cpp

📁 cppcheck is a static C/C++ code analyzer that checks for memory leaks, mismatching allocation-deallo
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                            "{\n"                            "    int a = 10;\n"                            "    a = g();\n"                            "    if (a);\n"                            "}\n";        // tokenize..        OurTokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        tokenizer.simplifyKnownVariables();        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        ASSERT_EQUALS(std::string(" void f ( ) { int a = 10 ; a = g ( ) ; if ( a ) ; }"), ostr.str());    }    void simplifyKnownVariables3()    {        const char code[] = "void f()\n"                            "{\n"                            "    int a = 4;\n"                            "    while(true){\n"                            "    break;\n"                            "    a = 10;\n"                            "    }\n"                            "    if (a);\n"                            "}\n";        // tokenize..        OurTokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        tokenizer.simplifyKnownVariables();        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        ASSERT_EQUALS(std::string(" void f ( ) { int a = 4 ; while ( true ) { break ; a = 10 ; } if ( a ) ; }"), ostr.str());    }    void simplifyKnownVariables4()    {        const char code[] = "void f()\n"                            "{\n"                            "    int a = 4;\n"                            "    if ( g(a));\n"                            "}\n";        // tokenize..        OurTokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        tokenizer.simplifyKnownVariables();        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        ASSERT_EQUALS(std::string(" void f ( ) { int a = 4 ; if ( g ( a ) ) ; }"), ostr.str());    }    void simplifyKnownVariables5()    {        const char code[] = "void f()\n"                            "{\n"                            "    int a = 4;\n"                            "    if ( a = 5 );\n"                            "}\n";        // tokenize..        OurTokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        tokenizer.simplifyKnownVariables();        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        ASSERT_EQUALS(std::string(" void f ( ) { int a = 4 ; if ( a = 5 ) ; }"), ostr.str());    }    void simplifyKnownVariables6()    {        const char code[] = "void f()\n"                            "{\n"                            "    char str[2];"                            "    int a = 4;\n"                            "    str[a] = 0;\n"                            "}\n";        // tokenize..        OurTokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        tokenizer.simplifyKnownVariables();        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        ASSERT_EQUALS(std::string(" void f ( ) { char str [ 2 ] ; int a = 4 ; str [ 4 ] = 0 ; }"), ostr.str());    }    void simplifyKnownVariables7()    {        const char code[] = "void foo()\n"                            "{\n"                            "    int i = 22;\n"                            "    abc[i++] = 1;\n"                            "    abc[++i] = 2;\n"                            "}\n";        // tokenize..        OurTokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        tokenizer.simplifyKnownVariables();        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        ASSERT_EQUALS(std::string(" void foo ( ) { int i = 24 ; abc [ 22 ] = 1 ; abc [ 24 ] = 2 ; }"), ostr.str());    }    void simplifyKnownVariables8()    {        const char code[] = "void foo()\n"                            "{\n"                            "    int i = 22;\n"                            "    i++;\n"                            "    abc[i] = 0;\n"                            "}\n";        // tokenize..        OurTokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        tokenizer.simplifyKnownVariables();        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        ASSERT_EQUALS(std::string(" void foo ( ) { int i = 23 ; ; abc [ 23 ] = 0 ; }"), ostr.str());    }    void multiCompare()    {        // Test for found        ASSERT_EQUALS(1, Token::multiCompare("one|two", "one"));        ASSERT_EQUALS(1, Token::multiCompare("one|two", "two"));        ASSERT_EQUALS(1, Token::multiCompare("verybig|two|", "two"));        // Test for empty string found        ASSERT_EQUALS(0, Token::multiCompare("|one|two", "notfound"));        ASSERT_EQUALS(0, Token::multiCompare("one||two", "notfound"));        ASSERT_EQUALS(0, Token::multiCompare("one|two|", "notfound"));        // Test for not found        ASSERT_EQUALS(-1, Token::multiCompare("one|two", "notfound"));        ASSERT_EQUALS(-1, Token::multiCompare("verybig|two", "s"));        ASSERT_EQUALS(-1, Token::multiCompare("one|two", "ne"));        ASSERT_EQUALS(-1, Token::multiCompare("abc|def", "a"));        ASSERT_EQUALS(-1, Token::multiCompare("abc|def", "abcd"));        ASSERT_EQUALS(-1, Token::multiCompare("abc|def", "default"));    }    void match1()    {        // Match "%var% | %var%"        {            const std::string code("abc|def");            // tokenize..            Tokenizer tokenizer;            std::istringstream istr(code);            tokenizer.tokenize(istr, "test.cpp");            // Match..            ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "%var% | %var%"));        }        // Match "%var% || %var%"        {            const std::string code("abc||def");            // tokenize..            Tokenizer tokenizer;            std::istringstream istr(code);            tokenizer.tokenize(istr, "test.cpp");            // Match..            ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "%var% || %var%"));        }    }    void match2()    {        {            const std::string code("");            // tokenize..            Tokenizer tokenizer;            std::istringstream istr(code);            tokenizer.tokenize(istr, "test.cpp");            // Match..            ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "!!else"));        }        {            const std::string code("");            // tokenize..            Tokenizer tokenizer;            std::istringstream istr(code);            tokenizer.tokenize(istr, "test.cpp");            // Match..            ASSERT_EQUALS(false, Token::Match(tokenizer.tokens(), "!!else something"));        }        {            const std::string code("if ;");            // tokenize..            Tokenizer tokenizer;            std::istringstream istr(code);            tokenizer.tokenize(istr, "test.cpp");            // Match..            ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "!!return if"));        }        {            const std::string code("if ;");            // tokenize..            Tokenizer tokenizer;            std::istringstream istr(code);            tokenizer.tokenize(istr, "test.cpp");            // Match..            ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "if ; !!else"));        }        {            const std::string code("if ; something");            // tokenize..            Tokenizer tokenizer;            std::istringstream istr(code);            tokenizer.tokenize(istr, "test.cpp");            // Match..            ASSERT_EQUALS(true, Token::Match(tokenizer.tokens(), "if ; !!else"));        }        {            const std::string code("else");            // tokenize..            Tokenizer tokenizer;            std::istringstream istr(code);            tokenizer.tokenize(istr, "test.cpp");            // Match..            ASSERT_EQUALS(false, Token::Match(tokenizer.tokens(), "!!else"));        }        {            const std::string code("if ; else");            // tokenize..            Tokenizer tokenizer;            std::istringstream istr(code);            tokenizer.tokenize(istr, "test.cpp");            // Match..            ASSERT_EQUALS(false, Token::Match(tokenizer.tokens(), "if ; !!else"));        }    }    void varid1()    {        const std::string code("static int i = 1;\n"                               "void f()\n"                               "{\n"                               "    int i = 2;\n"                               "    for (int i = 0; i < 10; ++i)\n"                               "        i = 3;\n"                               "    i = 4;\n"                               "}\n");        // tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        // result..        const std::string actual(tokenizer.tokens()->stringifyList(true));        const std::string expected("\n\n##file 0\n"                                   "1: static int i@1 = 1 ;\n"                                   "2: void f ( )\n"                                   "3: {\n"                                   "4: int i@2 = 2 ;\n"                                   "5: for ( int i@3 = 0 ; i@3 < 10 ; ++ i@3 )\n"                                   "6: i@3 = 3 ;\n"                                   "7: i@2 = 4 ;\n"                                   "8: }\n");        ASSERT_EQUALS(expected, actual);    }    void varid2()    {        const std::string code("void f()\n"                               "{\n"                               "    struct ABC abc;\n"                               "    abc.a = 3;\n"                               "    i = abc.a;\n"                               "}\n");        // tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        // result..        const std::string actual(tokenizer.tokens()->stringifyList(true));        const std::string expected("\n\n##file 0\n"                                   "1: void f ( )\n"                                   "2: {\n"                                   "3: struct ABC abc@1 ;\n"                                   "4: abc@1 . a@2 = 3 ;\n"                                   "5: i = abc@1 . a@2 ;\n"                                   "6: }\n");        ASSERT_EQUALS(expected, actual);    }    void varid3()    {        const std::string code("static char str[4];\n"                               "void f()\n"                               "{\n"                               "    char str[10];\n"                               "    str[0] = 0;\n"                               "}\n");        // tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        // result..        const std::string actual(tokenizer.tokens()->stringifyList(true));        const std::string expected("\n\n##file 0\n"                                   "1: static char str@1 [ 4 ] ;\n"                                   "2: void f ( )\n"                                   "3: {\n"                                   "4: char str@2 [ 10 ] ;\n"                                   "5: str@2 [ 0 ] = 0 ;\n"                                   "6: }\n");        ASSERT_EQUALS(expected, actual);    }    void varid4()    {        const std::string code("void f(const unsigned int a[])\n"                               "{\n"                               "    int i = *(a+10);\n"                               "}\n");        // tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.simplifyTokenList();        // result..        const std::string actual(tokenizer.tokens()->stringifyList(true));        const std::string expected("\n\n##file 0\n"                                   "1: void f ( const int a@1 [ ] )\n"                                   "2: {\n"                                   "3: int i@2 ; i@2 = a@1 [ 10 ] ;\n"                                   "4: }\n");        ASSERT_EQUALS(expected, actual);    }    void varid5()    {        const std::string code("void f()\n"                               "{\n"                               "    int a,b;\n"                               "}\n");        // tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        tokenizer.simplifyTokenList();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -