tmath.nim 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. discard """
  2. action: run
  3. output: '''[Suite] random int
  4. [Suite] random float
  5. [Suite] ^
  6. '''
  7. """
  8. import math, random, os
  9. import unittest
  10. import sets
  11. suite "random int":
  12. test "there might be some randomness":
  13. var set = initSet[int](128)
  14. randomize()
  15. for i in 1..1000:
  16. incl(set, random(high(int)))
  17. check len(set) == 1000
  18. test "single number bounds work":
  19. randomize()
  20. var rand: int
  21. for i in 1..1000:
  22. rand = random(1000)
  23. check rand < 1000
  24. check rand > -1
  25. test "slice bounds work":
  26. randomize()
  27. var rand: int
  28. for i in 1..1000:
  29. rand = random(100..1000)
  30. check rand < 1000
  31. check rand >= 100
  32. test "randomize() again gives new numbers":
  33. randomize()
  34. var rand1 = random(1000000)
  35. os.sleep(200)
  36. randomize()
  37. var rand2 = random(1000000)
  38. check rand1 != rand2
  39. suite "random float":
  40. test "there might be some randomness":
  41. var set = initSet[float](128)
  42. randomize()
  43. for i in 1..100:
  44. incl(set, random(1.0))
  45. check len(set) == 100
  46. test "single number bounds work":
  47. randomize()
  48. var rand: float
  49. for i in 1..1000:
  50. rand = random(1000.0)
  51. check rand < 1000.0
  52. check rand > -1.0
  53. test "slice bounds work":
  54. randomize()
  55. var rand: float
  56. for i in 1..1000:
  57. rand = random(100.0..1000.0)
  58. check rand < 1000.0
  59. check rand >= 100.0
  60. test "randomize() again gives new numbers":
  61. randomize()
  62. var rand1:float = random(1000000.0)
  63. os.sleep(200)
  64. randomize()
  65. var rand2:float = random(1000000.0)
  66. check rand1 != rand2
  67. suite "^":
  68. test "compiles for valid types":
  69. check: compiles(5 ^ 2)
  70. check: compiles(5.5 ^ 2)
  71. check: compiles(5.5 ^ 2.int8)
  72. check: compiles(5.5 ^ 2.uint)
  73. check: compiles(5.5 ^ 2.uint8)
  74. check: not compiles(5.5 ^ 2.2)