diff options
author | andri lim <jangko128@gmail.com> | 2018-06-04 22:43:15 +0700 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-06-04 17:43:15 +0200 |
commit | 069a53ad4bff8a3160794360227eceb1fc37d8d8 (patch) | |
tree | 5194be0f4d8afa40b4598d71b64016967908e847 /tests/array | |
parent | 05b447374bb6c8d2d09cee46e4f1fd68f5a8067f (diff) | |
download | Nim-069a53ad4bff8a3160794360227eceb1fc37d8d8.tar.gz |
fixes #7906, array and openarray arg vs. ptr/ref generic (#7909)
* fixes #7906, array and openarray arg vs. ptr/ref generic * add comment
Diffstat (limited to 'tests/array')
-rw-r--r-- | tests/array/t7818.nim | 141 |
1 files changed, 114 insertions, 27 deletions
diff --git a/tests/array/t7818.nim b/tests/array/t7818.nim index 5d73efec5..4e43bff85 100644 --- a/tests/array/t7818.nim +++ b/tests/array/t7818.nim @@ -1,24 +1,5 @@ discard """ - msg: '''BracketExpr - Sym "array" - Infix - Ident ".." - IntLit 0 - IntLit 2 - BracketExpr - Sym "Vehicle" - Sym "int" ---------- -BracketExpr - Sym "array" - Infix - Ident ".." - IntLit 0 - IntLit 2 - BracketExpr - Sym "Vehicle" - Sym "int" ----------''' + output: "OK" """ # bug #7818 @@ -34,12 +15,118 @@ type Bike[T] = object of Vehicle[T] macro peek(n: typed): untyped = - echo getTypeImpl(n).treeRepr - echo "---------" + let val = getTypeImpl(n).treeRepr + newLit(val) + +block test_t7818: + var v = Vehicle[int](tire: 3) + var c = Car[int](tire: 4) + var b = Bike[int](tire: 2) + + let y = peek([c, b, v]) + let z = peek([v, c, b]) + doAssert(y == z) + +block test_t7906_1: + proc init(x: typedesc, y: int): ref x = + result = new(ref x) + result.tire = y + + var v = init(Vehicle[int], 3) + var c = init(Car[int], 4) + var b = init(Bike[int], 2) + + let y = peek([c, b, v]) + let z = peek([v, c, b]) + doAssert(y == z) + +block test_t7906_2: + var v = Vehicle[int](tire: 3) + var c = Car[int](tire: 4) + var b = Bike[int](tire: 2) + + let y = peek([c.addr, b.addr, v.addr]) + let z = peek([v.addr, c.addr, b.addr]) + doAssert(y == z) + +block test_t7906_3: + type + Animal[T] = object of RootObj + hair: T + Mammal[T] = object of Animal[T] + Monkey[T] = object of Mammal[T] + + var v = Animal[int](hair: 3) + var c = Mammal[int](hair: 4) + var b = Monkey[int](hair: 2) + + let z = peek([c.addr, b.addr, v.addr]) + let y = peek([v.addr, c.addr, b.addr]) + doAssert(y == z) + +type + Fruit[T] = ref object of RootObj + color: T + Apple[T] = ref object of Fruit[T] + Banana[T] = ref object of Fruit[T] + +proc testArray[T](x: array[3, Fruit[T]]): string = + result = "" + for c in x: + result.add $c.color + +proc testOpenArray[T](x: openArray[Fruit[T]]): string = + result = "" + for c in x: + result.add $c.color + +block test_t7906_4: + var v = Fruit[int](color: 3) + var c = Apple[int](color: 4) + var b = Banana[int](color: 2) + + let y = peek([c, b, v]) + let z = peek([v, c, b]) + doAssert(y == z) + +block test_t7906_5: + var a = Fruit[int](color: 1) + var b = Apple[int](color: 2) + var c = Banana[int](color: 3) + + doAssert(testArray([a, b, c]) == "123") + doAssert(testArray([b, c, a]) == "231") + + doAssert(testOpenArray([a, b, c]) == "123") + doAssert(testOpenArray([b, c, a]) == "231") + + doAssert(testOpenArray(@[a, b, c]) == "123") + doAssert(testOpenArray(@[b, c, a]) == "231") + +proc testArray[T](x: array[3, ptr Vehicle[T]]): string = + result = "" + for c in x: + result.add $c.tire + +proc testOpenArray[T](x: openArray[ptr Vehicle[T]]): string = + result = "" + for c in x: + result.add $c.tire + +block test_t7906_6: + var u = Vehicle[int](tire: 1) + var v = Bike[int](tire: 2) + var w = Car[int](tire: 3) + + doAssert(testArray([u.addr, v.addr, w.addr]) == "123") + doAssert(testArray([w.addr, u.addr, v.addr]) == "312") + + doAssert(testOpenArray([u.addr, v.addr, w.addr]) == "123") + doAssert(testOpenArray([w.addr, u.addr, v.addr]) == "312") + + doAssert(testOpenArray(@[u.addr, v.addr, w.addr]) == "123") + doAssert(testOpenArray(@[w.addr, u.addr, v.addr]) == "312") + +echo "OK" -var v = Vehicle[int](tire: 3) -var c = Car[int](tire: 4) -var b = Bike[int](tire: 2) -peek([c, b, v]) -peek([v, c, b]) |