github.com/lab47/exprcore@v0.0.0-20210525052339-fb7d6bd9331e/exprcore/testdata/int.star (about) 1 # Tests of Starlark 'int' 2 # option:float 3 4 load("assert.star", "assert") 5 6 # basic arithmetic 7 assert.eq(0 - 1, -1) 8 assert.eq(0 + 1, +1) 9 assert.eq(1 + 1, 2) 10 assert.eq(5 + 7, 12) 11 assert.eq(5 * 7, 35) 12 assert.eq(5 - 7, -2) 13 14 # int boundaries 15 maxint64 = (1 << 63) - 1 16 minint64 = -1 << 63 17 maxint32 = (1 << 31) - 1 18 minint32 = -1 << 31 19 assert.eq(maxint64, 9223372036854775807) 20 assert.eq(minint64, -9223372036854775808) 21 assert.eq(maxint32, 2147483647) 22 assert.eq(minint32, -2147483648) 23 24 25 # truth 26 def truth() { 27 assert.true(not 0) 28 for m in [1, maxint32] { # Test small/big ranges 29 assert.true(123*m) 30 assert.true(-1*m) 31 } 32 } 33 truth() 34 35 # floored division 36 # (For real division, see float.star.) 37 def division() { 38 for m in [1, maxint32] { # Test small/big ranges 39 assert.eq((100*m) // (7*m), 14) 40 assert.eq((100*m) // (-7*m), -15) 41 assert.eq((-100*m) // (7*m), -15) # NB: different from Go/Java 42 assert.eq((-100*m) // (-7*m), 14) # NB: different from Go/Java 43 assert.eq((98*m) // (7*m), 14) 44 assert.eq((98*m) // (-7*m), -14) 45 assert.eq((-98*m) // (7*m), -14) 46 assert.eq((-98*m) // (-7*m), 14) 47 } 48 } 49 50 division() 51 52 # remainder 53 def remainder() { 54 for m in [1, maxint32] { # Test small/big ranges 55 assert.eq((100*m) % (7*m), 2*m) 56 assert.eq((100*m) % (-7*m), -5*m) # NB: different from Go/Java 57 assert.eq((-100*m) % (7*m), 5*m) # NB: different from Go/Java 58 assert.eq((-100*m) % (-7*m), -2*m) 59 assert.eq((98*m) % (7*m), 0) 60 assert.eq((98*m) % (-7*m), 0) 61 assert.eq((-98*m) % (7*m), 0) 62 assert.eq((-98*m) % (-7*m), 0) 63 } 64 } 65 remainder() 66 67 # compound assignment 68 def compound() { 69 x = 1 70 x += 1 71 assert.eq(x, 2) 72 x -= 3 73 assert.eq(x, -1) 74 x *= 39 75 assert.eq(x, -39) 76 x //= 4 77 assert.eq(x, -10) 78 x /= -2 79 assert.eq(x, 5) 80 x %= 3 81 assert.eq(x, 2) 82 # use resolve.AllowBitwise to enable the ops: 83 x = 2 84 x &= 1 85 assert.eq(x, 0) 86 x |= 2 87 assert.eq(x, 2) 88 x ^= 3 89 assert.eq(x, 1) 90 x <<= 2 91 assert.eq(x, 4) 92 x >>=2 93 assert.eq(x, 1) 94 } 95 compound() 96 97 # int conversion 98 # See float.star for float-to-int conversions. 99 # We follow Python 3 here, but I can't see the method in its madness. 100 # int from bool/int/float 101 assert.fails(int, 'missing argument') # int() 102 assert.eq(int(False), 0) 103 assert.eq(int(True), 1) 104 assert.eq(int(3), 3) 105 assert.eq(int(3.1), 3) 106 assert.fails(=> int(3, base:10), "non-string with explicit base") 107 assert.fails(=> int(True, 10), "non-string with explicit base") 108 # int from string, base implicitly 10 109 assert.eq(int("100000000000000000000"), 10000000000 * 10000000000) 110 assert.eq(int("-100000000000000000000"), -10000000000 * 10000000000) 111 assert.eq(int("123"), 123) 112 assert.eq(int("-123"), -123) 113 assert.eq(int("0123"), 123) # not octal 114 assert.eq(int("-0123"), -123) 115 assert.fails(=> int("0x12"), "invalid literal with base 10") 116 assert.fails(=> int("-0x12"), "invalid literal with base 10") 117 assert.fails(=> int("0o123"), "invalid literal.*base 10") 118 assert.fails(=> int("-0o123"), "invalid literal.*base 10") 119 # int from string, explicit base 120 assert.eq(int("0"), 0) 121 assert.eq(int("00"), 0) 122 assert.eq(int("0", base:10), 0) 123 assert.eq(int("00", base:10), 0) 124 assert.eq(int("0", base:8), 0) 125 assert.eq(int("00", base:8), 0) 126 assert.eq(int("-0"), 0) 127 assert.eq(int("-00"), 0) 128 assert.eq(int("-0", base:10), 0) 129 assert.eq(int("-00", base:10), 0) 130 assert.eq(int("-0", base:8), 0) 131 assert.eq(int("-00", base:8), 0) 132 assert.eq(int("+0"), 0) 133 assert.eq(int("+00"), 0) 134 assert.eq(int("+0", base:10), 0) 135 assert.eq(int("+00", base:10), 0) 136 assert.eq(int("+0", base:8), 0) 137 assert.eq(int("+00", base:8), 0) 138 assert.eq(int("11", base:9), 10) 139 assert.eq(int("-11", base:9), -10) 140 assert.eq(int("10011", base:2), 19) 141 assert.eq(int("-10011", base:2), -19) 142 assert.eq(int("123", 8), 83) 143 assert.eq(int("-123", 8), -83) 144 assert.eq(int("0123", 8), 83) # redundant zeros permitted 145 assert.eq(int("-0123", 8), -83) 146 assert.eq(int("00123", 8), 83) 147 assert.eq(int("-00123", 8), -83) 148 assert.eq(int("0o123", 8), 83) 149 assert.eq(int("-0o123", 8), -83) 150 assert.eq(int("123", 7), 66) # 1*7*7 + 2*7 + 3 151 assert.eq(int("-123", 7), -66) 152 assert.eq(int("12", 16), 18) 153 assert.eq(int("-12", 16), -18) 154 assert.eq(int("0x12", 16), 18) 155 assert.eq(int("-0x12", 16), -18) 156 assert.eq(0x1000000000000001 * 0x1000000000000001, 0x1000000000000002000000000000001) 157 assert.eq(int("1010", 2), 10) 158 assert.eq(int("111111101", 2), 509) 159 assert.eq(int("0b0101", 0), 5) 160 assert.eq(int("0b00000", 0), 0) 161 assert.eq(1111111111111111 * 1111111111111111, 1234567901234567654320987654321) 162 assert.fails(=> int("0x123", 8), "invalid literal.*base 8") 163 assert.fails(=> int("-0x123", 8), "invalid literal.*base 8") 164 assert.fails(=> int("0o123", 16), "invalid literal.*base 16") 165 assert.fails(=> int("-0o123", 16), "invalid literal.*base 16") 166 assert.fails(=> int("0x110", 2), "invalid literal.*base 2") 167 # int from string, auto detect base 168 assert.eq(int("123", 0), 123) 169 assert.eq(int("+123", 0), +123) 170 assert.eq(int("-123", 0), -123) 171 assert.eq(int("0x12", 0), 18) 172 assert.eq(int("+0x12", 0), +18) 173 assert.eq(int("-0x12", 0), -18) 174 assert.eq(int("0o123", 0), 83) 175 assert.eq(int("+0o123", 0), +83) 176 assert.eq(int("-0o123", 0), -83) 177 assert.fails(=> int("0123", 0), "invalid literal.*base 0") # valid in Python 2.7 178 assert.fails(=> int("-0123", 0), "invalid literal.*base 0") 179 # github.com/google/starlark-go/issues/108 180 assert.fails(=> int("0Oxa", 8), "invalid literal with base 8: 0Oxa") 181 # follow-on bugs to issue 108 182 assert.fails(=> int("--4"), "invalid literal with base 10: --4") 183 assert.fails(=> int("++4"), "invalid literal with base 10: \\+\\+4") 184 assert.fails(=> int("+-4"), "invalid literal with base 10: \\+-4") 185 assert.fails(=> int("0x-4", 16), "invalid literal with base 16: 0x-4") 186 187 # bitwise union (int|int), intersection (int&int), XOR (int^int), unary not (~int), 188 # left shift (int<<int), and right shift (int>>int). 189 # use resolve.AllowBitwise to enable the ops. 190 # TODO(adonovan): this is not yet in the Starlark spec, 191 # but there is consensus that it should be. 192 assert.eq(1|2, 3) 193 assert.eq(3|6, 7) 194 assert.eq((1|2) & (2|4), 2) 195 assert.eq(1 ^ 2, 3) 196 assert.eq(2 ^ 2, 0) 197 assert.eq(1 | 0 ^ 1, 1) # check | and ^ operators precedence 198 assert.eq(~1, -2) 199 assert.eq(~-2, 1) 200 assert.eq(~0, -1) 201 assert.eq(1 << 2, 4) 202 assert.eq(2 >> 1, 1) 203 assert.fails(=> 2 << -1, "negative shift count") 204 assert.fails(=> 1 << 512, "shift count too large") 205 206 # comparisons 207 # TODO(adonovan): test: < > == != etc 208 def comparisons() { 209 for m in [1, maxint32/2, maxint32] { # Test small/big ranges 210 assert.lt(-2*m, -1*m) 211 assert.lt(-1*m, 0*m) 212 assert.lt(0*m, 1*m) 213 assert.lt(1*m, 2*m) 214 assert.true(2*m >= 2*m) 215 assert.true(2*m > 1*m) 216 assert.true(1*m >= 1*m) 217 assert.true(1*m > 0*m) 218 assert.true(0*m >= 0*m) 219 assert.true(0*m > -1*m) 220 assert.true(-1*m >= -1*m) 221 assert.true(-1*m > -2*m) 222 } 223 } 224 225 comparisons() 226 227 # precision 228 assert.eq(str(maxint64), "9223372036854775807") 229 assert.eq(str(maxint64+1), "9223372036854775808") 230 assert.eq(str(minint64), "-9223372036854775808") 231 assert.eq(str(minint64-1), "-9223372036854775809") 232 assert.eq(str(minint64 * minint64), "85070591730234615865843651857942052864") 233 assert.eq(str(maxint32+1), "2147483648") 234 assert.eq(str(minint32-1), "-2147483649") 235 assert.eq(str(minint32 * minint32), "4611686018427387904") 236 assert.eq(str(minint32 | maxint32), "-1") 237 assert.eq(str(minint32 & minint32), "-2147483648") 238 assert.eq(str(minint32 ^ maxint32), "-1") 239 assert.eq(str(minint32 // -1), "2147483648") 240 241 # string formatting 242 assert.eq("%o %x %d" % (0o755, 0xDEADBEEF, 42), "755 deadbeef 42") 243 nums = [-95, -1, 0, +1, +95] 244 assert.eq(' '.join(["%o" % x for x in nums]), "-137 -1 0 1 137") 245 assert.eq(' '.join(["%d" % x for x in nums]), "-95 -1 0 1 95") 246 assert.eq(' '.join(["%i" % x for x in nums]), "-95 -1 0 1 95") 247 assert.eq(' '.join(["%x" % x for x in nums]), "-5f -1 0 1 5f") 248 assert.eq(' '.join(["%X" % x for x in nums]), "-5F -1 0 1 5F") 249 assert.eq("%o %x %d" % (123, 123, 123), "173 7b 123") 250 assert.eq("%o %x %d" % (123.1, 123.1, 123.1), "173 7b 123") # non-int operands are acceptable 251 assert.fails(=> "%d" % True, "cannot convert bool to int")