MCPcopy Create free account
hub / github.com/csmith-project/csmith / rnd_mutate

Method rnd_mutate

src/ArrayVariable.cpp:342–401  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

340}
341
342ArrayVariable*
343ArrayVariable::rnd_mutate(void)
344{
345 assert(0 && "invalid call to rnd_mutate");
346 bool use_existing = rnd_flipcoin(20);
347 ERROR_GUARD(NULL);
348 size_t i;
349 if (use_existing) {
350 vector<Variable*> ok_vars;
351 for (i=0; i<parent->local_vars.size(); i++) {
352 if (is_variant(parent->local_vars[i])) {
353 ok_vars.push_back(parent->local_vars[i]);
354 }
355 }
356 Variable* v = VariableSelector::choose_ok_var(ok_vars);
357 ERROR_GUARD(NULL);
358 if (v) {
359 ArrayVariable* av = dynamic_cast<ArrayVariable*>(v);
360 return av;
361 }
362 }
363 vector<const Expression*> new_indices;
364 vector<bool> mutate_flags;
365 bool no_mutate = true;
366 for (i=0; i<get_dimension(); i++) {
367 bool mutate = rnd_flipcoin(10);
368 ERROR_GUARD(0);
369 mutate_flags.push_back(mutate);
370 if (mutate) {
371 no_mutate = false;
372 }
373 }
374 // if no mutation, return self
375 if (no_mutate) {
376 return this;
377 }
378 for (i=0; i<get_dimension(); i++) {
379 if (mutate_flags[i]) {
380 const Expression* e = indices[i];
381 assert(e->term_type == eVariable);
382 const ExpressionVariable* ev = dynamic_cast<const ExpressionVariable*>(e);
383 // create a mutated index from the original by adding an constant offset
384 FunctionInvocation* fi = new FunctionInvocationBinary(eAdd, 0);
385 fi->add_operand(new ExpressionVariable(*(ev->get_var())));
386 int offset = rnd_upto(sizes[i]);
387 if (offset == 0) offset = 1; // give offset 1 more chance
388 ERROR_GUARD(NULL);
389 ostringstream oss;
390 oss << offset;
391 fi->add_operand(new Constant(get_int_type(), oss.str()));
392 Expression* mutated_e = new ExpressionFuncall(*fi);
393 new_indices.push_back(mutated_e);
394 }
395 else {
396 new_indices.push_back(indices[i]->clone());
397 }
398 }
399 // if index of at least one dimension mutated, return the new variable

Callers

nothing calls this directly

Calls 7

rnd_flipcoinFunction · 0.85
rnd_uptoFunction · 0.85
get_int_typeFunction · 0.85
sizeMethod · 0.80
add_operandMethod · 0.80
get_varMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected