summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2022-04-07 14:38:01 -0700
committerGitHub <noreply@github.com>2022-04-07 17:38:01 -0400
commite78ef57c93d66da483e0482ce0907dfe16dc8d27 (patch)
tree32ba3900b479bc2e046aca563df99c1be77a5d9d /tests
parent1807de38e52f45c2fb88dac9b99b47729b12ebae (diff)
downloadNim-e78ef57c93d66da483e0482ce0907dfe16dc8d27.tar.gz
typetraits: add toSigned, toUnsigned (#18445)
* typetraits: add toSigned, toUnsigned

* improve and add tests

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: flywind <xzsflywind@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/metatype/ttypetraits.nim15
-rw-r--r--tests/stdlib/tbitops_utils.nim14
-rw-r--r--tests/stdlib/ttypetraits.nim4
3 files changed, 31 insertions, 2 deletions
diff --git a/tests/metatype/ttypetraits.nim b/tests/metatype/ttypetraits.nim
index 3ff5c5ea6..bfaa23057 100644
--- a/tests/metatype/ttypetraits.nim
+++ b/tests/metatype/ttypetraits.nim
@@ -1,6 +1,21 @@
 import typetraits
 import macros
 
+block: # toUnsigned, toSigned
+  var a1: toSigned(int16)
+  doAssert a1 is int16
+  var a2: toSigned(uint16)
+  doAssert $a2.typeof == "int16"
+  doAssert toSigned(uint32) is int32
+  doAssert uint64.toSigned is int64
+  doAssert int64.toSigned is int64
+  doAssert int64.toUnsigned is uint64
+  doAssert int.toUnsigned is uint
+  doAssert $uint.toUnsigned == "uint"
+  # disallowed for now
+  doAssert not compiles(toUnsigned(range[0..7]))
+  doAssert not compiles(toSigned(range[0..7]))
+
 block: # isNamedTuple
   type Foo1 = (a:1,).type
   type Foo2 = (Field0:1,).type
diff --git a/tests/stdlib/tbitops_utils.nim b/tests/stdlib/tbitops_utils.nim
new file mode 100644
index 000000000..b571baeae
--- /dev/null
+++ b/tests/stdlib/tbitops_utils.nim
@@ -0,0 +1,14 @@
+import std/private/bitops_utils
+
+template chk(a, b) =
+  let a2 = castToUnsigned(a)
+  doAssert a2 == b
+  doAssert type(a2) is type(b)
+  doAssert type(b) is type(a2)
+
+chk 1'i8, 1'u8
+chk -1'i8, 255'u8
+chk 1'u8, 1'u8
+chk 1'u, 1'u
+chk -1, cast[uint](-1)
+chk -1'i64, cast[uint64](-1)
diff --git a/tests/stdlib/ttypetraits.nim b/tests/stdlib/ttypetraits.nim
index de8259ab0..799bcf6e2 100644
--- a/tests/stdlib/ttypetraits.nim
+++ b/tests/stdlib/ttypetraits.nim
@@ -2,9 +2,9 @@ discard """
   targets: "c cpp js"
 """
 
-import std/typetraits
-
+# xxx merge with tests/metatype/ttypetraits.nim
 
+import std/typetraits
 
 macro testClosure(fn: typed, flag: static bool) =
   if flag: