From e7878c0d0887b198583c96c854d6811e1a5907ad Mon Sep 17 00:00:00 2001 From: Miran Date: Sat, 23 Feb 2019 10:41:35 +0100 Subject: [PATCH] add tests for recently closed issues (#10722) --- tests/destructor/t7346.nim | 3 +- tests/enum/tenum.nim | 9 ++++ tests/iter/titer12.nim | 82 ++++++++++++++++++++++++++++++++++ tests/misc/tsizeof3.nim | 19 ++++++++ tests/misc/tunsignedconv.nim | 4 ++ tests/misc/tunsignedinc.nim | 6 +++ tests/pragmas/tdeprecated.nim | 10 +++++ tests/template/tmethodcall.nim | 24 ++++++++++ tests/types/tcast1.nim | 21 +++++++++ 9 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 tests/iter/titer12.nim create mode 100644 tests/misc/tsizeof3.nim create mode 100644 tests/pragmas/tdeprecated.nim create mode 100644 tests/template/tmethodcall.nim create mode 100644 tests/types/tcast1.nim diff --git a/tests/destructor/t7346.nim b/tests/destructor/t7346.nim index ef2fd5b79..17f56681e 100644 --- a/tests/destructor/t7346.nim +++ b/tests/destructor/t7346.nim @@ -7,4 +7,5 @@ type proc `=`(a: var Obj, b: Obj) = discard -let a: seq[Obj] = @[] \ No newline at end of file +let a: seq[Obj] = @[] # bug #7346 +let b = newSeq[Obj]() # bug #7345 diff --git a/tests/enum/tenum.nim b/tests/enum/tenum.nim index 72f1837f5..8cc578d3c 100644 --- a/tests/enum/tenum.nim +++ b/tests/enum/tenum.nim @@ -145,3 +145,12 @@ block toptions: optOverflowCheck, optAssert, optWarns, optHints, optLineDir, optStackTrace} compilerArgs: int gExitcode: int8 + + + +block nonzero: # bug #6959 + type SomeEnum = enum + A = 10 + B + C + let slice = SomeEnum.low..SomeEnum.high diff --git a/tests/iter/titer12.nim b/tests/iter/titer12.nim new file mode 100644 index 000000000..f7fc64da4 --- /dev/null +++ b/tests/iter/titer12.nim @@ -0,0 +1,82 @@ +discard """ +output: ''' +Selecting 2 +1.0 +Selecting 4 +2.0 +''' +""" + + +# bug #5522 +import macros, sugar, sequtils + +proc tryS(f: () -> void): void = + (try: f() except: discard) + +template trySTImpl(body: untyped): untyped = + tryS do() -> auto: + `body` + +macro tryST*(body: untyped): untyped = + var b = if body.kind == nnkDo: body[^1] else: body + result = quote do: + trySTImpl((block: + `b` + )) + +iterator testIt(): int {.closure.} = + for x in 0..10: + yield x + +var xs = newSeq[int]() +proc test = tryST do: + for x in testIt(): + xs.add(x) + +test() + +doAssert xs == toSeq(0..10) + + + +# bug #5690 +proc filter[T](it: (iterator : T), f: proc(x: T): bool): (iterator : T) = + return iterator (): T {.closure.} = + for x in it(): + if f(x): + yield x + +proc len[T](it : iterator : T) : Natural = + for i in it(): + result += 1 + +proc simpleSeqIterator(s :seq[int]) : iterator : int = + iterator it: int {.closure.} = + for x in s: + yield x + result = it + +let a = newSeq[int](99) + +doAssert len(simpleSeqIterator(a).filter(proc(x : int) : bool = true)) == 99 + + + +# bug #5340 +proc where[A](input: seq[A], filter: (A) -> bool): iterator (): A = + result = iterator (): A {.closure.} = + for item in input: + if filter(item): + yield item + +proc select[A,B](input: iterator(): A {.closure.}, selector: (A) -> B): iterator (): B {.closure.} = + result = iterator (): B = + for item in input(): + echo "Selecting " & $item + yield selector(item) + +let query = @[1,2,3,4].where(x=>x mod 2==0).select((x)=>x/2) + +for i in query(): + echo $i diff --git a/tests/misc/tsizeof3.nim b/tests/misc/tsizeof3.nim new file mode 100644 index 000000000..e04ce8a0c --- /dev/null +++ b/tests/misc/tsizeof3.nim @@ -0,0 +1,19 @@ +discard """ +output: ''' +[0, 0, 0, 0, 0, 0, 48, 57] +''' +""" +# bug #7238 + +type ByteArrayBE*[N: static[int]] = array[N, byte] + ## A byte array that stores bytes in big-endian order + +proc toByteArrayBE*[T: SomeInteger](num: T): ByteArrayBE[sizeof(T)]= + ## Convert an integer (in native host endianness) to a big-endian byte array + ## Notice the result type + const N = T.sizeof + for i in 0 ..< N: + result[i] = byte(num shr ((N-1-i) * 8)) + +let a = 12345.toByteArrayBE +echo a diff --git a/tests/misc/tunsignedconv.nim b/tests/misc/tunsignedconv.nim index 956e014da..efe5f1302 100644 --- a/tests/misc/tunsignedconv.nim +++ b/tests/misc/tunsignedconv.nim @@ -39,3 +39,7 @@ var n32 = ar[v32] var n64 = ar[v64] +block t4176: + var yyy: uint8 = 0 + yyy = yyy - 127 + doAssert type(yyy) is uint8 diff --git a/tests/misc/tunsignedinc.nim b/tests/misc/tunsignedinc.nim index 60c0559b0..6d1b627d6 100644 --- a/tests/misc/tunsignedinc.nim +++ b/tests/misc/tunsignedinc.nim @@ -26,3 +26,9 @@ block: var x = 123'u16 x -= 125 doAssert(x == 65534'u16) + +block t4175: + let i = 0u - 1u + const j = 0u - 1u + doAssert i == j + doAssert j + 1u == 0u diff --git a/tests/pragmas/tdeprecated.nim b/tests/pragmas/tdeprecated.nim new file mode 100644 index 000000000..a5d07f727 --- /dev/null +++ b/tests/pragmas/tdeprecated.nim @@ -0,0 +1,10 @@ +# bug #6436 +proc foo(size: int, T: typedesc): seq[T] {.deprecated.}= + result = newSeq[T](size) + +proc foo[T](size: int): seq[T]= + result = newSeq[T](size) + +let bar = foo[int](3) # Warning foo is deprecated + +doAssert bar == @[0, 0, 0] diff --git a/tests/template/tmethodcall.nim b/tests/template/tmethodcall.nim new file mode 100644 index 000000000..d209443c8 --- /dev/null +++ b/tests/template/tmethodcall.nim @@ -0,0 +1,24 @@ +# bug #5909 +type + Vec2[T] = tuple + x,y: T + Vec2f = Vec2[float32] + +proc vec2f(x,y: float): Vec2f = + result.x = x + result.y = y + +proc `-`[T](a,b: Vec2[T]): Vec2[T] = + result.x = a.x - b.x + result.y = a.y - b.y + +proc foo[T](a: Vec2[T]): Vec2[T] = + result = a + +block: + # this being called foo is a problem when calling .foo() + var foo = true + + let a = vec2f(1.0,0.0) + let b = vec2f(3.0,1.0) + let c = (a - b).foo() # breaks diff --git a/tests/types/tcast1.nim b/tests/types/tcast1.nim new file mode 100644 index 000000000..10e876ded --- /dev/null +++ b/tests/types/tcast1.nim @@ -0,0 +1,21 @@ +discard """ +output: ''' +@[1.0, 2.0, 3.0] +@[1.0, 2.0, 3.0] +''' +""" + +# bug #6406 + +import sequtils + +proc remap1(s: seq[int], T: typedesc): seq[T] = + s.map do (x: int) -> T: + x.T + +proc remap2[T](s: seq[int], typ: typedesc[T]): seq[T] = + s.map do (x: int) -> T: + x.T + +echo remap1(@[1,2,3], float) +echo remap2(@[1,2,3], float)