diff options
Diffstat (limited to 'tests/converter')
-rw-r--r-- | tests/converter/m18986.nim | 3 | ||||
-rw-r--r-- | tests/converter/mdontleak.nim | 3 | ||||
-rw-r--r-- | tests/converter/t18986.nim | 10 | ||||
-rw-r--r-- | tests/converter/t21531.nim | 10 | ||||
-rw-r--r-- | tests/converter/t7097.nim | 38 | ||||
-rw-r--r-- | tests/converter/t7098.nim | 35 | ||||
-rw-r--r-- | tests/converter/t9165.nim | 11 | ||||
-rw-r--r-- | tests/converter/tconvcolors.nim | 7 | ||||
-rw-r--r-- | tests/converter/tconvert.nim | 44 | ||||
-rw-r--r-- | tests/converter/tconverter.nim | 11 | ||||
-rw-r--r-- | tests/converter/tconverter_unique_ptr.nim | 149 | ||||
-rw-r--r-- | tests/converter/tconverter_with_constraint.nim | 20 | ||||
-rw-r--r-- | tests/converter/tconverter_with_varargs.nim | 18 | ||||
-rw-r--r-- | tests/converter/tdontleak.nim | 10 | ||||
-rw-r--r-- | tests/converter/texplicit_conversion.nim | 19 | ||||
-rw-r--r-- | tests/converter/tgenericconverter.nim | 54 | ||||
-rw-r--r-- | tests/converter/tgenericconverter2.nim | 85 | ||||
-rw-r--r-- | tests/converter/tor_in_converter.nim | 23 | ||||
-rw-r--r-- | tests/converter/ttypeconverter1.nim | 15 |
19 files changed, 565 insertions, 0 deletions
diff --git a/tests/converter/m18986.nim b/tests/converter/m18986.nim new file mode 100644 index 000000000..0ebf343ae --- /dev/null +++ b/tests/converter/m18986.nim @@ -0,0 +1,3 @@ +import std/macros + +converter Lit*(x: uint): NimNode = newLit(x) diff --git a/tests/converter/mdontleak.nim b/tests/converter/mdontleak.nim new file mode 100644 index 000000000..e55c3f87c --- /dev/null +++ b/tests/converter/mdontleak.nim @@ -0,0 +1,3 @@ + +converter toBool(x: uint32): bool = x != 0 +# Note: This convertes is not exported! diff --git a/tests/converter/t18986.nim b/tests/converter/t18986.nim new file mode 100644 index 000000000..ef300fa49 --- /dev/null +++ b/tests/converter/t18986.nim @@ -0,0 +1,10 @@ +discard """ + output: "Found a 0" +""" + +import m18986 except Lit +import std/macros + +# bug #18986 +var x = 0.uint +echo "Found a ", x diff --git a/tests/converter/t21531.nim b/tests/converter/t21531.nim new file mode 100644 index 000000000..b0198684d --- /dev/null +++ b/tests/converter/t21531.nim @@ -0,0 +1,10 @@ +import std/typetraits + +type Foo* = distinct string + +converter toBase*(headers: var Foo): var string = + headers.distinctBase + +proc bar*(headers: var Foo) = + for x in headers: discard + diff --git a/tests/converter/t7097.nim b/tests/converter/t7097.nim new file mode 100644 index 000000000..d8e953080 --- /dev/null +++ b/tests/converter/t7097.nim @@ -0,0 +1,38 @@ +type + Byte* = uint8 + Bytes* = seq[Byte] + + BytesRange* = object + bytes: Bytes + ibegin, iend: int + +proc initBytesRange*(s: var Bytes, ibegin = 0, iend = -1): BytesRange = + let e = if iend < 0: s.len + iend + 1 + else: iend + assert ibegin > 0 and e <= s.len + when defined(gcRefc): + shallow(s) + result.bytes = s + result.ibegin = ibegin + result.iend = e + +template `[]=`*(r: var BytesRange, i: int, v: Byte) = + r.bytes[r.ibegin + i] = v + +converter fromSeq*(s: Bytes): BytesRange = + var seqCopy = s + return initBytesRange(seqCopy) + +type + Reader* = object + data: BytesRange + position: int + +proc readerFromHex*(input: string): Reader = + let totalBytes = input.len div 2 + var backingStore = newSeq[Byte](totalBytes) + result.data = initBytesRange(backingStore) + + for i in 0 ..< totalBytes: + var nextByte = 0 + result.data[i] = Byte(nextByte) # <-------- instantiated from here diff --git a/tests/converter/t7098.nim b/tests/converter/t7098.nim new file mode 100644 index 000000000..30c9c1e25 --- /dev/null +++ b/tests/converter/t7098.nim @@ -0,0 +1,35 @@ +discard """ +action: compile +""" + +type + Byte* = uint8 + Bytes* = seq[Byte] + + BytesRange* = object + bytes: Bytes + ibegin, iend: int + +proc initBytesRange*(s: var Bytes, ibegin = 0, iend = -1): BytesRange = + let e = if iend < 0: s.len + iend + 1 + else: iend + assert ibegin >= 0 and e <= s.len + when defined(gcRefc): + shallow(s) + result.bytes = s + result.ibegin = ibegin + result.iend = e + +converter fromSeq*(s: Bytes): BytesRange = + var seqCopy = s + return initBytesRange(seqCopy) + +type + Reader* = object + data: BytesRange + position: int + +proc readerFromBytes*(input: BytesRange): Reader = + discard + +let r = readerFromBytes(@[]) diff --git a/tests/converter/t9165.nim b/tests/converter/t9165.nim new file mode 100644 index 000000000..d0225ffbc --- /dev/null +++ b/tests/converter/t9165.nim @@ -0,0 +1,11 @@ +type ustring = distinct string + +converter toUString(s: string): ustring = ustring(s) +converter toString(s: ustring): string = string(s) + +proc `[]=`*(s: var ustring, slice: Slice[int], replacement: ustring) {.inline.} = + s = replacement + +var s = ustring("123") +s[1..2] = "3" +doAssert s == "3" \ No newline at end of file diff --git a/tests/converter/tconvcolors.nim b/tests/converter/tconvcolors.nim new file mode 100644 index 000000000..7b440cabd --- /dev/null +++ b/tests/converter/tconvcolors.nim @@ -0,0 +1,7 @@ +discard """ +output: "16777215A" +""" + +import colors + +echo int32(colWhite), 'A' diff --git a/tests/converter/tconvert.nim b/tests/converter/tconvert.nim new file mode 100644 index 000000000..5eee2a92d --- /dev/null +++ b/tests/converter/tconvert.nim @@ -0,0 +1,44 @@ + +converter FloatConversion64(x: int): float64 = return toFloat(x) +converter FloatConversion32(x: int): float32 = return toFloat(x) +converter FloatConversionPlain(x: int): float = return toFloat(x) + +const width = 500 +const height = 500 + +proc ImageSurfaceCreate(w, h: float) = discard + +ImageSurfaceCreate(width, height) + +type TFoo = object + +converter toPtr*(some: var TFoo): ptr TFoo = (addr some) + + +proc zoot(x: ptr TFoo) = discard +var x: Tfoo +zoot(x) + +# issue #6544 +converter withVar(b: var string): int = ord(b[1]) + +block: + var x = "101" + var y: int = x # instantiate withVar + doAssert(y == ord('0')) + + +###################### +# bug #3503 +type Foo = object + r: float + +converter toFoo(r: float): Foo = + result.r = r + +proc `+=`*(x: var Foo, r: float) = + x.r += r + +var a: Foo +a.r += 3.0 + diff --git a/tests/converter/tconverter.nim b/tests/converter/tconverter.nim new file mode 100644 index 000000000..0bf067c55 --- /dev/null +++ b/tests/converter/tconverter.nim @@ -0,0 +1,11 @@ +discard """ + output: '''fooo fooo''' +""" + +converter intToString[T](i: T): string = "fooo" + +let + foo: string = 1 + bar: string = intToString(2) + +echo foo, " ", bar \ No newline at end of file diff --git a/tests/converter/tconverter_unique_ptr.nim b/tests/converter/tconverter_unique_ptr.nim new file mode 100644 index 000000000..6902f9e9e --- /dev/null +++ b/tests/converter/tconverter_unique_ptr.nim @@ -0,0 +1,149 @@ + +discard """ + targets: "c cpp" + output: "" +""" + +## Bugs 9698 and 9699 + +type + UniquePtr*[T] = object + ## non copyable pointer to object T, exclusive ownership of the object is assumed + val: ptr T + + MyLen* = distinct int + + MySeq* = object + ## Vectorized matrix + len: MyLen # scalar size + data: ptr UncheckedArray[float] + +proc `$`(x: MyLen): string {.borrow.} +proc `==`(x1, x2: MyLen): bool {.borrow.} + + +proc `=destroy`*(m: MySeq) {.inline.} = + if m.data != nil: + deallocShared(m.data) + +proc `=copy`*(m: var MySeq, m2: MySeq) = + if m.data == m2.data: return + if m.data != nil: + `=destroy`(m) + + m.len = m2.len + let bytes = m.len.int * sizeof(float) + if bytes > 0: + m.data = cast[ptr UncheckedArray[float]](allocShared(bytes)) + copyMem(m.data, m2.data, bytes) + +proc `=sink`*(m: var MySeq, m2: MySeq) {.inline.} = + if m.data != m2.data: + if m.data != nil: + `=destroy`(m) + m.len = m2.len + m.data = m2.data + +proc len*(m: MySeq): MyLen {.inline.} = m.len + +proc lenx*(m: var MySeq): MyLen {.inline.} = m.len + +proc `[]`*(m: MySeq; i: MyLen): float {.inline.} = + m.data[i.int] + +proc `[]`*(m: var MySeq; i: MyLen): var float {.inline.} = + m.data[i.int] + +proc `[]=`*(m: var MySeq; i: MyLen, val: float) {.inline.} = + m.data[i.int] = val + +proc setTo(s: var MySeq, val: float) = + for i in 0..<s.len.int: + s.data[i] = val + +proc newMySeq*(size: int, initial_value = 0.0): MySeq = + result.len = size.MyLen + if size > 0: + result.data = cast[ptr UncheckedArray[float]](createShared(float, size)) + + result.setTo(initial_value) + +converter literalToLen*(x: int{lit}): MyLen = + x.MyLen + + +#------------------------------------------------------------- +# Unique pointer implementation +#------------------------------------------------------------- + +proc `=destroy`*[T](p: UniquePtr[T]) = + if p.val != nil: + `=destroy`(p.val[]) + dealloc(p.val) + +proc `=copy`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.error.} + +proc `=sink`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.inline.} = + if dest.val != nil and dest.val != src.val: + `=destroy`(dest) + dest.val = src.val + +proc newUniquePtr*[T](val: sink T): UniquePtr[T] = + result.val = cast[type(result.val)](alloc(sizeof(result.val[]))) + reset(result.val[]) + result.val[] = val + +converter convertPtrToObj*[T](p: UniquePtr[T]): var T = + result = p.val[] + +var pu = newUniquePtr(newMySeq(5, 1.0)) +let pu2 = newUniquePtr(newMySeq(5, 1.0)) +doAssert: pu.len == 5 +doAssert: pu2.len == 5 +doAssert: pu.lenx == 5 +doAssert: pu2.lenx == 5 + +pu[0] = 2.0 +pu2[0] = 2.0 +doAssert pu[0] == 2.0 +doAssert: pu2[0] == 2.0 + +##----------------------------------------------------------------------------------------- +## Bugs #9735 and #9736 +type + ConstPtr*[T] = object + ## This pointer makes it impossible to change underlying value + ## as it returns only `lent T` + val: ptr T + +proc `=destroy`*[T](p: ConstPtr[T]) = + if p.val != nil: + `=destroy`(p.val[]) + dealloc(p.val) + +proc `=copy`*[T](dest: var ConstPtr[T], src: ConstPtr[T]) {.error.} + +proc `=sink`*[T](dest: var ConstPtr[T], src: ConstPtr[T]) {.inline.} = + if dest.val != nil and dest.val != src.val: + `=destroy`(dest) + dest.val = src.val + +proc newConstPtr*[T](val: sink T): ConstPtr[T] = + result.val = cast[type(result.val)](alloc(sizeof(result.val[]))) + reset(result.val[]) + result.val[] = val + +converter convertConstPtrToObj*[T](p: ConstPtr[T]): lent T = + result = p.val[] + +var pc = newConstPtr(newMySeq(3, 1.0)) +let pc2 = newConstPtr(newMySeq(3, 1.0)) +doAssert: pc.len == 3 +doAssert: pc.len == 3 +doAssert: compiles(pc.lenx == 2) == false +doAssert: compiles(pc2.lenx == 2) == false +doAssert: compiles(pc[0] = 2.0) == false +doAssert: compiles(pc2[0] = 2.0) == false + +doAssert: pc[0] == 1.0 +doAssert: pc2[0] == 1.0 diff --git a/tests/converter/tconverter_with_constraint.nim b/tests/converter/tconverter_with_constraint.nim new file mode 100644 index 000000000..ce5135586 --- /dev/null +++ b/tests/converter/tconverter_with_constraint.nim @@ -0,0 +1,20 @@ + +discard """ + errormsg: "type mismatch: got <int>" + file: "tconverter_with_constraint.nim" + line: 20 +""" + +type + MyType = distinct int + +converter to_mytype(m: int{lit}): MyType = + m.MyType + +proc myproc(m: MyType) = + echo m.int, ".MyType" + +myproc(1) # call by literal is ok + +var x: int = 12 +myproc(x) # should fail diff --git a/tests/converter/tconverter_with_varargs.nim b/tests/converter/tconverter_with_varargs.nim new file mode 100644 index 000000000..fae83221b --- /dev/null +++ b/tests/converter/tconverter_with_varargs.nim @@ -0,0 +1,18 @@ + +# bug #888 + +type + PyRef = object + PPyRef* = ref PyRef + +converter to_py*(i: int) : PPyRef = nil + +when false: + proc to_tuple*(vals: openArray[PPyRef]): PPyRef = + discard + +proc abc(args: varargs[PPyRef]) = + #let args_tup = to_tuple(args) + discard + +abc(1, 2) diff --git a/tests/converter/tdontleak.nim b/tests/converter/tdontleak.nim new file mode 100644 index 000000000..4965fa90a --- /dev/null +++ b/tests/converter/tdontleak.nim @@ -0,0 +1,10 @@ +discard """ + output: '''5''' +joinable: false +""" + +import mdontleak +# bug #19213 + +let a = 5'u32 +echo a diff --git a/tests/converter/texplicit_conversion.nim b/tests/converter/texplicit_conversion.nim new file mode 100644 index 000000000..e36d78ad5 --- /dev/null +++ b/tests/converter/texplicit_conversion.nim @@ -0,0 +1,19 @@ +discard """ + output: "234" +""" + +# bug #4432 + +import strutils + +converter toInt(s: string): int = + result = parseInt(s) + +let x = (int)"234" +echo x + +block: # Test for nkconv + proc foo(o: var int) = + assert o == 0 + var a = 0 + foo(int(a)) \ No newline at end of file diff --git a/tests/converter/tgenericconverter.nim b/tests/converter/tgenericconverter.nim new file mode 100644 index 000000000..cbbd2e1b0 --- /dev/null +++ b/tests/converter/tgenericconverter.nim @@ -0,0 +1,54 @@ +discard """ + output: '''666 +666''' +""" + +# test the new generic converters: + +type + TFoo2[T] = object + x: T + + TFoo[T] = object + data: array[0..100, T] + +converter toFoo[T](a: TFoo2[T]): TFoo[T] = + result.data[0] = a.x + +proc p(a: TFoo[int]) = + echo a.data[0] + +proc q[T](a: TFoo[T]) = + echo a.data[0] + + +var + aa: TFoo2[int] +aa.x = 666 + +p aa +q aa + + +#------------------------------------------------------------- +# issue #16651 +type + PointTup = tuple + x: float32 + y: float32 + +converter tupleToPoint[T1, T2: SomeFloat](self: tuple[x: T1, y: T2]): PointTup = + result = (self.x.float32, self.y.float32) + +proc tupleToPointX(self: tuple[x: SomeFloat, y: SomeFloat]): PointTup = + result = (self.x.float32, self.y.float32) + +proc tupleToPointX2(self: tuple[x: SomeFloat, y: distinct SomeFloat]): PointTup = + result = (self.x.float32, self.y.float32) + +var t1: PointTup = tupleToPointX((1.0, 0.0)) +var t2: PointTup = tupleToPointX2((1.0, 0.0)) +var t3: PointTup = tupleToPointX2((1.0'f32, 0.0)) +var t4: PointTup = tupleToPointX2((1.0, 0.0'f32)) + +var x2: PointTup = (1.0, 0.0) \ No newline at end of file diff --git a/tests/converter/tgenericconverter2.nim b/tests/converter/tgenericconverter2.nim new file mode 100644 index 000000000..97b7846c1 --- /dev/null +++ b/tests/converter/tgenericconverter2.nim @@ -0,0 +1,85 @@ +# bug #3799 + +import strutils + +const output = splitLines(""" +00000000000000000000000000000000000000000 +00000000000001111111111111110000000000000 +00000000001111111111111111111110000000000 +00000000111111111111111111111111100000000 +00000011111222222221111111111111111000000 +00000111122222222222221111111111111100000 +00001112222333333459432111111111111110000 +00011122355544344463533221111111111111000 +00111124676667556896443322211111111111100 +00111126545561919686543322221111111111100 +01111123333346967807554322222211111111110 +01111122233334455582015332222221111111110 +01111122222333344567275432222222111111110 +01111112222222334456075443222222211111110 +01111111222222233459965444332222221111110 +01111111122222223457486554433322222111110 +01111111112222222367899655543333322111110 +01111111111122222344573948465444332111110 +00111111111112222334467987727667762111100 +00111111111111122233474655557836432111100 +00011111111111112233 454433334 4321111000 +00001111111111111122354333322222211110000 +00000111111111111111222222222222111100000 +00000001111111111111111122222111110000000 +00000000111111111111111111111111100000000 +00000000000111111111111111111100000000000 +00000000000000111111111111100000000000000 +""") + +const nmax = 500 + +type + Complex*[T] = object + re*: T + im*: T + +converter toComplex*[T](x: tuple[re, im: T]): Complex[T] = + result.re = x.re + result.im = x.im + + +proc julia*[T](z0, c: Complex[T], er2: T, nmax: int): int = + result = 0 + var z = z0 + var sre = z0.re * z0.re + var sim = z0.im * z0.im + while (result < nmax) and (sre + sim < er2): + z.im = z.re * z.im + z.im = z.im + z.im + z.im = z.im + c.im + z.re = sre - sim + c.re + sre = z.re * z.re + sim = z.im * z.im + inc result + +template dendriteFractal*[T](z0: Complex[T], er2: T, nmax: int): int = + julia(z0, (T(0.0), T(1.0)), er2, nmax) + +iterator stepIt[T](start, step: T, iterations: int): (int, T) = + for i in 0 .. iterations: + yield (i, start + T(i) * step) + +var errors = 0 + +let c = (0.36237, 0.32) +for j, y in stepIt(2.0, -0.0375 * 4, 107 div 4): + var row = "" + for i, x in stepIt(-2.0, 0.025 * 4, 160 div 4): + #let n = julia((x, y), c, 4.0, nmax) ### this works + let n = dendriteFractal((x, y), 4.0, nmax) + let c = char(int('0') + n mod 10) + let e = output[j][i] # expected + if c != e: + errors += 1 + row.add(c) + + # Printing apparently makes the test fail when joined. + # echo row + +doAssert errors < 10, "total errors: " & $errors diff --git a/tests/converter/tor_in_converter.nim b/tests/converter/tor_in_converter.nim new file mode 100644 index 000000000..df2334647 --- /dev/null +++ b/tests/converter/tor_in_converter.nim @@ -0,0 +1,23 @@ +discard """ + output: '''test +test''' +""" +# bug #4537 + +# nim js -d:nodejs + +type + Str = distinct string + +when true: + # crashes + converter convert(s: string | cstring): Str = Str($s) +else: + # works! + converter convert(s: string): Str = Str($s) + converter convert(s: cstring): Str = Str($s) + +proc echoStr(s: Str) = echo s.string + +echoStr("test") +echoStr("test".cstring) diff --git a/tests/converter/ttypeconverter1.nim b/tests/converter/ttypeconverter1.nim new file mode 100644 index 000000000..39eb6eff1 --- /dev/null +++ b/tests/converter/ttypeconverter1.nim @@ -0,0 +1,15 @@ +discard """ + output: '''foo +true''' +""" + +converter p(i: int): bool = return i != 0 + +if 1: + echo if 4: "foo" else: "barr" +while 0: + echo "bar" + +var a: array[3, bool] +a[0] = 3 +echo a[0] |