summary refs log tree commit diff stats
path: root/tests/concepts
diff options
context:
space:
mode:
Diffstat (limited to 'tests/concepts')
-rw-r--r--tests/concepts/mvarconcept.nim13
-rw-r--r--tests/concepts/tmanual.nim43
-rw-r--r--tests/concepts/tswizzle.nim80
-rw-r--r--tests/concepts/tusertypeclasses.nim68
-rw-r--r--tests/concepts/tusertypeclasses2.nim24
-rw-r--r--tests/concepts/tvarconcept.nim9
6 files changed, 237 insertions, 0 deletions
diff --git a/tests/concepts/mvarconcept.nim b/tests/concepts/mvarconcept.nim
new file mode 100644
index 000000000..0f9d0beff
--- /dev/null
+++ b/tests/concepts/mvarconcept.nim
@@ -0,0 +1,13 @@
+type RNG* = concept var rng
+  rng.randomUint32() is uint32
+
+type MersenneTwister* = object
+
+proc randomUint32*(self: var MersenneTwister): uint32 = 5
+
+proc randomInt*(rng: var RNG; max: Positive): Natural = 5
+
+var mersenneTwisterInst = MersenneTwister()
+
+proc randomInt*(max: Positive): Natural =
+  mersenneTwisterInst.randomInt(max)
diff --git a/tests/concepts/tmanual.nim b/tests/concepts/tmanual.nim
new file mode 100644
index 000000000..7cf08af06
--- /dev/null
+++ b/tests/concepts/tmanual.nim
@@ -0,0 +1,43 @@
+discard """
+  output: '''1
+2
+3
+4
+5
+6
+a
+b
+t
+e
+s
+t
+'''
+"""
+
+template accept(e: expr) =
+  static: assert compiles(e)
+
+template reject(e: expr) =
+  static: assert(not compiles(e))
+
+type
+  Container[T] = concept c
+    c.len is Ordinal
+    items(c) is iterator
+    for value in c:
+      type(value) is T
+
+proc takesIntContainer(c: Container[int]) =
+  for e in c: echo e
+
+takesIntContainer(@[1, 2, 3])
+reject takesIntContainer(@["x", "y"])
+
+proc takesContainer(c: Container) =
+  for e in c: echo e
+
+takesContainer(@[4, 5, 6])
+takesContainer(@["a", "b"])
+takesContainer "test"
+reject takesContainer(10)
+
diff --git a/tests/concepts/tswizzle.nim b/tests/concepts/tswizzle.nim
new file mode 100644
index 000000000..07205d454
--- /dev/null
+++ b/tests/concepts/tswizzle.nim
@@ -0,0 +1,80 @@
+discard """
+  output: '''3
+[1, 3]
+[2, 1, 2]
+'''
+  disabled: "true"
+"""
+
+import macros, strutils
+
+template accept(e: expr) =
+  static: assert(compiles(e))
+
+template reject(e: expr) =
+  static: assert(not compiles(e))
+
+proc swizzleIdx(c: char): int =
+  return case c
+    of 'x': 0
+    of 'y': 1
+    of 'z': 2
+    of 'w': 3
+    of 'r': 0
+    of 'g': 1
+    of 'b': 2
+    of 'a': 3
+    else: 0
+
+proc isSwizzle(s: string): bool {.compileTime.} =
+  template trySet(name, set) =
+    block search:
+      for c in s:
+        if c notin set:
+          break search
+      return true
+
+  trySet coords, {'x', 'y', 'z', 'w'}
+  trySet colors, {'r', 'g', 'b', 'a'}
+
+  return false
+
+type
+  StringIsSwizzle = concept value
+    value.isSwizzle
+
+  SwizzleStr = static[string] and StringIsSwizzle
+
+proc foo(x: SwizzleStr) =
+  echo "sw"
+
+#foo("xx")
+reject foo("xe")
+
+type
+  Vec[N: static[int]; T] = array[N, T]
+
+when false:
+  proc card(x: Vec): int = x.N
+  proc `$`(x: Vec): string = x.repr.strip
+
+  macro `.`(x: Vec, swizzle: SwizzleStr): expr =
+    var
+      cardinality = swizzle.len
+      values = newNimNode(nnkBracket)
+      v = genSym()
+
+    for c in swizzle:
+      values.add newNimNode(nnkBracketExpr).add(
+        v, c.swizzleIdx.newIntLitNode)
+
+    return quote do:
+      let `v` = `x`
+      Vec[`cardinality`, `v`.T](`values`)
+
+var z = Vec([1, 2, 3])
+
+#echo z.card
+#echo z.xz
+#echo z.yxy
+
diff --git a/tests/concepts/tusertypeclasses.nim b/tests/concepts/tusertypeclasses.nim
new file mode 100644
index 000000000..612556949
--- /dev/null
+++ b/tests/concepts/tusertypeclasses.nim
@@ -0,0 +1,68 @@
+discard """
+  output: '''Sortable
+Sortable
+Container
+true
+true
+false
+false
+false
+'''
+"""
+
+import typetraits
+
+type
+  TObj = object
+    x: int
+
+  Sortable = concept x, y
+    (x < y) is bool
+
+  ObjectContainer = concept C
+    C.len is Ordinal
+    for v in items(C):
+      v.type is tuple|object
+
+proc foo(c: ObjectContainer) =
+  echo "Container"
+
+proc foo(x: Sortable) =
+  echo "Sortable"
+
+foo 10
+foo "test"
+foo(@[TObj(x: 10), TObj(x: 20)])
+
+proc intval(x: int): int = 10
+
+# check real and virtual fields
+type
+  TFoo = concept T
+    T.x
+    y(T)
+    intval T.y
+    let z = intval(T.y)
+
+proc y(x: TObj): int = 10
+
+proc testFoo(x: TFoo) = discard
+testFoo(TObj(x: 10))
+
+type
+  Matrix[Rows, Cols: static[int]; T] = concept 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]
+
diff --git a/tests/concepts/tusertypeclasses2.nim b/tests/concepts/tusertypeclasses2.nim
new file mode 100644
index 000000000..ae05540cd
--- /dev/null
+++ b/tests/concepts/tusertypeclasses2.nim
@@ -0,0 +1,24 @@
+type
+  hasFieldX = concept z
+    z.x is int
+
+  obj_x = object
+    x: int
+
+  ref_obj_x = ref object
+    x: int
+
+  ref_to_obj_x = ref obj_x
+
+  p_o_x = ptr obj_x
+  v_o_x = var obj_x
+
+template check(x) =
+  static: assert(x)
+
+check obj_x is hasFieldX
+check ref_obj_x is hasFieldX
+check ref_to_obj_x is hasFieldX
+check p_o_x is hasFieldX
+check v_o_x is hasFieldX
+
diff --git a/tests/concepts/tvarconcept.nim b/tests/concepts/tvarconcept.nim
new file mode 100644
index 000000000..203ef3cdc
--- /dev/null
+++ b/tests/concepts/tvarconcept.nim
@@ -0,0 +1,9 @@
+discard """
+  output: "5"
+"""
+
+# bug #2346, bug #2404
+
+import mvarconcept
+
+echo randomInt(5)