home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / test / textio.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  7.0 KB  |  321 lines  |  [TEXT/R*ch]

  1. (* test/textio.sml
  2.    PS 1995-11-22, 1996-04-18
  3. *)
  4.  
  5. use "auxil.sml";
  6.  
  7. local 
  8.     open TextIO
  9.  
  10.     fun fileSize s = 
  11.     let val is = openIn s
  12.     in size (inputAll is) before closeIn is end;
  13.  
  14.     fun dup 0 s = s
  15.       | dup n s = dup (n-1) (s^s)
  16.  
  17.     val longstring = dup 16 "abcdefg"
  18. in
  19.  
  20. val empty  = openOut "empty.dat";
  21. val small  = openOut "small1.dat";
  22. val medium = openOut "medium.dat";
  23. val text   = openOut "text.dat";
  24.  
  25. val test1 = 
  26.     check'(fn _ => 
  27.        (closeOut empty; 
  28.         fileSize "empty.dat" = 0 
  29.         andalso fileSize "empty.dat" = 0));
  30.     
  31. val test2 = 
  32.     check'(fn _ => 
  33.        (output1(small, #"+");
  34.         closeOut small;
  35.         fileSize "small1.dat" = 1
  36.         andalso fileSize "small1.dat" = 1));
  37.  
  38. val test3 = 
  39.     check'(fn _ =>
  40.        let val small = openOut "small2.dat"
  41.        in 
  42.            output(small, "*");
  43.            closeOut small;
  44.            fileSize "small2.dat" = 1 andalso fileSize "small2.dat" = 1
  45.        end);
  46.  
  47. val test4 = 
  48.     check'(fn _ => 
  49.        (output(medium, longstring);
  50.         closeOut medium;
  51.         fileSize "medium.dat" = size longstring
  52.         andalso fileSize "medium.dat" = size longstring))
  53.  
  54. val test5 = 
  55.     check'(fn _ => 
  56.        let val small = openAppend "small2.dat"
  57.        in 
  58.            output(small, "1");
  59.            closeOut small;
  60.            fileSize "small2.dat" = 2 andalso fileSize "small2.dat" = 2
  61.        end);
  62.  
  63. val test6 = 
  64.     check'(fn _ => 
  65.        (output(text, "Line 1\n");
  66.         output(text, "Line 2\n");
  67.         output(text, "Line 3");
  68.         closeOut text;
  69.         fileSize "text.dat" = 20 andalso fileSize "text.dat" = 20));
  70.     
  71. (* Test that stdErr is flushed immediately, that flushOut works, and
  72.  * that print flushes stdOut.  Assumes that stdOut is *not* flushed
  73.  * immediately: *)
  74.  
  75. val _ = 
  76.     let fun stdo s = output(stdOut, s)
  77.     fun stde s = output(stdErr, s)
  78.     in
  79.     print "Two lines of output follow:\n";
  80.     stdo "3"; stde "1"; stdo "4"; stde "2";
  81.     flushOut stdOut; 
  82.     stde "  <--- this should read 1234\n";
  83.     stdo "2"; stde "1"; print "3"; stde "4"; stdo "5"; 
  84.     flushOut stdOut; 
  85.     stde " <--- this should read 12345\n"
  86.     end;
  87.  
  88. val test7a = 
  89.     check'(fn _ => 
  90.        let val is = openIn "empty.dat"
  91.        in 
  92.            (endOfStream is 
  93.         andalso input1 is = NONE 
  94.         andalso endOfStream is 
  95.         andalso input1 is = NONE)
  96.            before closeIn is 
  97.        end);
  98.  
  99. val test7b = 
  100.     check'(fn _ => 
  101.        let val is = openIn "small1.dat"
  102.        in 
  103.            (not (endOfStream is)
  104.         andalso input1 is = SOME #"+" 
  105.         andalso endOfStream is
  106.         andalso input1 is = NONE 
  107.         andalso input1 is = NONE)
  108.            before closeIn is 
  109.        end);
  110.  
  111. val test7c = 
  112.     check'(fn _ => 
  113.        let val is = openIn "small2.dat"
  114.        in 
  115.            (not (endOfStream is)
  116.         andalso input1 is = SOME #"*" 
  117.         andalso not (endOfStream is)
  118.         andalso input1 is = SOME #"1" 
  119.         andalso endOfStream is
  120.         andalso input1 is = NONE 
  121.         andalso input1 is = NONE)
  122.            before closeIn is 
  123.        end);
  124.  
  125. val test8a =
  126.     check'(fn _ =>
  127.        let val is = openIn "empty.dat"
  128.        in 
  129.            (inputN(is, 0) = ""
  130.         andalso inputN(is, 1) = ""
  131.         andalso inputN(is, 100) = ""
  132.         andalso endOfStream is)
  133.            before closeIn is 
  134.        end);
  135.  
  136. val test8b =
  137.     check'(fn _ =>
  138.        let val is = openIn "small1.dat"
  139.        in 
  140.            (inputN(is, 0) = ""
  141.         andalso inputN(is, 1) = "+"
  142.         andalso inputN(is, 100) = "")
  143.            before closeIn is 
  144.        end);
  145.  
  146. val test8c =
  147.     check'(fn _ =>
  148.        let val is = openIn "small1.dat"
  149.        in 
  150.            (inputN(is, 0) = ""
  151.         andalso inputN(is, 100) = "+"
  152.         andalso inputN(is, 100) = "")
  153.            before closeIn is 
  154.        end);
  155.  
  156. val test8d =
  157.     check'(fn _ =>
  158.        let val is = openIn "small2.dat"
  159.        in 
  160.            (inputN(is, 0) = ""
  161.         andalso inputN(is, 1) = "*"
  162.         andalso inputN(is, 100) = "1"
  163.         andalso inputN(is, 100) = "")
  164.            before closeIn is 
  165.        end);
  166.  
  167. val test8e =
  168.     check'(fn _ =>
  169.        let val is = openIn "medium.dat"
  170.        in 
  171.            (inputN(is, 0) = ""
  172.         andalso inputN(is, 15) = "abcdefgabcdefga"
  173.         andalso inputN(is, 15) = "bcdefgabcdefgab"
  174.         andalso inputN(is, 0) = ""
  175.         andalso not (endOfStream is))
  176.            before closeIn is 
  177.        end);
  178.    
  179. val test8f =
  180.     check'(fn _ =>
  181.        let val is = openIn "medium.dat"
  182.        in 
  183.            (inputN(is, 500000) = longstring
  184.         andalso inputN(is, 100) = ""
  185.         andalso endOfStream is)
  186.            before closeIn is 
  187.        end);
  188.    
  189. val test9a =
  190.     check'(fn _ =>
  191.        let val is = openIn "empty.dat"
  192.        in 
  193.            (lookahead is = NONE 
  194.         andalso input is = "" 
  195.         andalso lookahead is = NONE
  196.         andalso input is = "")
  197.            before closeIn is 
  198.        end);
  199.  
  200. val test9b =
  201.     check'(fn _ =>
  202.        let val is = openIn "small1.dat"
  203.        in 
  204.            (lookahead is = SOME #"+"
  205.         andalso input is = "+" 
  206.         andalso input is = ""
  207.         andalso lookahead is = NONE)
  208.            before closeIn is 
  209.        end);
  210.  
  211. val test9c =
  212.     check'(fn _ =>
  213.        let val is = openIn "small2.dat"
  214.        in 
  215.            (lookahead is = SOME #"*"
  216.         andalso input is = "*1" 
  217.         andalso input is = ""
  218.         andalso lookahead is = NONE)
  219.            before closeIn is 
  220.        end);
  221.  
  222. val test9d =
  223.     check'(fn _ =>
  224.        let val is = openIn "small2.dat"
  225.        in 
  226.            (input is = "*1" 
  227.         andalso input is = "")
  228.            before closeIn is 
  229.        end);
  230.  
  231. val test9e =
  232.     check'(fn _ =>
  233.        let val is = openIn "medium.dat"
  234.        in 
  235.            lookahead is = SOME #"a"
  236.            andalso String.substring(input is, 0, 15) = "abcdefgabcdefga"
  237.            before closeIn is 
  238.        end);
  239.    
  240. val test10 = 
  241.     check'(fn _ =>
  242.        let val is = openIn "medium.dat"
  243.        in 
  244.            (lookahead is = SOME #"a"
  245.         andalso input1 is = SOME #"a"
  246.         andalso lookahead is = SOME #"b"
  247.         andalso input1 is = SOME #"b"
  248.         andalso lookahead is = SOME #"c")
  249.            before closeIn is 
  250.        end);
  251.  
  252. val test11 = 
  253.     check'(fn _ =>
  254.        let val is = openIn "medium.dat"
  255.        in 
  256.            (lookahead is = SOME #"a"
  257.         andalso inputN(is, 5) = "abcde"
  258.         andalso lookahead is = SOME #"f"
  259.         andalso inputN(is, 4) = "fgab"
  260.         andalso lookahead is = SOME #"c")
  261.            before closeIn is 
  262.        end);
  263.  
  264. val test12a =
  265.     check'(fn _ => 
  266.        let val is = openIn "empty.dat"
  267.        in 
  268.            (inputLine is = ""
  269.         andalso inputLine is = "")
  270.            before closeIn is
  271.        end);
  272.  
  273. val test12b =
  274.     check'(fn _ => 
  275.        let val is = openIn "small1.dat"
  276.        in 
  277.            (inputLine is = "+\n"
  278.         andalso inputLine is = "")
  279.            before closeIn is
  280.        end);
  281.  
  282. val test12c =
  283.     check'(fn _ => 
  284.        let val is = openIn "text.dat"
  285.        in 
  286.            (inputLine is = "Line 1\n"
  287.            andalso inputLine is = "Line 2\n"
  288.            andalso inputLine is = "Line 3\n"
  289.            andalso inputLine is = "")
  290.            before closeIn is
  291.        end);
  292.  
  293. val test12d =
  294.     check'(fn _ => 
  295.        let val is = openIn "medium.dat"
  296.        in 
  297.            (inputLine is = longstring ^ "\n"
  298.            andalso inputLine is = "")
  299.            before closeIn is
  300.        end);
  301.  
  302. (* Test that outputSubstr works *)
  303.  
  304. val _ = 
  305.     let fun stdo s i n = outputSubstr(stdOut, Substring.substring(s, i, n))
  306.     fun stde s = output(stdErr, s)
  307.     val abcde = "abcde"
  308.     in
  309.     print "Two lines of output follow:\n";
  310.     stdo abcde 0 1; stdo abcde 1 3; 
  311.     stdo "" 0 0; stdo abcde 0 0; stdo abcde 5 0; stdo abcde 3 0; 
  312.     stdo abcde 4 1;
  313.     flushOut stdOut; 
  314.     stde " <--- this should read abcde\n";
  315.     stdo abcde 0 5;
  316.     flushOut stdOut; 
  317.     stde " <--- this should read abcde\n"
  318.     end;
  319.  
  320. end
  321.