summary refs log tree commit diff stats
path: root/tests/compile
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-01-13 01:22:03 +0100
committerAraq <rumpf_a@web.de>2014-01-13 01:22:03 +0100
commit51ee524109cf7e3e86c676bc1676063a01bfd979 (patch)
tree0f611cc00834f0452a3f8ed1729c7b62f676f237 /tests/compile
parent4045d7829b0ab9c8749aa701515a53c279db9958 (diff)
parent1280c56f386111b542c8f0effdd3e5cbc5e84622 (diff)
downloadNim-51ee524109cf7e3e86c676bc1676063a01bfd979.tar.gz
Merge branch 'devel' of https://github.com/Araq/Nimrod into devel
Diffstat (limited to 'tests/compile')
-rw-r--r--tests/compile/compilehelpers.nim6
-rw-r--r--tests/compile/tbindtypedesc.nim60
-rw-r--r--tests/compile/tcompositetypeclasses.nim59
-rw-r--r--tests/compile/tgenericshardcases.nim24
-rw-r--r--tests/compile/tloops.nim128
-rw-r--r--tests/compile/tmatrix3.nim41
6 files changed, 215 insertions, 103 deletions
diff --git a/tests/compile/compilehelpers.nim b/tests/compile/compilehelpers.nim
new file mode 100644
index 000000000..cb26ca5b5
--- /dev/null
+++ b/tests/compile/compilehelpers.nim
@@ -0,0 +1,6 @@
+template accept(e: expr) =
+  static: assert(compiles(e))
+
+template reject(e: expr) =
+  static: assert(not compiles(e))
+
diff --git a/tests/compile/tbindtypedesc.nim b/tests/compile/tbindtypedesc.nim
index 4ebfd12bb..5ea8cf063 100644
--- a/tests/compile/tbindtypedesc.nim
+++ b/tests/compile/tbindtypedesc.nim
@@ -16,10 +16,10 @@ type
   TBar = tuple
     x, y: int
 
-template good(e: expr) =
+template accept(e: expr) =
   static: assert(compiles(e))
 
-template bad(e: expr) =
+template reject(e: expr) =
   static: assert(not compiles(e))
 
 proc genericParamRepeated[T: typedesc](a: T, b: T) =
@@ -27,22 +27,22 @@ proc genericParamRepeated[T: typedesc](a: T, b: T) =
     echo a.name
     echo b.name
 
-good(genericParamRepeated(int, int))
-good(genericParamRepeated(float, float))
+accept genericParamRepeated(int, int)
+accept genericParamRepeated(float, float)
 
-bad(genericParamRepeated(string, int))
-bad(genericParamRepeated(int, float))
+reject genericParamRepeated(string, int)
+reject 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))
+accept genericParamOnce(int, int)
+accept genericParamOnce(TFoo, TFoo)
 
-bad(genericParamOnce(string, int))
-bad(genericParamOnce(TFoo, float))
+reject genericParamOnce(string, int)
+reject genericParamOnce(TFoo, float)
 
 type
   type1 = typedesc
@@ -50,42 +50,42 @@ type
 
 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))
+accept typePairs(int, int, TFoo, TFOO)
+accept typePairs(TBAR, TBar, TBAR, TBAR)
+accept typePairs(int, int, string, string)
 
-bad(typePairs(TBAR, TBar, TBar, TFoo))
-bad(typePairs(string, int, TBAR, TBAR))
+reject typePairs(TBAR, TBar, TBar, TFoo)
+reject 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))
+accept typePairs2(int, int, TFoo, TFOO)
+accept typePairs2(TBAR, TBar, TBAR, TBAR)
+accept typePairs2(int, int, string, string)
 
-bad(typePairs2(TBAR, TBar, TBar, TFoo))
-bad(typePairs2(string, int, TBAR, TBAR))
+reject typePairs2(TBAR, TBar, TBar, TFoo)
+reject 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))
+accept dontBind(int, float)
+accept dontBind(TFoo, TFoo)
 
 proc dontBind2(a, b: typedesc) = nil
 
