diff options
Diffstat (limited to 'tests/distinct/tdistinct.nim')
-rw-r--r-- | tests/distinct/tdistinct.nim | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/tests/distinct/tdistinct.nim b/tests/distinct/tdistinct.nim index 2c0196745..b6ba7aa99 100644 --- a/tests/distinct/tdistinct.nim +++ b/tests/distinct/tdistinct.nim @@ -1,4 +1,5 @@ discard """ + targets: "c js" output: ''' tdistinct 25 @@ -7,6 +8,7 @@ false false false Foo +foo ''' """ @@ -106,3 +108,120 @@ type FooD = distinct int proc `<=`(a, b: FooD): bool {.borrow.} for f in [FooD(0): "Foo"]: echo f + +block tRequiresInit: + template accept(x) = + static: doAssert compiles(x) + + template reject(x) = + static: doAssert not compiles(x) + + type + Foo = object + x: string + + DistinctFoo {.requiresInit, borrow: `.`.} = distinct Foo + DistinctString {.requiresInit.} = distinct string + + reject: + var foo: DistinctFoo + foo.x = "test" + doAssert foo.x == "test" + + accept: + let foo = DistinctFoo(Foo(x: "test")) + doAssert foo.x == "test" + + reject: + var s: DistinctString + s = "test" + doAssert string(s) == "test" + + accept: + let s = DistinctString("test") + doAssert string(s) == "test" + +block: #17322 + type + A[T] = distinct string + + proc foo(a: var A) = + a.string.add "foo" + + type + B = distinct A[int] + + var b: B + foo(A[int](b)) + echo A[int](b).string + b.string.add "bar" + assert b.string == "foobar" + +type Foo = distinct string + +proc main() = # proc instead of template because of MCS/UFCS. + # xxx put everything here to test under RT + VM + block: # bug #12282 + block: + proc test() = + var s: Foo + s.string.add('c') + doAssert s.string == "c" # was failing + test() + + block: + proc add(a: var Foo, b: char) {.borrow.} + proc test() = + var s: Foo + s.add('c') + doAssert s.string == "c" # was ok + test() + + block: + proc add(a: var Foo, b: char) {.borrow.} + proc test() = + var s: string + s.Foo.add('c') + doAssert s.string == "c" # was failing + test() + block: #18061 + type + A = distinct (0..100) + B = A(0) .. A(10) + proc test(b: B) = discard + let + a = A(10) + b = B(a) + test(b) + + proc test(a: A) = discard + discard cast[B](A(1)) + var c: B + + + block: # bug #9423 + block: + type Foo = seq[int] + type Foo2 = distinct Foo + template fn() = + var a = Foo2(@[1]) + a.Foo.add 2 + doAssert a.Foo == @[1, 2] + fn() + + block: + type Stack[T] = distinct seq[T] + proc newStack[T](): Stack[T] = + Stack[T](newSeq[T]()) + proc push[T](stack: var Stack[T], elem: T) = + seq[T](stack).add(elem) + proc len[T](stack: Stack[T]): int = + seq[T](stack).len + proc fn() = + var stack = newStack[int]() + stack.push(5) + doAssert stack.len == 1 + fn() + +static: main() +main() |