add tests for recently closed issues (#10722)

This commit is contained in:
Miran
2019-02-23 10:41:35 +01:00
committed by Andreas Rumpf
parent ea409fb15a
commit e7878c0d08
9 changed files with 177 additions and 1 deletions

View File

@@ -7,4 +7,5 @@ type
proc `=`(a: var Obj, b: Obj) = discard
let a: seq[Obj] = @[]
let a: seq[Obj] = @[] # bug #7346
let b = newSeq[Obj]() # bug #7345

View File

@@ -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

82
tests/iter/titer12.nim Normal file
View File

@@ -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

19
tests/misc/tsizeof3.nim Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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]

View File

@@ -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

21
tests/types/tcast1.nim Normal file
View File

@@ -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)