summary refs log tree commit diff stats
path: root/tests/stdlib/tmath.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/stdlib/tmath.nim')
-rw-r--r--tests/stdlib/tmath.nim30
1 files changed, 26 insertions, 4 deletions
diff --git a/tests/stdlib/tmath.nim b/tests/stdlib/tmath.nim
index fbc6b80ad..e5cb58eba 100644
--- a/tests/stdlib/tmath.nim
+++ b/tests/stdlib/tmath.nim
@@ -10,6 +10,10 @@ import std/[math, random, os]
 import std/[unittest]
 import std/[sets, tables]
 
+
+# Function for approximate comparison of floats
+proc `==~`(x, y: float): bool = (abs(x-y) < 1e-9)
+
 block: # random int
   block: # there might be some randomness
     var set = initHashSet[int](128)
@@ -158,10 +162,6 @@ block:
     doAssert(erf(6.0) > erf(5.0))
     doAssert(erfc(6.0) < erfc(5.0))
 
-    proc `==~`(x, y: float): bool = (abs(x-y) < 1e-9)
-      # Function for approximate comparison of floats
-      # xxx use `almostEqual`
-
     block: # prod
       doAssert prod([1, 2, 3, 4]) == 24
       doAssert prod([1.5, 3.4]).almostEqual 5.1
@@ -349,6 +349,28 @@ template main =
     doAssert copySign(-NaN, 0.0).isNaN
     doAssert copySign(-NaN, -0.0).isNaN
 
+    block: # round() tests
+      # Round to 0 decimal places
+      doAssert round(54.652) == 55.0
+      doAssert round(54.352) == 54.0
+      doAssert round(-54.652) == -55.0
+      doAssert round(-54.352) == -54.0
+      doAssert round(0.0) == 0.0
+      doAssert 1 / round(0.0) == Inf
+      doAssert 1 / round(-0.0) == -Inf
+      doAssert round(Inf) == Inf
+      doAssert round(-Inf) == -Inf
+      doAssert round(NaN).isNaN
+      doAssert round(-NaN).isNaN
+      doAssert round(-0.5) == -1.0
+      doAssert round(0.5) == 1.0
+      doAssert round(-1.5) == -2.0
+      doAssert round(1.5) == 2.0
+      doAssert round(-2.5) == -3.0
+      doAssert round(2.5) == 3.0
+      doAssert round(2.5'f32) == 3.0'f32
+      doAssert round(2.5'f64) == 3.0'f64
+
     when nimvm:
       discard
     else:
'#n79'>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