Paul Sokolovsky | 027594e | 2014-01-31 17:13:51 +0200 | [diff] [blame] | 1 | # This tests small int range for 32-bit machine |
Paul Sokolovsky | 757ac81 | 2014-01-12 17:06:25 +0200 | [diff] [blame] | 2 | |
Paul Sokolovsky | 56e5ef2 | 2014-02-22 16:39:45 +0200 | [diff] [blame] | 3 | # Small ints are variable-length encoded in MicroPython, so first |
| 4 | # test that encoding works as expected. |
| 5 | |
| 6 | print(0) |
| 7 | print(1) |
| 8 | print(-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" |
| 12 | print(63) |
| 13 | print(64) |
| 14 | print(65) |
| 15 | print(-63) |
| 16 | print(-64) |
| 17 | print(-65) |
| 18 | # Maximum values of small ints on 32-bit platform |
| 19 | print(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. |
| 23 | print(-1073741823) |
| 24 | |
| 25 | # Operations tests |
| 26 | |
Paul Sokolovsky | 757ac81 | 2014-01-12 17:06:25 +0200 | [diff] [blame] | 27 | a = 0x3fffff |
| 28 | print(a) |
| 29 | a *= 0x10 |
| 30 | print(a) |
| 31 | a *= 0x10 |
| 32 | print(a) |
| 33 | a += 0xff |
| 34 | print(a) |
| 35 | # This would overflow |
| 36 | #a += 1 |
| 37 | |
| 38 | a = -0x3fffff |
| 39 | print(a) |
| 40 | a *= 0x10 |
| 41 | print(a) |
| 42 | a *= 0x10 |
| 43 | print(a) |
| 44 | a -= 0xff |
| 45 | print(a) |
| 46 | # This still doesn't overflow |
| 47 | a -= 1 |
| 48 | print(a) |
| 49 | # This would overflow |
| 50 | #a -= 1 |