summary refs log tree commit diff stats
path: root/tests/run
diff options
context:
space:
mode:
authorSimon Hafner <hafnersimon@gmail.com>2012-09-05 17:01:39 -0500
committerSimon Hafner <hafnersimon@gmail.com>2012-09-06 16:42:53 -0500
commit33cabeb04d4dc00f4edf40096f790b510f915dd9 (patch)
tree5171ab914a444171508d6c698129ffb7d96c81a6 /tests/run
parent3a1a1976645ac9ad2c449b0afea57c7c154ec37f (diff)
downloadNim-33cabeb04d4dc00f4edf40096f790b510f915dd9.tar.gz
+ random number generator for floats and slices - with tests
I also took the liberty to unify the API between ECMAScript and C.
Diffstat (limited to 'tests/run')
-rw-r--r--tests/run/tmath.nim50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/run/tmath.nim b/tests/run/tmath.nim
new file mode 100644
index 000000000..67837b21a
--- /dev/null
+++ b/tests/run/tmath.nim
@@ -0,0 +1,50 @@
+import math
+import unittest
+import sets
+
+suite "random int":
+  test "there might be some randomness":
+    var set = initSet[int](128)
+    for i in 1..10:
+      for j in 1..10:
+        randomize()
+        incl(set, random(high(int)))
+    check len(set) == 100
+  test "single number bounds work":
+    randomize()
+    var rand: int
+    for i in 1..1000:
+      rand = random(1000)
+      check rand < 1000
+      check rand > -1
+  test "slice bounds work":
+    randomize()
+    var rand: int
+    for i in 1..1000:
+      rand = random(100..1000)
+      check rand < 1000
+      check rand >= 100
+
+suite "random float":
+  # Enable this once #197 has been resolved
+  # test "there might be some randomness":
+  #   var set = initSet[float](128)
+  #   for i in 1..10:
+  #     for j in 1..10:
+  #       randomize()
+  #       incl(set, random(1.0))
+  #   check len(set) == 100
+  test "single number bounds work":
+    randomize()
+    var rand: float
+    for i in 1..1000:
+      rand = random(1000.0)
+      check rand < 1000.0
+      check rand > -1.0
+  test "slice bounds work":
+    randomize()
+    var rand: float
+    for i in 1..1000:
+      rand = random(100.0..1000.0)
+      check rand < 1000.0
+      check rand >= 100.0