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

📄 testtokenize.cpp

📁 cppcheck is a static C/C++ code analyzer that checks for memory leaks, mismatching allocation-deallo
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        // 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: int a@1 ; int b@2 ;\n"                                   "4: }\n");        ASSERT_EQUALS(expected, actual);    }    void varid6()    {        const std::string code("int f(int a, int b)\n"                               "{\n"                               "    return a+b;\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 ( int a@1 , int b@2 )\n"                                   "2: {\n"                                   "3: return a@1 + b@2 ;\n"                                   "4: }\n");        ASSERT_EQUALS(expected, actual);    }    void varid7()    {        const std::string code("void func()\n"                               "{\n"                               "	char a[256] = \"test\";\n"                               "	{\n"                               "		char b[256] = \"test\";\n"                               "	}\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 func ( )\n"                                   "2: {\n"                                   "3: char a@1 [ 256 ] = \"test\" ;\n"                                   "4: {\n"                                   "5: char b@2 [ 256 ] = \"test\" ;\n"                                   "6: }\n"                                   "7: }\n");        ASSERT_EQUALS(expected, actual);    }    void varidReturn()    {        const std::string code("int f()\n"                               "{\n"                               "    int a;\n"                               "    return a;\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: int f ( )\n"                                   "2: {\n"                                   "3: int a@1 ;\n"                                   "4: return a@1 ;\n"                                   "5: }\n");        ASSERT_EQUALS(expected, actual);    }    void file1()    {        const char code[] = "a1\n"                            "#file \"b\"\n"                            "b1\n"                            "b2\n"                            "#endfile\n"                            "a3\n";        // tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "a");        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())        {            std::ostringstream ostr;            ostr << char('a' + tok->fileIndex()) << tok->linenr();            ASSERT_EQUALS(tok->str(), ostr.str());        }    }    void file2()    {        const char code[] = "a1\n"                            "#file \"b\"\n"                            "b1\n"                            "b2\n"                            "#file \"c\"\n"                            "c1\n"                            "c2\n"                            "#endfile\n"                            "b4\n"                            "#endfile\n"                            "a3\n"                            "#file \"d\"\n"                            "d1\n"                            "#endfile\n"                            "a5\n";        // tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "a");        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())        {            std::ostringstream ostr;            ostr << char('a' + tok->fileIndex()) << tok->linenr();            ASSERT_EQUALS(tok->str(), ostr.str());        }    }    void file3()    {        const char code[] = "#file \"c:\\a.h\"\n"                            "123\n"                            "#endfile\n";        // tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "a.cpp");        ASSERT_EQUALS("[c:\\a.h:1]", tokenizer.fileLine(tokenizer.tokens()));    }    void doublesharp()    {        const char code[] = "TEST(var,val) var##_##val = val\n";        // Tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "");        // Stringify the tokens..        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << tok->str() << " ";        ASSERT_EQUALS("TEST ( var , val ) var ## _ ## val = val ", ostr.str());    }    void macrodoublesharp()    {        const char code[] = "DBG(fmt,args...) printf(fmt, ## args)\n";        // Tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "");        // Stringify the tokens..        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << tok->str() << " ";        ASSERT_EQUALS("DBG ( fmt , args . . . ) printf ( fmt , ## args ) ", ostr.str());    }    void simplify_function_parameters()    {        {            const char code[] = "void f(x) int x;\n"                                "{\n"                                "}\n";            // tokenize..            Tokenizer tokenizer;            std::istringstream istr(code);            tokenizer.tokenize(istr, "test.cpp");            tokenizer.simplifyTokenList();            std::ostringstream ostr;            for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())                ostr << " " << tok->str();            ASSERT_EQUALS(std::string(" void f ( int x ) { }"), ostr.str());        }        {            const char code[] = "void f(x,y) int x; char y;\n"                                "{\n"                                "}\n";            // tokenize..            Tokenizer tokenizer;            std::istringstream istr(code);            tokenizer.tokenize(istr, "test.cpp");            tokenizer.simplifyTokenList();            std::ostringstream ostr;            for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())                ostr << " " << tok->str();            ASSERT_EQUALS(std::string(" void f ( int x , char y ) { }"), ostr.str());        }        {            // This is not a function but the pattern is similar..            const char code[] = "void foo()\n"                                "{\n"                                "    if (x)\n"                                "        int x;\n"                                "    { }\n"                                "}\n";            // tokenize..            Tokenizer tokenizer;            std::istringstream istr(code);            tokenizer.tokenize(istr, "test.cpp");            tokenizer.simplifyTokenList();            std::ostringstream ostr;            for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())                ostr << " " << tok->str();            ASSERT_EQUALS(std::string(" void foo ( ) { if ( x ) { int x ; } { } }"), ostr.str());        }    }    // Simplify "((..))" into "(..)"    void reduce_redundant_paranthesis()    {        const char code[] = "void foo()\n"                            "{\n"                            "    free(((void*)p));\n"                            "}";        // tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.simplifyTokenList();        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        ASSERT_EQUALS(std::string(" void foo ( ) { free ( p ) ; }"), ostr.str());    }    void simplify_numeric_condition()    {        const char code[] =            "void f()\n"            "{\n"            "int x = 0;\n"            "if( !x || 0 )\n"            "{\n"            "}\n"            "}";        // tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        tokenizer.simplifyTokenList();        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        ASSERT_EQUALS(std::string(" void f ( ) { int x ; x = 0 ; if ( ! x ) { } }"), ostr.str());    }    void tokenize_double()    {        const char code[] = "void f()\n"                            "{\n"                            "    double a = 4.2;\n"                            "    float b = 4.2f;\n"                            "    double c = 4.2e+10;\n"                            "    double d = 4.2e-10;\n"                            "    int e = 4+2;\n"                            "}\n";        // tokenize..        OurTokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        ASSERT_EQUALS(std::string(" void f ( ) { double a = 4.2 ; float b = 4.2f ; double c = 4.2e+10 ; double d = 4.2e-10 ; int e = 4 + 2 ; }"), ostr.str());    }    void tokenize_strings()    {        const char code[] =   "void f()\n"                              "{\n"                              "const char *a =\n"                              "{\n"                              "\"hello \"\n"                              "\"more \"\n"                              "\"world\"\n"                              "};\n"                              "}\n";        // tokenize..        OurTokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.simplifyTokenList();        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        ASSERT_EQUALS(std::string(" void f ( ) { const char * a = { \"hello more world\" } ; }"), ostr.str());    }    void simplify_constants()    {        const char code[] =            "void f()\n"            "{\n"            "const int a = 45;\n"            "if( a )\n"            "{ int b = a; }\n"            "}\n"            "void g()\n"            "{\n"            "int a = 2;\n"            "}\n";        // tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        tokenizer.simplifyTokenList();        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        ASSERT_EQUALS(std::string(" void f ( ) { const int a = 45 ; { int b ; b = 45 ; } } void g ( ) { int a ; a = 2 ; }"), ostr.str());    }    void simplify_constants2()    {        const char code[] =            "void f( Foo &foo, Foo *foo2 )\n"            "{\n"            "const int a = 45;\n"            "foo.a=a+a;\n"            "foo2->a=a;\n"            "}\n";        // tokenize..        Tokenizer tokenizer;        std::istringstream istr(code);        tokenizer.tokenize(istr, "test.cpp");        tokenizer.setVarId();        tokenizer.simplifyTokenList();        std::ostringstream ostr;        for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())            ostr << " " << tok->str();        std::ostringstream oss;        oss << " void f ( Foo & foo , Foo * foo2 ) { const int a = 45 ; foo . a = 90 ; foo2 . a = 45 ; }";        ASSERT_EQUALS(oss.str(), ostr.str());    }};REGISTER_TEST(TestTokenizer)

⌨️ 快捷键说明

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