summaryrefslogtreecommitdiff
path: root/examples/scala-js/test-suite/src/test/scala/scala/scalajs/testsuite/javalib/LongTest.scala
blob: 86783c387e58668d986509b17293ce6527f32d5b (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
/*                     __                                               *\
**     ________ ___   / /  ___      __ ____  Scala.js Test Suite        **
**    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, LAMP/EPFL        **
**  __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \    http://scala-js.org/       **
** /____/\___/_/ |_/____/_/ | |__/ /____/                               **
**                          |/____/                                     **
\*                                                                      */
package scala.scalajs.testsuite.javalib

import java.lang.{Long => JLong}

import org.scalajs.jasminetest.JasmineTest

/**
 * tests the implementation of the java standard library Long
 * requires jsinterop/LongTest to work to make sense
 */
object LongTest extends JasmineTest {

  describe("java.lang.Long") {
    it("should provide `reverseBytes`") {
      expect(JLong.reverseBytes(0xf5ab689cd401ff14L) == 0x14ff01d49c68abf5L).toBeTruthy
    }

    it("should provide `rotateLeft`") {
      expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 0) == 0xf5ab689cd401ff14L).toBeTruthy
      expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 1) == 0xeb56d139a803fe29L).toBeTruthy
      expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 8) == 0xab689cd401ff14f5L).toBeTruthy
      expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 13) == 0x6d139a803fe29eb5L).toBeTruthy
      expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 64) == 0xf5ab689cd401ff14L).toBeTruthy
      expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 65) == 0xeb56d139a803fe29L).toBeTruthy
      expect(JLong.rotateLeft(0xf5ab689cd401ff14L, 80) == 0x689cd401ff14f5abL).toBeTruthy
      expect(JLong.rotateLeft(0xf5ab689cd401ff14L, -1) == 0x7ad5b44e6a00ff8aL).toBeTruthy
      expect(JLong.rotateLeft(0xf5ab689cd401ff14L, -56) == 0xab689cd401ff14f5L).toBeTruthy
      expect(JLong.rotateLeft(0xf5ab689cd401ff14L, -70) == 0x53d6ada2735007fcL).toBeTruthy
    }

    it("should provide `rotateRight`") {
      expect(JLong.rotateRight(0xf5ab689cd401ff14L, 0) == 0xf5ab689cd401ff14L).toBeTruthy
      expect(JLong.rotateRight(0xf5ab689cd401ff14L, 1) == 0x7ad5b44e6a00ff8aL).toBeTruthy
      expect(JLong.rotateRight(0xf5ab689cd401ff14L, 8) == 0x14f5ab689cd401ffL).toBeTruthy
      expect(JLong.rotateRight(0xf5ab689cd401ff14L, 13) == 0xf8a7ad5b44e6a00fL).toBeTruthy
      expect(JLong.rotateRight(0xf5ab689cd401ff14L, 64) == 0xf5ab689cd401ff14L).toBeTruthy
      expect(JLong.rotateRight(0xf5ab689cd401ff14L, 65) == 0x7ad5b44e6a00ff8aL).toBeTruthy
      expect(JLong.rotateRight(0xf5ab689cd401ff14L, 80) == 0xff14f5ab689cd401L).toBeTruthy
      expect(JLong.rotateRight(0xf5ab689cd401ff14L, -1) == 0xeb56d139a803fe29L).toBeTruthy
      expect(JLong.rotateRight(0xf5ab689cd401ff14L, -56) == 0x14f5ab689cd401ffL).toBeTruthy
      expect(JLong.rotateRight(0xf5ab689cd401ff14L, -70) == 0x6ada2735007fc53dL).toBeTruthy
    }

    it("should implement bitCount") {
      expect(JLong.bitCount(0L)).toEqual(0)
      expect(JLong.bitCount(35763829229342837L)).toEqual(26)
      expect(JLong.bitCount(-350003829229342837L)).toEqual(32)
    }

    it("should provide `compareTo`") {
      def compare(x: Long, y: Long): Int =
        new JLong(x).compareTo(new JLong(y))

      expect(compare(0L, 5L)).toBeLessThan(0)
      expect(compare(10L, 9L)).toBeGreaterThan(0)
      expect(compare(-2L, -1L)).toBeLessThan(0)
      expect(compare(3L, 3L)).toEqual(0)
    }

    it("should be a Comparable") {
      def compare(x: Any, y: Any): Int =
        x.asInstanceOf[Comparable[Any]].compareTo(y)

      expect(compare(0L, 5L)).toBeLessThan(0)
      expect(compare(10L, 9L)).toBeGreaterThan(0)
      expect(compare(-2L, -1L)).toBeLessThan(0)
      expect(compare(3L, 3L)).toEqual(0)
    }

    it("should parse strings") {
      def test(s: String, v: Long): Unit = {
        expect(JLong.parseLong(s)).toEqual(v)
        expect(JLong.valueOf(s).longValue()).toEqual(v)
        expect(new JLong(s).longValue()).toEqual(v)
      }

      test("0", 0L)
      test("5", 5L)
      test("127", 127L)
      test("-100", -100L)
      test("30000", 30000L)
      test("-90000", -90000L)
      test("4", 4L)
      test("-4", -4L)
      test("4000000000", 4000000000L)
      test("-18014398509482040", -18014398509482040L)
    }

    it("should reject invalid strings when parsing") {
      def test(s: String): Unit =
        expect(() => JLong.parseLong(s)).toThrow

      test("abc")
      test("asdf")
      test("")
    }

    it("should parse strings in base 16") {
      def test(s: String, v: Long): Unit = {
        expect(JLong.parseLong(s, 16)).toEqual(v)
        expect(JLong.valueOf(s, 16).longValue()).toEqual(v)
      }

      test("0", 0x0L)
      test("5", 0x5L)
      test("ff", 0xffL)
      test("-24", -0x24L)
      test("30000", 0x30000L)
      test("-90000", -0x90000L)
    }

    it("should implement toString") {
      expect(Int.MaxValue.toLong.toString).toEqual("2147483647")
      expect((-50L).toString).toEqual("-50")
      expect((-1000000000L).toString).toEqual("-1000000000")
      expect((Int.MaxValue.toLong+1L).toString).toEqual("2147483648")
      expect(Int.MinValue.toLong.toString).toEqual("-2147483648")
    }

    it("should implement toBinaryString") {
      expect(JLong.toBinaryString(              0L)).toEqual("0")
      expect(JLong.toBinaryString(             -1L)).toEqual("1111111111111111111111111111111111111111111111111111111111111111")
      expect(JLong.toBinaryString(      456324454L)).toEqual("11011001100101111010101100110")
      expect(JLong.toBinaryString(     -456324454L)).toEqual("1111111111111111111111111111111111100100110011010000101010011010")
      expect(JLong.toBinaryString( 98765432158845L)).toEqual("10110011101001110011110011111111111101001111101")
      expect(JLong.toBinaryString(-49575304457780L)).toEqual("1111111111111111110100101110100101011001100101101001000111001100")
      expect(JLong.toBinaryString(Long.MinValue   )).toEqual("1000000000000000000000000000000000000000000000000000000000000000")
      expect(JLong.toBinaryString(Long.MaxValue   )).toEqual("111111111111111111111111111111111111111111111111111111111111111")
    }

    it("should implement toHexString") {
      expect(JLong.toHexString(              0L)).toEqual("0")
      expect(JLong.toHexString(             -1L)).toEqual("ffffffffffffffff")
      expect(JLong.toHexString(      456324454L)).toEqual("1b32f566")
      expect(JLong.toHexString(     -456324454L)).toEqual("ffffffffe4cd0a9a")
      expect(JLong.toHexString( 98765432158845L)).toEqual("59d39e7ffa7d")
      expect(JLong.toHexString(-49575304457780L)).toEqual("ffffd2e9599691cc")
      expect(JLong.toHexString(Long.MinValue   )).toEqual("8000000000000000")
      expect(JLong.toHexString(Long.MaxValue   )).toEqual("7fffffffffffffff")
    }

    it("should implement toOctalString") {
      expect(JLong.toOctalString(              0L)).toEqual("0")
      expect(JLong.toOctalString(             -1L)).toEqual("1777777777777777777777")
      expect(JLong.toOctalString(      456324454L)).toEqual("3314572546")
      expect(JLong.toOctalString(     -456324454L)).toEqual("1777777777774463205232")
      expect(JLong.toOctalString( 98765432158845L)).toEqual("2635163637775175")
      expect(JLong.toOctalString(-49575304457780L)).toEqual("1777776456453145510714")
      expect(JLong.toOctalString(Long.MinValue   )).toEqual("1000000000000000000000")
      expect(JLong.toOctalString(Long.MaxValue   )).toEqual("777777777777777777777")
    }

    it("should correctly compute trailing zeros") {
      expect(JLong.numberOfTrailingZeros(0xff10000000000000L)).toEqual(52)
      expect(JLong.numberOfTrailingZeros(0xff20000000000000L)).toEqual(53)
      expect(JLong.numberOfTrailingZeros(0xff40000000000000L)).toEqual(54)
      expect(JLong.numberOfTrailingZeros(0xff80000000000000L)).toEqual(55)

      expect(JLong.numberOfTrailingZeros(0x0000010000000000L)).toEqual(40)
      expect(JLong.numberOfTrailingZeros(0x0000020000000000L)).toEqual(41)
      expect(JLong.numberOfTrailingZeros(0x0000040000000000L)).toEqual(42)
      expect(JLong.numberOfTrailingZeros(0x0000080000000000L)).toEqual(43)

      expect(JLong.numberOfTrailingZeros(0x0000000000010000L)).toEqual(16)
      expect(JLong.numberOfTrailingZeros(0x0000000000020000L)).toEqual(17)
      expect(JLong.numberOfTrailingZeros(0x0000000000040000L)).toEqual(18)
      expect(JLong.numberOfTrailingZeros(0x0000000000080000L)).toEqual(19)
    }

  }
}