StructificationMutation implements Mutation { public static List<StructDefinitionType> randomStruct(int currentDepth, IRandom generator, IdGenerator idGenerator, ShadingLanguageVersion shadingLanguageVersion, GenerationParams generationParams) { final int numFields = generator.nextInt(generationParams.getMaxStructFields() - 1) + 1; List<StructDefinitionType> subStructs = new ArrayList<>(); List<String> fieldNames = new ArrayList<>(); List<Type> fieldTypes = new ArrayList<>(); for (int i = 0; i < numFields; i++) { fieldNames.add(Constants.STRUCTIFICATION_FIELD_PREFIX + i); if (currentDepth < generationParams.getMaxStructNestingDepth() && generator.nextBoolean()) { List<StructDefinitionType> newStructs = randomStruct(currentDepth + 1, generator, idGenerator, shadingLanguageVersion, generationParams); subStructs.addAll(newStructs); fieldTypes.add(newStructs.get(0).getStructNameType()); } else { while (true) { BasicType candidate = BasicType.allBasicTypes() .get(generator.nextInt(BasicType.allBasicTypes() .size())); if (SupportedTypes.supported(candidate, shadingLanguageVersion)) { fieldTypes.add(candidate); break; } } } } List<StructDefinitionType> result = new ArrayList<>(); result.add(new StructDefinitionType( new StructNameType(Constants.STRUCTIFICATION_STRUCT_PREFIX + idGenerator.freshId()), fieldNames, fieldTypes)); result.addAll(subStructs); return result; } StructificationMutation(DeclarationStmt declToTransform, BlockStmt block,
                                 TranslationUnit tu,
                                 IdGenerator idGenerator,
                                 IRandom generator,
                                 GenerationParams generationParams); static List<StructDefinitionType> randomStruct(int currentDepth, IRandom generator,
                                                 IdGenerator idGenerator,
                                                 ShadingLanguageVersion shadingLanguageVersion,
                                                 GenerationParams generationParams); @Override void apply();  }
@Test public void createRandomStruct() { CannedRandom generator = new CannedRandom( 2 , true , 1 , false , 0 , false , 1 , false , 0 , true , 0 , false , 0 ); List<StructDefinitionType> structs = StructificationMutation.randomStruct(0, generator, new IdGenerator(), ShadingLanguageVersion.GLSL_440, GenerationParams.normal(ShaderKind.FRAGMENT, true)); assertEquals(3, structs.size()); StructDefinitionType enclosingStruct = structs.get(0); assertEquals(enclosingStruct.getFieldName(0), "_f0"); assertEquals(enclosingStruct.getFieldName(1), "_f1"); assertEquals(enclosingStruct.getFieldName(2), "_f2"); assertTrue(enclosingStruct.getFieldType(0) instanceof StructNameType); assertEquals(enclosingStruct.getFieldType(0), structs.get(1).getStructNameType()); assertEquals(enclosingStruct.getFieldName(1), "_f1"); assertTrue(enclosingStruct.getFieldType(2) instanceof StructNameType); assertEquals(enclosingStruct.getFieldType(2), structs.get(2).getStructNameType()); assertEquals(enclosingStruct.getFieldType(1), BasicType.FLOAT); assertEquals(lookupDeclaration((StructNameType) enclosingStruct.getFieldType(0), structs) .getFieldType(0), BasicType.FLOAT); assertEquals(lookupDeclaration((StructNameType) enclosingStruct.getFieldType(0), structs) .getFieldType(1), BasicType.VEC2); assertEquals(lookupDeclaration((StructNameType) enclosingStruct.getFieldType(2), structs) .getFieldType(0), BasicType.FLOAT); assertTrue(generator.isExhausted()); }
@Test public void randomStruct() { for (int i = 0; i < 10; i++) { List<StructDefinitionType> structs = StructificationMutation.randomStruct(0, new RandomWrapper(0), new IdGenerator(), ShadingLanguageVersion.ESSL_100, GenerationParams.normal(ShaderKind.FRAGMENT, true)); checkDisjointSubStructs(structs.get(0), structs); } }