diff options
Diffstat (limited to 'tests/generics')
-rw-r--r-- | tests/generics/tbadgenericlambda.nim | 7 | ||||
-rw-r--r-- | tests/generics/tgeneric3.nim | 6 | ||||
-rw-r--r-- | tests/generics/tgenericlambda.nim | 23 | ||||
-rw-r--r-- | tests/generics/tgenericshardcases.nim | 18 | ||||
-rw-r--r-- | tests/generics/tinferredgenericprocs.nim | 20 | ||||
-rw-r--r-- | tests/generics/tlateboundstatic.nim | 16 | ||||
-rw-r--r-- | tests/generics/tmetafield.nim | 18 | ||||
-rw-r--r-- | tests/generics/tsigtypeop.nim | 9 |
8 files changed, 108 insertions, 9 deletions
diff --git a/tests/generics/tbadgenericlambda.nim b/tests/generics/tbadgenericlambda.nim new file mode 100644 index 000000000..38e7f6cd7 --- /dev/null +++ b/tests/generics/tbadgenericlambda.nim @@ -0,0 +1,7 @@ +discard """ + errmsg: "nested proc can have generic parameters only when" + line: 6 +""" + +let x = proc (x, y): auto = x + y + diff --git a/tests/generics/tgeneric3.nim b/tests/generics/tgeneric3.nim index 3c543ecfa..963d0ccfb 100644 --- a/tests/generics/tgeneric3.nim +++ b/tests/generics/tgeneric3.nim @@ -32,7 +32,7 @@ const proc len[T,D] (n:PNode[T,D]): Int {.inline.} = return n.Count -proc clean[T: TOrdinal|TNumber](o: var T) {.inline.} = nil +proc clean[T: TOrdinal|TNumber](o: var T) {.inline.} = discard proc clean[T: string|seq](o: var T) {.inline.} = o = nil @@ -98,7 +98,7 @@ proc DeleteItem[T,D] (n: PNode[T,D], x: Int): PNode[T,D] {.inline.} = of cLen3 : setLen(n.slots, cLen3) of cLenCenter : setLen(n.slots, cLenCenter) of cLen4 : setLen(n.slots, cLen4) - else: nil + else: discard Result = n else : @@ -232,7 +232,7 @@ proc InsertItem[T,D](APath: RPath[T,D], ANode:PNode[T,D], AKey: T, AValue: D) = of cLen3: setLen(APath.Nd.slots, cLenCenter) of cLenCenter: setLen(APath.Nd.slots, cLen4) of cLen4: setLen(APath.Nd.slots, cLenMax) - else: nil + else: discard for i in countdown(APath.Nd.Count.int - 1, x + 1): shallowCopy(APath.Nd.slots[i], APath.Nd.slots[i - 1]) APath.Nd.slots[x] = setItem(AKey, AValue, ANode) diff --git a/tests/generics/tgenericlambda.nim b/tests/generics/tgenericlambda.nim new file mode 100644 index 000000000..eb6ada3e5 --- /dev/null +++ b/tests/generics/tgenericlambda.nim @@ -0,0 +1,23 @@ +discard """ + output: "10\n10\n1\n2\n3\n15" +""" + +proc test(x: proc (a, b: int): int) = + echo x(5, 5) + +test(proc (a, b): auto = a + b) + +test do (a, b) -> auto: a + b + +proc foreach[T](s: seq[T], body: proc(x: T)) = + for e in s: + body(e) + +foreach(@[1,2,3]) do (x): + echo x + +proc foo = + let x = proc (a, b: int): auto = a + b + echo x(5, 10) + +foo() diff --git a/tests/generics/tgenericshardcases.nim b/tests/generics/tgenericshardcases.nim index 2ef63bc20..e3b805db6 100644 --- a/tests/generics/tgenericshardcases.nim +++ b/tests/generics/tgenericshardcases.nim @@ -14,7 +14,8 @@ macro selectType(a, b: typedesc): typedesc = type Foo[T] = object data1: array[T.high, int] - data2: array[typeNameLen(T), float] # data3: array[0..T.typeNameLen, selectType(float, int)] + data2: array[typeNameLen(T), float] + data3: array[0..T.typeNameLen, selectType(float, int)] MyEnum = enum A, B, C, D @@ -27,10 +28,15 @@ echo high(f1.data2) # (MyEnum.len = 6) - 1 == 5 echo high(f2.data1) # 127 - 1 == 126 echo high(f2.data2) # int8.len - 1 == 3 -#static: -# assert high(f1.data1) == ord(D) -# assert high(f1.data2) == 6 # length of MyEnum +static: + assert high(f1.data1) == ord(C) + assert high(f1.data2) == 5 # length of MyEnum minus one, because we used T.high -# assert high(f2.data1) == 127 -# assert high(f2.data2) == 4 # length of int8 + assert high(f2.data1) == 126 + assert high(f2.data2) == 3 + + assert high(f1.data3) == 6 # length of MyEnum + assert high(f2.data3) == 4 # length of int8 + + assert f2.data3[0] is float diff --git a/tests/generics/tinferredgenericprocs.nim b/tests/generics/tinferredgenericprocs.nim new file mode 100644 index 000000000..ac445fd32 --- /dev/null +++ b/tests/generics/tinferredgenericprocs.nim @@ -0,0 +1,20 @@ +discard """ + output: '''123 +1 +2 +3''' +""" + +# https://github.com/Araq/Nimrod/issues/797 +proc foo[T](s:T):string = $s + +type IntStringProc = proc(x: int): string + +var f1 = IntStringProc(foo) +var f2: proc(x: int): string = foo +var f3: IntStringProc = foo + +echo f1(1), f2(2), f3(3) + +for x in map([1,2,3], foo): echo x + diff --git a/tests/generics/tlateboundstatic.nim b/tests/generics/tlateboundstatic.nim new file mode 100644 index 000000000..f68f95f8d --- /dev/null +++ b/tests/generics/tlateboundstatic.nim @@ -0,0 +1,16 @@ +discard """ + msg: "array[0..3, int]" +""" + +type + KK[I: static[int]] = object + x: array[I, int] + +proc foo(a: static[string]): KK[a.len] = + result.x[0] = 12 + +var x = foo "test" + +import typetraits +static: echo x.x.type.name + diff --git a/tests/generics/tmetafield.nim b/tests/generics/tmetafield.nim new file mode 100644 index 000000000..8e7f26549 --- /dev/null +++ b/tests/generics/tmetafield.nim @@ -0,0 +1,18 @@ +discard """ + cmd: "nimrod check $# $#" + errmsg: "'proc' is not a concrete type" + errmsg: "'Foo' is not a concrete type." + errmsg: "invalid type: 'TBaseMed'" +""" + +type + Foo[T] = object + x: T + + TBaseMed = object + doSmth: proc + data: seq[Foo] + +var a: TBaseMed + +# issue 188 diff --git a/tests/generics/tsigtypeop.nim b/tests/generics/tsigtypeop.nim new file mode 100644 index 000000000..4c863cba1 --- /dev/null +++ b/tests/generics/tsigtypeop.nim @@ -0,0 +1,9 @@ +type + Vec3[T] = array[3, T] + +proc foo(x: Vec3, y: Vec3.T, z: x.T): x.type.T = + return 10 + +var y: Vec3[int] = [1, 2, 3] +var z: int = foo(y, 3, 4) + |