* Randomly determine the parameters for a counting loop: initial value, limit * value, increment value, test operator, and increment operator. */
| 68 | * value, increment value, test operator, and increment operator. |
| 69 | */ |
| 70 | static void |
| 71 | make_random_loop_control(int &init, int &limit, int &incr, |
| 72 | eBinaryOps &test_op, |
| 73 | eAssignOps &incr_op, |
| 74 | bool iv_signed) |
| 75 | { |
| 76 | // We don't have to put error guards here because we are trying |
| 77 | // to get pure random numbers, and in this case, we cannot get |
| 78 | // errors |
| 79 | init = pure_rnd_flipcoin(50) ? 0 : (pure_rnd_upto(60)-30); |
| 80 | limit = iv_signed ? (pure_rnd_upto(60) - 30) : (pure_rnd_upto(60) + 1); |
| 81 | |
| 82 | eBinaryOps t_ops[] = { eCmpLt, eCmpLe, eCmpGt, eCmpGe, eCmpEq, eCmpNe }; |
| 83 | test_op = t_ops[pure_rnd_upto(sizeof(t_ops)/sizeof(*t_ops))]; |
| 84 | ERROR_RETURN(); |
| 85 | |
| 86 | if (pure_rnd_flipcoin(50)) { |
| 87 | ERROR_RETURN(); |
| 88 | // Do `+=' or `-=' by an increment between 0 and 9 inclusive. |
| 89 | // make sure the limit can be reached without wrap-around |
| 90 | incr = pure_rnd_upto(10); |
| 91 | // avoid an infinite loop due to inexact division, e.g, |
| 92 | // init = 0, limit = 3, step = 2, test_op = eCmpNe |
| 93 | if (test_op == eCmpNe && incr > 1) |
| 94 | limit = (limit - init) / incr * incr + init; |
| 95 | incr_op = (limit >= init) ? eAddAssign : eSubAssign; |
| 96 | if (incr == 0) incr = 1; |
| 97 | |
| 98 | // A rare case that could cause wrap around: distance between init |
| 99 | // and limit is multiple of incr, and the incr goes to wrong direction. |
| 100 | // For example: init = 8, limit = 1, incr = -7. test_op is >= |
| 101 | if (CGOptions::fast_execution() && (limit - init) % incr == 0 && |
| 102 | (test_op == eCmpGe || test_op == eCmpLe)) { |
| 103 | limit = (incr_op == eAddAssign) ? limit + 1 : limit - 1; |
| 104 | } |
| 105 | } else { |
| 106 | ERROR_RETURN(); |
| 107 | // Do `++' or `--', pre- or post-. |
| 108 | // make sure the limit can be reached without wrap-around |
| 109 | if ((limit < init) || ((limit == init) && (test_op == eCmpGe))) { |
| 110 | incr_op = pure_rnd_flipcoin(50) ? ePreDecr : ePostDecr; |
| 111 | } else { |
| 112 | incr_op = pure_rnd_flipcoin(50) ? ePreIncr : ePostIncr; |
| 113 | } |
| 114 | if (((incr_op == ePreIncr) && !CGOptions::pre_incr_operator()) |
| 115 | || ((incr_op == ePostIncr) && !CGOptions::post_incr_operator())) { |
| 116 | |
| 117 | incr_op = eAddAssign; |
| 118 | } |
| 119 | if (((incr_op == ePreDecr) && !CGOptions::pre_decr_operator()) |
| 120 | || ((incr_op == ePostDecr) && !CGOptions::post_decr_operator())) { |
| 121 | |
| 122 | incr_op = eSubAssign; |
| 123 | } |
| 124 | incr = 1; |
| 125 | } |
| 126 | ERROR_RETURN(); |
| 127 | } |
no test coverage detected