summary refs log tree commit diff stats
path: root/tests/compile
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2013-08-30 12:44:27 +0200
committerAraq <rumpf_a@web.de>2013-08-30 12:44:27 +0200
commit8710118b2cb1428a6ddb7929edaeef060f360be9 (patch)
tree96772e55b0e6bb92c70044083bbb9db4d9918f13 /tests/compile
parenta17076cf4fdfb27ee1d1f3be232b81584a99432a (diff)
parentc934a33ccd85716b66d44bca6a1764204507f33e (diff)
downloadNim-8710118b2cb1428a6ddb7929edaeef060f360be9.tar.gz
Merge branch 'master' of github.com:Araq/Nimrod
Diffstat (limited to 'tests/compile')
-rw-r--r--tests/compile/tbindtypedesc.nim87
-rw-r--r--tests/compile/tgenericshardcases.nim30
2 files changed, 117 insertions, 0 deletions
diff --git a/tests/compile/tbindtypedesc.nim b/tests/compile/tbindtypedesc.nim
new file mode 100644
index 000000000..dd4ef854c
--- /dev/null
+++ b/tests/compile/tbindtypedesc.nim
@@ -0,0 +1,87 @@
+discard """
+  msg: '''
+int
+float
+TFoo
+TFoo
+'''
+"""
+
+import typetraits
+
+type 
+  TFoo = object
+    x, y: int
+
+  TBar = tuple
+    x, y: int
+
+template good(e: expr) =
+  static: assert(compiles(e))
+
+template bad(e: expr) =
+  static: assert(not compiles(e))
+
+proc genericParamRepeated[T: typedesc](a: T, b: T) =
+  static:
+    echo a.name
+    echo b.name
+
+good(genericParamRepeated(int, int))
+good(genericParamRepeated(float, float))
+
+bad(genericParamRepeated(string, int))
+bad(genericParamRepeated(int, float))
+
+proc genericParamOnce[T: typedesc](a, b: T) =
+  static:
+    echo a.name
+    echo b.name
+
+good(genericParamOnce(int, int))
+good(genericParamOnce(TFoo, TFoo))
+
+bad(genericParamOnce(string, int))
+bad(genericParamOnce(TFoo, float))
+
+proc typePairs(A, B: type1; C, D: type2) = nil
+
+good(typePairs(int, int, TFoo, TFOO))
+good(typePairs(TBAR, TBar, TBAR, TBAR))
+good(typePairs(int, int, string, string))
+
+bad(typePairs(TBAR, TBar, TBar, TFoo))
+bad(typePairs(string, int, TBAR, TBAR))
+
+proc typePairs2[T: typedesc, U: typedesc](A, B: T; C, D: U) = nil
+
+good(typePairs2(int, int, TFoo, TFOO))
+good(typePairs2(TBAR, TBar, TBAR, TBAR))
+good(typePairs2(int, int, string, string))
+
+bad(typePairs2(TBAR, TBar, TBar, TFoo))
+bad(typePairs2(string, int, TBAR, TBAR))
+
+proc dontBind(a: typedesc, b: typedesc) =
+  static:
+    echo a.name
+    echo b.name
+
+good(dontBind(int, float))
+good(dontBind(TFoo, TFoo))
+
+proc dontBind2(a, b: typedesc) = nil
+
+good(dontBind2(int, float))
+good(dontBind2(TBar, int))
+
+proc bindArg(T: typedesc, U: typedesc, a, b: T, c, d: U) = nil
+
+good(bindArg(int, string, 10, 20, "test", "nest"))
+good(bindArg(int, int, 10, 20, 30, 40))
+
+bad(bindArg(int, string, 10, "test", "test", "nest"))
+bad(bindArg(int, int, 10, 20, 30, "test"))
+bad(bindArg(int, string, 10.0, 20, "test", "nest"))
+bad(bindArg(int, string, "test", "nest", 10, 20))
+
diff --git a/tests/compile/tgenericshardcases.nim b/tests/compile/tgenericshardcases.nim
new file mode 100644
index 000000000..90981c701
--- /dev/null
+++ b/tests/compile/tgenericshardcases.nim
@@ -0,0 +1,30 @@
+discard """
+  file: "tgenericshardcases.nim"
+  output: "int\nfloat\nint\nstring"
+"""
+
+import typetraits
+
+proc typeNameLen(x: typedesc): int {.compileTime.} =
+  result = x.name.len
+  
+macro selectType(a, b: typedesc): typedesc =
+  result = a
+
+type
+  Foo[T] = object
+    data1: array[high(T), int]
+    data2: array[1..typeNameLen(T), selectType(float, string)]
+
+  MyEnum = enum A, B, C,D
+
+var f1: Foo[MyEnum]
+var f2: Foo[int8]
+
+static:
+  assert high(f1.data1) == D
+  assert high(f1.data2) == 6 # length of MyEnum
+
+  assert high(f2.data1) == 127
+  assert high(f2.data2) == 4 # length of int8
+