blob: 1a5152fa7576468de8d0b77c3f17f1075b10085c [file] [log] [blame]
joshualittbab82ed2014-08-08 09:41:42 -07001/*
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
halcanary96fcdcc2015-08-27 07:41:13 -07008#include "SkTypes.h"
9
joshualittbab82ed2014-08-08 09:41:42 -070010#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;}"
joshualitt43466a12015-02-13 17:18:27 -080018 "void main()");
19const SkString input2("{nowin a function;{indenting;{abit more;dreadedfor((;;)(;)((;;);)){"
20 "doingstuff"
joshualittbab82ed2014-08-08 09:41:42 -070021 ";for(;;;){and more stufff;mixed garbage\n\n\t\t\t\t\n/*using this"
joshualitt43466a12015-02-13 17:18:27 -080022 " 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;} "
joshualittbab82ed2014-08-08 09:41:42 -070028 "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"
joshualitt43466a12015-02-13 17:18:27 -080061 " 30\t\t\t\t\t\t//a commentbreaking in commentcontinuing the comment\n"
joshualittbab82ed2014-08-08 09:41:42 -070062 " 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
joshualitt43466a12015-02-13 17:18:27 -080086const SkString neg1("{;;{{{{;;;{{{{{{{{{{{");
87const SkString neg2("###\n##\n#####(((((((((((((unbalanced verything;;;");
88const SkString neg3("}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}"
joshualittbab82ed2014-08-08 09:41:42 -070089 ";;;;;;/////");
90
91DEF_TEST(GrGLSLPrettyPrint, r) {
joshualitt43466a12015-02-13 17:18:27 -080092 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);
joshualittbab82ed2014-08-08 09:41:42 -0700109 ASSERT(output1 == test);
110
joshualitt43466a12015-02-13 17:18:27 -0800111 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
joshualittbab82ed2014-08-08 09:41:42 -0700120 // Just test we don't crash with garbage input
joshualitt43466a12015-02-13 17:18:27 -0800121 ASSERT(GrGLSLPrettyPrint::PrettyPrintGLSL(testStr.begin(), lengths.begin(), 1,
halcanary96fcdcc2015-08-27 07:41:13 -0700122 true).c_str() != nullptr);
joshualittbab82ed2014-08-08 09:41:42 -0700123}
124
125#endif