summary refs log tree commit diff stats
path: root/tests/openarray/topenarray.nim
blob: 25b983651a535237af18315204c3382d12202c66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
discard """
  targets: "c cpp js"
"""

proc fn1[T](a: openArray[T]): seq[T] =
  for ai in a: result.add ai

proc fn2[T](a: var openArray[T]): seq[T] =
  for ai in a: result.add ai

proc fn3[T](a: var openArray[T]) =
  for i, ai in mpairs(a): ai = i * 10

proc main =
  var a = [1,2,3,4,5]

  doAssert fn1(a.toOpenArray(1,3)) == @[2,3,4]

  doAssert fn2(toOpenArray(a, 1, 3)) == @[2,3,4]
  doAssert fn2(a.toOpenArray(1,3)) == @[2,3,4]

  fn3(a.toOpenArray(1,3))
  when defined(js): discard # xxx bug #15952: `a` left unchanged
  else: doAssert a == [1, 0, 10, 20, 5]

  block: # bug #12521
    block:
      type slice[T] = openArray[T]

      # Proc using that alias
      proc testing(sl: slice[int]): seq[int] =
        for item in sl:
          result.add item

      let mySeq = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
      doAssert testing(mySeq) == mySeq
      doAssert testing(mySeq[2..^2]) == mySeq[2..^2]

    block:
      type slice = openArray[int]

      # Proc using that alias
      proc testing(sl: slice): seq[int] =
        for item in sl:
          result.add item

      let mySeq = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
      doAssert testing(mySeq) == mySeq
      doAssert testing(mySeq[2..^2]) == mySeq[2..^2]

  block: # bug #23321
    block:
      proc foo(x: openArray[int]) =
        doAssert x[0] == 0

      var d = new array[1, int]
      foo d[].toOpenArray(0, 0)

    block:
      proc foo(x: openArray[int]) =
        doAssert x[0] == 0

      proc task(x: var array[1, int]): var array[1, int] =
        result = x
      var d: array[1, int]
      foo task(d).toOpenArray(0, 0)

    block:
      proc foo(x: openArray[int]) =
        doAssert x[0] == 0

      proc task(x: var array[1, int]): lent array[1, int] =
        result = x
      var d: array[1, int]
      foo task(d).toOpenArray(0, 0)

    block:
      proc foo(x: openArray[int]) =
        doAssert x[0] == 0

      proc task(x: var array[1, int]): ptr array[1, int] =
        result = addr x
      var d: array[1, int]
      foo task(d)[].toOpenArray(0, 0)


main()
static: main()