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