int_small.py 1.5 KB

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