blob: c0de6c02a819c77787a8841e5dda163ceb428d0b [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
Brian Salomone334c592017-05-15 11:00:58 -04008#include "Test.h"
halcanary96fcdcc2015-08-27 07:41:13 -07009
joshualittbab82ed2014-08-08 09:41:42 -070010#if SK_SUPPORT_GPU
Brian Salomone334c592017-05-15 11:00:58 -040011#include "GrSKSLPrettyPrint.h"
joshualittbab82ed2014-08-08 09:41:42 -070012
13#define ASSERT(x) REPORTER_ASSERT(r, x)
14
15const 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;}"
joshualitt43466a12015-02-13 17:18:27 -080017 "void main()");
18const SkString input2("{nowin a function;{indenting;{abit more;dreadedfor((;;)(;)((;;);)){"
19 "doingstuff"
joshualittbab82ed2014-08-08 09:41:42 -070020 ";for(;;;){and more stufff;mixed garbage\n\n\t\t\t\t\n/*using this"
joshualitt43466a12015-02-13 17:18:27 -080021 " comment\n is");
22const SkString input3(" dangerous\ndo so at your own\n risk*/;\n\n\t\t\t\n"
23 "//a comment");
24const SkString input4("breaking in comment");
25const SkString input5("continuing the comment");
26const SkString input6("\n}}a; little ; love; for ; leading; spaces;} "
joshualittbab82ed2014-08-08 09:41:42 -070027 "an struct = { int a; int b; };"
28 "int[5] arr = int[5](1,2,3,4,5);} some code at the bottom; for(;;) {} }");
29
Brian Osman93ba0a42017-08-14 14:48:10 -040030const SkSL::String output1(
joshualittbab82ed2014-08-08 09:41:42 -070031 " 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"
joshualitt43466a12015-02-13 17:18:27 -080060 " 30\t\t\t\t\t\t//a commentbreaking in commentcontinuing the comment\n"
joshualittbab82ed2014-08-08 09:41:42 -070061 " 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
joshualitt43466a12015-02-13 17:18:27 -080085const SkString neg1("{;;{{{{;;;{{{{{{{{{{{");
86const SkString neg2("###\n##\n#####(((((((((((((unbalanced verything;;;");
87const SkString neg3("}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}"
joshualittbab82ed2014-08-08 09:41:42 -070088 ";;;;;;/////");
89
Brian Salomone334c592017-05-15 11:00:58 -040090DEF_TEST(GrSKSLPrettyPrint, r) {
joshualitt43466a12015-02-13 17:18:27 -080091 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
Brian Osman93ba0a42017-08-14 14:48:10 -0400106 SkSL::String test = GrSKSLPrettyPrint::PrettyPrint(testStr.begin(), lengths.begin(),
Brian Salomone334c592017-05-15 11:00:58 -0400107 testStr.count(), true);
joshualittbab82ed2014-08-08 09:41:42 -0700108 ASSERT(output1 == test);
109
joshualitt43466a12015-02-13 17:18:27 -0800110 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
joshualittbab82ed2014-08-08 09:41:42 -0700119 // Just test we don't crash with garbage input
Brian Salomone334c592017-05-15 11:00:58 -0400120 ASSERT(GrSKSLPrettyPrint::PrettyPrint(testStr.begin(), lengths.begin(), 1,
121 true).c_str() != nullptr);
joshualittbab82ed2014-08-08 09:41:42 -0700122}
123
124#endif