aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/skylark/testdata/int.sky
blob: 095e9810d8b9828eaf127f98543f5e270b6c92ff (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
# Tests of Skylark 'int'

def assert_eq(x, y):
  if x != y:
    fail("%r != %r" % (x, y))

def assert_(cond, msg="assertion failed"):
  if not cond:
    fail(msg)

# basic arithmetic
assert_eq(0 - 1, -1)
assert_eq(1 + 1, 2)
assert_eq(5 + 7, 12)
assert_eq(5 * 7, 35)
assert_eq(5 - 7, -2)

# truth
assert_(123)
assert_(-1)
assert_(not 0)

# comparisons
assert_(5 > 2)
assert_(2 + 1 == 3)
assert_(2 + 1 >= 3)
assert_(not (2 + 1 > 3))
assert_(2 + 2 <= 5)
assert_(not (2 + 1 < 3))

# division
assert_eq(100 // 7, 14)
assert_eq(100 // -7, -15)
assert_eq(-100 // 7, -15) # NB: different from Go / Java
assert_eq(-100 // -7, 14) # NB: different from Go / Java
assert_eq(98 // 7, 14)
assert_eq(98 // -7, -14)
assert_eq(-98 // 7, -14)
assert_eq(-98 // -7, 14)

# remainder
assert_eq(100 % 7, 2)
assert_eq(100 % -7, -5) # NB: different from Go / Java
assert_eq(-100 % 7, 5) # NB: different from Go / Java
assert_eq(-100 % -7, -2)
assert_eq(98 % 7, 0)
assert_eq(98 % -7, 0)
assert_eq(-98 % 7, 0)
assert_eq(-98 % -7, 0)

# precedence
assert_eq(5 - 7 * 2 + 3, -6)
assert_eq(4 * 5 / 2 + 5 / 2 * 4, 18)

# compound assignment
def compound():
  x = 1
  x += 1
  assert_eq(x, 2)
  x -= 3
  assert_eq(x, -1)
  x *= 10
  assert_eq(x, -10)
  x /= -2
  assert_eq(x, 5)
  x %= 3
  assert_eq(x, 2)

compound()

---
1 / 0  ### integer division by zero
---
1 % 0  ### integer modulo by zero