Merge branch 'devel' of https://github.com/Araq/Nim into devel

This commit is contained in:
Araq 2015-01-05 02:27:36 +01:00
commit 4316fdddf3
25 changed files with 582 additions and 380 deletions

16
tests/generics/t1050.nim Normal file
View file

@ -0,0 +1,16 @@
discard """
msg: "int"
output: "4"
"""
import typetraits
type ArrayType[T] = distinct T
proc arrayItem(a: ArrayType): auto =
static: echo(name(type(a).T))
result = (type(a).T)(4)
var arr: ArrayType[int]
echo arrayItem(arr)

View file

@ -1,9 +1,15 @@
discard """
msg: "static 10\ndynamic\nstatic 20\n"
output: "s\nd\nd\ns"
disabled: "true"
"""
type
semistatic[T] =
static[T] or T
template isStatic*(x): expr =
compiles(static(x))
proc foo(x: semistatic[int]) =
when isStatic(x):
static: echo "static ", x

View file

@ -1,6 +1,6 @@
discard """
file: "tstaticparams.nim"
output: "abracadabra\ntest\n3\n15\n4\n2"
output: "abracadabra\ntest\n3\n15\n4\n2\nfloat\n3\nfloat\nyin\nyang"
"""
type
@ -56,3 +56,66 @@ type TTestSub[N: static[int]] = TTest[1, N]
var z: TTestSub[2]
echo z.high
# issue 1049
proc matrix_1*[M, N, T](mat: Matrix[M,N,T], a: array[N, int]) = discard
proc matrix_2*[M, N, T](mat: Matrix[M,N,T], a: array[N+1, int]) = discard
proc matrix_3*[M, N: static[int]; T](mat: Matrix[M,N,T], a: array[N, int]) = discard
proc matrix_4*[M, N: static[int]; T](mat: Matrix[M,N,T], a: array[N+1, int]) = discard
var
tmat: Matrix[4,4,int]
ar1: array[4, int]
ar2: array[5, int]
matrix_1(tmat, ar1)
matrix_2(tmat, ar2)
matrix_3(tmat, ar1)
matrix_4(tmat, ar2)
template reject(x): stmt =
static: assert(not compiles(x))
# test with arrays of wrong size
reject matrix_1(tmat, ar2)
reject matrix_2(tmat, ar1)
reject matrix_3(tmat, ar2)
reject matrix_4(tmat, ar1)
# bug 1820
type
T1820_1[T; Y: static[int]] = object
bar: T
proc intOrFloat*[Y](f: T1820_1[int, Y]) = echo "int"
proc intOrFloat*[Y](f: T1820_1[float, Y]) = echo "float"
proc threeOrFour*[T](f: T1820_1[T, 3]) = echo "3"
proc threeOrFour*[T](f: T1820_1[T, 4]) = echo "4"
var foo_1: T1820_1[float, 3]
foo_1.intOrFloat
foo_1.threeOrFour
type
YinAndYang = enum
Yin,
Yang
T1820_2[T; Y: static[YinAndYang]] = object
bar: T
proc intOrFloat*[Y](f: T1820_2[int, Y]) = echo "int"
proc intOrFloat*[Y](f: T1820_2[float, Y]) = echo "float"
proc yinOrYang*[T](f: T1820_2[T, YinAndYang.Yin]) = echo "yin"
proc yinOrYang*[T](f: T1820_2[T, Yang]) = echo "yang"
var foo_2: T1820_2[float, Yin]
var foo_3: T1820_2[float, YinAndYang.Yang]
foo_2.intOrFloat
foo_2.yinOrYang
foo_3.yinOrYang

View file

@ -1,5 +1,13 @@
discard """
output: "Sortable\nSortable\nContainer"
output: '''Sortable
Sortable
Container
true
true
false
false
false
'''
"""
import typetraits
@ -41,3 +49,20 @@ proc y(x: TObj): int = 10
proc testFoo(x: TFoo) = discard
testFoo(TObj(x: 10))
type
Matrix[Rows, Cols: static[int]; T] = generic M
M.M == Rows
M.N == Cols
M.T is T
MyMatrix[M, N: static[int]; T] = object
data: array[M*N, T]
var x: MyMatrix[3, 3, int]
echo x is Matrix
echo x is Matrix[3, 3, int]
echo x is Matrix[3, 3, float]
echo x is Matrix[4, 3, int]
echo x is Matrix[3, 4, int]

View file

@ -1,67 +1,67 @@
# Test various aspects
# Test various aspects
# bug #572
var a=12345678901'u64
var x = (x: 42, y: (a: 8, z: 10))
echo x.y
import
mvarious
type
PA = ref TA
PB = ref TB
TB = object
a: PA
TA = object
b: TB
x: int
proc getPA(): PA =
var
b: bool
b = not false
return nil
import
mvarious
type
PA = ref TA
PB = ref TB
TB = object
a: PA
TA = object
b: TB
x: int
proc getPA(): PA =
var
b: bool
b = not false
return nil
# bug #501
proc f(): int = 54
var
global: int
var
s: string
i: int
r: TA
r.b.a.x = 0
global = global + 1
exportme()
write(stdout, "Hallo wie heißt du? ")
write(stdout, getPA().x)
s = readLine(stdin)
i = 0
while i < s.len:
if s[i] == 'c': write(stdout, "'c' in deinem Namen gefunden\n")
i = i + 1
write(stdout, "Du heißt " & s)
var
global: int
var
s: string
i: int
r: TA
r.b.a.x = 0
global = global + 1
exportme()
write(stdout, "Hallo wie heißt du? ")
write(stdout, getPA().x)
s = readLine(stdin)
i = 0
while i < s.len:
if s[i] == 'c': write(stdout, "'c' in deinem Namen gefunden\n")
i = i + 1
write(stdout, "Du heißt " & s)
# bug #544
when false:
# yay, fails again
type Bar [T; I:range] = array[I, T]
proc foo*[T; I:range](a, b: Bar[T, I]): Bar[T, I] =
when len(a) != 3:
# Error: constant expression expected
{.fatal:"Dimensions have to be 3".}
#...
block:
var a, b: Bar[int, 0..2]
discard foo(a, b)
# yay, fails again
type Bar [T; I:range] = array[I, T]
proc foo*[T; I:range](a, b: Bar[T, I]): Bar[T, I] =
when len(a) != 3:
# Error: constant expression expected
{.fatal:"Dimensions have to be 3".}
#...
block:
var a, b: Bar[int, range[0..2]]
discard foo(a, b)
# bug #1788

View file

@ -0,0 +1,35 @@
discard """
outputsub: "channel is empty"
"""
# bug #1816
from math import random
from os import sleep
type PComm = ptr TChannel[int]
proc doAction(outC: PComm) {.thread.} =
for i in 0.. <5:
sleep(random(100))
send(outC[], i)
var
thr: TThread[PComm]
chan: TChannel[int]
open(chan)
createThread[PComm](thr, doAction, addr(chan))
while true:
let (flag, x) = tryRecv(chan)
if flag:
echo("received from chan: " & $x)
else:
echo "channel is empty"
break
echo "Finished listening"
joinThread(thr)
close(chan)