home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / tests / general / io.icn < prev    next >
Text File  |  2002-03-11  |  5KB  |  215 lines

  1. #SRC: JCON
  2.  
  3. #  I/O test -- writes ./tmp1 and ./tmp2 as well as stdout
  4.  
  5. procedure main()
  6.    local L, f, m, n, t1, t2
  7.  
  8.    L := [&input, &output, &errout,
  9.        m := open("/etc/motd") | stop("no /etc/motd"),
  10.     n := open("/dev/null", "w") | stop("no /dev/null")]
  11.    L := sort(L)
  12.    every f := !L do
  13.       write(type(f), ": ", image(f))
  14.  
  15.    write()
  16.    write(read())
  17.    write(read(&input))
  18.    while write(read()) do break
  19.    write(!&input)
  20.    every write(!&input \ 2)
  21.    every write(!&input) \ 2
  22.  
  23.    write()
  24.    write("flush /etc/motd: ", image(flush(m)) | "FAILED")
  25.    write("close /etc/motd: ", image(close(m)) | "FAILED")
  26.    write("close /etc/motd: ", image(close(m)) | "FAILED")
  27.    write("flush /etc/motd: ", image(flush(m)) | "FAILED")
  28.  
  29.    write()
  30.    write("flush /dev/null: ", image(flush(n)) | "FAILED")
  31.    write("close /dev/null: ", image(close(n)) | "FAILED")
  32.    write("close /dev/null: ", image(close(n)) | "FAILED")
  33.    write("flush /dev/null: ", image(flush(n)) | "FAILED")
  34.  
  35.    every remove("tmp1" | "tmp2")
  36.    every remove("tmp1" | "tmp2")
  37.    write(image(open("tmp1")))        # should fail
  38.  
  39.    write()
  40.    rfile("/dev/null")
  41.    wfile("tmp1", "w", "follow the yellow brick road")
  42.    rfile("tmp1")
  43.    wfile("tmp1", "w", "shorter file")
  44.    rfile("tmp1")
  45.    wfile("tmp1", "a", "gets extended")
  46.    rfile("tmp1")
  47.    wfile("tmp1", "rw", "changed")
  48.    rfile("tmp1")
  49.    wfile("tmp1", "b", "mode b ")
  50.    rfile("tmp1")
  51.    wfile("tmp1", "crw", "cleared anew")
  52.    rfile("tmp1")
  53.    rename("tmp1", "tmp2")
  54.    rfile("tmp2")
  55.  
  56.    write()
  57.    write(image(t1 := open("tmp1", "w")) | "can't open tmp1")
  58.    write(image(t2 := open("tmp2", "w")) | "can't open tmp2")
  59.    writes(">stdout", t1, ">1a", t2, ">2a", &output)
  60.    writes(">stdout", t2, ">2b", t1, ">1b", &output)
  61.    write(">stdout", t1, ">1c", t2, t2, ">2c", &output)
  62.    write(">stdout", t2, ">2d", t1, t1, ">1d", &output)
  63.    every write(t1 | t2)
  64.    writes(t1, ">1e")
  65.    writes(t2, ">2e")
  66.    write(t1, ">1f")
  67.    write(t2, ">2f")
  68.    every close(t1 | t2)
  69.    rfile("tmp1")
  70.    rfile("tmp2")
  71.  
  72.    every remove("tmp1" | "tmp2")
  73.    every remove("tmp1" | "tmp2")
  74.  
  75.    write()
  76.    writes("abc")
  77.    writes("def\nghi")
  78.    writes("\njklmno\n")
  79.    write("pqr\nstu")
  80.    writes("vwxyz")
  81.    writes()
  82.    writes("")
  83.    writes("\n")
  84.  
  85.    write()
  86.    tsys("echo hello world")            # simple echo
  87.    tsys("ls io.[ids][tca][dnt]")        # check wildcarding
  88.  
  89.    tpipe()
  90.  
  91. end
  92.  
  93.  
  94.  
  95. #  wfile(name, mode, s) -- break apart string and write file
  96.  
  97. procedure wfile(name, mode, s)
  98.    local f
  99.  
  100.    write()
  101.    writes("write ", name, ",", mode, ":\t ")
  102.    if f := open(name, mode) then s ? {
  103.       writes(s)
  104.       tab(many(' '))
  105.       while not pos(0) do {
  106.      write(f, tab(upto(' ') | 0))
  107.      tab(many(' '))
  108.          }
  109.       write(" : ", where(f))
  110.       flush(f)
  111.       close(f)
  112.       }
  113.    else {
  114.       write("can't open")
  115.       }
  116.    return
  117. end
  118.  
  119.  
  120.  
  121. #  rfile(name) -- read and echo file contents (several different ways)
  122.  
  123. procedure rfile(name)
  124.    local f, i
  125.  
  126.    writes("read  ", name, ":\t")
  127.    if not (f := open(name, "r")) then {
  128.       write(" can't open")
  129.       fail
  130.       }
  131.  
  132.    # read()
  133.    while writes(" ", read(f))
  134.    write()
  135.  
  136.    # bang
  137.    seek(f, 1)
  138.    every writes(" ", "  !f:\t\t" | !f | "\n")
  139.  
  140.    # both, mixed
  141.    seek(f, 1)
  142.    writes("   read/!f:\t")
  143.    while writes(" ", read(f)) do writes(" ", !f)
  144.    write()
  145.  
  146.    # reads()
  147.    seek(f, 1)
  148.    writes("   reads():\t")
  149.    while writes(" ", map(reads(f, 5), "\n", "."))
  150.    write()
  151.  
  152. #%#%   # nonsequential  (disabled because it's inconsistently buggy...)
  153. #%#%   writes("   nonseq:\t ")
  154. #%#%   every i := 30 to -30 by -1 do
  155. #%#%      if seek(f, i) then
  156. #%#%     writes(map(reads(f), "\n", ".") | "?")
  157. #%#%      else
  158. #%#%     writes("-")
  159. #%#%   write()
  160.  
  161.    close(f)
  162.    return
  163. end
  164.  
  165.  
  166. #  tsys(s) -- test system call
  167.  
  168. procedure tsys(s)
  169.    write("$ ", s)
  170.    system(s)
  171.    return
  172. end
  173.  
  174.  
  175. #  tpipe() -- test pipes
  176.  
  177. procedure tpipe()
  178.    local f, p
  179.  
  180.    # very simple case
  181.    write()
  182.    p := open("echo hello world", "rp") | stop("can't open echo pipe")
  183.    write(image(p))
  184.    while write("> ", read(p))
  185.    close(p)
  186.  
  187.    # check unclosed pipe
  188.    write()
  189.    p := open("sed 's/^/=()= /' io.icn", "p") | stop("can't open sed pipe")
  190.    write(image(p))
  191.    every 1 to 10 do write("> ", read(p))
  192.    # p is deliberately left unclosed
  193.  
  194.    # check wildcarding, and also !pipe
  195.    write()
  196.    p := open("ls io.i?n io.d?t io.s?d", "p") | stop("can't open ls pipe")
  197.    write(image(p))
  198.    every write("> ", !p)
  199.    close(p)
  200.  
  201.    # check output pipe
  202.    write()
  203.    p := open("tr aeiou oaeui", "wp") | stop("can't open tr pipe")
  204.    write(image(p))
  205.    write(p, "once upon a midnight dreary")
  206.    write(p, "two roads diverged in a yellow wood")
  207.    write(p, "and the mome raths outgrabe")
  208.    write("--- closing output pipe")
  209.    close(p)
  210.    write("--- done closing output pipe")
  211.    remove("tmp1")
  212.  
  213.    return
  214. end
  215.