titer.nim 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. discard """
  2. output: '''
  3. testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest2!test3?hi
  4. what's
  5. your
  6. name
  7. hi
  8. what's
  9. your
  10. name
  11. '''
  12. """
  13. # Test the new iterators
  14. iterator xrange(fromm, to: int, step = 1): int =
  15. var a = fromm
  16. while a <= to:
  17. yield a
  18. inc(a, step)
  19. iterator interval[T](a, b: T): T =
  20. var x = a
  21. while x <= b:
  22. yield x
  23. inc(x)
  24. #
  25. #iterator lines(filename: string): (line: string) =
  26. # var
  27. # f: tTextfile
  28. # shouldClose = open(f, filename)
  29. # if shouldClose:
  30. # setSpace(line, 256)
  31. # while readTextLine(f, line):
  32. # yield line
  33. # finally:
  34. # if shouldClose: close(f)
  35. #
  36. for i in xrange(0, 5):
  37. for k in xrange(1, 7):
  38. write(stdout, "test")
  39. for j in interval(45, 45):
  40. write(stdout, "test2!")
  41. write(stdout, "test3?")
  42. for x in items(["hi", "what's", "your", "name"]):
  43. echo(x)
  44. const
  45. stringArray = ["hi", "what's", "your", "name"]
  46. for i in 0..len(stringArray)-1:
  47. echo(stringArray[i])
  48. # bug #15360
  49. type Rule[T] = (int, T)
  50. var t: seq[Rule[int]]
  51. for (c, t) in t:
  52. discard
  53. import std/sugar
  54. # bug #14165
  55. iterator log_nodups_hamming(): int {.inline.} =
  56. let lb3 = 1
  57. let lb4 = 123
  58. proc mul3(): int = lb3 + lb4
  59. yield mul3()
  60. for h in log_nodups_hamming():
  61. break
  62. for h in log_nodups_hamming():
  63. break
  64. for h in log_nodups_hamming():
  65. break
  66. # bug #18536
  67. iterator envPairs*(): int =
  68. var foo: seq[int]
  69. proc fun() =
  70. foo = @[]
  71. fun()
  72. yield 3
  73. proc main() =
  74. for a in envPairs():
  75. discard
  76. for a in envPairs():
  77. discard
  78. static: main()
  79. main()
  80. # bug #6269
  81. iterator makeFn(outer_val: int): proc(a: int): int =
  82. for i in 0..1:
  83. yield proc(a:int): int =
  84. return a + i.int
  85. let v1 = 42
  86. let res = collect:
  87. for fn1 in makeFn(v1):
  88. let v2 = fn1(v1)
  89. for fn2 in makeFn(v2):
  90. fn2(v2)
  91. doAssert res == @[42, 43, 43, 44]
  92. block: # bug #21110
  93. iterator p(): int =
  94. when nimvm:
  95. yield 0
  96. else:
  97. yield 0
  98. template foo =
  99. for k in p():
  100. let m = ""
  101. proc e() = discard m & ""
  102. e()
  103. static: foo()
  104. foo()
  105. # bug #15924
  106. iterator walk(): (int, int) {.closure.} =
  107. yield (10,11)
  108. for (i,j) in walk():
  109. doAssert i == 10
  110. proc main123() =
  111. let x = false
  112. iterator it(): (bool, bool) {.closure.} = # normally {.closure.} here makes #21476 work
  113. discard x
  114. for (_, _) in it():
  115. discard
  116. main123()