| # int |
| assert_eq(int(0), 0) |
| assert_eq(int(42), 42) |
| assert_eq(int(-1), -1) |
| assert_eq(int(2147483647), 2147483647) |
| # -2147483648 is not actually a valid int literal even though it's a |
| # valid int value, hence the -1 expression. |
| assert_eq(int(-2147483647 - 1), -2147483647 - 1) |
| assert_eq(int(True), 1) |
| assert_eq(int(False), 0) |
| |
| --- |
| int(None) ### None is not of type string or int or bool |
| --- |
| # This case is allowed in Python but not Skylark |
| int() ### insufficient arguments received |
| --- |
| |
| # string, no base |
| # Includes same numbers as integer test cases above. |
| assert_eq(int('0'), 0) |
| assert_eq(int('42'), 42) |
| assert_eq(int('-1'), -1) |
| assert_eq(int('2147483647'), 2147483647) |
| assert_eq(int('-2147483648'), -2147483647 - 1) |
| # Leading zero allowed when not using base = 0. |
| assert_eq(int('016'), 16) |
| # Leading plus sign allowed for strings. |
| assert_eq(int('+42'), 42) |
| |
| --- |
| int(2147483648) ### invalid base-10 integer constant: 2147483648 |
| --- |
| int(-2147483649) ### invalid base-10 integer constant: 2147483649 |
| --- |
| int('') ### cannot be empty |
| --- |
| # Surrounding whitespace is not allowed |
| int(' 42 ') ### invalid literal for int() with base 10: " 42 " |
| --- |
| int('-') ### invalid literal for int() with base 10: "-" |
| --- |
| int('0x') ### invalid literal for int() with base 10: "0x" |
| --- |
| int('1.5') ### invalid literal for int() with base 10: "1.5" |
| --- |
| int('ab') ### invalid literal for int() with base 10: "ab" |
| --- |
| |
| assert_eq(int('11', 2), 3) |
| assert_eq(int('-11', 2), -3) |
| assert_eq(int('11', 9), 10) |
| assert_eq(int('AF', 16), 175) |
| assert_eq(int('11', 36), 37) |
| assert_eq(int('az', 36), 395) |
| assert_eq(int('11', 10), 11) |
| assert_eq(int('11', 0), 11) |
| assert_eq(int('016', 8), 14) |
| assert_eq(int('016', 16), 22) |
| |
| --- |
| # invalid base |
| int('016', 0) ### cannot infer base for int() when value begins with a 0: "016" |
| --- |
| int('123', 3) ### invalid literal for int() with base 3: "123" |
| --- |
| int('FF', 15) ### invalid literal for int() with base 15: "FF" |
| --- |
| int('123', -1) ### int() base must be >= 2 and <= 36 |
| --- |
| int('123', 1) ### int() base must be >= 2 and <= 36 |
| --- |
| int('123', 37) ### int() base must be >= 2 and <= 36 |
| --- |
| int('123', 'x') ### base must be an integer (got 'string') |
| --- |
| |
| # base with prefix |
| assert_eq(int('0b11', 0), 3) |
| assert_eq(int('-0b11', 0), -3) |
| assert_eq(int('+0b11', 0), 3) |
| assert_eq(int('0B11', 2), 3) |
| assert_eq(int('0o11', 0), 9) |
| assert_eq(int('0O11', 8), 9) |
| assert_eq(int('0XFF', 0), 255) |
| assert_eq(int('0xFF', 16), 255) |
| |
| --- |
| int('0xFF', 8) ### invalid literal for int() with base 8: "0xFF" |
| --- |
| int(True, 2) ### int() can't convert non-string with explicit base |
| --- |
| int(1, 2) ### int() can't convert non-string with explicit base |
| --- |
| int(True, 10) ### int() can't convert non-string with explicit base |