summary refs log tree commit diff stats
path: root/tests/stdlib/tstreams.nim
blob: cc1343651bee531498d26113556ce567cf8146be (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
89
90
91
92
93
94
95
96
97
discard """
  matrix: "--gc:refc; --gc:arc"
  input: "Arne"
  output: '''
Hello! What is your name?
Nice name: Arne
fs is: nil
threw exception
_heh_
'''
  nimout: '''
I
AM
GROOT
'''
"""


import streams


block tstreams:
  var outp = newFileStream(stdout)
  var inp = newFileStream(stdin)
  writeLine(outp, "Hello! What is your name?")
  var line = readLine(inp)
  writeLine(outp, "Nice name: " & line)


block tstreams2:
  var
    fs = newFileStream("amissingfile.txt")
    line = ""
  echo "fs is: ",repr(fs)
  if not isNil(fs):
    while fs.readLine(line):
      echo line
    fs.close()


block tstreams3:
  try:
    var fs = openFileStream("shouldneverexist.txt")
  except IOError:
    echo "threw exception"

  static:
    var s = newStringStream("I\nAM\nGROOT")
    for line in s.lines:
      echo line
    s.close

# bug #12410

var a = newStringStream "hehohihahuhyh"
a.readDataStrImpl = nil

var buffer = "_ooo_"

doAssert a.readDataStr(buffer, 1..3) == 3

echo buffer


block:
  var ss = newStringStream("The quick brown fox jumped over the lazy dog.\nThe lazy dog ran")
  doAssert(ss.getPosition == 0)
  doAssert(ss.peekStr(5) == "The q")
  doAssert(ss.getPosition == 0) # haven't moved
  doAssert(ss.readStr(5) == "The q")
  doAssert(ss.getPosition == 5) # did move
  doAssert(ss.peekLine() == "uick brown fox jumped over the lazy dog.")
  doAssert(ss.getPosition == 5) # haven't moved
  var str = newString(100)
  doAssert(ss.peekLine(str))
  doAssert(str == "uick brown fox jumped over the lazy dog.")
  doAssert(ss.getPosition == 5) # haven't moved
  # bug #19707 - Ensure we dont error with writing over literals on arc/orc
  ss.setPosition(0)
  ss.write("hello")
  ss.setPosition(0)
  doAssert(ss.peekStr(5) == "hello")

# bug #19716
static: # Ensure streams it doesnt break with nimscript on arc/orc #19716
  let s = newStringStream("a")
  doAssert s.data == "a"

template main =
  var strm = newStringStream("abcde")
  var buffer = "12345"
  doAssert strm.readDataStr(buffer, 0..3) == 4
  doAssert buffer == "abcd5"
  strm.close()

static: main()
main()