summary refs log tree commit diff stats
path: root/tests/stdlib/tjsbigints.nim
diff options
context:
space:
mode:
authorJuan Carlos <juancarlospaco@gmail.com>2021-01-14 16:19:41 -0300
committerGitHub <noreply@github.com>2021-01-14 20:19:41 +0100
commit41965880ce095da09a1f7e781a0c79e436432401 (patch)
tree8607f0b064b7e81b3b2fcdab1d255f6128cb8bae /tests/stdlib/tjsbigints.nim
parenta90f7a66edd393f04f12fb2f53ef2a6de553cf6b (diff)
downloadNim-41965880ce095da09a1f7e781a0c79e436432401.tar.gz
Add js BigInts (#16409)
* Add BigInts
* Renames tos plurals
* Improve Stringifications
* Update changelog.md

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>

* RunnableExamplerize
* discard the discardable pragma
* Several improvements from peer reviews, more docs
* More doc, more test
* More doc, more test
* Better error message 'Error: usage of low is an {.error.} defined at jsbigints.nim' instead of just 'type mismatch JsBigInt'
* is an overload, rename
* proc to scare kids away
* Update lib/js/jsbigints.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* https://github.com/nim-lang/Nim/pull/16409#discussion_r554365041

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
Diffstat (limited to 'tests/stdlib/tjsbigints.nim')
-rw-r--r--tests/stdlib/tjsbigints.nim38
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/stdlib/tjsbigints.nim b/tests/stdlib/tjsbigints.nim
new file mode 100644
index 000000000..1988f637b
--- /dev/null
+++ b/tests/stdlib/tjsbigints.nim
@@ -0,0 +1,38 @@
+discard """
+  targets: "js"
+"""
+
+import std/jsbigints
+
+
+let big1: JsBigInt = big"2147483647"
+let big2: JsBigInt = big"666"
+var big3: JsBigInt = big"2"
+
+doAssert big3 == big"2"
+doAssert (big3 xor big2) == big"664"
+doAssert (big1 mod big2) == big"613"
+doAssert -big1 == big"-2147483647"
+doAssert big1 div big2 == big"3224449"
+doAssert big1 + big2 == big"2147484313"
+doAssert big1 - big2 == big"2147482981"
+doAssert big1 shl big3 == big"8589934588"
+doAssert big1 shr big3 == big"536870911"
+doAssert big1 * big2 == big"1430224108902"
+doAssert $big1 == "2147483647n"
+doAssert big1.toCstring(10) == "2147483647".cstring
+doAssert big2 ** big3 == big(443556)
+var huge = big"999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"
+huge.inc
+huge = huge + big"-999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"
+doAssert huge == big"1"
+var list: seq[JsBigInt]
+for i in big"0" .. big"5":
+  doAssert i is JsBigInt
+  list.add i
+doAssert list == @[big"0", big"1", big"2", big"3", big"4", big"5"]
+list = @[]
+for i in big"0" ..< big"5":
+  doAssert i is JsBigInt
+  list.add i
+doAssert list == @[big"0", big"1", big"2", big"3", big"4"]