mirror of
https://github.com/nim-lang/Nim.git
synced 2026-05-01 03:24:41 +00:00
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>
This commit is contained in:
@@ -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
|
||||
|
||||
14
tests/stdlib/tbitops_utils.nim
Normal file
14
tests/stdlib/tbitops_utils.nim
Normal file
@@ -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)
|
||||
@@ -2,10 +2,10 @@ discard """
|
||||
targets: "c cpp js"
|
||||
"""
|
||||
|
||||
# xxx merge with tests/metatype/ttypetraits.nim
|
||||
|
||||
import std/typetraits
|
||||
|
||||
|
||||
|
||||
macro testClosure(fn: typed, flag: static bool) =
|
||||
if flag:
|
||||
doAssert hasClosure(fn)
|
||||
|
||||
Reference in New Issue
Block a user