]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-4241.rs
auto merge of #11251 : pcwalton/rust/remove-at-mut, r=pcwalton
[rust.git] / src / test / run-pass / issue-4241.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // xfail-fast
12 // xfail-test needs networking
13
14 extern mod extra;
15
16 use extra::net::tcp::TcpSocketBuf;
17
18 use std::io;
19 use std::int;
20
21 use std::io::{ReaderUtil,WriterUtil};
22
23 enum Result {
24   Nil,
25   Int(int),
26   Data(~[u8]),
27   List(~[Result]),
28   Error(~str),
29   Status(~str)
30 }
31
32 priv fn parse_data(len: uint, io: @io::Reader) -> Result {
33   let res =
34       if (len > 0) {
35       let bytes = io.read_bytes(len as uint);
36       assert_eq!(bytes.len(), len);
37       Data(bytes)
38   } else {
39       Data(~[])
40   };
41   assert_eq!(io.read_char(), '\r');
42   assert_eq!(io.read_char(), '\n');
43   return res;
44 }
45
46 priv fn parse_list(len: uint, io: @io::Reader) -> Result {
47   let mut list: ~[Result] = ~[];
48     do len.times {
49     let v =
50         match io.read_char() {
51         '$' => parse_bulk(io),
52         ':' => parse_int(io),
53          _ => fail!()
54     };
55     list.push(v);
56     }
57   return List(list);
58 }
59
60 priv fn chop(s: ~str) -> ~str {
61   s.slice(0, s.len() - 1).to_owned()
62 }
63
64 priv fn parse_bulk(io: @io::Reader) -> Result {
65     match from_str::<int>(chop(io.read_line())) {
66     None => fail!(),
67     Some(-1) => Nil,
68     Some(len) if len >= 0 => parse_data(len as uint, io),
69     Some(_) => fail!()
70     }
71 }
72
73 priv fn parse_multi(io: @io::Reader) -> Result {
74     match from_str::<int>(chop(io.read_line())) {
75     None => fail!(),
76     Some(-1) => Nil,
77     Some(0) => List(~[]),
78     Some(len) if len >= 0 => parse_list(len as uint, io),
79     Some(_) => fail!()
80     }
81 }
82
83 priv fn parse_int(io: @io::Reader) -> Result {
84     match from_str::<int>(chop(io.read_line())) {
85     None => fail!(),
86     Some(i) => Int(i)
87     }
88 }
89
90 priv fn parse_response(io: @io::Reader) -> Result {
91     match io.read_char() {
92     '$' => parse_bulk(io),
93     '*' => parse_multi(io),
94     '+' => Status(chop(io.read_line())),
95     '-' => Error(chop(io.read_line())),
96     ':' => parse_int(io),
97     _ => fail!()
98     }
99 }
100
101 priv fn cmd_to_str(cmd: ~[~str]) -> ~str {
102   let mut res = ~"*";
103   res.push_str(cmd.len().to_str());
104   res.push_str("\r\n");
105     for s in cmd.iter() {
106     res.push_str([~"$", s.len().to_str(), ~"\r\n",
107                   (*s).clone(), ~"\r\n"].concat() );
108     }
109   res
110 }
111
112 fn query(cmd: ~[~str], sb: TcpSocketBuf) -> Result {
113   let cmd = cmd_to_str(cmd);
114   //io::println(cmd);
115   sb.write_str(cmd);
116   let res = parse_response(@sb as @io::Reader);
117   res
118 }
119
120 fn query2(cmd: ~[~str]) -> Result {
121   let _cmd = cmd_to_str(cmd);
122     do io::with_str_reader(~"$3\r\nXXX\r\n") |sb| {
123     let res = parse_response(@sb as @io::Reader);
124     println!("{:?}", res);
125     res
126     }
127 }
128
129
130 pub fn main() {
131 }