Working test cases for the sophisticated matrix library example from the manual

Fixed the dot operator when used within return types (see tgenericdotrettype)
Fixed the matching of generic concepts aliases used with the implicit generics style
This commit is contained in:
Zahary Karadjov 2016-08-31 02:17:14 +03:00
commit cbf66e99a8
12 changed files with 211 additions and 53 deletions

15
tests/concepts/matrix.nim Normal file
View file

@ -0,0 +1,15 @@
type
Matrix*[M, N: static[int]; T] = object
data: array[M*N, T]
proc `[]`*(M: Matrix; m, n: int): M.T =
M.data[m * M.N + n]
proc `[]=`*(M: var Matrix; m, n: int; v: M.T) =
M.data[m * M.N + n] = v
# Adapt the Matrix type to the concept's requirements
template Rows*(M: type Matrix): expr = M.M
template Cols*(M: type Matrix): expr = M.N
template ValueType*(M: type Matrix): typedesc = M.T

View file

@ -0,0 +1,28 @@
import typetraits
type
AnyMatrix*[R, C: static[int]; T] = concept m, var mvar, type M
M.ValueType is T
M.Rows == R
M.Cols == C
m[int, int] is T
mvar[int, int] = T
type TransposedType = StripGenericParams(M)[C, R, T]
AnySquareMatrix*[N: static[int], T] = AnyMatrix[N, N, T]
AnyTransform3D* = AnyMatrix[4, 4, float]
proc transposed*(m: AnyMatrix): m.TransposedType =
for r in 0 .. <m.R:
for c in 0 .. <m.C:
result[r, c] = m[c, r]
proc determinant*(m: AnySquareMatrix): int =
return 0
proc setPerspectiveProjection*(m: AnyTransform3D) =
discard

View file

@ -1,7 +1,8 @@
discard """
output: "0\n0"
output: "0\n0\n0"
msg: '''
R=3 C=3 TE=9 FF=14 FC=20 T=int
R=3 C=3 T=int
'''
"""
@ -41,13 +42,25 @@ proc `[]=`(m: var MyMatrix; r, c: int, v: m.T) =
proc foo(x: MyMatrix, arr: array[15, x.T]) = discard
proc matrixProc[R, C, TE, FF, FC, T](m: Matrix[R, C, TE, FF, FC, T]): T =
proc genericMatrixProc[R, C, TE, FF, FC, T](m: Matrix[R, C, TE, FF, FC, T]): T =
static:
echo "R=", R, " C=", C, " TE=", TE, " FF=", FF, " FC=", FC, " T=", T.name
m[0, 0]
proc myMatrixProc(x: MyMatrix): MyMatrix.T = matrixProc(x)
proc implicitMatrixProc(m: Matrix): m.T =
static:
echo "R=", m.Rows,
" C=", m.Cols,
# XXX: fix these
#" TE=", m.TotalElements,
#" FF=", m.FromFoo,
#" FC=", m.FromConst,
" T=", m.T.name
m[0, 0]
proc myMatrixProc(x: MyMatrix): MyMatrix.T = genericMatrixProc(x)
var x: MyMatrix[3, 3, int]
@ -63,5 +76,6 @@ static:
no x is Matrix[3, 4, 9, 15, 20, int]
echo x.myMatrixProc
echo x.matrixProc
echo x.genericMatrixProc
echo x.implicitMatrixProc

View file

@ -0,0 +1,31 @@
discard """
output: "0"
"""
import matrix, matrixalgo
import typetraits # XXX: this should be removed
var m: Matrix[3, 3, int]
var projectionMatrix: Matrix[4, 4, float]
echo m.transposed.determinant
setPerspectiveProjection projectionMatrix
template ok(x) = assert x
template no(x) = assert(not x)
static:
ok projectionMatrix is AnyTransform3D
no m is AnyTransform3D
type SquareStringMatrix = Matrix[5, 5, string]
ok SquareStringMatrix is AnyMatrix
ok SquareStringMatrix is AnySquareMatrix
no SquareStringMatrix is AnyTransform3D
ok Matrix[5, 10, int] is AnyMatrix
no Matrix[7, 15, float] is AnySquareMatrix
no Matrix[4, 4, int] is AnyTransform3D