GlobalVariableDeclToExprReductionOpportunities extends ReductionOpportunitiesBase<GlobalVariableDeclToExprReductionOpportunity> { static List<GlobalVariableDeclToExprReductionOpportunity> findOpportunities( ShaderJob shaderJob, ReducerContext context) { return shaderJob.getShaders() .stream() .map(item -> findOpportunitiesForShader(item, context)) .reduce(Arrays.asList(), ListConcat::concatenate); } private  GlobalVariableDeclToExprReductionOpportunities(TranslationUnit tu,
                                                        ReducerContext context); @Override void visitVariablesDeclaration(VariablesDeclaration variablesDeclaration); @Override void visitFunctionDefinition(FunctionDefinition functionDefinition);  }
@Test public void testDoNotReplace() throws Exception { final String original = "int a = 1; void main() { }"; final TranslationUnit tu = ParseHelper.parse(original); final List<GlobalVariableDeclToExprReductionOpportunity> ops = GlobalVariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertTrue(ops.isEmpty()); }
@Test public void testZeroMethod() throws Exception { final String original = "int a = 1;"; final TranslationUnit tu = ParseHelper.parse(original); final List<GlobalVariableDeclToExprReductionOpportunity> ops = GlobalVariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertTrue(ops.isEmpty()); }
@Test public void testNoMainMethod() throws Exception { final String original = "int a = 1; void foo() { }"; final TranslationUnit tu = ParseHelper.parse(original); final List<GlobalVariableDeclToExprReductionOpportunity> ops = GlobalVariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertTrue(ops.isEmpty()); }
@Test public void testInsertAsFirstStatement() throws Exception { final String program = "" + "int a = 1;" + "void main() {" + " int b = a; " + "}"; final String expected = "" + "int a;" + "void main() {" + " a = 1;" + " int b = a;" + "}"; final TranslationUnit tu = ParseHelper.parse(program); final List<GlobalVariableDeclToExprReductionOpportunity> ops = GlobalVariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(1, ops.size()); ops.forEach(GlobalVariableDeclToExprReductionOpportunity::applyReductionImpl); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testMultipleDeclarations() throws Exception { final String program = "" + "int a = 1;" + "int b = foo();" + "int c;" + "void main() { }"; final String expected = "" + "int a;" + "int b;" + "int c;" + "void main() {" + " b = foo();" + " a = 1;" + "}"; final TranslationUnit tu = ParseHelper.parse(program); final List<GlobalVariableDeclToExprReductionOpportunity> ops = GlobalVariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(2, ops.size()); ops.forEach(GlobalVariableDeclToExprReductionOpportunity::applyReductionImpl); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testDoNotReplaceConst() throws Exception { final String original = "const int a = 1; void main() { }"; final TranslationUnit tu = ParseHelper.parse(original); final List<GlobalVariableDeclToExprReductionOpportunity> ops = GlobalVariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertTrue(ops.isEmpty()); }
@Test public void testMultipleLineDeclarationsOneLine() throws Exception { final String program = "" + "int b = 1, c, d = foo(), e, f = bar();" + "void main() { }"; final String expected = "" + "int b, c, d, e, f;" + "void main() {" + " f = bar();" + " d = foo();" + " b = 1;" + "}"; final TranslationUnit tu = ParseHelper.parse(program); final List<GlobalVariableDeclToExprReductionOpportunity> ops = GlobalVariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(3, ops.size()); ops.forEach(GlobalVariableDeclToExprReductionOpportunity::applyReductionImpl); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testAssignVariableIdentifier() throws Exception { final String program = "" + "int a = 1;" + "int b = a, c = b;" + "int d = c;" + "void main() { }"; final String expected = "" + "int a;" + "int b, c;" + "int d;" + "void main() {" + " d = c;" + " c = b;" + " b = a;" + " a = 1;" + "}"; final TranslationUnit tu = ParseHelper.parse(program); final List<GlobalVariableDeclToExprReductionOpportunity> ops = GlobalVariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(true, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertEquals(4, ops.size()); ops.forEach(GlobalVariableDeclToExprReductionOpportunity::applyReductionImpl); CompareAsts.assertEqualAsts(expected, tu); }
@Test public void testGlobalVariableDeclAfterMain() throws Exception { final String original = "void main() { } int a = 1;"; final TranslationUnit tu = ParseHelper.parse(original); final List<GlobalVariableDeclToExprReductionOpportunity> ops = GlobalVariableDeclToExprReductionOpportunities .findOpportunities(MakeShaderJobFromFragmentShader.make(tu), new ReducerContext(false, ShadingLanguageVersion.ESSL_100, new RandomWrapper(0), new IdGenerator())); assertTrue(ops.isEmpty()); }