summaryrefslogtreecommitdiff
path: root/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/compiler/IntTest.scala
blob: 60f877395caecb4a891542950e0ff6ce5abd05eb (plain) (blame)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js Test Suite        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */
package scala.scalajs.testsuite.compiler

import org.scalajs.jasminetest.JasmineTest

object IntTest extends JasmineTest {

  /* General note on the way these tests are written:
   * We leverage the constant folding applied by the Scala compiler to write
   * sound tests. We always perform the same operation, on the same operands,
   * once in a way constant folding understands, and once in a way it doesn't.
   * Since constant folding is performed on the JVM, we know it has the right
   * semantics.
   */

  // final val without type ascription to make sure these are constant-folded
  final val MinVal = Int.MinValue
  final val MaxVal = Int.MaxValue
  final val AlmostMinVal = Int.MinValue + 43
  final val AlmostMaxVal = Int.MaxValue - 36

  describe("Int primitives") {

    it("should support unary -") {
      def test(a: Int, expected: Int): Unit =
        expect(-a).toEqual(expected)

      test(56, -56)
      test(0, 0)
      test(-36, 36)

      test(MaxVal, -MaxVal)
      test(MinVal, -MinVal)
      test(-MaxVal, MaxVal)
      test(AlmostMinVal, -AlmostMinVal)
      test(AlmostMaxVal, -AlmostMaxVal)
    }

    it("should support +") {
      def test(a: Int, b: Int, expected: Int): Unit =
        expect(a + b).toEqual(expected)

      test(56, 654, 56 + 654)
      test(0, 25, 0 + 25)
      test(-36, 13, -36 + 13)

      test(MaxVal, 1, MaxVal + 1)
      test(MinVal, -1, MinVal - 1)
      test(MaxVal, MinVal, MaxVal + MinVal)
      test(AlmostMinVal, -100, AlmostMinVal - 100)
      test(AlmostMaxVal, 123, AlmostMaxVal + 123)
    }

    it("should support -") {
      def test(a: Int, b: Int, expected: Int): Unit =
        expect(a - b).toEqual(expected)

      test(56, 654, 56 - 654)
      test(0, 25, 0 - 25)
      test(-36, 13, -36 - 13)

      test(MaxVal, -1, MaxVal + 1)
      test(MinVal, 1, MinVal - 1)
      test(MaxVal, MinVal, MaxVal - MinVal)
      test(AlmostMinVal, 100, AlmostMinVal - 100)
      test(AlmostMaxVal, -123, AlmostMaxVal + 123)
    }

    it("should support *") {
      def test(a: Int, b: Int, expected: Int): Unit =
        expect(a * b).toEqual(expected)

      test(56, 654, 56 * 654)
      test(0, 25, 0 * 25)
      test(-36, 13, -36 * 13)
      test(-5, -6, -5 * -6)

      test(MinVal, 1, MinVal * 1)
      test(MinVal, -1, MinVal * -1)
      test(MaxVal, 1, MaxVal * 1)
      test(MaxVal, -1, MaxVal * -1)

      test(MaxVal, MinVal, MaxVal * MinVal)
      test(MaxVal, MaxVal, MaxVal * MaxVal)
      test(MinVal, MaxVal, MinVal * MaxVal)
      test(MinVal, MinVal, MinVal * MinVal)

      test(AlmostMaxVal, 2, AlmostMaxVal * 2)
      test(AlmostMaxVal, 5, AlmostMaxVal * 5)
      test(AlmostMaxVal, -7, AlmostMaxVal * -7)
      test(AlmostMaxVal, -14, AlmostMaxVal * -14)
      test(AlmostMinVal, 100, AlmostMinVal * 100)
      test(AlmostMaxVal, -123, AlmostMaxVal * -123)
    }

    it("should support /") {
      def test(a: Int, b: Int, expected: Int): Unit =
        expect(a / b).toEqual(expected)

      test(654, 56, 654 / 56)
      test(0, 25, 0 / 25)
      test(-36, 13, -36 / 13)
      test(-55, -6, -55 / -6)

      test(MinVal, 1, MinVal / 1)
      test(MinVal, -1, MinVal / -1)
      test(MaxVal, 1, MaxVal / 1)
      test(MaxVal, -1, MaxVal / -1)

      test(MaxVal, MinVal, MaxVal / MinVal)
      test(MaxVal, MaxVal, MaxVal / MaxVal)
      test(MinVal, MaxVal, MinVal / MaxVal)
      test(MinVal, MinVal, MinVal / MinVal)

      test(AlmostMaxVal, 2, AlmostMaxVal / 2)
      test(AlmostMaxVal, 5, AlmostMaxVal / 5)
      test(AlmostMaxVal, -7, AlmostMaxVal / -7)
      test(AlmostMaxVal, -14, AlmostMaxVal / -14)
      test(AlmostMinVal, 100, AlmostMinVal / 100)
      test(AlmostMaxVal, -123, AlmostMaxVal / -123)
    }

    unless("phantomjs"). // see #593
    it("should support %") {
      def test(a: Int, b: Int, expected: Int): Unit =
        expect(a % b).toEqual(expected)

      test(654, 56, 654 % 56)
      test(0, 25, 0 % 25)
      test(-36, 13, -36 % 13)
      test(-55, -6, -55 % -6)

      test(MinVal, 1, MinVal % 1)
      test(MinVal, -1, MinVal % -1)
      test(MaxVal, 1, MaxVal % 1)
      test(MaxVal, -1, MaxVal % -1)

      test(MaxVal, MinVal, MaxVal % MinVal)
      test(MaxVal, MaxVal, MaxVal % MaxVal)
      test(MinVal, MaxVal, MinVal % MaxVal)
      test(MinVal, MinVal, MinVal % MinVal)

      test(AlmostMaxVal, 2, AlmostMaxVal % 2)
      test(AlmostMaxVal, 5, AlmostMaxVal % 5)
      test(AlmostMaxVal, -7, AlmostMaxVal % -7)
      test(AlmostMaxVal, -14, AlmostMaxVal % -14)
      test(AlmostMinVal, 100, AlmostMinVal % 100)
      test(AlmostMaxVal, -123, AlmostMaxVal % -123)
    }

    it("should support <<") {
      def test(a: Int, b: Int, expected: Int): Unit =
        expect(a << b).toEqual(expected)

      test(0, 5, 0 << 5)
      test(1, 5, 1 << 5)
      test(13, 4, 13 << 4)
      test(-35, 5, -35 << 5)
      test(345, 0, 345 << 0)

      test(MinVal, 0, MinVal << 0)
      test(MaxVal, 0, MaxVal << 0)
      test(MinVal, 1, MinVal << 1)
      test(MaxVal, 1, MaxVal << 1)
    }

    it("should support >>") {
      def test(a: Int, b: Int, expected: Int): Unit =
        expect(a >> b).toEqual(expected)

      test(0, 5, 0 >> 5)
      test(32, 5, 32 >> 5)
      test(31, 4, 31 >> 4)
      test(-355, 5, -355 >> 5)
      test(345, 0, 345 >> 0)

      test(MinVal, 0, MinVal >> 0)
      test(MaxVal, 0, MaxVal >> 0)
      test(MinVal, 1, MinVal >> 1)
      test(MaxVal, 1, MaxVal >> 1)
    }

    it("should support >>>") {
      def test(a: Int, b: Int, expected: Int): Unit =
        expect(a >>> b).toEqual(expected)

      test(0, 5, 0 >>> 5)
      test(32, 5, 32 >>> 5)
      test(31, 4, 31 >>> 4)
      test(-355, 5, -355 >>> 5)
      test(345, 0, 345 >>> 0)

      test(MinVal, 0, MinVal >>> 0)
      test(MaxVal, 0, MaxVal >>> 0)
      test(MinVal, 1, MinVal >>> 1)
      test(MaxVal, 1, MaxVal >>> 1)
    }

  }
}