-good(dontBind2(int, float))
-good(dontBind2(TBar, int))
+accept dontBind2(int, float)
+accept 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))
+accept bindArg(int, string, 10, 20, "test", "nest")
+accept 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))
+reject bindArg(int, string, 10, "test", "test", "nest")
+reject bindArg(int, int, 10, 20, 30, "test")
+reject bindArg(int, string, 10.0, 20, "test", "nest")
+reject bindArg(int, string, "test", "nest", 10, 20)
 
diff --git a/tests/compile/tcompositetypeclasses.nim b/tests/compile/tcompositetypeclasses.nim
new file mode 100644
index 000000000..a2db73769
--- /dev/null
+++ b/tests/compile/tcompositetypeclasses.nim
@@ -0,0 +1,59 @@
+template accept(e) =
+  static: assert(compiles(e))
+
+template reject(e) =
+  static: assert(not compiles(e))
+
+type
+  TFoo[T, U] = tuple
+    x: T
+    y: U
+
+  TBar[K] = TFoo[K, K]
+
+  TUserClass = int|string
+
+  TBaz = TBar[TUserClass]
+
+var
+  vfoo: TFoo[int, string]
+  vbar: TFoo[string, string]
+  vbaz: TFoo[int, int]
+  vnotbaz: TFoo[TObject, TObject]
+
+proc foo(x: TFoo) = echo "foo"
+proc bar(x: TBar) = echo "bar"
+proc baz(x: TBaz) = echo "baz"
+
+accept foo(vfoo)
+accept bar(vbar)
+accept baz(vbar)
+accept baz(vbaz)
+
+reject baz(vnotbaz)
+reject bar(vfoo)
+
+# https://github.com/Araq/Nimrod/issues/517
+type
+  TVecT*[T] = array[0..1, T]|array[0..2, T]|array[0..3, T]
+  TVec2* = array[0..1, float32]
+
+proc f[T](a: TVecT[T], b: TVecT[T]): T = discard
+
+var x: float = f([0.0'f32, 0.0'f32], [0.0'f32, 0.0'f32])
+var y = f(TVec2([0.0'f32, 0.0'f32]), TVec2([0.0'f32, 0.0'f32]))
+
+# https://github.com/Araq/Nimrod/issues/602
+type
+  TTest = object
+  TTest2* = object
+  TUnion = TTest | TTest2
+
+proc f(src: ptr TUnion, dst: ptr TUnion) =
+  echo("asd")
+
+var tx: TTest
+var ty: TTest2
+
+accept f(addr tx, addr tx)
+reject f(addr tx, addr ty)
diff --git a/tests/compile/tgenericshardcases.nim b/tests/compile/tgenericshardcases.nim
index 90981c701..2ef63bc20 100644
--- a/tests/compile/tgenericshardcases.nim
+++ b/tests/compile/tgenericshardcases.nim
@@ -1,6 +1,6 @@
 discard """
   file: "tgenericshardcases.nim"
-  output: "int\nfloat\nint\nstring"
+  output: "2\n5\n126\n3"
 """
 
 import typetraits
@@ -13,18 +13,24 @@ macro selectType(a, b: typedesc): typedesc =
 
 type
   Foo[T] = object
-    data1: array[high(T), int]
-    data2: array[1..typeNameLen(T), selectType(float, string)]
+    data1: array[T.high, int]
+    data2: array[typeNameLen(T), float] # data3: array[0..T.typeNameLen, selectType(float, int)]
 
-  MyEnum = enum A, B, C,D
+  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
+echo high(f1.data1) # (D = 3) - 1 == 2
+echo high(f1.data2) # (MyEnum.len = 6) - 1 == 5
 
-  assert high(f2.data1) == 127
-  assert high(f2.data2) == 4 # length of int8
+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
+
+# assert high(f2.data1) == 127
+# assert high(f2.data2) == 4 # length of int8
 
diff --git a/tests/compile/tloops.nim b/tests/compile/tloops.nim
index 2b1765b00..f6f939769 100644
--- a/tests/compile/tloops.nim
+++ b/tests/compile/tloops.nim
@@ -1,67 +1,67 @@
-# Test nested loops and some other things

