tjsbigints.nim 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. discard """
  2. targets: "js"
  3. """
  4. import std/[jsbigints, assertions]
  5. let big1: JsBigInt = big"2147483647"
  6. let big2: JsBigInt = big"666"
  7. var big3: JsBigInt = big"2"
  8. doAssert big3 == big"2"
  9. doAssert (big3 xor big2) == big"664"
  10. doAssert (big"555" and big"2") == big"2"
  11. doAssert (big"555" or big"2") == big"555"
  12. doAssert (big1 mod big2) == big"613"
  13. doAssert -big1 == big"-2147483647"
  14. doAssert big1 div big2 == big"3224449"
  15. doAssert big1 + big2 == big"2147484313"
  16. doAssert big1 - big2 == big"2147482981"
  17. doAssert big1 shl big3 == big"8589934588"
  18. doAssert big1 shr big3 == big"536870911"
  19. doAssert big1 * big2 == big"1430224108902"
  20. doAssert $big1 == "2147483647n"
  21. doAssert big1.toCstring(10) == "2147483647".cstring
  22. doAssert big2 ** big3 == big(443556)
  23. var huge = big"999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"
  24. huge.inc
  25. huge = huge + -999999999999999999999999999999999999999999999999999999999999999999999999999999999999999'big
  26. doAssert huge == big"1"
  27. var list: seq[JsBigInt]
  28. for i in big"0" .. big"5":
  29. doAssert i is JsBigInt
  30. list.add i
  31. doAssert list == @[big"0", big"1", big"2", big"3", big"4", big"5"]
  32. list = @[]
  33. for i in big"0" ..< big"5":
  34. doAssert i is JsBigInt
  35. list.add i
  36. doAssert list == @[big"0", big"1", big"2", big"3", big"4"]
  37. block:
  38. let b = 2'big
  39. doAssert -b ** 3'big == -8'big
  40. doAssert -b ** big"2" == big"4" # not -4 because of precedence
  41. doAssert -big"3" == big"-3"