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

  1. (* test/stringcvt.sml -- 1995-10-26, 1996-07-05 *)
  2.  
  3. use "auxil.sml";
  4.  
  5. local 
  6.     open StringCvt
  7.     (* Read all upper case letters, skip lowercase letters, scan an
  8.      * integer, and return the excess characters: *)
  9.  
  10.     fun triple getc src =
  11.     let open StringCvt
  12.         val (str1, src1) = splitl Char.isUpper getc src
  13.         val src2         = dropl Char.isLower getc src1
  14.     in case Int.scan DEC getc src2 of
  15.         NONE            => NONE
  16.       | SOME (i, src3)  => 
  17.         let val str2 = takel (fn _ => true) getc src3
  18.         in SOME((str1, i, str2), src3) end
  19.     end
  20.  
  21.     (* Testing TextIO.scanStream: *)
  22.  
  23.     val tmpfile = "textio.tmp";
  24.     fun putandscan scan s = 
  25.     let open TextIO 
  26.         val os = openOut tmpfile
  27.         val _  = output(os, s)
  28.         val _  = closeOut os 
  29.         val is = openIn tmpfile
  30.     in
  31.         scanStream scan is
  32.         before 
  33.         closeIn is
  34.     end;
  35.         
  36.     fun testtrip (s, res) = 
  37.     scanString triple s = res
  38.     andalso putandscan triple s = res
  39.  
  40.     datatype result = Bool of bool | Int of int
  41.  
  42.     fun backtrack getc src = 
  43.     case Bool.scan getc src of
  44.         SOME(b, rest) => SOME (Bool b, rest)
  45.       | NONE          => 
  46.         case Int.scan StringCvt.DEC getc src of
  47.             SOME(i, rest) => SOME(Int i, rest)
  48.           | NONE   => 
  49.             case Int.scan StringCvt.HEX getc src of
  50.                 SOME(i, rest) => SOME(Int i, rest)
  51.               | NONE   => NONE
  52.  
  53.     fun testback (s, res) =
  54.     scanString backtrack s = res
  55.     andalso putandscan backtrack s = res
  56.  
  57. in
  58.  
  59. val test1 = 
  60.     check'(fn _ =>
  61.        padLeft #"#" 0 "abcdef" = "abcdef"
  62.        andalso padLeft #"#" 6 "abcdef" = "abcdef"
  63.        andalso padLeft #"#" 7 "abcdef" = "#abcdef"
  64.        andalso padLeft #"#" 10 "abcdef" = "####abcdef"
  65.        andalso padLeft #"#" ~3 "abcdef" = "abcdef");
  66.  
  67. val test2 = 
  68.     check'(fn _ =>
  69.        padRight #"#" 0 "abcdef" = "abcdef"
  70.        andalso padRight #"#" 6 "abcdef" = "abcdef"
  71.        andalso padRight #"#" 7 "abcdef" = "abcdef#"
  72.        andalso padRight #"#" 10 "abcdef" = "abcdef####"
  73.        andalso padRight #"#" ~3 "abcdef" = "abcdef");
  74.  
  75. val test3 = 
  76.     check'(fn _ =>
  77.     testtrip ("", NONE)
  78.     andalso testtrip(" a1", NONE)
  79.     andalso testtrip(" A1", NONE)
  80.     andalso testtrip("ABC A1", NONE)
  81.     andalso testtrip("ABC a1", NONE)
  82.     andalso testtrip(" *1", NONE)
  83.     andalso testtrip("ABC *1", NONE));
  84.  
  85. val test4 = 
  86.     check'(fn _ =>
  87.     testtrip ("1", SOME("", 1, ""))
  88.     andalso testtrip ("1", SOME("", 1, ""))
  89.     andalso testtrip (" 1", SOME("", 1, ""))
  90.     andalso testtrip (" 1  ", SOME("", 1, "  ")));
  91.  
  92. val test5 =
  93.     check'(fn _ =>
  94.     testtrip ("1a123+ +&D", SOME("", 1, "a123+ +&D"))
  95.     andalso testtrip ("1a123+ +&D", SOME("", 1, "a123+ +&D"))
  96.     andalso testtrip ("a1a123+ +&D", SOME("", 1, "a123+ +&D"))
  97.     andalso testtrip ("a1a123+ +&D", SOME("", 1, "a123+ +&D"))
  98.     andalso testtrip ("azbc1a123+ +&D", SOME("", 1, "a123+ +&D"))
  99.     andalso testtrip ("azbc1a123+ +&D", SOME("", 1, "a123+ +&D"))
  100.     andalso testtrip ("azbc  1a123+ +&D", SOME("", 1, "a123+ +&D"))
  101.     andalso testtrip ("azbc  1a123+ +&D", SOME("", 1, "a123+ +&D")))
  102.  
  103. val test6 = 
  104.     check'(fn _ =>
  105.     testtrip ("~1234a123+ +&D", SOME("", ~1234, "a123+ +&D"))
  106.     andalso testtrip ("~1234a123+ +&D", SOME("", ~1234, "a123+ +&D"))
  107.     andalso testtrip ("a~1234a123+ +&D", SOME("", ~1234, "a123+ +&D"))
  108.     andalso testtrip ("a~1234a123+ +&D", SOME("", ~1234, "a123+ +&D"))
  109.     andalso testtrip ("azbc~1234a123+ +&D", SOME("", ~1234, "a123+ +&D"))
  110.     andalso testtrip ("azbc~1234a123+ +&D", SOME("", ~1234, "a123+ +&D"))
  111.     andalso testtrip ("azbc  ~1234a123+ +&D", SOME("", ~1234, "a123+ +&D"))
  112.     andalso testtrip ("azbc  ~1234a123+ +&D", SOME("", ~1234, "a123+ +&D")))
  113.  
  114. val test7 =
  115.     check'(fn _ =>
  116.     testtrip ("A1a123+ +&D", SOME("A", 1, "a123+ +&D"))
  117.     andalso testtrip ("ABCDEFG1a123+ +&D", SOME("ABCDEFG", 1, "a123+ +&D"))
  118.     andalso testtrip ("Aa1a123+ +&D", SOME("A", 1, "a123+ +&D"))
  119.     andalso testtrip ("ABCDEFGa1a123+ +&D", SOME("ABCDEFG", 1, "a123+ +&D"))
  120.     andalso testtrip ("Aazbc1a123+ +&D", SOME("A", 1, "a123+ +&D"))
  121.     andalso testtrip ("ABCDEFGazbc1a123+ +&D", SOME("ABCDEFG", 1, "a123+ +&D"))
  122.     andalso testtrip ("Aazbc  1a123+ +&D", SOME("A", 1, "a123+ +&D"))
  123.     andalso testtrip ("ABCDEFGazbc  1a123+ +&D", SOME("ABCDEFG", 1, "a123+ +&D")))
  124.  
  125. val test8 = 
  126.     check'(fn _ =>
  127.     testtrip ("A~1234a123+ +&D", SOME("A", ~1234, "a123+ +&D"))
  128.     andalso 
  129.     testtrip ("ABCDEFG~1234a123+ +&D", SOME("ABCDEFG", ~1234, "a123+ +&D"))
  130.     andalso testtrip ("Aa~1234a123+ +&D", SOME("A", ~1234, "a123+ +&D"))
  131.     andalso 
  132.     testtrip ("ABCDEFGa~1234a123+ +&D", SOME("ABCDEFG", ~1234, "a123+ +&D"))
  133.     andalso testtrip ("Aazbc~1234a123+ +&D", SOME("A", ~1234, "a123+ +&D"))
  134.     andalso 
  135.     testtrip ("ABCDEFGazbc~1234a123+ +&D", SOME("ABCDEFG", ~1234, "a123+ +&D"))
  136.     andalso testtrip ("Aazbc  ~1234a123+ +&D", SOME("A", ~1234, "a123+ +&D"))
  137.     andalso 
  138.     testtrip ("ABCDEFGazbc  ~1234a123+ +&D", SOME("ABCDEFG", ~1234, "a123+ +&D")))
  139.  
  140. val test9 = 
  141.     check'(fn _ =>
  142.     let fun getstring b getc src = 
  143.         SOME(takel (fn _ => b) getc src, src)
  144.     fun dup 0 s = s
  145.       | dup n s = dup (n-1) (s^s);
  146.     val longstring = dup 13 "abcDEFGHI"
  147.     in 
  148.     scanString (getstring true) longstring = SOME longstring 
  149.     andalso scanString (getstring false) longstring = SOME ""
  150.         andalso putandscan (getstring true) longstring = SOME longstring
  151.     end)
  152.  
  153. val test10 = 
  154.     check'(fn _ => 
  155.        List.all testback
  156.        [("false",  SOME (Bool false)),
  157.         ("true",   SOME (Bool true)),
  158.         ("tru e",  NONE),
  159.         ("fals e", SOME (Int 250)),
  160.         ("fa",     SOME (Int 250)),
  161.         ("fa00",   SOME (Int 64000)),
  162.         ("21a",    SOME (Int 21)),
  163.         ("a21",    SOME (Int 2593)),
  164.         ("",       NONE),
  165.         ("gryf",   NONE)
  166.         ]);
  167.     
  168. val _ = FileSys.remove tmpfile
  169.  
  170. end
  171.