-

-proc andTest() =

-  var a = 0 == 5 and 6 == 6

-

-proc incx(x: var int) = # is built-in proc

-  x = x + 1

-

-proc decx(x: var int) =

-  x = x - 1

-

-proc First(y: var int) =

-  var x: int

-  i_ncx(x)

-  if x == 10:

-    y = 0

-  else:

-    if x == 0:

-      incx(x)

-    else:

-      x=11

-

-proc TestLoops() =

-  var i, j: int

-  while i >= 0:

-    if i mod 3 == 0:

-      break

-    i = i + 1

-    while j == 13:

-      j = 13

-      break

-    break

-

-  while True:

-    break

-

-

-proc Foo(n: int): int =

-    var

-        a, old: int

-        b, c: bool

-    F_irst(a)

-    if a == 10:

-        a = 30

-    elif a == 11:

-        a = 22

-    elif a == 12:

-        a = 23

-    elif b:

-        old = 12

-    else:

-        a = 40

-

-    #

-    b = false or 2 == 0 and 3 == 9

-    a = 0 + 3 * 5 + 6 + 7 + +8 # 36

-    while b:

-        a = a + 3

-    a = a + 5

-    write(stdout, "Hello!")

-

-

-# We should come till here :-)

-discard Foo(345)

+# Test nested loops and some other things
+
+proc andTest() =
+  var a = 0 == 5 and 6 == 6
+
+proc incx(x: var int) = # is built-in proc
+  x = x + 1
+
+proc decx(x: var int) =
+  x = x - 1
+
+proc First(y: var int) =
+  var x: int
+  i_ncx(x)
+  if x == 10:
+    y = 0
+  else:
+    if x == 0:
+      incx(x)
+    else:
+      x=11
+
+proc TestLoops() =
+  var i, j: int
+  while i >= 0:
+    if i mod 3 == 0:
+      break
+    i = i + 1
+    while j == 13:
+      j = 13
+      break
+    break
+
+  while True:
+    break
+
+
+proc Foo(n: int): int =
+    var
+        a, old: int
+        b, c: bool
+    F_irst(a)
+    if a == 10:
+        a = 30
+    elif a == 11:
+        a = 22
+    elif a == 12:
+        a = 23
+    elif b:
+        old = 12
+    else:
+        a = 40
+
+    #
+    b = false or 2 == 0 and 3 == 9
+    a = 0 + 3 * 5 + 6 + 7 + +8 # 36
+    while b:
+        a = a + 3
+    a = a + 5
+    write(stdout, "Hello!")
+
+
+# We should come till here :-)
+discard Foo(345)
 
 # test the new type symbol lookup feature:
 
diff --git a/tests/compile/tmatrix3.nim b/tests/compile/tmatrix3.nim
new file mode 100644
index 000000000..900404524
--- /dev/null
+++ b/tests/compile/tmatrix3.nim
@@ -0,0 +1,41 @@
+discard """
+  output: '''0.0000000000000000e+00
+0.0000000000000000e+00
+0
+0
+0
+'''
+"""
+
+include compilehelpers
+
+type
+  Matrix*[M, N, T] = object 
+    aij*: array[M, array[N, T]]
+  
+  Matrix2*[T] = Matrix[range[0..1], range[0..1], T]
+  
+  Matrix3*[T] = Matrix[range[0..2], range[0..2], T]
+
+proc mn(x: Matrix): Matrix.T = x.aij[0][0]
+
+proc m2(x: Matrix2): Matrix2.T = x.aij[0][0]
+
+proc m3(x: Matrix3): auto = x.aij[0][0]
+
+var 
+  matn: Matrix[range[0..3], range[0..2], int]
+  mat2: Matrix2[int]
+  mat3: Matrix3[float]
+
+echo m3(mat3)
+echo mn(mat3)
+echo m2(mat2)
+echo mn(mat2)
+echo mn(matn)
+
+reject m3(mat2)
+reject m3(matn)
+reject m2(mat3)
+reject m2(matn)
+