PageRenderTime 35ms CodeModel.GetById 23ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/test/numbers.coffee

http://github.com/jashkenas/coffee-script
CoffeeScript | 88 lines | 51 code | 21 blank | 16 comment | 1 complexity | 3a0225f3ae2e18ab8a122d54ddc7590c MD5 | raw file
 1# Number Literals
 2# ---------------
 3
 4# * Decimal Integer Literals
 5# * Octal Integer Literals
 6# * Hexadecimal Integer Literals
 7# * Scientific Notation Integer Literals
 8# * Scientific Notation Non-Integer Literals
 9# * Non-Integer Literals
10# * Binary Integer Literals
11
12
13# Binary Integer Literals
14# Binary notation is understood as would be decimal notation.
15
16test "Parser recognises binary numbers", ->
17  eq 4, 0b100
18
19# Decimal Integer Literals
20
21test "call methods directly on numbers", ->
22  eq 4, 4.valueOf()
23  eq '11', 4.toString 3
24
25eq -1, 3 -4
26
27#764: Numbers should be indexable
28eq Number::toString, 42['toString']
29
30eq Number::toString, 42.toString
31
32eq Number::toString, 2e308['toString'] # Infinity
33
34
35# Non-Integer Literals
36
37# Decimal number literals.
38value = .25 + .75
39ok value is 1
40value = 0.0 + -.25 - -.75 + 0.0
41ok value is 0.5
42
43#764: Numbers should be indexable
44eq Number::toString,   4['toString']
45eq Number::toString, 4.2['toString']
46eq Number::toString, .42['toString']
47eq Number::toString, (4)['toString']
48
49eq Number::toString,   4.toString
50eq Number::toString, 4.2.toString
51eq Number::toString, .42.toString
52eq Number::toString, (4).toString
53
54test '#1168: leading floating point suppresses newline', ->
55  eq 1, do ->
56    1
57    .5 + 0.5
58
59test "Python-style octal literal notation '0o777'", ->
60  eq 511, 0o777
61  eq 1, 0o1
62  eq 1, 0o00001
63  eq parseInt('0777', 8), 0o777
64  eq '777', 0o777.toString 8
65  eq 4, 0o4.valueOf()
66  eq Number::toString, 0o777['toString']
67  eq Number::toString, 0o777.toString
68
69test "#2060: Disallow uppercase radix prefixes and exponential notation", ->
70  for char in ['b', 'o', 'x', 'e']
71    program = "0#{char}0"
72    doesNotThrowCompileError program, bare: yes
73    throwsCompileError program.toUpperCase(), bare: yes
74
75test "#2224: hex literals with 0b or B or E", ->
76  eq 176, 0x0b0
77  eq 177, 0x0B1
78  eq 225, 0xE1
79
80test "Infinity", ->
81  eq Infinity, CoffeeScript.eval "0b#{Array(1024 + 1).join('1')}"
82  eq Infinity, CoffeeScript.eval "0o#{Array(342 + 1).join('7')}"
83  eq Infinity, CoffeeScript.eval "0x#{Array(256 + 1).join('f')}"
84  eq Infinity, CoffeeScript.eval Array(500 + 1).join('9')
85  eq Infinity, 2e308
86
87test "NaN", ->
88  ok isNaN 1/NaN