| 1 |
import pytest |
| 2 |
|
| 3 |
from copy import copy |
| 4 |
import sys |
| 5 |
import operator |
| 6 |
|
| 7 |
from rpython.jit.metainterp.optimizeopt.intutils import (IntBound, |
| 8 |
next_pow2_m1, msbonly, MININT, MAXINT, lowest_set_bit_only, |
| 9 |
leading_zeros_mask, saturating_mul, same_sign) |
| 10 |
from rpython.jit.metainterp.optimizeopt.info import (INFO_NONNULL, |
| 11 |
INFO_UNKNOWN, INFO_NULL) |
| 12 |
from rpython.rlib.rarithmetic import LONG_BIT, ovfcheck, r_uint, intmask |
| 13 |
from rpython.jit.metainterp.optimize import InvalidLoop |
| 14 |
|
| 15 |
from hypothesis import given, strategies, example, seed, assume |
| 16 |
|
| 17 |
special_values_set = ( |
| 18 |
range(100) + range(-1, -100, -1) + |
| 19 |
[2 ** i for i in range(1, LONG_BIT)] + |
| 20 |
[-2 ** i for i in range(1, LONG_BIT)] + |
| 21 |
[2 ** i - 1 for i in range(1, LONG_BIT)] + |
| 22 |
[-2 ** i - 1 for i in range(1, LONG_BIT)] + |
| 23 |
[2 ** i + 1 for i in range(1, LONG_BIT)] + |
| 24 |
[-2 ** i + 1 for i in range(1, LONG_BIT)] + |
| 25 |
[sys.maxint, -sys.maxint-1] |
| 26 |
) |
| 27 |
|
| 28 |
special_values = strategies.sampled_from( |
| 29 |
[int(v) for v in special_values_set if type(int(v)) is int]) |
| 30 |
|
| 31 |
pos_special_values_set = ( |
| 32 |
range(0, 100) + |
| 33 |
[sys.maxint] + |
| 34 |
[2 ** i for i in range(1, LONG_BIT)] + |
| 35 |
[2 ** i - 1 for i in range(1, LONG_BIT)] + |
| 36 |
[2 ** i + 1 for i in range(1, LONG_BIT)]) |
| 37 |
|
| 38 |
pos_special_values = strategies.sampled_from( |
| 39 |
[int(v) for v in pos_special_values_set if type(int(v)) is int]) |
| 40 |
|
| 41 |
pos_relatively_small_values = strategies.sampled_from( |
| 42 |
[int(v) for v in range(0, 128)]) |
| 43 |
|
| 44 |
ints = strategies.builds( |
| 45 |
int, |
| 46 |
special_values | strategies.integers( |
| 47 |
min_value=int(-sys.maxint-1), max_value=sys.maxint)) |
| 48 |
|
| 49 |
ints_or_none = strategies.none() | ints |
| 50 |
|
| 51 |
pos_ints = strategies.builds( |
| 52 |
int, |
| 53 |
pos_special_values | strategies.integers( |
| 54 |
min_value=int(0), max_value=sys.maxint)) |
| 55 |
|
| 56 |
def bound_eq(a, b): |
| 57 |
return a.__dict__ == b.__dict__ |
| 58 |
|
| 59 |
def bound(a, b): |
| 60 |
if a is None and b is None: |
| 61 |
return IntBound.unbounded() |
| 62 |
elif a is None: |
| 63 |
return IntBound(upper=b) |
| 64 |
elif b is None: |
| 65 |
return IntBound(lower=a) |
| 66 |
else: |
| 67 |
return IntBound(a, b) |
| 68 |
|
| 69 |
def const(a): |
| 70 |
return IntBound.from_constant(a) |
| 71 |
|
| 72 |
|
| 73 |
def build_bound_with_contained_number(a, b, c): |
| 74 |
a, b, c = sorted([a, b, c]) |
| 75 |
r = bound(a, c) |
| 76 |
assert r.contains(b) |
| 77 |
return r, b |
| 78 |
|
| 79 |
def build_some_bits_known(a, b): |
| 80 |
return knownbits(a&~b, b), a |
| 81 |
|
| 82 |
def build_some_bits_known_bounded(res_value, tmask, data): |
| 83 |
|
| 84 |
tmask = r_uint(tmask) |
| 85 |
b = IntBound.from_knownbits(tvalue=r_uint(res_value), tmask=tmask, do_unmask=True) |
| 86 |
assert b.contains(res_value) |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
space_at_bottom = res_value - b.lower |
| 91 |
if space_at_bottom: |
| 92 |
shrink_by = data.draw(strategies.integers(0, space_at_bottom - 1)) |
| 93 |
b.make_ge_const(int(b.lower + shrink_by)) |
| 94 |
assert b.contains(res_value) |
| 95 |
space_at_top = b.upper - res_value |
| 96 |
if space_at_top: |
| 97 |
shrink_by = data.draw(strategies.integers(0, space_at_top - 1)) |
| 98 |
b.make_le_const(int(b.upper - shrink_by)) |
| 99 |
assert b.contains(res_value) |
| 100 |
repr(b) |
| 101 |
return b, res_value |
| 102 |
|
| 103 |
def build_two_ints_tuple(a, b): |
| 104 |
return (a, b) |
| 105 |
|
| 106 |
def build_valid_mask_value_pair(a, b): |
| 107 |
return (a & ~b, b) |
| 108 |
|
| 109 |
unbounded = strategies.builds( |
| 110 |
lambda x: (bound(None, None), int(x)), |
| 111 |
ints |
| 112 |
) |
| 113 |
|
| 114 |
lower_bounded = strategies.builds( |
| 115 |
lambda x, y: (bound(min(x, y), None), max(x, y)), |
| 116 |
ints, ints |
| 117 |
) |
| 118 |
|
| 119 |
upper_bounded = strategies.builds( |
| 120 |
lambda x, y: (bound(None, max(x, y)), min(x, y)), |
| 121 |
ints, ints |
| 122 |
) |
| 123 |
|
| 124 |
bounded = strategies.builds( |
| 125 |
build_bound_with_contained_number, |
| 126 |
ints, ints, ints |
| 127 |
) |
| 128 |
|
| 129 |
constant = strategies.builds( |
| 130 |
lambda x: (const(x), x), |
| 131 |
ints |
| 132 |
) |
| 133 |
|
| 134 |
some_bits_known = strategies.builds( |
| 135 |
build_some_bits_known, |
| 136 |
ints, ints |
| 137 |
) |
| 138 |
|
| 139 |
some_bits_known_bounded = strategies.builds( |
| 140 |
build_some_bits_known_bounded, |
| 141 |
ints, ints, strategies.data(), |
| 142 |
) |
| 143 |
|
| 144 |
random_ints_tuple = strategies.builds( |
| 145 |
build_two_ints_tuple, |
| 146 |
ints, ints |
| 147 |
) |
| 148 |
|
| 149 |
random_valid_mask_value_pair = strategies.builds( |
| 150 |
build_valid_mask_value_pair, |
| 151 |
ints, ints |
| 152 |
) |
| 153 |
|
| 154 |
maybe_valid_value_mask_pair = strategies.one_of( |
| 155 |
random_ints_tuple, random_valid_mask_value_pair |
| 156 |
) |
| 157 |
|
| 158 |
|
| 159 |
knownbits_and_bound_with_contained_number = strategies.one_of( |
| 160 |
|
| 161 |
constant, |
| 162 |
lower_bounded, |
| 163 |
upper_bounded, |
| 164 |
bounded, |
| 165 |
some_bits_known, |
| 166 |
some_bits_known_bounded, |
| 167 |
unbounded |
| 168 |
) |
| 169 |
|
| 170 |
shift_amount = strategies.builds( |
| 171 |
lambda x: (const(x), x), |
| 172 |
strategies.integers(min_value=0, max_value=LONG_BIT) |
| 173 |
) | strategies.builds( |
| 174 |
build_bound_with_contained_number, |
| 175 |
*(strategies.integers(min_value=0, max_value=LONG_BIT), ) * 3 |
| 176 |
) |
| 177 |
|
| 178 |
|
| 179 |
nbr = range(-5, 6) |
| 180 |
nnbr = list(set(range(-9, 10)) - set(nbr)) |
| 181 |
|
| 182 |
def some_bounds(): |
| 183 |
brd = [None] + range(-2, 3) |
| 184 |
for lower in brd: |
| 185 |
for upper in brd: |
| 186 |
if lower is not None and upper is not None and lower > upper: |
| 187 |
continue |
| 188 |
yield (lower, upper, bound(lower, upper)) |
| 189 |
|
| 190 |
def some_bits(): |
| 191 |
tvals = nbr |
| 192 |
tmsks = nbr |
| 193 |
for tval in tvals: |
| 194 |
for tmsk in tmsks: |
| 195 |
yield (tval, tmsk, knownbits(tval, tmsk, True)) |
| 196 |
|
| 197 |
def test_known(): |
| 198 |
for lower, upper, b in some_bounds(): |
| 199 |
inside = [] |
| 200 |
border = [] |
| 201 |
for n in nbr: |
| 202 |
if (lower is None or n >= lower) and \ |
| 203 |
(upper is None or n <= upper): |
| 204 |
if n == lower or n ==upper: |
| 205 |
border.append(n) |
| 206 |
else: |
| 207 |
inside.append(n) |
| 208 |
|
| 209 |
for n in nbr: |
| 210 |
c = const(n) |
| 211 |
if n in inside: |
| 212 |
assert b.contains(n) |
| 213 |
assert not b.known_lt(c) |
| 214 |
assert not b.known_gt(c) |
| 215 |
assert not b.known_le(c) |
| 216 |
assert not b.known_ge(c) |
| 217 |
assert not b.known_lt_const(n) |
| 218 |
assert not b.known_gt_const(n) |
| 219 |
assert not b.known_le_const(n) |
| 220 |
assert not b.known_ge_const(n) |
| 221 |
elif n in border: |
| 222 |
assert b.contains(n) |
| 223 |
if n == upper: |
| 224 |
assert b.known_le(const(upper)) |
| 225 |
assert b.known_le_const(upper) |
| 226 |
else: |
| 227 |
assert b.known_ge_const(lower) |
| 228 |
assert b.known_ge(const(lower)) |
| 229 |
else: |
| 230 |
assert not b.contains(n) |
| 231 |
some = (border + inside)[0] |
| 232 |
if n < some: |
| 233 |
assert b.known_gt(c) |
| 234 |
else: |
| 235 |
assert b.known_lt(c) |
| 236 |
|
| 237 |
|
| 238 |
def test_make(): |
| 239 |
for _, _, b1 in some_bounds(): |
| 240 |
for _, _, b2 in some_bounds(): |
| 241 |
lt = IntBound.unbounded() |
| 242 |
lt.make_lt(b1) |
| 243 |
lt.make_lt(b2) |
| 244 |
for n in nbr: |
| 245 |
c = const(n) |
| 246 |
if b1.known_le(c) or b2.known_le(c): |
| 247 |
assert lt.known_lt(c) |
| 248 |
else: |
| 249 |
assert not lt.known_lt(c) |
| 250 |
assert not lt.known_gt(c) |
| 251 |
assert not lt.known_ge(c) |
| 252 |
|
| 253 |
gt = IntBound.unbounded() |
| 254 |
gt.make_gt(b1) |
| 255 |
try: |
| 256 |
gt.make_gt(b2) |
| 257 |
except InvalidLoop: |
| 258 |
pass |
| 259 |
else: |
| 260 |
for n in nbr: |
| 261 |
c = const(n) |
| 262 |
if b1.known_ge(c) or b2.known_ge(c): |
| 263 |
assert gt.known_gt(c) |
| 264 |
else: |
| 265 |
assert not gt.known_gt(c) |
| 266 |
assert not gt.known_lt(c) |
| 267 |
assert not gt.known_le(c) |
| 268 |
|
| 269 |
le = IntBound.unbounded() |
| 270 |
le.make_le(b1) |
| 271 |
try: |
| 272 |
le.make_le(b2) |
| 273 |
except InvalidLoop: |
| 274 |
pass |
| 275 |
else: |
| 276 |
for n in nbr: |
| 277 |
c = const(n) |
| 278 |
if b1.known_le(c) or b2.known_le(c): |
| 279 |
assert le.known_le(c) |
| 280 |
else: |
| 281 |
assert not le.known_le(c) |
| 282 |
assert not le.known_gt(c) |
| 283 |
assert not le.known_ge(c) |
| 284 |
|
| 285 |
|
| 286 |
ge = IntBound.unbounded() |
| 287 |
ge.make_ge(b1) |
| 288 |
try: |
| 289 |
ge.make_ge(b2) |
| 290 |
except InvalidLoop: |
| 291 |
pass |
| 292 |
else: |
| 293 |
for n in nbr: |
| 294 |
c = const(n) |
| 295 |
if b1.known_ge(c) or b2.known_ge(c): |
| 296 |
assert ge.known_ge(c) |
| 297 |
else: |
| 298 |
assert not ge.known_ge(c) |
| 299 |
assert not ge.known_lt(c) |
| 300 |
assert not ge.known_le(c) |
| 301 |
|
| 302 |
gl = IntBound.unbounded() |
| 303 |
gl.make_ge(b1) |
| 304 |
try: |
| 305 |
gl.make_le(b2) |
| 306 |
except InvalidLoop: |
| 307 |
pass |
| 308 |
else: |
| 309 |
for n in nbr: |
| 310 |
c = const(n) |
| 311 |
if b1.known_ge(c): |
| 312 |
assert gl.known_ge(c) |
| 313 |
else: |
| 314 |
assert not gl.known_ge(c) |
| 315 |
assert not gl.known_gt(c) |
| 316 |
if b2.known_le(c): |
| 317 |
assert gl.known_le(c) |
| 318 |
else: |
| 319 |
assert not gl.known_le(c) |
| 320 |
assert not gl.known_lt(c) |
| 321 |
|
| 322 |
def test_make_invalid_loop_cases(): |
| 323 |
b1 = IntBound.unbounded() |
| 324 |
b2 = IntBound.from_constant(MININT) |
| 325 |
with pytest.raises(InvalidLoop): |
| 326 |
b1.make_lt(b2) |
| 327 |
|
| 328 |
b1 = IntBound.unbounded() |
| 329 |
b2 = IntBound.from_constant(MAXINT) |
| 330 |
with pytest.raises(InvalidLoop): |
| 331 |
b1.make_gt(b2) |
| 332 |
|
| 333 |
b1 = IntBound.nonnegative() |
| 334 |
with pytest.raises(InvalidLoop): |
| 335 |
b1.make_eq_const(-1) |
| 336 |
|
| 337 |
def test_make_ne(): |
| 338 |
ge = IntBound.unbounded() |
| 339 |
res = ge.make_ne_const(MININT) |
| 340 |
assert res |
| 341 |
res = ge.make_ne_const(MININT) |
| 342 |
assert not res |
| 343 |
assert not ge.contains(MININT) |
| 344 |
assert ge.contains(MININT + 1) |
| 345 |
assert ge.contains(MAXINT) |
| 346 |
|
| 347 |
def test_intersect(): |
| 348 |
for _, _, b1 in some_bounds(): |
| 349 |
for _, _, b2 in some_bounds(): |
| 350 |
if b1.known_gt(b2) or b1.known_lt(b2): |
| 351 |
|
| 352 |
continue |
| 353 |
b = copy(b1) |
| 354 |
b.intersect(b2) |
| 355 |
for n in nbr: |
| 356 |
if b1.contains(n) and b2.contains(n): |
| 357 |
assert b.contains(n) |
| 358 |
else: |
| 359 |
assert not b.contains(n) |
| 360 |
|
| 361 |
def test_intersect_bug(): |
| 362 |
b1 = bound(17, 17) |
| 363 |
b2 = bound(1, 1) |
| 364 |
with pytest.raises(InvalidLoop): |
| 365 |
b1.intersect(b2) |
| 366 |
|
| 367 |
def test_intersect_contradiction_range_knownbits(): |
| 368 |
b1 = IntBound(-1, 0) |
| 369 |
b2 = IntBound.from_knownbits(r_uint(0b11100), r_uint(-0b1100000)) |
| 370 |
with pytest.raises(InvalidLoop): |
| 371 |
b1.intersect(b2) |
| 372 |
|
| 373 |
def test_intersect_contradiction_range_knownbits2(): |
| 374 |
|
| 375 |
b1 = IntBound(0, 256) |
| 376 |
b2 = IntBound.from_knownbits(r_uint(0b100000001), r_uint(-0b100000010)) |
| 377 |
b = b1.clone() |
| 378 |
with pytest.raises(InvalidLoop): |
| 379 |
b.intersect(b2) |
| 380 |
|
| 381 |
b1 = IntBound(1, 3) |
| 382 |
b2 = IntBound.from_knownbits(r_uint(0b0), r_uint(-0b101100)) |
| 383 |
b = b1.clone() |
| 384 |
with pytest.raises(InvalidLoop): |
| 385 |
b.intersect(b2) |
| 386 |
|
| 387 |
def test_add_bound(): |
| 388 |
for _, _, b1 in some_bounds(): |
| 389 |
for _, _, b2 in some_bounds(): |
| 390 |
b3 = b1.add_bound(b2) |
| 391 |
for n1 in nbr: |
| 392 |
for n2 in nbr: |
| 393 |
if b1.contains(n1) and b2.contains(n2): |
| 394 |
assert b3.contains(n1 + n2) |
| 395 |
|
| 396 |
a=bound(2, 4).add_bound(bound(1, 2)) |
| 397 |
assert not a.contains(2) |
| 398 |
assert not a.contains(7) |
| 399 |
|
| 400 |
def test_add_bound_bug(): |
| 401 |
b = bound(MININT, MAXINT) |
| 402 |
bval = MAXINT |
| 403 |
assert b.contains(bval) |
| 404 |
r = b.add_bound(IntBound.from_constant(1)) |
| 405 |
rval = intmask(r_uint(bval)+r_uint(1)) |
| 406 |
assert r.contains(rval) |
| 407 |
|
| 408 |
def test_mul_bound(): |
| 409 |
for _, _, b1 in some_bounds(): |
| 410 |
for _, _, b2 in some_bounds(): |
| 411 |
b3 = b1.mul_bound(b2) |
| 412 |
for n1 in nbr: |
| 413 |
for n2 in nbr: |
| 414 |
if b1.contains(n1) and b2.contains(n2): |
| 415 |
assert b3.contains(n1 * n2) |
| 416 |
|
| 417 |
a=bound(2, 4).mul_bound(bound(1, 2)) |
| 418 |
assert not a.contains(1) |
| 419 |
assert not a.contains(9) |
| 420 |
|
| 421 |
a=bound(-3, 2).mul_bound(bound(1, 2)) |
| 422 |
assert not a.contains(-7) |
| 423 |
assert not a.contains(5) |
| 424 |
assert a.contains(-6) |
| 425 |
assert a.contains(4) |
| 426 |
|
| 427 |
a=bound(-3, 2).mul_bound(bound(-1, -1)) |
| 428 |
for i in range(-2,4): |
| 429 |
assert a.contains(i) |
| 430 |
assert not a.contains(4) |
| 431 |
assert not a.contains(-3) |
| 432 |
|
| 433 |
def test_square_bound_no_overflow(): |
| 434 |
b = bound(10, 20) |
| 435 |
b2 = b.square_bound_no_overflow() |
| 436 |
assert bound_eq(b2, bound(100, 400)) |
| 437 |
b = bound(-20, -10) |
| 438 |
b2 = b.square_bound_no_overflow() |
| 439 |
assert bound_eq(b2, bound(100, 400)) |
| 440 |
b = bound(-20, 10) |
| 441 |
b2 = b.square_bound_no_overflow() |
| 442 |
assert bound_eq(b2, bound(0, 400)) |
| 443 |
b = bound(-20, MAXINT) |
| 444 |
b2 = b.square_bound_no_overflow() |
| 445 |
assert bound_eq(b2, bound(0, MAXINT)) |
| 446 |
b = bound(MININT, -10) |
| 447 |
b2 = b.square_bound_no_overflow() |
| 448 |
assert bound_eq(b2, bound(100, MAXINT)) |
| 449 |
|
| 450 |
def test_shift_bound(): |
| 451 |
for _, _, b1 in some_bounds(): |
| 452 |
for _, _, b2 in some_bounds(): |
| 453 |
bleft = b1.lshift_bound(b2) |
| 454 |
bright = b1.rshift_bound(b2) |
| 455 |
for n1 in nbr: |
| 456 |
for n2 in range(10): |
| 457 |
if b1.contains(n1) and b2.contains(n2): |
| 458 |
assert bleft.contains(n1 << n2) |
| 459 |
assert bright.contains(n1 >> n2) |
| 460 |
|
| 461 |
def test_shift_overflow(): |
| 462 |
b10 = IntBound(0, 10) |
| 463 |
b100 = IntBound(0, 100) |
| 464 |
bmax = IntBound(0, sys.maxint/2) |
| 465 |
assert b10.lshift_bound(b100).upper == MAXINT |
| 466 |
assert bmax.lshift_bound(b10).upper == MAXINT |
| 467 |
assert b10.lshift_bound(b10).upper == 10 << 10 |
| 468 |
for b in (b10, b100, bmax, IntBound.from_constant(0)): |
| 469 |
for shift_count_bound in (IntBound(7, LONG_BIT), IntBound(-7, 7)): |
| 470 |
assert b.rshift_bound(shift_count_bound).upper == MAXINT |
| 471 |
|
| 472 |
def test_div_bound(): |
| 473 |
for _, _, b1 in some_bounds(): |
| 474 |
for _, _, b2 in some_bounds(): |
| 475 |
b3 = b1.py_div_bound(b2) |
| 476 |
for n1 in nbr: |
| 477 |
for n2 in nbr: |
| 478 |
if b1.contains(n1) and b2.contains(n2): |
| 479 |
if n2 != 0: |
| 480 |
assert b3.contains(n1 / n2) |
| 481 |
|
| 482 |
a=bound(2, 4).py_div_bound(bound(1, 2)) |
| 483 |
assert not a.contains(0) |
| 484 |
assert not a.contains(5) |
| 485 |
|
| 486 |
a=bound(-3, 2).py_div_bound(bound(1, 2)) |
| 487 |
assert not a.contains(-4) |
| 488 |
assert not a.contains(3) |
| 489 |
assert a.contains(-3) |
| 490 |
assert a.contains(0) |
| 491 |
|
| 492 |
def test_mod_bound(): |
| 493 |
for _, _, b1 in some_bounds(): |
| 494 |
for _, _, b2 in some_bounds(): |
| 495 |
b3 = b1.mod_bound(b2) |
| 496 |
for n1 in nbr: |
| 497 |
for n2 in nbr: |
| 498 |
if b1.contains(n1) and b2.contains(n2): |
| 499 |
if n2 != 0: |
| 500 |
assert b3.contains(n1 % n2) |
| 501 |
|
| 502 |
def test_sub_bound(): |
| 503 |
for _, _, b1 in some_bounds(): |
| 504 |
for _, _, b2 in some_bounds(): |
| 505 |
b3 = b1.sub_bound(b2) |
| 506 |
for n1 in nbr: |
| 507 |
for n2 in nbr: |
| 508 |
if b1.contains(n1) and b2.contains(n2): |
| 509 |
assert b3.contains(n1 - n2) |
| 510 |
|
| 511 |
a=bound(2, 4).sub_bound(bound(1, 2)) |
| 512 |
assert not a.contains(-1) |
| 513 |
assert not a.contains(4) |
| 514 |
|
| 515 |
a = IntBound.from_constant(0).sub_bound(bound(0, None)) |
| 516 |
assert a.lower == -MAXINT |
| 517 |
assert a.upper == 0 |
| 518 |
|
| 519 |
a = IntBound.from_constant(0).sub_bound(bound(None, 0)) |
| 520 |
assert a.lower == MININT |
| 521 |
assert a.upper == MAXINT |
| 522 |
|
| 523 |
|
| 524 |
def test_sub_bound_bug(): |
| 525 |
b = bound(MININT, MAXINT) |
| 526 |
bval = MININT |
| 527 |
assert b.contains(bval) |
| 528 |
r = b.sub_bound(IntBound.from_constant(1)) |
| 529 |
rval = intmask(r_uint(bval)-r_uint(1)) |
| 530 |
assert r.contains(rval) |
| 531 |
|
| 532 |
def test_and_bound(): |
| 533 |
for _, _, b1 in some_bounds(): |
| 534 |
for _, _, b2 in some_bounds(): |
| 535 |
b3 = b1.and_bound(b2) |
| 536 |
for n1 in nbr: |
| 537 |
for n2 in nbr: |
| 538 |
if b1.contains(n1) and b2.contains(n2): |
| 539 |
assert b3.contains(n1 & n2) |
| 540 |
|
| 541 |
def test_and_bound_example(): |
| 542 |
b1 = IntBound(0, 16) |
| 543 |
b2 = IntBound.unbounded() |
| 544 |
b3 = b1.and_bound(b2) |
| 545 |
assert bound_eq(b3, b1) |
| 546 |
|
| 547 |
def test_or_bound(): |
| 548 |
for _, _, b1 in some_bounds(): |
| 549 |
for _, _, b2 in some_bounds(): |
| 550 |
b3 = b1.or_bound(b2) |
| 551 |
for n1 in nbr: |
| 552 |
for n2 in nbr: |
| 553 |
if b1.contains(n1) and b2.contains(n2): |
| 554 |
assert b3.contains(n1 | n2) |
| 555 |
|
| 556 |
def test_xor_bound(): |
| 557 |
for _, _, b1 in some_bounds(): |
| 558 |
for _, _, b2 in some_bounds(): |
| 559 |
b3 = b1.xor_bound(b2) |
| 560 |
for n1 in nbr: |
| 561 |
for n2 in nbr: |
| 562 |
if b1.contains(n1) and b2.contains(n2): |
| 563 |
assert b3.contains(n1 ^ n2) |
| 564 |
|
| 565 |
|
| 566 |
def test_next_pow2_m1(): |
| 567 |
assert next_pow2_m1(r_uint(0)) == r_uint(0) |
| 568 |
assert next_pow2_m1(r_uint(1)) == r_uint(1) |
| 569 |
assert next_pow2_m1(r_uint(7)) == r_uint(7) |
| 570 |
assert next_pow2_m1(r_uint(256)) == r_uint(511) |
| 571 |
assert next_pow2_m1(r_uint(255)) == r_uint(255) |
| 572 |
assert next_pow2_m1(r_uint(80)) == r_uint(127) |
| 573 |
assert next_pow2_m1(r_uint((1 << 32) - 5)) == r_uint((1 << 32) - 1) |
| 574 |
assert next_pow2_m1(r_uint((1 << 64) - 1)) == r_uint((1 << 64) - 1) |
| 575 |
|
| 576 |
def test_leading_zeros_mask(): |
| 577 |
assert leading_zeros_mask(r_uint(0)) == r_uint(-1) |
| 578 |
assert leading_zeros_mask(r_uint(-1)) == r_uint(0) |
| 579 |
assert leading_zeros_mask(r_uint(0b100)) == ~r_uint(0b111) |
| 580 |
assert leading_zeros_mask(r_uint(MAXINT)) == ~r_uint(MAXINT) |
| 581 |
|
| 582 |
def test_shrink_bug(): |
| 583 |
lower = 1 |
| 584 |
upper = MAXINT - 1 |
| 585 |
assert r_uint(lower) ^ r_uint(upper) == r_uint(MAXINT) |
| 586 |
b = IntBound(lower, upper, do_shrinking=False) |
| 587 |
b._shrink_knownbits_by_bounds() |
| 588 |
|
| 589 |
assert b.tmask == r_uint(MAXINT) |
| 590 |
assert b.tvalue == r_uint(0) |
| 591 |
|
| 592 |
def test_invert_bound(): |
| 593 |
for _, _, b1 in some_bounds(): |
| 594 |
b2 = b1.invert_bound() |
| 595 |
for n1 in nbr: |
| 596 |
if b1.contains(n1): |
| 597 |
assert b2.contains(~n1) |
| 598 |
|
| 599 |
def test_neg_bound(): |
| 600 |
for _, _, b1 in some_bounds(): |
| 601 |
b2 = b1.neg_bound() |
| 602 |
for n1 in nbr: |
| 603 |
if b1.contains(n1): |
| 604 |
assert b2.contains(-n1) |
| 605 |
|
| 606 |
def test_widen(): |
| 607 |
for _, _, b1 in some_bounds(): |
| 608 |
b2 = b1.widen() |
| 609 |
b3 = b2.clone() |
| 610 |
b3.intersect(b1) |
| 611 |
bound_eq(b3, b1) |
| 612 |
b = bound(MININT + 1, MAXINT).widen() |
| 613 |
assert bound_eq(b, bound(None, None)) |
| 614 |
b = bound(MININT, MAXINT - 1).widen() |
| 615 |
assert bound_eq(b, bound(None, None)) |
| 616 |
b = bound(-10, 10) |
| 617 |
b1 = b.widen() |
| 618 |
assert bound_eq(b, b1) |
| 619 |
|
| 620 |
|
| 621 |
@given(knownbits_and_bound_with_contained_number, |
| 622 |
knownbits_and_bound_with_contained_number, |
| 623 |
strategies.sampled_from(("lt", "le", "gt", "ge"))) |
| 624 |
def test_make_random(t1, t2, name): |
| 625 |
def d(b): |
| 626 |
return b.lower, b.upper, b.tvalue, b.tmask |
| 627 |
b1, n1 = t1 |
| 628 |
b2, n2 = t2 |
| 629 |
|
| 630 |
meth = getattr(IntBound, "make_" + name) |
| 631 |
b = b1.clone() |
| 632 |
try: |
| 633 |
changed = meth(b, b2) |
| 634 |
except InvalidLoop: |
| 635 |
assert not getattr(operator, name)(n1, n2) |
| 636 |
return |
| 637 |
data = d(b) |
| 638 |
assert not meth(b, b2) |
| 639 |
assert data == d(b) |
| 640 |
assert changed == (d(b1) != d(b)) |
| 641 |
if not b.contains(n1): |
| 642 |
assert not getattr(operator, name)(n1, n2) |
| 643 |
|
| 644 |
def test_make_unsigned_less_example(): |
| 645 |
b1 = IntBound(0, 10) |
| 646 |
b2 = IntBound.unbounded() |
| 647 |
res = b2.make_unsigned_le(b1) |
| 648 |
assert res is True |
| 649 |
assert b2.known_nonnegative() |
| 650 |
assert b2.known_le_const(10) |
| 651 |
|
| 652 |
b1 = IntBound(0, 10) |
| 653 |
b2 = IntBound.unbounded() |
| 654 |
res = b2.make_unsigned_lt(b1) |
| 655 |
assert res is True |
| 656 |
assert b2.known_nonnegative() |
| 657 |
assert b2.known_lt_const(10) |
| 658 |
|
| 659 |
b1 = IntBound(0, 0) |
| 660 |
b2 = IntBound.unbounded() |
| 661 |
with pytest.raises(InvalidLoop): |
| 662 |
b2.make_unsigned_lt(b1) |
| 663 |
|
| 664 |
def test_make_unsigned_greater_example(): |
| 665 |
|
| 666 |
b1 = IntBound.unbounded() |
| 667 |
b2 = IntBound(lower=-100, upper=-2) |
| 668 |
res = b1.make_unsigned_ge(b2) |
| 669 |
assert res is True |
| 670 |
assert b1.known_lt_const(0) |
| 671 |
assert b1.known_ge_const(-100) |
| 672 |
|
| 673 |
b1 = IntBound.unbounded() |
| 674 |
b2 = IntBound(lower=-100, upper=-2) |
| 675 |
res = b1.make_unsigned_gt(b2) |
| 676 |
assert res is True |
| 677 |
assert b1.known_lt_const(0) |
| 678 |
assert b1.known_gt_const(-100) |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
b1 = IntBound.nonnegative() |
| 683 |
b2 = IntBound(2, 100) |
| 684 |
res = b1.make_unsigned_ge(b2) |
| 685 |
assert res is True |
| 686 |
assert b1.known_ge_const(2) |
| 687 |
|
| 688 |
b1 = IntBound.nonnegative() |
| 689 |
b2 = IntBound(lower=5) |
| 690 |
res = b1.make_unsigned_gt(b2) |
| 691 |
assert res is True |
| 692 |
assert b1.known_gt_const(5) |
| 693 |
|
| 694 |
@example(t1=(IntBound(MININT, 0), 0), t2=(IntBound(-4, -3), -3), name='gt') |
| 695 |
@given(knownbits_and_bound_with_contained_number, |
| 696 |
knownbits_and_bound_with_contained_number, |
| 697 |
strategies.sampled_from(("lt", "le", "gt", "ge"))) |
| 698 |
def test_make_unsigned_random(t1, t2, name): |
| 699 |
def d(b): |
| 700 |
return b.lower, b.upper, b.tvalue, b.tmask |
| 701 |
b1, n1 = t1 |
| 702 |
b2, n2 = t2 |
| 703 |
|
| 704 |
meth = getattr(IntBound, "make_unsigned_" + name) |
| 705 |
b = b1.clone() |
| 706 |
try: |
| 707 |
changed = meth(b, b2) |
| 708 |
except InvalidLoop: |
| 709 |
|
| 710 |
|
| 711 |
assert not getattr(operator, name)(r_uint(n1), r_uint(n2)) |
| 712 |
return |
| 713 |
data = d(b) |
| 714 |
assert not meth(b, b2) |
| 715 |
assert data == d(b) |
| 716 |
assert changed == (d(b1) != d(b)) |
| 717 |
if not b.contains(n1): |
| 718 |
assert changed |
| 719 |
|
| 720 |
|
| 721 |
assert not getattr(operator, name)(r_uint(n1), r_uint(n2)) |
| 722 |
|
| 723 |
|
| 724 |
@given(knownbits_and_bound_with_contained_number) |
| 725 |
def test_add_zero_is_zero_random(t1): |
| 726 |
b1, n1 = t1 |
| 727 |
|
| 728 |
b1viaadd0 = b1.add_bound(IntBound.from_constant(0)) |
| 729 |
assert bound_eq(b1, b1viaadd0) |
| 730 |
|
| 731 |
|
| 732 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 733 |
def test_add_random(t1, t2): |
| 734 |
b1, n1 = t1 |
| 735 |
b2, n2 = t2 |
| 736 |
b3 = b1.add_bound(b2) |
| 737 |
|
| 738 |
assert b3.contains(intmask(r_uint(n1) + r_uint(n2))) |
| 739 |
|
| 740 |
b3noovf = b1.add_bound_no_overflow(b2) |
| 741 |
try: |
| 742 |
r = ovfcheck(n1 + n2) |
| 743 |
except OverflowError: |
| 744 |
assert not b1.add_bound_cannot_overflow(b2) |
| 745 |
else: |
| 746 |
assert b3.contains(r) |
| 747 |
assert b3noovf.contains(r) |
| 748 |
|
| 749 |
|
| 750 |
@given(knownbits_and_bound_with_contained_number) |
| 751 |
def test_mul2_random(t1): |
| 752 |
b1, n1 = t1 |
| 753 |
b2 = b1.mul2_bound_no_overflow() |
| 754 |
try: |
| 755 |
r = ovfcheck(n1 + n1) |
| 756 |
except OverflowError: |
| 757 |
assert not b1.add_bound_cannot_overflow(b1) |
| 758 |
else: |
| 759 |
assert b2.contains(r) |
| 760 |
|
| 761 |
assert b2.and_bound(IntBound.from_constant(1)).get_constant_int() == 0 |
| 762 |
|
| 763 |
|
| 764 |
@given(knownbits_and_bound_with_contained_number) |
| 765 |
def test_sub_zero_is_zero_random(t1): |
| 766 |
b1, n1 = t1 |
| 767 |
|
| 768 |
b1viasub0 = b1.sub_bound(IntBound.from_constant(0)) |
| 769 |
assert bound_eq(b1, b1viasub0) |
| 770 |
|
| 771 |
@example((bound(-100, None), -99), (bound(None, -100), -100)) |
| 772 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 773 |
def test_sub_random(t1, t2): |
| 774 |
b1, n1 = t1 |
| 775 |
b2, n2 = t2 |
| 776 |
print b1, n1 |
| 777 |
print b2, n2 |
| 778 |
b3 = b1.sub_bound(b2) |
| 779 |
|
| 780 |
assert b3.contains(intmask(r_uint(n1) - r_uint(n2))) |
| 781 |
|
| 782 |
b3noovf = b1.sub_bound_no_overflow(b2) |
| 783 |
try: |
| 784 |
r = ovfcheck(n1 - n2) |
| 785 |
except OverflowError: |
| 786 |
assert not b1.sub_bound_cannot_overflow(b2) |
| 787 |
else: |
| 788 |
assert b3.contains(r) |
| 789 |
assert b3noovf.contains(r) |
| 790 |
|
| 791 |
|
| 792 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 793 |
def test_mul_random(t1, t2): |
| 794 |
b1, n1 = t1 |
| 795 |
b2, n2 = t2 |
| 796 |
b3 = b1.mul_bound(b2) |
| 797 |
try: |
| 798 |
r = ovfcheck(n1 * n2) |
| 799 |
except OverflowError: |
| 800 |
assert not b1.mul_bound_cannot_overflow(b2) |
| 801 |
else: |
| 802 |
assert b3.contains(r) |
| 803 |
|
| 804 |
@given(knownbits_and_bound_with_contained_number) |
| 805 |
def test_square_random(t1): |
| 806 |
b1, n1 = t1 |
| 807 |
b2 = b1.square_bound_no_overflow() |
| 808 |
try: |
| 809 |
r = ovfcheck(n1 * n1) |
| 810 |
except OverflowError: |
| 811 |
assert not b1.mul_bound_cannot_overflow(b2) |
| 812 |
else: |
| 813 |
assert b2.contains(r) |
| 814 |
if not b2.is_unbounded(): |
| 815 |
assert b2.known_ge_const(0) |
| 816 |
|
| 817 |
@given(ints, ints) |
| 818 |
def test_same_sign_random(a, b): |
| 819 |
assert same_sign(a, b) == ((a >= 0) == (b >= 0)) |
| 820 |
|
| 821 |
@given(ints, ints) |
| 822 |
def test_saturating_mul(a, b): |
| 823 |
res = a * b |
| 824 |
satres = saturating_mul(a, b) |
| 825 |
if res < MININT: |
| 826 |
assert satres == MININT |
| 827 |
elif res > MAXINT: |
| 828 |
assert satres == MAXINT |
| 829 |
else: |
| 830 |
assert satres == res |
| 831 |
|
| 832 |
|
| 833 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 834 |
def test_div_random(t1, t2): |
| 835 |
b1, n1 = t1 |
| 836 |
b2, n2 = t2 |
| 837 |
b3 = b1.py_div_bound(b2) |
| 838 |
if n1 == -sys.maxint-1 and n2 == -1: |
| 839 |
return |
| 840 |
if n2 != 0: |
| 841 |
assert b3.contains(n1 / n2) |
| 842 |
|
| 843 |
def test_mod_bound_example(): |
| 844 |
b1 = IntBound() |
| 845 |
b2 = IntBound(-20, 10) |
| 846 |
r = b1.mod_bound(b2) |
| 847 |
assert r.known_gt_const(-20) |
| 848 |
assert r.known_lt_const(10) |
| 849 |
|
| 850 |
b1 = IntBound() |
| 851 |
b2 = IntBound(upper=10) |
| 852 |
r = b1.mod_bound(b2) |
| 853 |
assert r.known_gt_const(MININT) |
| 854 |
assert r.known_lt_const(10) |
| 855 |
|
| 856 |
b1 = IntBound() |
| 857 |
b2 = IntBound(lower=10) |
| 858 |
r = b1.mod_bound(b2) |
| 859 |
assert r.known_ge_const(0) |
| 860 |
assert r.known_lt_const(MAXINT) |
| 861 |
|
| 862 |
b1 = IntBound() |
| 863 |
b2 = IntBound(upper=-10) |
| 864 |
r = b1.mod_bound(b2) |
| 865 |
assert r.known_le_const(0) |
| 866 |
assert r.known_gt_const(MININT) |
| 867 |
|
| 868 |
b1 = IntBound() |
| 869 |
b2 = IntBound(lower=-10) |
| 870 |
r = b1.mod_bound(b2) |
| 871 |
assert r.known_gt_const(-10) |
| 872 |
assert r.known_lt_const(MAXINT) |
| 873 |
|
| 874 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 875 |
def test_mod_bound_random(t1, t2): |
| 876 |
b1, n1 = t1 |
| 877 |
b2, n2 = t2 |
| 878 |
b3 = b1.mod_bound(b2) |
| 879 |
if n1 == -sys.maxint-1 and n2 == -1: |
| 880 |
return |
| 881 |
if n2 != 0: |
| 882 |
assert b3.contains(n1 % n2) |
| 883 |
|
| 884 |
@given(knownbits_and_bound_with_contained_number, shift_amount) |
| 885 |
def test_lshift_random(t1, t2): |
| 886 |
b1, n1 = t1 |
| 887 |
b2, n2 = t2 |
| 888 |
b3 = b1.lshift_bound(b2) |
| 889 |
try: |
| 890 |
r = ovfcheck(n1 << n2) |
| 891 |
except OverflowError: |
| 892 |
assert not b1.lshift_bound_cannot_overflow(b2) |
| 893 |
else: |
| 894 |
b3.contains(r) |
| 895 |
assert b3.contains(intmask(r_uint(n1) << r_uint(n2))) |
| 896 |
|
| 897 |
@given(knownbits_and_bound_with_contained_number, pos_relatively_small_values) |
| 898 |
def test_lshift_const_random(t1, t2): |
| 899 |
b1, n1 = t1 |
| 900 |
b2 = IntBound.from_constant(t2) |
| 901 |
r = b1.lshift_bound(b2) |
| 902 |
|
| 903 |
assert r.contains(intmask(r_uint(n1) << r_uint(t2))) |
| 904 |
|
| 905 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 906 |
def test_and_random(t1, t2): |
| 907 |
b1, n1 = t1 |
| 908 |
b2, n2 = t2 |
| 909 |
b3 = b1.and_bound(b2) |
| 910 |
r = n1 & n2 |
| 911 |
assert b3.contains(r) |
| 912 |
|
| 913 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 914 |
def test_or_random(t1, t2): |
| 915 |
b1, n1 = t1 |
| 916 |
b2, n2 = t2 |
| 917 |
b3 = b1.or_bound(b2) |
| 918 |
r = n1 | n2 |
| 919 |
assert b3.contains(r) |
| 920 |
|
| 921 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 922 |
def test_xor_random(t1, t2): |
| 923 |
b1, n1 = t1 |
| 924 |
b2, n2 = t2 |
| 925 |
b3 = b1.xor_bound(b2) |
| 926 |
r = n1 ^ n2 |
| 927 |
assert b3.contains(r) |
| 928 |
|
| 929 |
@given(knownbits_and_bound_with_contained_number) |
| 930 |
def test_invert_random(t1): |
| 931 |
b1, n1 = t1 |
| 932 |
b2 = b1.invert_bound() |
| 933 |
assert b2.contains(~n1) |
| 934 |
|
| 935 |
@given(knownbits_and_bound_with_contained_number) |
| 936 |
@example((IntBound.from_constant(MININT), MININT)) |
| 937 |
@example((IntBound(upper=-100), MININT)) |
| 938 |
@example((IntBound(MININT, MININT+9), MININT)) |
| 939 |
def test_neg_random(t1): |
| 940 |
b1, n1 = t1 |
| 941 |
b2 = b1.neg_bound() |
| 942 |
if (n1 != MININT): |
| 943 |
assert b2.contains(intmask(-n1)) |
| 944 |
|
| 945 |
|
| 946 |
b2.contains(intmask(-r_uint(n1))) |
| 947 |
|
| 948 |
|
| 949 |
if b1.contains(MININT): |
| 950 |
assert b2.contains(MININT) |
| 951 |
|
| 952 |
|
| 953 |
b2viasub = IntBound.from_constant(0).sub_bound(b1) |
| 954 |
assert bound_eq(b2, b2viasub) |
| 955 |
|
| 956 |
@given(ints) |
| 957 |
def test_neg_const_random(t1): |
| 958 |
b1 = IntBound.from_constant(t1) |
| 959 |
r = b1.neg_bound() |
| 960 |
if t1 != -sys.maxint-1: |
| 961 |
assert r.is_constant() |
| 962 |
assert r.known_eq_const(-t1) |
| 963 |
|
| 964 |
|
| 965 |
@given(knownbits_and_bound_with_contained_number, strategies.data()) |
| 966 |
def test_are_knownbits_implied(t, data): |
| 967 |
b, _ = t |
| 968 |
if b._are_knownbits_implied(): |
| 969 |
n2 = data.draw(strategies.integers(b.lower, b.upper)) |
| 970 |
assert b.contains(n2) |
| 971 |
|
| 972 |
|
| 973 |
@given(knownbits_and_bound_with_contained_number) |
| 974 |
def test_widen_then_intersect_random(t): |
| 975 |
b, n = t |
| 976 |
b1 = b.widen() |
| 977 |
assert b1._are_knownbits_implied() |
| 978 |
assert b1.contains(n) |
| 979 |
b2 = b1.clone() |
| 980 |
b2.intersect(b) |
| 981 |
assert bound_eq(b2, b) |
| 982 |
|
| 983 |
@given(knownbits_and_bound_with_contained_number, ints, ints) |
| 984 |
def test_is_within_range_random(t, x, y): |
| 985 |
b, n = t |
| 986 |
x, y = sorted([x, y]) |
| 987 |
if b.is_within_range(x, y): |
| 988 |
assert x <= n <= y |
| 989 |
|
| 990 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 991 |
def test_known_lt_gt_le_ge_random(t1, t2): |
| 992 |
b1, n1 = t1 |
| 993 |
b2, n2 = t2 |
| 994 |
if b1.known_lt(b2): |
| 995 |
assert n1 < n2 |
| 996 |
assert b1.known_lt_const(n2) |
| 997 |
assert b2.known_gt(b1) |
| 998 |
assert b2.known_gt_const(n1) |
| 999 |
if b1.known_gt(b2): |
| 1000 |
assert n1 > n2 |
| 1001 |
assert b1.known_gt_const(n2) |
| 1002 |
assert b2.known_lt(b1) |
| 1003 |
assert b2.known_lt_const(n1) |
| 1004 |
if b1.known_le(b2): |
| 1005 |
assert n1 <= n2 |
| 1006 |
assert b1.known_le_const(n2) |
| 1007 |
assert b2.known_ge(b1) |
| 1008 |
assert b2.known_ge_const(n1) |
| 1009 |
if b1.known_ge(b2): |
| 1010 |
assert n1 >= n2 |
| 1011 |
assert b1.known_ge_const(n2) |
| 1012 |
assert b2.known_le(b1) |
| 1013 |
assert b2.known_le_const(n1) |
| 1014 |
|
| 1015 |
def test_known_lt_gt_le_ge_unsigned_example(): |
| 1016 |
b1 = IntBound.from_constant(10) |
| 1017 |
b2 = IntBound.from_constant(100) |
| 1018 |
assert b1.known_unsigned_lt(b2) |
| 1019 |
|
| 1020 |
b1 = IntBound.from_constant(10) |
| 1021 |
b2 = IntBound.from_constant(10) |
| 1022 |
assert b1.known_unsigned_le(b2) |
| 1023 |
|
| 1024 |
b1 = IntBound(0, 10) |
| 1025 |
b2 = IntBound(128, 255) |
| 1026 |
assert b1.known_unsigned_le(b2) |
| 1027 |
|
| 1028 |
b1 = IntBound(-510, -101) |
| 1029 |
b2 = IntBound(-100, -10) |
| 1030 |
assert b1.known_unsigned_le(b2) |
| 1031 |
|
| 1032 |
zero = IntBound.from_constant(0) |
| 1033 |
unknown = IntBound.unbounded() |
| 1034 |
assert zero.known_unsigned_le(unknown) |
| 1035 |
|
| 1036 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 1037 |
def test_known_lt_gt_le_ge_unsigned_random(t1, t2): |
| 1038 |
b1, n1 = t1 |
| 1039 |
n1 = r_uint(n1) |
| 1040 |
b2, n2 = t2 |
| 1041 |
n2 = r_uint(n2) |
| 1042 |
if b1.known_unsigned_lt(b2): |
| 1043 |
assert n1 < n2 |
| 1044 |
assert b2.known_unsigned_gt(b1) |
| 1045 |
if b1.known_unsigned_gt(b2): |
| 1046 |
assert n1 > n2 |
| 1047 |
assert b2.known_unsigned_lt(b1) |
| 1048 |
if b1.known_unsigned_le(b2): |
| 1049 |
assert n1 <= n2 |
| 1050 |
assert b2.known_unsigned_ge(b1) |
| 1051 |
if b1.known_unsigned_ge(b2): |
| 1052 |
assert n1 >= n2 |
| 1053 |
assert b2.known_unsigned_le(b1) |
| 1054 |
|
| 1055 |
def test_known_ne_example(): |
| 1056 |
b1 = knownbits(0b000010, |
| 1057 |
0b111100) |
| 1058 |
b2 = knownbits(0b000001, |
| 1059 |
0b111100) |
| 1060 |
assert b1.known_ne(b2) |
| 1061 |
|
| 1062 |
b1 = IntBound(lower=0, upper=10) |
| 1063 |
b2 = IntBound(lower=5, upper=10) |
| 1064 |
assert not b1.known_ne(b2) |
| 1065 |
|
| 1066 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 1067 |
def test_known_ne_random(t1, t2): |
| 1068 |
b1, n1 = t1 |
| 1069 |
b2, n2 = t2 |
| 1070 |
known_ne = b1.known_ne(b2) |
| 1071 |
if known_ne: |
| 1072 |
assert not b1.contains(n2) |
| 1073 |
assert not b2.contains(n1) |
| 1074 |
|
| 1075 |
@example((IntBound(-1, 0), 0), |
| 1076 |
(IntBound.from_knownbits(r_uint(0b11100), r_uint(-0b1100000)), -100)) |
| 1077 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 1078 |
def test_known_ne_compatible_intersect_random(t1, t2): |
| 1079 |
|
| 1080 |
b1, _ = t1 |
| 1081 |
b2, _ = t2 |
| 1082 |
known_ne = b1.known_ne(b2) |
| 1083 |
try: |
| 1084 |
b1.intersect(b2) |
| 1085 |
except InvalidLoop: |
| 1086 |
assert known_ne |
| 1087 |
else: |
| 1088 |
assert not known_ne |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
def test_lowest_set_bit_only(): |
| 1093 |
n1 = r_uint(0b10001) |
| 1094 |
r1 = lowest_set_bit_only(n1) |
| 1095 |
assert r1 == r_uint(1) |
| 1096 |
n2 = r_uint(0b00100) |
| 1097 |
r2 = lowest_set_bit_only(n2) |
| 1098 |
assert r2 == r_uint(4) |
| 1099 |
n3 = r_uint(-1) |
| 1100 |
r3 = lowest_set_bit_only(n3) |
| 1101 |
assert r3 == r_uint(1) |
| 1102 |
n4 = r_uint(0) |
| 1103 |
r4 = lowest_set_bit_only(n4) |
| 1104 |
assert r4 == r_uint(0) |
| 1105 |
|
| 1106 |
def test_knownbits_intconst_examples(): |
| 1107 |
b1 = IntBound.from_constant(0b010010) |
| 1108 |
assert b1.is_constant() |
| 1109 |
assert b1.get_constant_int() == 0b010010 |
| 1110 |
assert b1.known_eq_const(0b010010) |
| 1111 |
b2 = IntBound.from_constant(0b1) |
| 1112 |
assert b2.is_constant() |
| 1113 |
assert b2.get_constant_int() == 0b1 |
| 1114 |
assert b2.known_eq_const(0b1) |
| 1115 |
b3 = IntBound.from_constant(0b0) |
| 1116 |
assert b3.is_constant() |
| 1117 |
assert b3.get_constant_int() == 0b0 |
| 1118 |
assert b3.known_eq_const(0b0) |
| 1119 |
|
| 1120 |
|
| 1121 |
def test_knownbits_minmax_nobounds_examples(): |
| 1122 |
|
| 1123 |
b1 = IntBound.from_constant(42) |
| 1124 |
assert b1._get_minimum_signed() == 42 |
| 1125 |
assert b1._get_maximum_signed() == 42 |
| 1126 |
|
| 1127 |
b2 = knownbits(0b0110010, |
| 1128 |
0b0001001) |
| 1129 |
assert b2._get_minimum_signed() == 0b0110010 |
| 1130 |
assert not b2.contains(b2._get_minimum_signed() - 1) |
| 1131 |
assert b2._get_maximum_signed() == 0b0111011 |
| 1132 |
assert not b2.contains(b2._get_maximum_signed() + 1) |
| 1133 |
|
| 1134 |
b3 = knownbits(~0b0110010, |
| 1135 |
0b0010000) |
| 1136 |
assert b3._get_minimum_signed() == ~0b0110010 |
| 1137 |
assert not b3.contains(b3._get_minimum_signed() - 1) |
| 1138 |
assert b3._get_maximum_signed() == ~0b0100010 |
| 1139 |
assert not b3.contains(b3._get_maximum_signed() + 1) |
| 1140 |
|
| 1141 |
def test_knownbits_minmax_bounds_examples(): |
| 1142 |
|
| 1143 |
b1 = IntBound(lower=0, |
| 1144 |
tvalue=r_uint(5), tmask=r_uint(-8)) |
| 1145 |
assert b1._get_minimum_signed() == 5 |
| 1146 |
assert b1._get_maximum_signed() == intmask((r_uint(5) | r_uint(-8)) & ~MININT) |
| 1147 |
|
| 1148 |
b2 = IntBound(upper=0, |
| 1149 |
tvalue=r_uint(5), tmask=r_uint(-8)) |
| 1150 |
assert b2._get_minimum_signed() == intmask(r_uint(5) | MININT) |
| 1151 |
assert b2._get_maximum_signed() == -3 |
| 1152 |
|
| 1153 |
def test_knownbits_const_strings_examples(): |
| 1154 |
b1 = IntBound.from_constant(0b010010) |
| 1155 |
assert check_knownbits_string(b1, "00010010", '0') |
| 1156 |
b2 = IntBound.from_constant(0b1) |
| 1157 |
assert check_knownbits_string(b2, "001", '0') |
| 1158 |
b3 = IntBound.from_constant(0b0) |
| 1159 |
assert check_knownbits_string(b3, "0", '0') |
| 1160 |
b4 = IntBound.from_constant(-1) |
| 1161 |
assert check_knownbits_string(b4, "1", '1') |
| 1162 |
|
| 1163 |
def test_knownbits_unknowns_strings_examples(): |
| 1164 |
b1 = knownbits(0b010010, |
| 1165 |
0b001100) |
| 1166 |
assert check_knownbits_string(b1, "01??10", '0') |
| 1167 |
b2 = knownbits( 0b1010, |
| 1168 |
~0b1011) |
| 1169 |
assert check_knownbits_string(b2, "1?10") |
| 1170 |
|
| 1171 |
def test_knownbits_or_and_known_example(): |
| 1172 |
b1 = IntBound.unbounded() |
| 1173 |
b2 = b1.or_bound(IntBound.from_constant(1)) |
| 1174 |
assert check_knownbits_string(b2, "1") |
| 1175 |
b3 = b2.and_bound(IntBound.from_constant(1)) |
| 1176 |
assert b3.is_constant() |
| 1177 |
assert b3.get_constant_int() == 1 |
| 1178 |
assert b3.known_eq_const(1) |
| 1179 |
|
| 1180 |
def test_knownbits_or_and_unknown_example(): |
| 1181 |
b1 = IntBound.unbounded() |
| 1182 |
assert not b1.is_constant() |
| 1183 |
b2 = b1.or_bound(IntBound.from_constant(42)) |
| 1184 |
assert not b2.is_constant() |
| 1185 |
b3 = b2.and_bound(IntBound.from_constant(-1)) |
| 1186 |
assert not b3.is_constant() |
| 1187 |
|
| 1188 |
def test_knownbits_intersect_example(): |
| 1189 |
b1 = knownbits(0b10001010, |
| 1190 |
0b01110000) |
| 1191 |
b2 = knownbits(0b11000010, |
| 1192 |
0b00011100) |
| 1193 |
b1.intersect(b2) |
| 1194 |
assert b1.tvalue == r_uint(0b11001010) |
| 1195 |
assert b1.tmask == r_uint(0b00010000) |
| 1196 |
|
| 1197 |
def test_knownbits_intersect_disagree_examples(): |
| 1198 |
|
| 1199 |
|
| 1200 |
b0 = knownbits(0b001000, |
| 1201 |
0b110111) |
| 1202 |
b2 = knownbits(0b000100, |
| 1203 |
0b110011) |
| 1204 |
b1 = knownbits(0b001000, |
| 1205 |
0b110111) |
| 1206 |
b3 = knownbits(0b000000, |
| 1207 |
0b111111) |
| 1208 |
|
| 1209 |
with pytest.raises(Exception): |
| 1210 |
b0.intersect(b2) |
| 1211 |
|
| 1212 |
b1.intersect(b3) |
| 1213 |
|
| 1214 |
@example(t1=(IntBound.nonnegative(), 1), t2=(IntBound.unbounded(), 0)) |
| 1215 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 1216 |
def test_knownbits_intersect_random(t1, t2): |
| 1217 |
b1, n1 = t1 |
| 1218 |
b = b1.clone() |
| 1219 |
b2, n2 = t2 |
| 1220 |
try: |
| 1221 |
changed = b.intersect(b2) |
| 1222 |
except InvalidLoop: |
| 1223 |
|
| 1224 |
|
| 1225 |
assert not b1.contains(n2) |
| 1226 |
assert not b2.contains(n1) |
| 1227 |
else: |
| 1228 |
|
| 1229 |
|
| 1230 |
assert b1.contains(b._get_minimum_signed()) |
| 1231 |
assert b1.contains(b._get_maximum_signed()) |
| 1232 |
assert b2.contains(b._get_minimum_signed()) |
| 1233 |
assert b2.contains(b._get_maximum_signed()) |
| 1234 |
if b1.contains(n2): |
| 1235 |
assert b.contains(n2) |
| 1236 |
if b2.contains(n1): |
| 1237 |
assert b.contains(n1) |
| 1238 |
if changed: |
| 1239 |
assert not bound_eq(b, b1) |
| 1240 |
else: |
| 1241 |
assert bound_eq(b, b1) |
| 1242 |
|
| 1243 |
def test_get_minimum_signed_by_knownbits_above_full_range_bug(): |
| 1244 |
b1 = IntBound.from_constant(0) |
| 1245 |
with pytest.raises(InvalidLoop): |
| 1246 |
b1._get_maximum_signed_by_knownbits_atmost(-1) |
| 1247 |
with pytest.raises(InvalidLoop): |
| 1248 |
b1._get_minimum_signed_by_knownbits_atleast(1) |
| 1249 |
|
| 1250 |
@given(knownbits_and_bound_with_contained_number, strategies.data()) |
| 1251 |
def test_get_maximum_signed_by_knownbits_atmost_random(t1, data): |
| 1252 |
b1, n1 = t1 |
| 1253 |
minimum = b1._get_minimum_signed_by_knownbits() |
| 1254 |
threshold = data.draw(strategies.integers(minimum, MAXINT)) |
| 1255 |
new = b1._get_maximum_signed_by_knownbits_atmost(threshold) |
| 1256 |
assert new <= threshold |
| 1257 |
|
| 1258 |
@given(knownbits_and_bound_with_contained_number, strategies.data()) |
| 1259 |
def test_get_minimum_signed_by_knownbits_atleast_random(t1, data): |
| 1260 |
b1, n1 = t1 |
| 1261 |
maximum = b1._get_maximum_signed_by_knownbits_atmost() |
| 1262 |
threshold = data.draw(strategies.integers(MININT, maximum)) |
| 1263 |
new = b1._get_minimum_signed_by_knownbits_atleast(threshold) |
| 1264 |
assert new >= threshold |
| 1265 |
|
| 1266 |
@given(knownbits_and_bound_with_contained_number, ints) |
| 1267 |
def test_get_maximum_signed_by_knownbits_atmost_full_range_random(t1, threshold): |
| 1268 |
b1, n1 = t1 |
| 1269 |
try: |
| 1270 |
new = b1._get_maximum_signed_by_knownbits_atmost(threshold) |
| 1271 |
except InvalidLoop: |
| 1272 |
assert threshold < n1 |
| 1273 |
else: |
| 1274 |
assert new <= threshold |
| 1275 |
|
| 1276 |
@given(knownbits_and_bound_with_contained_number, ints) |
| 1277 |
def test_get_minimum_signed_by_knownbits_atleast_full_range_random(t1, threshold): |
| 1278 |
b1, n1 = t1 |
| 1279 |
try: |
| 1280 |
new = b1._get_minimum_signed_by_knownbits_atleast(threshold) |
| 1281 |
except InvalidLoop: |
| 1282 |
assert threshold > n1 |
| 1283 |
else: |
| 1284 |
assert new >= threshold |
| 1285 |
|
| 1286 |
def test_validtnum_assertion_examples(): |
| 1287 |
|
| 1288 |
|
| 1289 |
with pytest.raises(AssertionError): |
| 1290 |
b0 = knownbits(r_uint(0b111), |
| 1291 |
r_uint(0b010)) |
| 1292 |
|
| 1293 |
with pytest.raises(AssertionError): |
| 1294 |
b2 = IntBound.from_knownbits(0b101, |
| 1295 |
0b010) |
| 1296 |
with pytest.raises(AssertionError): |
| 1297 |
b3 = IntBound.from_knownbits(r_uint(0b101), |
| 1298 |
0b010) |
| 1299 |
with pytest.raises(AssertionError): |
| 1300 |
b4 = IntBound.from_knownbits(0b101, |
| 1301 |
r_uint(0b010)) |
| 1302 |
|
| 1303 |
IntBound.from_knownbits(r_uint(0b101), |
| 1304 |
r_uint(0b010)) |
| 1305 |
|
| 1306 |
def test_widen_tnum(): |
| 1307 |
b = knownbits(0b10001010, |
| 1308 |
0b00110100) |
| 1309 |
b.widen_update() |
| 1310 |
assert b._are_knownbits_implied() |
| 1311 |
|
| 1312 |
def test_shrink_bounds_by_knownbits(): |
| 1313 |
|
| 1314 |
b1 = knownbits(0b101000, |
| 1315 |
0b000101) |
| 1316 |
assert b1.lower == 0b101000 |
| 1317 |
assert b1.upper == 0b101101 |
| 1318 |
|
| 1319 |
b2 = knownbits(~0b010111, |
| 1320 |
0b000101) |
| 1321 |
assert b2.lower == ~0b010111 |
| 1322 |
assert b2.upper == ~0b010010 |
| 1323 |
|
| 1324 |
def test_shrink_knownbits_by_bounds(): |
| 1325 |
|
| 1326 |
b1 = IntBound(lower=27, upper=27, |
| 1327 |
tvalue=r_uint(0), |
| 1328 |
tmask=r_uint(-1)) |
| 1329 |
assert b1.is_constant() |
| 1330 |
assert b1.known_eq_const(27) |
| 1331 |
|
| 1332 |
b2 = IntBound(lower=-27, upper=-27, |
| 1333 |
tvalue=r_uint(0), |
| 1334 |
tmask=r_uint(-1)) |
| 1335 |
assert b2.is_constant() |
| 1336 |
assert b2.known_eq_const(-27) |
| 1337 |
|
| 1338 |
b3 = IntBound(lower=49, upper=52, |
| 1339 |
tvalue=r_uint(0), |
| 1340 |
tmask=r_uint(-1)) |
| 1341 |
assert not b3.is_constant() |
| 1342 |
assert check_knownbits_string(b3, "110???", '0') |
| 1343 |
|
| 1344 |
def test_shrink_knownbits_by_bounds_invalid(): |
| 1345 |
b1 = IntBound(lower=0, upper=1, |
| 1346 |
tvalue=r_uint(0b10), |
| 1347 |
tmask=r_uint(~0b10), do_shrinking=False) |
| 1348 |
with pytest.raises(InvalidLoop): |
| 1349 |
b1._shrink_knownbits_by_bounds() |
| 1350 |
|
| 1351 |
|
| 1352 |
def test_intbound_repr(): |
| 1353 |
b = IntBound() |
| 1354 |
assert repr(b) == 'IntBound.unbounded()' |
| 1355 |
b = IntBound.nonnegative() |
| 1356 |
assert repr(b) == 'IntBound.nonnegative()' |
| 1357 |
b = IntBound(lower=0, upper=100) |
| 1358 |
assert repr(b) == 'IntBound(0, 100)' |
| 1359 |
b = IntBound().urshift_bound(IntBound.from_constant(10)) |
| 1360 |
assert repr(b) == 'IntBound(0, 0x3fffff%s)' % ('ffffffff' * (LONG_BIT == 64),) |
| 1361 |
b = IntBound.from_constant(MININT) |
| 1362 |
assert repr(b) == 'IntBound.from_constant(MININT)' |
| 1363 |
b = IntBound.from_constant(-56) |
| 1364 |
assert repr(b) == 'IntBound.from_constant(-56)' |
| 1365 |
b = IntBound.from_knownbits(r_uint(0b0110), r_uint(0b1011), do_unmask=True) |
| 1366 |
assert repr(b) == 'IntBound.from_knownbits(r_uint(0b100), r_uint(0b1011))' |
| 1367 |
|
| 1368 |
b = IntBound(5, 16, r_uint(0b0100), r_uint(0b1011)) |
| 1369 |
|
| 1370 |
assert repr(b) == 'IntBound(5, 15, r_uint(0b100), r_uint(0b1011))' |
| 1371 |
|
| 1372 |
@given(knownbits_and_bound_with_contained_number) |
| 1373 |
def test_hypothesis_repr(t): |
| 1374 |
b, _ = t |
| 1375 |
s = repr(b) |
| 1376 |
b2 = eval(s, {"IntBound": IntBound, "r_uint": r_uint, |
| 1377 |
"MININT": MININT, "MAXINT": MAXINT}) |
| 1378 |
assert bound_eq(b, b2) |
| 1379 |
|
| 1380 |
@given(knownbits_and_bound_with_contained_number) |
| 1381 |
def test_hypothesis_is_constant_consistent(t): |
| 1382 |
b, num = t |
| 1383 |
b.is_constant() |
| 1384 |
|
| 1385 |
def test_intbound_str(): |
| 1386 |
b = IntBound() |
| 1387 |
assert str(b) == '(?)' |
| 1388 |
b = IntBound.nonnegative() |
| 1389 |
assert str(b) == '(0 <= 0b0?...?)' |
| 1390 |
b = IntBound(lower=0, upper=100) |
| 1391 |
assert str(b) == '(0 <= 0b0...0??????? <= 100)' |
| 1392 |
b = IntBound().urshift_bound(IntBound.from_constant(10)) |
| 1393 |
assert str(b) == '(0 <= 0b0000000000?...? <= 0x3fffff%s)' % ('ffffffff' * (LONG_BIT == 64),) |
| 1394 |
b = IntBound(lower=0, upper=1230000000) |
| 1395 |
if LONG_BIT == 64: |
| 1396 |
assert str(b) == '(0 <= 0b0...0??????????????????????????????? <= 1230000000)' |
| 1397 |
else: |
| 1398 |
assert str(b) == '(0 <= 0b0?...? <= 1230000000)' |
| 1399 |
b = IntBound(lower=0, upper=1230505081) |
| 1400 |
if LONG_BIT == 64: |
| 1401 |
assert str(b) == '(0 <= 0b0...0??????????????????????????????? <= 0x49580479)' |
| 1402 |
else: |
| 1403 |
assert str(b) == '(0 <= 0b0?...? <= 0x49580479)' |
| 1404 |
b = IntBound.from_constant(MININT) |
| 1405 |
assert str(b) == '(MININT)' |
| 1406 |
b = IntBound.from_constant(-56) |
| 1407 |
assert str(b) == '(-56)' |
| 1408 |
b = IntBound(0, 1) |
| 1409 |
assert str(b) == '(bool)' |
| 1410 |
b = IntBound(upper=MAXINT-16) |
| 1411 |
assert str(b) == "(? <= MAXINT - 16)" |
| 1412 |
b = IntBound(lower=MININT+16) |
| 1413 |
assert str(b) == "(MININT + 16 <= ?)" |
| 1414 |
|
| 1415 |
@given(knownbits_and_bound_with_contained_number) |
| 1416 |
def test_minmax_shrinking_random(t1): |
| 1417 |
b0, n0 = t1 |
| 1418 |
assert not isinstance(n0, r_uint) |
| 1419 |
b1 = IntBound(lower=b0.lower, upper=b0.upper, |
| 1420 |
tvalue=b0.tvalue, tmask=b0.tmask, |
| 1421 |
do_shrinking=True) |
| 1422 |
assert b1.lower <= n0 |
| 1423 |
assert n0 <= b1.upper |
| 1424 |
minimum = b1._get_minimum_signed() |
| 1425 |
assert minimum >= b1.lower |
| 1426 |
assert minimum <= n0 |
| 1427 |
assert b1.contains(minimum) |
| 1428 |
maximum = b1._get_maximum_signed() |
| 1429 |
assert maximum <= b1.upper |
| 1430 |
assert maximum >= n0 |
| 1431 |
assert b1.contains(maximum) |
| 1432 |
assert minimum <= maximum |
| 1433 |
|
| 1434 |
@given(knownbits_and_bound_with_contained_number) |
| 1435 |
def test_minmax_noshrink_random(t1): |
| 1436 |
b1, n1 = t1 |
| 1437 |
assert b1.lower <= n1 |
| 1438 |
assert n1 <= b1.upper |
| 1439 |
minimum = b1._get_minimum_signed() |
| 1440 |
assert minimum >= b1.lower |
| 1441 |
assert minimum <= n1 |
| 1442 |
assert b1.contains(minimum) |
| 1443 |
maximum = b1._get_maximum_signed() |
| 1444 |
assert maximum <= b1.upper |
| 1445 |
assert maximum >= n1 |
| 1446 |
assert b1.contains(maximum) |
| 1447 |
assert minimum <= maximum |
| 1448 |
|
| 1449 |
|
| 1450 |
def test_knownbits_and(): |
| 1451 |
for _, _, b1 in some_bits(): |
| 1452 |
for _, _, b2 in some_bits(): |
| 1453 |
b3 = b1.and_bound(b2) |
| 1454 |
for n1 in nnbr: |
| 1455 |
for n2 in nbr: |
| 1456 |
if b1.contains(n1) and b2.contains(n2): |
| 1457 |
assert b3.contains(n1 & n2) |
| 1458 |
|
| 1459 |
def test_knownbits_or(): |
| 1460 |
for _, _, b1 in some_bits(): |
| 1461 |
for _, _, b2 in some_bits(): |
| 1462 |
b3 = b1.or_bound(b2) |
| 1463 |
for n1 in nnbr: |
| 1464 |
for n2 in nbr: |
| 1465 |
if b1.contains(n1) and b2.contains(n2): |
| 1466 |
assert b3.contains(n1 | n2) |
| 1467 |
|
| 1468 |
def test_knownbits_xor(): |
| 1469 |
for _, _, b1 in some_bits(): |
| 1470 |
for _, _, b2 in some_bits(): |
| 1471 |
b3 = b1.xor_bound(b2) |
| 1472 |
for n1 in nnbr: |
| 1473 |
for n2 in nbr: |
| 1474 |
if b1.contains(n1) and b2.contains(n2): |
| 1475 |
assert b3.contains(n1 ^ n2) |
| 1476 |
|
| 1477 |
def test_knownbits_invert(): |
| 1478 |
for _, _, b1 in some_bits(): |
| 1479 |
b2 = b1.invert_bound() |
| 1480 |
for n1 in nbr: |
| 1481 |
if b1.contains(n1): |
| 1482 |
assert b2.contains(~n1) |
| 1483 |
else: |
| 1484 |
assert not b2.contains(~n1) |
| 1485 |
|
| 1486 |
def test_knownbits_neg(): |
| 1487 |
for _, _, b1 in some_bits(): |
| 1488 |
b2 = b1.neg_bound() |
| 1489 |
for n1 in nbr: |
| 1490 |
if b1.contains(n1): |
| 1491 |
assert b2.contains(-n1) |
| 1492 |
|
| 1493 |
def test_knownbits_add(): |
| 1494 |
for _, _, b1 in some_bits(): |
| 1495 |
for _, _, b2 in some_bits(): |
| 1496 |
b3 = b1.add_bound(b2) |
| 1497 |
for n1 in nnbr: |
| 1498 |
for n2 in nbr: |
| 1499 |
if b1.contains(n1) and b2.contains(n2): |
| 1500 |
assert b3.contains(n1 + n2) |
| 1501 |
|
| 1502 |
def test_knownbits_sub(): |
| 1503 |
for _, _, b1 in some_bits(): |
| 1504 |
for _, _, b2 in some_bits(): |
| 1505 |
b3 = b1.sub_bound(b2) |
| 1506 |
for n1 in nnbr: |
| 1507 |
for n2 in nbr: |
| 1508 |
if b1.contains(n1) and b2.contains(n2): |
| 1509 |
assert b3.contains(n1 - n2) |
| 1510 |
|
| 1511 |
def test_knownbits_lshift_examples(): |
| 1512 |
|
| 1513 |
a1 = IntBound.from_constant(21) |
| 1514 |
b1 = IntBound.from_constant(3) |
| 1515 |
r1 = a1.lshift_bound(b1) |
| 1516 |
assert a1.is_constant() |
| 1517 |
assert (21 << 3) == r1.get_constant_int() |
| 1518 |
assert 0 == (r1.get_constant_int() & 0b111) |
| 1519 |
|
| 1520 |
tv2 = 0b0100010 |
| 1521 |
tm2 = 0b0001100 |
| 1522 |
a2 = knownbits(tv2, tm2) |
| 1523 |
b2 = IntBound.from_constant(3) |
| 1524 |
r2 = a2.lshift_bound(b2) |
| 1525 |
assert not r2.is_constant() |
| 1526 |
assert r2.contains(tv2 << 3) |
| 1527 |
assert r2.contains((tv2|tm2) << 3) |
| 1528 |
assert check_knownbits_string(r2, "010??10000", '0') |
| 1529 |
|
| 1530 |
tv3 = 0b1001 |
| 1531 |
tm3 = 0b0110 |
| 1532 |
a3 = knownbits(tv3, tm3) |
| 1533 |
b3 = IntBound.from_constant(LONG_BIT+1) |
| 1534 |
r3 = a3.lshift_bound(b3) |
| 1535 |
assert r3.is_constant() |
| 1536 |
assert r3.get_constant_int() == 0 |
| 1537 |
|
| 1538 |
def test_knownbits_rshift_signed_consts_examples(): |
| 1539 |
|
| 1540 |
a1a = IntBound.from_constant(21) |
| 1541 |
b1 = IntBound.from_constant(3) |
| 1542 |
r1a = a1a.rshift_bound(b1) |
| 1543 |
assert r1a.is_constant() |
| 1544 |
assert (21 >> 3) == r1a.get_constant_int() |
| 1545 |
|
| 1546 |
a1b = IntBound.from_constant(-21) |
| 1547 |
r1b = a1b.rshift_bound(b1) |
| 1548 |
assert r1b.is_constant() |
| 1549 |
assert (-21 >> 3) == r1b.get_constant_int() |
| 1550 |
|
| 1551 |
def test_knownbits_rshift_signed_partialunknowns_examples(): |
| 1552 |
|
| 1553 |
tv2a = 0b0100010 |
| 1554 |
tm2a = 0b0001100 |
| 1555 |
a2a = knownbits(tv2a, tm2a) |
| 1556 |
b2 = IntBound.from_constant(3) |
| 1557 |
r2a = a2a.rshift_bound(b2) |
| 1558 |
assert not r2a.is_constant() |
| 1559 |
assert r2a.contains(0b0100) |
| 1560 |
assert r2a.contains(0b0101) |
| 1561 |
|
| 1562 |
tv2b = ~0b0101010 |
| 1563 |
tm2b = 0b0100010 |
| 1564 |
a2b = knownbits(tv2b, tm2b) |
| 1565 |
r2b = a2b.rshift_bound(b2) |
| 1566 |
assert not r2b.is_constant() |
| 1567 |
assert r2b.contains(~0b0101) |
| 1568 |
assert r2b.contains(~0b0001) |
| 1569 |
|
| 1570 |
tv2c = 0b0101010 |
| 1571 |
tm2c = intmask(1 << (LONG_BIT-1)) |
| 1572 |
a2c = knownbits(tv2c, tm2c) |
| 1573 |
r2c = a2c.rshift_bound(b2) |
| 1574 |
assert not r2c.is_constant() |
| 1575 |
assert r2c.contains(0b0101 | (tm2c>>3)) |
| 1576 |
assert r2c.contains(0b0101) |
| 1577 |
|
| 1578 |
def test_knownbits_rshift_signed_completeshiftout_examples(): |
| 1579 |
|
| 1580 |
tv3a = 0b1001 |
| 1581 |
tm3a = 0b0110 |
| 1582 |
a3a = knownbits(tv3a, tm3a) |
| 1583 |
b3 = IntBound.from_constant(LONG_BIT+1) |
| 1584 |
r3a = a3a.rshift_bound(b3) |
| 1585 |
assert r3a.is_constant() |
| 1586 |
assert r3a.get_constant_int() == 0 |
| 1587 |
|
| 1588 |
tv3b = ~0b1101 |
| 1589 |
tm3b = 0b1001 |
| 1590 |
a3b = knownbits(tv3b, tm3b) |
| 1591 |
r3b = a3b.rshift_bound(b3) |
| 1592 |
assert r3b.is_constant() |
| 1593 |
assert r3b.get_constant_int() == -1 |
| 1594 |
|
| 1595 |
tv3c = 0b1000 |
| 1596 |
tm3c = ~0b1001 |
| 1597 |
a3c = knownbits(tv3c, tm3c) |
| 1598 |
r3c = a3c.rshift_bound(b3) |
| 1599 |
assert not r3c.is_constant() |
| 1600 |
assert r3c.contains(-1) |
| 1601 |
|
| 1602 |
def test_knownbits_rshift_unsigned_consts_examples(): |
| 1603 |
|
| 1604 |
a1a = IntBound.from_constant(21) |
| 1605 |
b1 = IntBound.from_constant(3) |
| 1606 |
r1a = a1a.urshift_bound(b1) |
| 1607 |
assert r1a.is_constant() |
| 1608 |
assert intmask(r_uint(21) >> r_uint(3)) == r1a.get_constant_int() |
| 1609 |
|
| 1610 |
a1b = IntBound.from_constant(-21) |
| 1611 |
r1b = a1b.urshift_bound(b1) |
| 1612 |
assert r1b.is_constant() |
| 1613 |
assert intmask(r_uint(-21) >> r_uint(3)) == r1b.get_constant_int() |
| 1614 |
|
| 1615 |
def test_knownbits_rshift_unsigned_partialunknowns_examples(): |
| 1616 |
|
| 1617 |
tv2a = 0b0100010 |
| 1618 |
tm2a = 0b0001100 |
| 1619 |
a2a = knownbits(tv2a, tm2a) |
| 1620 |
b2 = IntBound.from_constant(3) |
| 1621 |
r2a = a2a.urshift_bound(b2) |
| 1622 |
assert not r2a.is_constant() |
| 1623 |
assert r2a.contains(0b0100) |
| 1624 |
assert r2a.contains(0b0101) |
| 1625 |
|
| 1626 |
tv2b = ~0b0101010 |
| 1627 |
tm2b = 0b0100010 |
| 1628 |
a2b = knownbits(tv2b, tm2b) |
| 1629 |
r2b = a2b.urshift_bound(b2) |
| 1630 |
assert not r2b.is_constant() |
| 1631 |
assert r2b.contains(intmask(r_uint(~0b0001010) >> r_uint(3))) |
| 1632 |
assert r2b.contains(intmask(r_uint(~0b0101010) >> r_uint(3))) |
| 1633 |
|
| 1634 |
tv2c = 0b0101010 |
| 1635 |
tm2c = intmask(1<<(LONG_BIT-1)) |
| 1636 |
a2c = knownbits(tv2c, tm2c) |
| 1637 |
r2c = a2c.urshift_bound(b2) |
| 1638 |
assert not r2c.is_constant() |
| 1639 |
assert r2c.contains(0b0101 | intmask(r_uint(tm2c)>>r_uint(3))) |
| 1640 |
assert r2c.contains(0b0101) |
| 1641 |
|
| 1642 |
def test_knownbits_rshift_unsigned_completeshiftout_examples(): |
| 1643 |
|
| 1644 |
tv3a = 0b1001 |
| 1645 |
tm3a = 0b0110 |
| 1646 |
a3a = knownbits(tv3a, tm3a) |
| 1647 |
b3 = IntBound.from_constant(LONG_BIT+1) |
| 1648 |
r3a = a3a.urshift_bound(b3) |
| 1649 |
assert r3a.is_constant() |
| 1650 |
assert r3a.get_constant_int() == 0 |
| 1651 |
|
| 1652 |
tv3b = ~0b1101 |
| 1653 |
tm3b = 0b1001 |
| 1654 |
a3b = knownbits(tv3b, tm3b) |
| 1655 |
r3b = a3b.urshift_bound(b3) |
| 1656 |
assert r3b.is_constant() |
| 1657 |
assert r3b.get_constant_int() == 0 |
| 1658 |
|
| 1659 |
tv3c = 0b1000 |
| 1660 |
tm3c = ~0b1001 |
| 1661 |
a3c = knownbits(tv3c, tm3c) |
| 1662 |
r3c = a3c.urshift_bound(b3) |
| 1663 |
assert r3c.is_constant() |
| 1664 |
assert r3c.known_eq_const(0) |
| 1665 |
|
| 1666 |
@pytest.mark.skipif('sys.maxint <= 2**31 - 1') |
| 1667 |
def test_urshift_bound_improvements(): |
| 1668 |
|
| 1669 |
b = IntBound(0x7ff02a56, 0x80102a54, r_uint(0b0), r_uint(0b11111111111111111111111111111110)) |
| 1670 |
b2 = b.urshift_bound(IntBound.from_constant(1)) |
| 1671 |
assert b2.lower == 0x7ff02a56 >> 1 |
| 1672 |
assert b2.upper == 0x80102a54 >> 1 |
| 1673 |
|
| 1674 |
def test_knownbits_add_concrete_example(): |
| 1675 |
a1 = knownbits( |
| 1676 |
0b100010, |
| 1677 |
0b001100) |
| 1678 |
b1 = 3 |
| 1679 |
r1 = a1.add(b1) |
| 1680 |
|
| 1681 |
assert not r1.is_constant() |
| 1682 |
|
| 1683 |
assert r1.contains(0b100101) |
| 1684 |
assert r1.contains(0b101001) |
| 1685 |
assert r1.contains(0b101101) |
| 1686 |
assert r1.contains(0b110001) |
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
assert not r1.contains(0b111111) |
| 1691 |
assert not r1.contains(0b1111101) |
| 1692 |
|
| 1693 |
def test_knownbits_sub_concrete_example(): |
| 1694 |
a1 = knownbits( |
| 1695 |
0b100001, |
| 1696 |
0b001100) |
| 1697 |
b1 = 3 |
| 1698 |
r1 = a1.add(intmask(-b1)) |
| 1699 |
|
| 1700 |
assert not r1.is_constant() |
| 1701 |
assert r1.contains(0b011110) |
| 1702 |
assert r1.contains(0b100010) |
| 1703 |
assert r1.contains(0b100110) |
| 1704 |
assert r1.contains(0b101010) |
| 1705 |
|
| 1706 |
def test_knownbits_and_backwards_otherconst_examples(): |
| 1707 |
r = IntBound.from_constant(0b11).and_bound_backwards(IntBound.from_constant(0b00)) |
| 1708 |
assert check_knownbits_string(r, "??00") |
| 1709 |
r = IntBound.from_constant(0b11).and_bound_backwards(IntBound.from_constant(0b11)) |
| 1710 |
assert check_knownbits_string(r, "??11") |
| 1711 |
x = knownbits( 0b10000, |
| 1712 |
~0b11000) |
| 1713 |
r = x.and_bound_backwards(IntBound.from_constant(0)) |
| 1714 |
assert check_knownbits_string(r, "??0????") |
| 1715 |
x = knownbits( 0b1010, |
| 1716 |
~0b1111) |
| 1717 |
r = x.and_bound_backwards(IntBound.from_constant(0)) |
| 1718 |
assert check_knownbits_string(r, "??0?0?") |
| 1719 |
r = IntBound.from_constant(0b11).and_bound_backwards(IntBound.from_constant(0b10)) |
| 1720 |
assert check_knownbits_string(r, "??10") |
| 1721 |
r = IntBound.from_constant(0b111).and_bound_backwards(IntBound.from_knownbits(r_uint(0b100), ~r_uint(0b110))) |
| 1722 |
assert check_knownbits_string(r, "?10?") |
| 1723 |
x = IntBound.unbounded() |
| 1724 |
o = knownbits(0b101010, |
| 1725 |
0b010100) |
| 1726 |
r = o.and_bound_backwards(IntBound.from_constant(0b110)) |
| 1727 |
assert check_knownbits_string(r, "0?011?") |
| 1728 |
|
| 1729 |
def test_knownbits_and_backwards_example(): |
| 1730 |
o = knownbits(0b11100000, |
| 1731 |
0b00000111) |
| 1732 |
r = knownbits(0b10000100, |
| 1733 |
0b00101001) |
| 1734 |
s = o.and_bound_backwards(r) |
| 1735 |
assert check_knownbits_string(s, '?10???1??', '?') |
| 1736 |
|
| 1737 |
def test_knownbits_and_backwards_example_inconsistent(): |
| 1738 |
o = knownbits(0b111000000, 0b000000111) |
| 1739 |
r = knownbits(0b100100100, 0b001001001) |
| 1740 |
with pytest.raises(InvalidLoop): |
| 1741 |
o.and_bound_backwards(r) |
| 1742 |
|
| 1743 |
def test_knownbits_or_backwards_example(): |
| 1744 |
o = knownbits(0b11000000, |
| 1745 |
0b00000111) |
| 1746 |
r = knownbits(0b10100100, |
| 1747 |
0b01001001) |
| 1748 |
s = o.or_bound_backwards(r) |
| 1749 |
assert check_knownbits_string(s, '00??10??0?', '0') |
| 1750 |
|
| 1751 |
o = knownbits(0b111000000, 0b000000111) |
| 1752 |
r = knownbits(0b100100100, 0b001001001) |
| 1753 |
with pytest.raises(InvalidLoop): |
| 1754 |
s = o.or_bound_backwards(r) |
| 1755 |
|
| 1756 |
def test_knownbits_rshift_backwards_example(): |
| 1757 |
o = IntBound.from_constant(3) |
| 1758 |
x1 = IntBound.unbounded() |
| 1759 |
r1 = knownbits(0b101010, |
| 1760 |
0b010100) |
| 1761 |
res1 = r1.rshift_bound_backwards(o) |
| 1762 |
assert check_knownbits_string(res1, "1?1?10???", '0') |
| 1763 |
r2 = knownbits(0b101010, |
| 1764 |
0b010100) |
| 1765 |
x3 = IntBound.unbounded() |
| 1766 |
r3 = IntBound.from_constant(1) |
| 1767 |
res3 = r3.rshift_bound_backwards(o) |
| 1768 |
assert not res3.is_constant() |
| 1769 |
assert check_knownbits_string(res3, "1???", '0') |
| 1770 |
|
| 1771 |
def test_knownbits_lshift_backwards_example(): |
| 1772 |
o = IntBound.from_constant(3) |
| 1773 |
r1 = knownbits(0b101000, |
| 1774 |
0b010000) |
| 1775 |
res1 = r1.lshift_bound_backwards(o) |
| 1776 |
assert not res1.is_constant() |
| 1777 |
assert res1.knownbits_string().startswith("???") \ |
| 1778 |
and res1.knownbits_string().endswith("001?1") |
| 1779 |
r2 = knownbits(0b100000, |
| 1780 |
0b011000) |
| 1781 |
res2 = r2.lshift_bound_backwards(o) |
| 1782 |
assert res2.knownbits_string().endswith("1??") |
| 1783 |
assert res2.knownbits_string().startswith("???0") |
| 1784 |
r3 = knownbits(MININT, 0) |
| 1785 |
res3 = r3.lshift_bound_backwards(o) |
| 1786 |
assert not res3.is_constant() |
| 1787 |
assert res3.knownbits_string().startswith("???1") |
| 1788 |
|
| 1789 |
def test_knownbits_lshift_backwards_example_inconsistent(): |
| 1790 |
o = IntBound.from_constant(3) |
| 1791 |
r1 = knownbits(0b101001, |
| 1792 |
0b010000) |
| 1793 |
with pytest.raises(InvalidLoop): |
| 1794 |
r1.lshift_bound_backwards(o) |
| 1795 |
|
| 1796 |
@given(knownbits_and_bound_with_contained_number, shift_amount) |
| 1797 |
def test_knownbits_lshift_backwards_random(t1, t2): |
| 1798 |
b1, n1 = t1 |
| 1799 |
b2, n2 = t2 |
| 1800 |
result = b1.lshift_bound(b2) |
| 1801 |
try: |
| 1802 |
r = ovfcheck(n1 << n2) |
| 1803 |
except OverflowError: |
| 1804 |
assume(False) |
| 1805 |
orig = result.lshift_bound_backwards(b2) |
| 1806 |
assert orig.contains(n1) |
| 1807 |
b1.intersect(orig) |
| 1808 |
|
| 1809 |
|
| 1810 |
@given(constant, constant) |
| 1811 |
def test_const_stays_const_or(t1, t2): |
| 1812 |
b1, n1 = t1 |
| 1813 |
b2, n2 = t2 |
| 1814 |
r = b1.or_bound(b2) |
| 1815 |
assert r.is_constant() |
| 1816 |
assert r.known_eq_const(n1 | n2) |
| 1817 |
assert r.get_constant_int() == n1 | n2 |
| 1818 |
|
| 1819 |
@given(constant, constant) |
| 1820 |
def test_const_stays_const_and(t1, t2): |
| 1821 |
b1, n1 = t1 |
| 1822 |
b2, n2 = t2 |
| 1823 |
r = b1.and_bound(b2) |
| 1824 |
assert r.is_constant() |
| 1825 |
assert r.known_eq_const(n1 & n2) |
| 1826 |
assert r.get_constant_int() == n1 & n2 |
| 1827 |
|
| 1828 |
@given(constant, constant) |
| 1829 |
def test_const_stays_const_xor(t1, t2): |
| 1830 |
b1, n1 = t1 |
| 1831 |
b2, n2 = t2 |
| 1832 |
r = b1.xor_bound(b2) |
| 1833 |
assert r.is_constant() |
| 1834 |
assert r.known_eq_const(n1 ^ n2) |
| 1835 |
assert r.get_constant_int() == n1 ^ n2 |
| 1836 |
|
| 1837 |
@given(constant) |
| 1838 |
def test_const_stays_const_invert(t1): |
| 1839 |
b1, n1 = t1 |
| 1840 |
r = b1.invert_bound() |
| 1841 |
assert r.is_constant() |
| 1842 |
assert r.known_eq_const(~n1) |
| 1843 |
assert r.get_constant_int() == ~n1 |
| 1844 |
|
| 1845 |
@given(constant, constant) |
| 1846 |
def test_const_stays_const_lshift(t1, t2): |
| 1847 |
b1, n1 = t1 |
| 1848 |
b2, n2 = t2 |
| 1849 |
r = b1.lshift_bound(b2) |
| 1850 |
if n2 >= LONG_BIT: |
| 1851 |
assert r.is_constant() |
| 1852 |
assert r.known_eq_const(0) |
| 1853 |
elif n2 >= 0: |
| 1854 |
assert r.is_constant() |
| 1855 |
assert r.known_eq_const(intmask(n1 << n2)) |
| 1856 |
|
| 1857 |
@given(constant, constant) |
| 1858 |
def test_const_stays_const_urshift(t1, t2): |
| 1859 |
b1, n1 = t1 |
| 1860 |
b2, n2 = t2 |
| 1861 |
r = b1.urshift_bound(b2) |
| 1862 |
if n2 >= LONG_BIT: |
| 1863 |
assert r.is_constant() |
| 1864 |
assert r.known_eq_const(0) |
| 1865 |
elif n2 >= 0: |
| 1866 |
assert r.is_constant() |
| 1867 |
assert r.known_eq_const(intmask(r_uint(n1) >> r_uint(n2))) |
| 1868 |
|
| 1869 |
@given(constant, constant) |
| 1870 |
def test_const_stays_const_rshift(t1, t2): |
| 1871 |
b1, n1 = t1 |
| 1872 |
b2, n2 = t2 |
| 1873 |
r = b1.rshift_bound(b2) |
| 1874 |
if n2 >= LONG_BIT: |
| 1875 |
assert r.is_constant() |
| 1876 |
if n1 < 0: |
| 1877 |
assert r.known_eq_const(-1) |
| 1878 |
else: |
| 1879 |
assert r.known_eq_const(0) |
| 1880 |
elif n2 >= 0: |
| 1881 |
assert r.is_constant() |
| 1882 |
assert r.known_eq_const(intmask(n1) >> intmask(n2)) |
| 1883 |
|
| 1884 |
@given(maybe_valid_value_mask_pair) |
| 1885 |
def test_validtnum_assertion_random(t1): |
| 1886 |
|
| 1887 |
val, msk = t1 |
| 1888 |
is_valid = (0 == val & msk) |
| 1889 |
if is_valid: |
| 1890 |
b = knownbits(val, msk) |
| 1891 |
else: |
| 1892 |
with pytest.raises(AssertionError): |
| 1893 |
b = knownbits(val, msk) |
| 1894 |
|
| 1895 |
@given(knownbits_and_bound_with_contained_number, strategies.integers(-5, LONG_BIT + 10), strategies.integers(0, LONG_BIT + 10), strategies.integers(0, LONG_BIT + 10)) |
| 1896 |
def test_rshift_signed_random(t1, a, b, c): |
| 1897 |
b1, n1 = t1 |
| 1898 |
a, n2, c = sorted([a, b, c]) |
| 1899 |
b2 = IntBound(a, c) |
| 1900 |
r = b1.rshift_bound(b2) |
| 1901 |
print b1, b2, r, n1, n2, n1 >> n2 |
| 1902 |
assert r.contains(n1 >> n2) |
| 1903 |
|
| 1904 |
@given(knownbits_and_bound_with_contained_number, strategies.integers(-5, LONG_BIT + 10), strategies.integers(0, LONG_BIT + 10), strategies.integers(0, LONG_BIT + 10)) |
| 1905 |
def test_rshift_unsigned_random(t1, a, b, c): |
| 1906 |
b1, n1 = t1 |
| 1907 |
a, n2, c = sorted([a, b, c]) |
| 1908 |
b2 = IntBound(a, c) |
| 1909 |
r = b1.urshift_bound(b2) |
| 1910 |
assert r.contains(intmask(r_uint(n1) >> r_uint(n2))) |
| 1911 |
if n1 < 0 and n2 > 0 and r.is_constant(): |
| 1912 |
assert r.get_constant_int() >= 0 |
| 1913 |
|
| 1914 |
|
| 1915 |
@given(knownbits_and_bound_with_contained_number, pos_relatively_small_values) |
| 1916 |
def test_rshift_signed_const_random(t1, n2): |
| 1917 |
b1, n1 = t1 |
| 1918 |
b2 = IntBound.from_constant(n2) |
| 1919 |
r = b1.rshift_bound(b2) |
| 1920 |
assert r.contains(n1 >> n2) |
| 1921 |
|
| 1922 |
@given(knownbits_and_bound_with_contained_number, pos_relatively_small_values) |
| 1923 |
def test_rshift_unsigned_const_random(t1, n2): |
| 1924 |
b1, n1 = t1 |
| 1925 |
b2 = IntBound.from_constant(n2) |
| 1926 |
r = b1.urshift_bound(b2) |
| 1927 |
assert r.contains(intmask(r_uint(n1) >> r_uint(n2))) |
| 1928 |
if n1 < 0 and n2 > 0 and r.is_constant(): |
| 1929 |
assert r.get_constant_int() >= 0 |
| 1930 |
|
| 1931 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 1932 |
def test_knownbits_and_backwards_random(t1, t2): |
| 1933 |
b1, n1 = t1 |
| 1934 |
b2, n2 = t2 |
| 1935 |
rb = b1.and_bound(b2) |
| 1936 |
rn = n1 & n2 |
| 1937 |
newb1 = b2.and_bound_backwards(rb) |
| 1938 |
assert newb1.contains(n1) |
| 1939 |
|
| 1940 |
b1.intersect(newb1) |
| 1941 |
|
| 1942 |
@given(knownbits_and_bound_with_contained_number, knownbits_and_bound_with_contained_number) |
| 1943 |
def test_knownbits_or_backwards_random(t1, t2): |
| 1944 |
b1, n1 = t1 |
| 1945 |
b2, n2 = t2 |
| 1946 |
rb = b1.or_bound(b2) |
| 1947 |
rn = n1 & n2 |
| 1948 |
newb1 = b2.or_bound_backwards(rb) |
| 1949 |
assert newb1.contains(n1) |
| 1950 |
|
| 1951 |
b1.intersect(newb1) |
| 1952 |
|
| 1953 |
@given(knownbits_and_bound_with_contained_number, constant) |
| 1954 |
def test_knownbits_urshift_backwards_random(t1, t2): |
| 1955 |
b1, n1 = t1 |
| 1956 |
b2, n2 = t2 |
| 1957 |
rb = b1.urshift_bound(b2) |
| 1958 |
newb1 = rb.urshift_bound_backwards(b2) |
| 1959 |
assert newb1.contains(n1) |
| 1960 |
|
| 1961 |
b1.intersect(newb1) |
| 1962 |
|
| 1963 |
@given(knownbits_and_bound_with_contained_number, constant) |
| 1964 |
def test_knownbits_rshift_backwards_random(t1, t2): |
| 1965 |
b1, n1 = t1 |
| 1966 |
b2, n2 = t2 |
| 1967 |
rb = b1.rshift_bound(b2) |
| 1968 |
newb1 = rb.rshift_bound_backwards(b2) |
| 1969 |
assert newb1.contains(n1) |
| 1970 |
|
| 1971 |
b1.intersect(newb1) |
| 1972 |
|
| 1973 |
def test_knownbits_div_bug(): |
| 1974 |
b1 = IntBound.unbounded() |
| 1975 |
b2 = knownbits(0b1, r_uint(-2)) |
| 1976 |
r = b1.py_div_bound(b2) |
| 1977 |
assert r.lower == MININT and r.upper == MAXINT |
| 1978 |
|
| 1979 |
def knownbits(tvalue, tmask=0, do_unmask=False): |
| 1980 |
if not isinstance(tvalue, r_uint): |
| 1981 |
tvalue = r_uint(tvalue) |
| 1982 |
if not isinstance(tmask, r_uint): |
| 1983 |
tmask = r_uint(tmask) |
| 1984 |
return IntBound.from_knownbits(tvalue, tmask, do_unmask) |
| 1985 |
|
| 1986 |
def check_knownbits_string(r, lower_bits, upper_fill='?'): |
| 1987 |
return r.knownbits_string() == upper_fill*(LONG_BIT-len(lower_bits)) + lower_bits |
| 1988 |
|
| 1989 |
def test_getnullness_examples(): |
| 1990 |
b = IntBound(10, 1000) |
| 1991 |
assert b.getnullness() == INFO_NONNULL |
| 1992 |
b = IntBound.from_constant(0) |
| 1993 |
assert b.getnullness() == INFO_NULL |
| 1994 |
b = IntBound(-10000, -10) |
| 1995 |
assert b.getnullness() == INFO_NONNULL |
| 1996 |
b = IntBound.from_knownbits(r_uint(0b1), ~r_uint(0b1)) |
| 1997 |
assert b.getnullness() == INFO_NONNULL |
| 1998 |
|
| 1999 |
@given(knownbits_and_bound_with_contained_number) |
| 2000 |
def test_getnullness_random(t1): |
| 2001 |
b1, n1 = t1 |
| 2002 |
res = b1.getnullness() |
| 2003 |
if res == INFO_NONNULL: |
| 2004 |
assert n1 != 0 |
| 2005 |
elif res == INFO_NULL: |
| 2006 |
assert n1 == 0 |
| 2007 |
else: |
| 2008 |
assert res == INFO_UNKNOWN |
| 2009 |
|
| 2010 |
@given(knownbits_and_bound_with_contained_number) |
| 2011 |
def test_make_bool(t1): |
| 2012 |
b1, n1 = t1 |
| 2013 |
if b1.contains(0) or b1.contains(1): |
| 2014 |
b1.make_bool() |
| 2015 |
assert b1.is_bool() |