public async Task<CodeWithDifference> CreateDifferenceListing(CodeLanguage language, Mutant mutant, MutationResult mutationResult) { _log.Debug("CreateDifferenceListing in object: " + ToString() + GetHashCode()); try { var whiteCode = await VisualizeOriginalCode(language, mutant); var mutatedCode = await VisualizeMutatedCode(language, mutationResult); CodePair pair = new CodePair { OriginalCode = whiteCode, MutatedCode = mutatedCode }; return _differenceCreator.GetDiff(language, pair.OriginalCode, pair.MutatedCode); } catch (Exception e) { _log.Error(e); return new CodeWithDifference { Code = "Exception occurred while decompiling: " + e, LineChanges = Enumerable.Empty<LineChange>().ToList() }; } }
public async Task<string> VisualizeMutatedCode(CodeLanguage language, MutationResult mutationResult) { var result = Visualize(language, mutationResult.MethodMutated, mutationResult.MutatedModules); // _mutantsCache.Release(mutationResult); return result; }
public async Task<string> VisualizeMutatedCode(CodeLanguage language, MutationResult mutationResult) { var result = Visualize(language, mutationResult.MethodMutated, mutationResult.MutatedModules);//oryginalnie była tylko ta linijka i return if (mutationResult.AdditionalMethodsMutated != null && mutationResult.MethodMutated!=mutationResult.AdditionalMethodsMutated[0]) { result += Visualize(language, mutationResult.AdditionalMethodsMutated[0], mutationResult.MutatedModules); } // _mutantsCache.Release(mutationResult);*/ return result; }
public async void LoadCode(CodeLanguage selectedLanguage) { _viewModel.IsCodeLoading = true; _viewModel.ClearCode(); var mutant = _currentMutant; if (mutant != null) { MutationResult mutationResult = await _mutantsCache.GetMutatedModulesAsync(mutant); CodeWithDifference diff = await _codeVisualizer.CreateDifferenceListing(selectedLanguage, mutant, mutationResult); if (diff != null) { _viewModel.PresentCode(diff); _viewModel.IsCodeLoading = false; } } }
private async Task StoreToDb() { var mutationResult = new MutationResult { Key = _source.SHA256, Source = _source.FilePath.GithubPath(), Test = _source.TestClaz.FilePath.GithubPath(), NoOfTests = _source.NumberOfTests, DateCreated = DateTime.UtcNow, Mutation = new Mutation { Survived = _source.MutationScore.Survived, Killed = _source.MutationScore.Killed, }, Coverage = new CodeCoverage { Covered = _source.Coverage.LinesCovered, Uncovered = _source.Coverage.LinesNotCovered }, ExternalCoverage = _source.ExternalCoverage }; foreach (var score in _source.MutatorWiseMutationScores) { mutationResult.MutatorWiseMutations.Add(score.Mutator, new Mutation { Survived = score.MutationScore.Survived, Killed = score.MutationScore.Killed }); } if (_source.StoreToDb) { await _client.StoreInDatabaseAsync(mutationResult); } }
public void IntegrationTestMutation() { var cci = new CciModuleSource(TestProjects.MiscUtil); var original = new OriginalCodebase(cci.InList()); var type = cci.Modules.Single().Module.GetAllTypes().Single(t => t.Name.Value == "Adler32") as NamedTypeDefinition; var method = type.Methods.First(m => m.Name.Value == "ComputeChecksum"); var choices = new MutationSessionChoices { Filter = new MutationFilter( new List <TypeIdentifier>(), new MethodIdentifier(method).InList()), SelectedOperators = new LOR_LogicalOperatorReplacement().InList <IMutationOperator>(), }; var exec = new MutationExecutor(new OptionsModel(), choices, null); var container = new MutantsContainer(exec, original, new OptionsModel()); IList <AssemblyNode> assemblies = container.InitMutantsForOperators(ProgressCounter.Inactive()); var mutants = assemblies.Cast <CheckedNode>() .SelectManyRecursive(n => n.Children ?? new NotifyingCollection <CheckedNode>()) .OfType <Mutant>(); var diff = new CodeDifferenceCreator(); var vis = new CodeVisualizer(original, diff); foreach (var mutant in mutants) { var copy = new CciModuleSource(TestProjects.MiscUtil); MutationResult result = exec.ExecuteMutation(mutant, copy).Result; CodeWithDifference differenceListing = vis.CreateDifferenceListing(CodeLanguage.CSharp, mutant, result).Result; differenceListing.LineChanges.Count.ShouldEqual(2); } }
protected bool TryApply(MutationPacket mutationPacket, out Mutation mutation, out MutationResult result) { FragmentKey key = fragmentKeyFactory.FromBytes(mutationPacket.GetKey()); mutation = mutationFactory.FromBytes(mutationPacket.GetPayload()); result = mutation.Mutate(state, key, mutationPacket.GetRequester()); if (result.IsFailure()) { return(false); } if (result.IsSuccess()) { bool isUpdate = state.ContainsKey(key); state = state.CloneWith(key, result.GetFragment()); if (isUpdate) { SafeInvoker.Invoke(log, OnFragmentUpdatedCallback, result.GetFragment(), state, key, mutation); } else { SafeInvoker.Invoke(log, OnFragmentInsertedCallback, result.GetFragment(), state, key, mutation); } } else if (result.IsDelete()) { if (state.ContainsKey(key)) { state = state.CloneWithout(key); SafeInvoker.Invoke(log, OnFragmentDeletedCallback, state, key, mutation); } } SafeInvoker.Invoke(log, OnStateChangedCallback, state, key, mutation); return(true); }
public async Task <CodeWithDifference> CreateDifferenceListing(CodeLanguage language, Mutant mutant, MutationResult mutationResult) { _log.Debug("CreateDifferenceListing in object: " + ToString() + GetHashCode()); try { var whiteCode = await VisualizeOriginalCode(language, mutant); var mutatedCode = await VisualizeMutatedCode(language, mutationResult); CodePair pair = new CodePair { OriginalCode = whiteCode, MutatedCode = mutatedCode }; return(_differenceCreator.GetDiff(language, pair.OriginalCode, pair.MutatedCode)); } catch (Exception e) { _log.Error(e); return(new CodeWithDifference { Code = "Exception occurred while decompiling: " + e, LineChanges = Enumerable.Empty <LineChange>().ToList() }); } }
/// <summary> /// Applies the result of some mutation operation to this instance. /// </summary> /// <param name="result">The result.</param> private bool Apply(MutationResult result) { this.Root = result.Root; _count += result.CountAdjustment; return(result.CountAdjustment != 0); }
public void Test00() { const string code = @"using System; namespace Ns { public class Test { public int Method1(int a, int b) { return a + b; } } }"; var cci = new CciModuleSource(TestProjects.DsaPath); var cci2 = new CciModuleSource(TestProjects.DsaPath); var type = cci.Modules.Single().Module.GetAllTypes().Single(t => t.Name.Value == "Deque") as NamedTypeDefinition; var method = type.Methods.Single(m => m.Name.Value == "EnqueueFront"); var cci3 = MutationTestsHelper.CreateModuleFromCode(code); var choices = new MutationSessionChoices { Filter = new MutationFilter( new List <TypeIdentifier>(), new MethodIdentifier(method).InList()), //Filter = MutationFilter.AllowAll(), SelectedOperators = new AOR_ArithmeticOperatorReplacement().InList <IMutationOperator>(), }; // var type = cci.Modules.Single().Module.GetAllTypes().Single(t => t.Name.Value == "Test") as NamedTypeDefinition; // var method = type.Methods.Single(m => m.Name.Value == "Method1"); var exec = new MutationExecutor(null, choices, null); var container = new MutantsContainer(exec, new OriginalCodebase(cci.InList()), new OptionsModel()); IList <AssemblyNode> assemblies = container.InitMutantsForOperators(ProgressCounter.Inactive()); var mut = assemblies.Cast <CheckedNode>() .SelectManyRecursive(n => n.Children ?? new NotifyingCollection <CheckedNode>()) .OfType <Mutant>().ElementAt(4); MutationResult executeMutation = exec.ExecuteMutation(mut, cci2).Result; var c = new CodeDeepCopier(cci.Host); MethodDefinition methodDefinition = c.Copy(mut.MutationTarget.MethodRaw); var vis = new CodeVisualizer(null, null); var s = vis.Visualize(CodeLanguage.CSharp, cci2); var v = new MutantsCache.Viss(cci2.Host, methodDefinition); var modClean = v.Rewrite(cci2.Modules.Single().Module); cci2.ReplaceWith(modClean); var debug = new DebugOperatorCodeVisitor(); var debug2 = new DebugOperatorCodeVisitor(); new DebugCodeTraverser(debug).Traverse(cci.Modules.Single().Module); new DebugCodeTraverser(debug2).Traverse(cci2.Modules.Single().Module); File.WriteAllText(@"C:\PLIKI\tree1.txt", debug.ToString()); File.WriteAllText(@"C:\PLIKI\tree2.txt", debug2.ToString()); // Console.WriteLine(debug); // Console.WriteLine(debug2); // cci.ReplaceWith(executeMutation.MutatedModules.Modules.Single().Module); // var s2 = vis.Visualize(CodeLanguage.CSharp, cci2); // Console.WriteLine(s); // Console.WriteLine(s2); // var viss = new Viss(cci2.Host, sourceMethod); // IModule newMod = viss.Rewrite(executeMutation.MutatedModules.Modules.Single().Module); // cci2.ReplaceWith(executeMutation.MutatedModules.Modules.Single().Module); // MutationResult executeMutation2 = exec.ExecuteMutation(mut, cci2).Result; }
private async Task<MutationResult> CreateNew(Mutant mutant) { MutationResult result; if (mutant.MutationTarget == null || mutant.MutationTarget.ProcessingContext == null) { result = new MutationResult(mutant, new CciModuleSource(), null, null); } else { if(_options.ParsedParams.LegacyCreation) { _log.Debug("Cache#Mutant " + mutant.Id + ": Awaiting white cache."); var cci = await _whiteCache.GetWhiteModulesAsyncOld(); _log.Debug("Cache#Mutant " + mutant.Id + ": Awaiting mutation start."); result = await _mutationExecutor.ExecuteMutation(mutant, cci); _log.Debug("Cache#Mutant " + mutant.Id + ": Awaiting mutation finished."); } else { _log.Debug("Cache#Mutant " + mutant.Id + ": Awaiting white cache."); var cci = await _whiteCache.GetWhiteSourceAsync(mutant.MutationTarget.ProcessingContext.ModuleName); _log.Debug("Cache#Mutant " + mutant.Id + ": taken source: "+cci.Guid); _log.Debug("Cache#Mutant " + mutant.Id + ": Awaiting mutation start."); result = await _mutationExecutor.ExecuteMutation(mutant, cci); _log.Debug("Cache#Mutant " + mutant.Id + ": Awaiting mutation finished."); } if (!_disableCache) { _cache.Add(new CacheItem(mutant.Id, result), new CacheItemPolicy()); } } return result; }
public void Release(MutationResult mutationResult) { var cci = (CciModuleSource)mutationResult.MutatedModules; _whiteCache.ReturnToCache( cci.Modules.Single().Name, cci); // var tt = mutationResult.WhiteModules.Modules.Single().Module.GetAllTypes().Single(t => t.Name.Value == "Range"); //tt.ToString(); // var type = cci.Modules.Single().Module.GetAllTypes().Single(t => t.Name.Value == "Deque") as NamedTypeDefinition; // var method = type.Methods.Single(m => m.Name.Value == "EnqueueFront"); // // var whiteCci = _choices.WhiteSource; // var c = new CodeDeepCopier(whiteCci.Host); // c. // MethodDefinition methodDefinition = c.Copy(mutationResult.Mutant.MutationTarget.MethodRaw); // var v = new Viss(mutationResult.MutatedModules.Host, methodDefinition); // var modClean = v.Rewrite(mutationResult.MutatedModules.Modules.Single().Module); // // var cci = (CciModuleSource) mutationResult.MutatedModules; // cci.ReplaceWith(modClean); // _whiteCache.ReturnToCache( // cci.Modules.Single().Name, // cci); }
public virtual async Task <MutationResult> Mutate(INeatNetwork network) { double val = await Helpers.Random.NextUDoubleAsync(); MutationResult result = MutationResult.success; bool Between(double value, double lower, double upper) { return(value <= upper && value >= lower); } bool addNode; bool addConnection; if (AddConnectionChance > AddNodeChance) { // 0 1 // |- Add Node -|---- Add Connection ----|-------------- Roll Fail -------------| addNode = Between(val, 0, AddNodeChance); addConnection = Between(val, AddNodeChance, AddNodeChance + AddConnectionChance); } else { // 0 1 // |- Add Connection -|---- Add Node ----|-------------- Roll Fail -------------| addNode = Between(val, 0, AddConnectionChance); addConnection = Between(val, AddConnectionChance, AddNodeChance + AddConnectionChance); } if (addNode) { var status = await AddNode(network); // cast the add node result to a mutation result result = status switch { AddNodeResult.success => MutationResult.success, AddNodeResult.error => MutationResult.error, AddNodeResult.noEligibleConnections => MutationResult.noValidMutations, AddNodeResult.alreadyExists => MutationResult.noValidMutations, _ => MutationResult.error, }; } else if (addConnection) { var status = await AddConnection(network); // cast the add connection result to a mutation result result = status switch { AddConnectionResult.success => MutationResult.success, AddConnectionResult.error => MutationResult.error, AddConnectionResult.noEligibleNodes => MutationResult.noValidMutations, AddConnectionResult.alreadyExists => MutationResult.noValidMutations, _ => MutationResult.error, }; } // mutate weights await MutateWeights(network); return(result); }
public MutationResult Create(V2TestEntity newEntity, bool dryRun) { newEntity.Spec.StringOrInteger = "42"; return(MutationResult.Modified(newEntity)); }
public MutationResult Create(V1DemoEntity newEntity, bool dryRun) { newEntity.Spec.Username = "******"; return(MutationResult.Modified(newEntity)); }
protected ActionResult <MutationResult> GetResult(MutationResult result) => GetResult(result, result.Status);