joshualitt | bab82ed | 2014-08-08 09:41:42 -0700 | [diff] [blame] | 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 | |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 8 | #include "Test.h" |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 9 | |
joshualitt | bab82ed | 2014-08-08 09:41:42 -0700 | [diff] [blame] | 10 | #if SK_SUPPORT_GPU |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 11 | #include "GrSKSLPrettyPrint.h" |
joshualitt | bab82ed | 2014-08-08 09:41:42 -0700 | [diff] [blame] | 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;}" |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 17 | "void main()"); |
| 18 | const SkString input2("{nowin a function;{indenting;{abit more;dreadedfor((;;)(;)((;;);)){" |
| 19 | "doingstuff" |
joshualitt | bab82ed | 2014-08-08 09:41:42 -0700 | [diff] [blame] | 20 | ";for(;;;){and more stufff;mixed garbage\n\n\t\t\t\t\n/*using this" |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 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;} " |
joshualitt | bab82ed | 2014-08-08 09:41:42 -0700 | [diff] [blame] | 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 | |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 30 | const SkSL::String output1( |
joshualitt | bab82ed | 2014-08-08 09:41:42 -0700 | [diff] [blame] | 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" |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 60 | " 30\t\t\t\t\t\t//a commentbreaking in commentcontinuing the comment\n" |
joshualitt | bab82ed | 2014-08-08 09:41:42 -0700 | [diff] [blame] | 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 | |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 85 | const SkString neg1("{;;{{{{;;;{{{{{{{{{{{"); |
| 86 | const SkString neg2("###\n##\n#####(((((((((((((unbalanced verything;;;"); |
| 87 | const SkString neg3("}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}" |
joshualitt | bab82ed | 2014-08-08 09:41:42 -0700 | [diff] [blame] | 88 | ";;;;;;/////"); |
| 89 | |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 90 | DEF_TEST(GrSKSLPrettyPrint, r) { |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 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 | |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 106 | SkSL::String test = GrSKSLPrettyPrint::PrettyPrint(testStr.begin(), lengths.begin(), |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 107 | testStr.count(), true); |
joshualitt | bab82ed | 2014-08-08 09:41:42 -0700 | [diff] [blame] | 108 | ASSERT(output1 == test); |
| 109 | |
joshualitt | 43466a1 | 2015-02-13 17:18:27 -0800 | [diff] [blame] | 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 | |
joshualitt | bab82ed | 2014-08-08 09:41:42 -0700 | [diff] [blame] | 119 | // Just test we don't crash with garbage input |
Brian Salomon | e334c59 | 2017-05-15 11:00:58 -0400 | [diff] [blame] | 120 | ASSERT(GrSKSLPrettyPrint::PrettyPrint(testStr.begin(), lengths.begin(), 1, |
| 121 | true).c_str() != nullptr); |
joshualitt | bab82ed | 2014-08-08 09:41:42 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | #endif |