]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-4241.rs
auto merge of #12185 : nikomatsakis/rust/issue-12033-tweak-test, r=alexchrichton
[rust.git] / src / test / run-pass / issue-4241.rs
1 // Copyright 2013-2014 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 // ignore-fast
12 // ignore-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     for _ in range(0, len) {
49         let v = match io.read_char() {
50             '$' => parse_bulk(io),
51             ':' => parse_int(io),
52              _ => fail!()
53         };
54         list.push(v);
55     }
56     return List(list);
57 }
58
59 priv fn chop(s: ~str) -> ~str {
60   s.slice(0, s.len() - 1).to_owned()
61 }
62
63 priv fn parse_bulk(io: @io::Reader) -> Result {
64     match from_str::<int>(chop(io.read_line())) {
65     None => fail!(),
66     Some(-1) => Nil,
67     Some(len) if len >= 0 => parse_data(len as uint, io),
68     Some(_) => fail!()
69     }
70 }
71
72 priv fn parse_multi(io: @io::Reader) -> Result {
73     match from_str::<int>(chop(io.read_line())) {
74     None => fail!(),
75     Some(-1) => Nil,
76     Some(0) => List(~[]),
77     Some(len) if len >= 0 => parse_list(len as uint, io),
78     Some(_) => fail!()
79     }
80 }
81
82 priv fn parse_int(io: @io::Reader) -> Result {
83     match from_str::<int>(chop(io.read_line())) {
84     None => fail!(),
85     Some(i) => Int(i)
86     }
87 }
88
89 priv fn parse_response(io: @io::Reader) -> Result {
90     match io.read_char() {
91     '$' => parse_bulk(io),
92     '*' => parse_multi(io),
93     '+' => Status(chop(io.read_line())),
94     '-' => Error(chop(io.read_line())),
95     ':' => parse_int(io),
96     _ => fail!()
97     }
98 }
99
100 priv fn cmd_to_str(cmd: ~[~str]) -> ~str {
101   let mut res = ~"*";
102   res.push_str(cmd.len().to_str());
103   res.push_str("\r\n");
104     for s in cmd.iter() {
105     res.push_str([~"$", s.len().to_str(), ~"\r\n",
106                   (*s).clone(), ~"\r\n"].concat() );
107     }
108   res
109 }
110
111 fn query(cmd: ~[~str], sb: TcpSocketBuf) -> Result {
112   let cmd = cmd_to_str(cmd);
113   //println!("{}", cmd);
114   sb.write_str(cmd);
115   let res = parse_response(@sb as @io::Reader);
116   res
117 }
118
119 fn query2(cmd: ~[~str]) -> Result {
120   let _cmd = cmd_to_str(cmd);
121     io::with_str_reader(~"$3\r\nXXX\r\n")(|sb| {
122     let res = parse_response(@sb as @io::Reader);
123     println!("{:?}", res);
124     res
125     });
126 }
127
128
129 pub fn main() {
130 }