summary refs log tree commit diff stats
path: root/tests/generics
diff options
context:
space:
mode:
Diffstat (limited to 'tests/generics')
-rw-r--r--tests/generics/t88.nim33
-rw-r--r--tests/generics/tforward_generic.nim28
-rw-r--r--tests/generics/tgenerictmpl2.nim31
-rw-r--r--tests/generics/ttable_alias.nim7
-rw-r--r--tests/generics/ttempl_in_generic.nim8
-rw-r--r--tests/generics/twrong_explicit_typeargs.nim16
6 files changed, 123 insertions, 0 deletions
diff --git a/tests/generics/t88.nim b/tests/generics/t88.nim
new file mode 100644
index 000000000..35bdeb8f1
--- /dev/null
+++ b/tests/generics/t88.nim
@@ -0,0 +1,33 @@
+# Issue 88
+
+type
+  BaseClass[V] = object of RootObj
+    b: V
+
+proc new[V](t: typedesc[BaseClass], v: V): BaseClass[V] =
+  BaseClass[V](b: v)
+
+proc baseMethod[V](v: BaseClass[V]): V = v.b
+proc overridedMethod[V](v: BaseClass[V]): V = v.baseMethod
+
+type
+  ChildClass[V] = object of BaseClass[V]
+    c: V
+
+proc new[V](t: typedesc[ChildClass], v1, v2: V): ChildClass[V] =
+  ChildClass[V](b: v1, c: v2)
+
+proc overridedMethod[V](v: ChildClass[V]): V = v.c
+
+let c = ChildClass[string].new("Base", "Child")
+
+assert c.baseMethod == "Base"
+assert c.overridedMethod == "Child"
+
+
+# bug #4528
+type GenericBase[T] = ref object of RootObj
+type GenericSubclass[T] = ref object of GenericBase[T]
+proc foo[T](g: GenericBase[T]) = discard
+var bar: GenericSubclass[int]
+foo(bar)
diff --git a/tests/generics/tforward_generic.nim b/tests/generics/tforward_generic.nim
new file mode 100644
index 000000000..169279cb3
--- /dev/null
+++ b/tests/generics/tforward_generic.nim
@@ -0,0 +1,28 @@
+discard """
+  output: '''b()
+720 120.0'''
+"""
+
+# bug #3055
+proc b(t: int | string)
+proc a(t: int) = b(t)
+proc b(t: int | string) = echo "b()"
+a(1)
+
+# test recursive generics still work:
+proc fac[T](x: T): T =
+  if x == 0: return 1
+  else: return fac(x-1)*x
+
+echo fac(6), " ", fac(5.0)
+
+when false:
+  # This still doesn't work...
+  # test recursive generic with forwarding:
+  proc fac2[T](x: T): T
+
+  echo fac2(6), " ", fac2(5.0)
+
+  proc fac2[T](x: T): T =
+    if x == 0: return 1
+    else: return fac2(x-1)*x
diff --git a/tests/generics/tgenerictmpl2.nim b/tests/generics/tgenerictmpl2.nim
new file mode 100644
index 000000000..0ecaf9ded
--- /dev/null
+++ b/tests/generics/tgenerictmpl2.nim
@@ -0,0 +1,31 @@
+discard """
+  output: '''1
+1
+1
+1
+999
+999
+999
+2'''
+"""
+
+# test if we can pass explicit generic arguments to generic templates
+# based on bug report #3496
+
+proc     tproc[T](t: T = 999) = echo t
+template ttmpl[T](t: T = 999) = echo t
+
+tproc(1)
+tproc[int](1)
+ttmpl(1)
+ttmpl[int](1) #<- crash case #1
+
+tproc[int]()
+discard tproc[int]
+ttmpl[int]()  #<- crash case #2
+ttmpl[int]    #<- crash case #3
+
+# but still allow normal use of [] on non-generic templates
+
+template tarr: expr = [1, 2, 3, 4]
+echo tarr[1]
diff --git a/tests/generics/ttable_alias.nim b/tests/generics/ttable_alias.nim
new file mode 100644
index 000000000..992ca85e0
--- /dev/null
+++ b/tests/generics/ttable_alias.nim
@@ -0,0 +1,7 @@
+# bug #4589
+
+import tables
+type SimpleTable*[TKey, TVal] = TableRef[TKey, TVal]
+template newSimpleTable*(TKey, TVal: typedesc): SimpleTable[TKey, TVal] = newTable[TKey, TVal]()
+var fontCache : SimpleTable[string, SimpleTable[int32, int]]
+fontCache = newSimpleTable(string, SimpleTable[int32, int])
diff --git a/tests/generics/ttempl_in_generic.nim b/tests/generics/ttempl_in_generic.nim
new file mode 100644
index 000000000..f04b9d216
--- /dev/null
+++ b/tests/generics/ttempl_in_generic.nim
@@ -0,0 +1,8 @@
+
+# bug #4600
+template foo(x: untyped): untyped = echo 1
+template foo(x,y: untyped): untyped = echo 2
+
+proc bar1[T](x: T) = foo(x)
+proc bar2(x: float) = foo(x,x)
+proc bar3[T](x: T) = foo(x,x)
diff --git a/tests/generics/twrong_explicit_typeargs.nim b/tests/generics/twrong_explicit_typeargs.nim
new file mode 100644
index 000000000..e47b38e99
--- /dev/null
+++ b/tests/generics/twrong_explicit_typeargs.nim
@@ -0,0 +1,16 @@
+discard """
+  errormsg: "cannot instantiate: 'newImage[string]'"
+  line: 16
+"""
+
+# bug #4084
+type
+  Image[T] = object
+    data: seq[T]
+
+proc newImage[T: int32|int64](w, h: int): ref Image[T] =
+  new(result)
+  result.data = newSeq[T](w * h)
+
+var correct = newImage[int32](320, 200)
+var wrong = newImage[string](320, 200)