aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/skylark/testdata/int_function.sky
blob: 7d6a382a65dd172e95b66c26fb30e9e9e0d1f4f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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