Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2014 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "Test.h"
      9 
     10 #if SK_SUPPORT_GPU
     11 #include "GrSKSLPrettyPrint.h"
     12 
     13 #define ASSERT(x) REPORTER_ASSERT(r, x)
     14 
     15 const SkString input1("#this is not a realshader\nvec4 some stuff;outside of a function;"
     16                      "int i(int b, int c) { { some stuff;} fake block; //comments\n return i;}"
     17                      "void main()");
     18 const SkString input2("{nowin a function;{indenting;{abit more;dreadedfor((;;)(;)((;;);)){"
     19                      "doingstuff"
     20                      ";for(;;;){and more stufff;mixed garbage\n\n\t\t\t\t\n/*using this"
     21                      " comment\n is");
     22 const SkString input3(" dangerous\ndo so at your own\n risk*/;\n\n\t\t\t\n"
     23                      "//a comment");
     24 const SkString input4("breaking in comment");
     25 const SkString input5("continuing the comment");
     26 const SkString input6("\n}}a; little ;  love; for   ; leading;  spaces;} "
     27                      "an struct = { int a; int b; };"
     28                      "int[5] arr = int[5](1,2,3,4,5);} some code at the bottom; for(;;) {} }");
     29 
     30 const SkSL::String output1(
     31         "   1\t#this is not a realshader\n"
     32         "   2\tvec4 some stuff;\n"
     33         "   3\toutside of a function;\n"
     34         "   4\tint i(int b, int c) \n"
     35         "   5\t{\n"
     36         "   6\t\t{\n"
     37         "   7\t\t\tsome stuff;\n"
     38         "   8\t\t}\n"
     39         "   9\t\tfake block;\n"
     40         "  10\t\t//comments\n"
     41         "  11\t\treturn i;\n"
     42         "  12\t}\n"
     43         "  13\tvoid main()\n"
     44         "  14\t{\n"
     45         "  15\t\tnowin a function;\n"
     46         "  16\t\t{\n"
     47         "  17\t\t\tindenting;\n"
     48         "  18\t\t\t{\n"
     49         "  19\t\t\t\tabit more;\n"
     50         "  20\t\t\t\tdreadedfor((;;)(;)((;;);))\n"
     51         "  21\t\t\t\t{\n"
     52         "  22\t\t\t\t\tdoingstuff;\n"
     53         "  23\t\t\t\t\tfor(;;;)\n"
     54         "  24\t\t\t\t\t{\n"
     55         "  25\t\t\t\t\t\tand more stufff;\n"
     56         "  26\t\t\t\t\t\tmixed garbage/*using this comment\n"
     57         "  27\t\t\t\t\t\t is dangerous\n"
     58         "  28\t\t\t\t\t\tdo so at your own\n"
     59         "  29\t\t\t\t\t\t risk*/;\n"
     60         "  30\t\t\t\t\t\t//a commentbreaking in commentcontinuing the comment\n"
     61         "  31\t\t\t\t\t}\n"
     62         "  32\t\t\t\t}\n"
     63         "  33\t\t\t\ta;\n"
     64         "  34\t\t\t\tlittle ;\n"
     65         "  35\t\t\t\tlove;\n"
     66         "  36\t\t\t\tfor   ;\n"
     67         "  37\t\t\t\tleading;\n"
     68         "  38\t\t\t\tspaces;\n"
     69         "  39\t\t\t}\n"
     70         "  40\t\t\tan struct = \n"
     71         "  41\t\t\t{\n"
     72         "  42\t\t\t\tint a;\n"
     73         "  43\t\t\t\tint b;\n"
     74         "  44\t\t\t}\n"
     75         "  45\t\t\t;\n"
     76         "  46\t\t\tint[5] arr = int[5](1,2,3,4,5);\n"
     77         "  47\t\t}\n"
     78         "  48\t\tsome code at the bottom;\n"
     79         "  49\t\tfor(;;) \n"
     80         "  50\t\t{\n"
     81         "  51\t\t}\n"
     82         "  52\t}\n"
     83         "  53\t");
     84 
     85 const SkString neg1("{;;{{{{;;;{{{{{{{{{{{");
     86 const SkString neg2("###\n##\n#####(((((((((((((unbalanced verything;;;");
     87 const SkString neg3("}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}"
     88         ";;;;;;/////");
     89 
     90 DEF_TEST(GrSKSLPrettyPrint, r) {
     91     SkTArray<const char*> testStr;
     92     SkTArray<int> lengths;
     93     testStr.push_back(input1.c_str());
     94     lengths.push_back((int)input1.size());
     95     testStr.push_back(input2.c_str());
     96     lengths.push_back((int)input2.size());
     97     testStr.push_back(input3.c_str());
     98     lengths.push_back((int)input3.size());
     99     testStr.push_back(input4.c_str());
    100     lengths.push_back((int)input4.size());
    101     testStr.push_back(input5.c_str());
    102     lengths.push_back((int)input5.size());
    103     testStr.push_back(input6.c_str());
    104     lengths.push_back((int)input6.size());
    105 
    106     SkSL::String test = GrSKSLPrettyPrint::PrettyPrint(testStr.begin(), lengths.begin(),
    107                                                    testStr.count(), true);
    108     ASSERT(output1 == test);
    109 
    110     testStr.reset();
    111     lengths.reset();
    112     testStr.push_back(neg1.c_str());
    113     lengths.push_back((int)neg1.size());
    114     testStr.push_back(neg2.c_str());
    115     lengths.push_back((int)neg2.size());
    116     testStr.push_back(neg3.c_str());
    117     lengths.push_back((int)neg3.size());
    118 
    119     // Just test we don't crash with garbage input
    120     ASSERT(GrSKSLPrettyPrint::PrettyPrint(testStr.begin(), lengths.begin(), 1,
    121                                           true).c_str() != nullptr);
    122 }
    123 
    124 #endif
    125