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