1# This tests small int range for 32-bit machine 2 3# Small ints are variable-length encoded in MicroPython, so first 4# test that encoding works as expected. 5 6print(0) 7print(1) 8print(-1) 9# Value is split in 7-bit "subwords", and taking into account that all 10# ints in Python are signed, there're 6 bits of magnitude. So, around 2^6 11# there's "turning point" 12print(63) 13print(64) 14print(65) 15print(-63) 16print(-64) 17print(-65) 18# Maximum values of small ints on 32-bit platform 19print(1073741823) 20# Per python semantics, lexical integer is without a sign (i.e. positive) 21# and '-' is unary minus operation applied to it. That's why -1073741824 22# (min two-complement's negative value) is not allowed. 23print(-1073741823) 24 25# Operations tests 26 27# compile-time constexprs 28print(1 + 3) 29print(3 - 2) 30print(2 * 3) 31print(1 & 3) 32print(1 | 2) 33print(1 ^ 3) 34print(+3) 35print(-3) 36print(~3) 37 38a = 0x3fffff 39print(a) 40a *= 0x10 41print(a) 42a *= 0x10 43print(a) 44a += 0xff 45print(a) 46# This would overflow 47#a += 1 48 49a = -0x3fffff 50print(a) 51a *= 0x10 52print(a) 53a *= 0x10 54print(a) 55a -= 0xff 56print(a) 57# This still doesn't overflow 58a -= 1 59print(a) 60# This would overflow 61#a -= 1 62 63# negative shifts are not allowed 64try: 65 a << -1 66except ValueError: 67 print("ValueError") 68try: 69 a >> -1 70except ValueError: 71 print("ValueError") 72 73# Shifts to big amounts are undefined behavior in C and is CPU-specific 74 75# These are compile-time constexprs 76print(1 >> 32) 77print(1 >> 64) 78print(1 >> 128) 79 80# These are runtime calcs 81a = 1 82print(a >> 32) 83print(a >> 64) 84print(a >> 128) 85