summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-04-27 08:33:03 -0700
committerGitHub <noreply@github.com>2020-04-27 17:33:03 +0200
commitbb982c644b8e3e8a59cf2c60b28ff91741c7dc9b (patch)
treea922e3a355509a5d12fe7848a1e6c9f5ba3b2386 /tests
parent664cb2c0be4e31d40cd3a5a2b9013f1afe47df97 (diff)
downloadNim-bb982c644b8e3e8a59cf2c60b28ff91741c7dc9b.tar.gz
`$(a: float)` now works consistently in nim js, avoiding printing floats as ints (#14134)
* fix https://github.com/timotheecour/Nim/issues/133; $(a: float) works in nim js like in other backends

* fix tests

* fix test for windows that prints 1.1e17 differently than other OS
Diffstat (limited to 'tests')
-rw-r--r--tests/js/tbyvar.nim11
-rw-r--r--tests/js/tdollar_float.nim62
2 files changed, 68 insertions, 5 deletions
diff --git a/tests/js/tbyvar.nim b/tests/js/tbyvar.nim
index 705d62574..a4c60b0b3 100644
--- a/tests/js/tbyvar.nim
+++ b/tests/js/tbyvar.nim
@@ -1,15 +1,16 @@
 discard """
-  output: '''foo 12
+  output: '''
+foo 12
 bar 12
 2
 foo 12
 bar 12
 2
 12.5
-(nums: @[5, 5, 10, 5, 5, 5, 5, 5, 5, 5])
-(nums: @[5, 5, 50, 5, 5, 5, 5, 5, 5, 5])
-(nums: @[5, 5, 45, 5, 5, 5, 5, 5, 5, 5])
-(nums: @[5, 5, 9, 5, 5, 5, 5, 5, 5, 5])
+(nums: @[5.0, 5.0, 10.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0])
+(nums: @[5.0, 5.0, 50.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0])
+(nums: @[5.0, 5.0, 45.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0])
+(nums: @[5.0, 5.0, 9.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0])
 asd
 '''
 """
diff --git a/tests/js/tdollar_float.nim b/tests/js/tdollar_float.nim
new file mode 100644
index 000000000..4fd8e3cba
--- /dev/null
+++ b/tests/js/tdollar_float.nim
@@ -0,0 +1,62 @@
+#[
+merge into tests/system/tdollars.nim once https://github.com/nim-lang/Nim/pull/14122
+is merged
+]#
+
+import unittest
+
+block: # https://github.com/timotheecour/Nim/issues/133
+  # simple test
+  var a: float = 2
+  check $a == "2.0"
+
+  # systematic tests
+  template fun(a2: static float) =
+    const a: float = a2 # needed pending https://github.com/timotheecour/Nim/issues/132
+    var b = a
+    check $b == $a
+
+  fun 2
+  fun 2.0
+  fun 2.1
+  fun 1_000
+  fun 1_000.1
+  fun 1_000_000_000.1
+  fun 1_000_000_000_000.1
+
+  # negatives
+  fun -2.0
+  fun -2.1
+
+  # 0
+  fun 0
+  fun -0
+  fun 0.0
+
+  block:
+    var a = -0.0
+    check $a in ["-0.0", "0.0"]
+
+  # exponents
+  block:
+    var a = 5e20
+    check $a in ["5e20", "500000000000000000000.0"]
+
+  fun 3.4e1'f32
+  fun 3.4e-1'f32
+  fun -3.4e-1'f32
+  fun 3.4e-1'f32
+  fun 3e-1'f32
+
+  block:
+    var a = 3.4e38'f32
+    check $a in ["3.4e+38", "3.4e+038"]
+      # on windows, printf (used in VM) prints as 3.4e+038
+      # but js prints as 3.4e+38
+      # on osx, both print as 3.4e+38
+      # see https://github.com/timotheecour/Nim/issues/138
+
+  when false: # edge cases
+    fun -0.0 # see https://github.com/timotheecour/Nim/issues/136
+    fun 5e20
+    fun 3.4e38'f32