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