(index<- ) ./libstd/rt/io/extensions.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 //! Utility mixins that apply to all Readers and Writers
12
13 // XXX: Not sure how this should be structured
14 // XXX: Iteration should probably be considered separately
15
16 use uint;
17 use int;
18 use iter::Iterator;
19 use vec;
20 use rt::io::{Reader, Writer, Decorator};
21 use rt::io::{read_error, standard_error, EndOfFile, DEFAULT_BUF_SIZE};
22 use option::{Option, Some, None};
23 use unstable::finally::Finally;
24 use cast;
25 use io::{u64_to_le_bytes, u64_to_be_bytes};
26
27 pub trait ReaderUtil {
28
29 /// Reads a single byte. Returns `None` on EOF.
30 ///
31 /// # Failure
32 ///
33 /// Raises the same conditions as the `read` method. Returns
34 /// `None` if the condition is handled.
35 fn read_byte(&mut self) -> Option<u8>;
36
37 /// Reads `len` bytes and appends them to a vector.
38 ///
39 /// May push fewer than the requested number of bytes on error
40 /// or EOF. Returns true on success, false on EOF or error.
41 ///
42 /// # Failure
43 ///
44 /// Raises the same conditions as `read`. Additionally raises `read_error`
45 /// on EOF. If `read_error` is handled then `push_bytes` may push less
46 /// than the requested number of bytes.
47 fn push_bytes(&mut self, buf: &mut ~[u8], len: uint);
48
49 /// Reads `len` bytes and gives you back a new vector of length `len`
50 ///
51 /// # Failure
52 ///
53 /// Raises the same conditions as `read`. Additionally raises `read_error`
54 /// on EOF. If `read_error` is handled then the returned vector may
55 /// contain less than the requested number of bytes.
56 fn read_bytes(&mut self, len: uint) -> ~[u8];
57
58 /// Reads all remaining bytes from the stream.
59 ///
60 /// # Failure
61 ///
62 /// Raises the same conditions as the `read` method.
63 fn read_to_end(&mut self) -> ~[u8];
64
65 /// Create an iterator that reads a single byte on
66 /// each iteration, until EOF.
67 ///
68 /// # Failure
69 ///
70 /// Raises the same conditions as the `read` method, for
71 /// each call to its `.next()` method.
72 /// Ends the iteration if the condition is handled.
73 fn bytes(self) -> ByteIterator<Self>;
74
75 }
76
77 pub trait ReaderByteConversions {
78 /// Reads `n` little-endian unsigned integer bytes.
79 ///
80 /// `n` must be between 1 and 8, inclusive.
81 fn read_le_uint_n_(&mut self, nbytes: uint) -> u64;
82
83 /// Reads `n` little-endian signed integer bytes.
84 ///
85 /// `n` must be between 1 and 8, inclusive.
86 fn read_le_int_n_(&mut self, nbytes: uint) -> i64;
87
88 /// Reads `n` big-endian unsigned integer bytes.
89 ///
90 /// `n` must be between 1 and 8, inclusive.
91 fn read_be_uint_n_(&mut self, nbytes: uint) -> u64;
92
93 /// Reads `n` big-endian signed integer bytes.
94 ///
95 /// `n` must be between 1 and 8, inclusive.
96 fn read_be_int_n_(&mut self, nbytes: uint) -> i64;
97
98 /// Reads a little-endian unsigned integer.
99 ///
100 /// The number of bytes returned is system-dependant.
101 fn read_le_uint_(&mut self) -> uint;
102
103 /// Reads a little-endian integer.
104 ///
105 /// The number of bytes returned is system-dependant.
106 fn read_le_int_(&mut self) -> int;
107
108 /// Reads a big-endian unsigned integer.
109 ///
110 /// The number of bytes returned is system-dependant.
111 fn read_be_uint_(&mut self) -> uint;
112
113 /// Reads a big-endian integer.
114 ///
115 /// The number of bytes returned is system-dependant.
116 fn read_be_int_(&mut self) -> int;
117
118 /// Reads a big-endian `u64`.
119 ///
120 /// `u64`s are 8 bytes long.
121 fn read_be_u64_(&mut self) -> u64;
122
123 /// Reads a big-endian `u32`.
124 ///
125 /// `u32`s are 4 bytes long.
126 fn read_be_u32_(&mut self) -> u32;
127
128 /// Reads a big-endian `u16`.
129 ///
130 /// `u16`s are 2 bytes long.
131 fn read_be_u16_(&mut self) -> u16;
132
133 /// Reads a big-endian `i64`.
134 ///
135 /// `i64`s are 8 bytes long.
136 fn read_be_i64_(&mut self) -> i64;
137
138 /// Reads a big-endian `i32`.
139 ///
140 /// `i32`s are 4 bytes long.
141 fn read_be_i32_(&mut self) -> i32;
142
143 /// Reads a big-endian `i16`.
144 ///
145 /// `i16`s are 2 bytes long.
146 fn read_be_i16_(&mut self) -> i16;
147
148 /// Reads a big-endian `f64`.
149 ///
150 /// `f64`s are 8 byte, IEEE754 double-precision floating point numbers.
151 fn read_be_f64_(&mut self) -> f64;
152
153 /// Reads a big-endian `f32`.
154 ///
155 /// `f32`s are 4 byte, IEEE754 single-precision floating point numbers.
156 fn read_be_f32_(&mut self) -> f32;
157
158 /// Reads a little-endian `u64`.
159 ///
160 /// `u64`s are 8 bytes long.
161 fn read_le_u64_(&mut self) -> u64;
162
163 /// Reads a little-endian `u32`.
164 ///
165 /// `u32`s are 4 bytes long.
166 fn read_le_u32_(&mut self) -> u32;
167
168 /// Reads a little-endian `u16`.
169 ///
170 /// `u16`s are 2 bytes long.
171 fn read_le_u16_(&mut self) -> u16;
172
173 /// Reads a little-endian `i64`.
174 ///
175 /// `i64`s are 8 bytes long.
176 fn read_le_i64_(&mut self) -> i64;
177
178 /// Reads a little-endian `i32`.
179 ///
180 /// `i32`s are 4 bytes long.
181 fn read_le_i32_(&mut self) -> i32;
182
183 /// Reads a little-endian `i16`.
184 ///
185 /// `i16`s are 2 bytes long.
186 fn read_le_i16_(&mut self) -> i16;
187
188 /// Reads a little-endian `f64`.
189 ///
190 /// `f64`s are 8 byte, IEEE754 double-precision floating point numbers.
191 fn read_le_f64_(&mut self) -> f64;
192
193 /// Reads a little-endian `f32`.
194 ///
195 /// `f32`s are 4 byte, IEEE754 single-precision floating point numbers.
196 fn read_le_f32_(&mut self) -> f32;
197
198 /// Read a u8.
199 ///
200 /// `u8`s are 1 byte.
201 fn read_u8_(&mut self) -> u8;
202
203 /// Read an i8.
204 ///
205 /// `i8`s are 1 byte.
206 fn read_i8_(&mut self) -> i8;
207
208 }
209
210 pub trait WriterByteConversions {
211 /// Write the result of passing n through `int::to_str_bytes`.
212 fn write_int_(&mut self, n: int);
213
214 /// Write the result of passing n through `uint::to_str_bytes`.
215 fn write_uint_(&mut self, n: uint);
216
217 /// Write a little-endian uint (number of bytes depends on system).
218 fn write_le_uint_(&mut self, n: uint);
219
220 /// Write a little-endian int (number of bytes depends on system).
221 fn write_le_int_(&mut self, n: int);
222
223 /// Write a big-endian uint (number of bytes depends on system).
224 fn write_be_uint_(&mut self, n: uint);
225
226 /// Write a big-endian int (number of bytes depends on system).
227 fn write_be_int_(&mut self, n: int);
228
229 /// Write a big-endian u64 (8 bytes).
230 fn write_be_u64_(&mut self, n: u64);
231
232 /// Write a big-endian u32 (4 bytes).
233 fn write_be_u32_(&mut self, n: u32);
234
235 /// Write a big-endian u16 (2 bytes).
236 fn write_be_u16_(&mut self, n: u16);
237
238 /// Write a big-endian i64 (8 bytes).
239 fn write_be_i64_(&mut self, n: i64);
240
241 /// Write a big-endian i32 (4 bytes).
242 fn write_be_i32_(&mut self, n: i32);
243
244 /// Write a big-endian i16 (2 bytes).
245 fn write_be_i16_(&mut self, n: i16);
246
247 /// Write a big-endian IEEE754 double-precision floating-point (8 bytes).
248 fn write_be_f64_(&mut self, f: f64);
249
250 /// Write a big-endian IEEE754 single-precision floating-point (4 bytes).
251 fn write_be_f32_(&mut self, f: f32);
252
253 /// Write a little-endian u64 (8 bytes).
254 fn write_le_u64_(&mut self, n: u64);
255
256 /// Write a little-endian u32 (4 bytes).
257 fn write_le_u32_(&mut self, n: u32);
258
259 /// Write a little-endian u16 (2 bytes).
260 fn write_le_u16_(&mut self, n: u16);
261
262 /// Write a little-endian i64 (8 bytes).
263 fn write_le_i64_(&mut self, n: i64);
264
265 /// Write a little-endian i32 (4 bytes).
266 fn write_le_i32_(&mut self, n: i32);
267
268 /// Write a little-endian i16 (2 bytes).
269 fn write_le_i16_(&mut self, n: i16);
270
271 /// Write a little-endian IEEE754 double-precision floating-point
272 /// (8 bytes).
273 fn write_le_f64_(&mut self, f: f64);
274
275 /// Write a little-endian IEEE754 single-precision floating-point
276 /// (4 bytes).
277 fn write_le_f32_(&mut self, f: f32);
278
279 /// Write a u8 (1 byte).
280 fn write_u8_(&mut self, n: u8);
281
282 /// Write a i8 (1 byte).
283 fn write_i8_(&mut self, n: i8);
284 }
285
286 impl<T: Reader> ReaderUtil for T {
287 fn read_byte(&mut self) -> Option<u8> {
288 let mut buf = [0];
289 match self.read(buf) {
290 Some(0) => {
291 debug2!("read 0 bytes. trying again");
292 self.read_byte()
293 }
294 Some(1) => Some(buf[0]),
295 Some(_) => unreachable!(),
296 None => None
297 }
298 }
299
300 fn push_bytes(&mut self, buf: &mut ~[u8], len: uint) {
301 unsafe {
302 let start_len = buf.len();
303 let mut total_read = 0;
304
305 buf.reserve_additional(len);
306 vec::raw::set_len(buf, start_len + len);
307
308 do (|| {
309 while total_read < len {
310 let len = buf.len();
311 let slice = buf.mut_slice(start_len + total_read, len);
312 match self.read(slice) {
313 Some(nread) => {
314 total_read += nread;
315 }
316 None => {
317 read_error::cond.raise(standard_error(EndOfFile));
318 break;
319 }
320 }
321 }
322 }).finally {
323 vec::raw::set_len(buf, start_len + total_read);
324 }
325 }
326 }
327
328 fn read_bytes(&mut self, len: uint) -> ~[u8] {
329 let mut buf = vec::with_capacity(len);
330 self.push_bytes(&mut buf, len);
331 return buf;
332 }
333
334 fn read_to_end(&mut self) -> ~[u8] {
335 let mut buf = vec::with_capacity(DEFAULT_BUF_SIZE);
336 let mut keep_reading = true;
337 do read_error::cond.trap(|e| {
338 if e.kind == EndOfFile {
339 keep_reading = false;
340 } else {
341 read_error::cond.raise(e)
342 }
343 }).inside {
344 while keep_reading {
345 self.push_bytes(&mut buf, DEFAULT_BUF_SIZE)
346 }
347 }
348 return buf;
349 }
350
351 fn bytes(self) -> ByteIterator<T> {
352 ByteIterator{reader: self}
353 }
354 }
355
356 /// An iterator that reads a single byte on each iteration,
357 /// until `.read_byte()` returns `None`.
358 ///
359 /// # Notes about the Iteration Protocol
360 ///
361 /// The `ByteIterator` may yield `None` and thus terminate
362 /// an iteration, but continue to yield elements if iteration
363 /// is attempted again.
364 ///
365 /// # Failure
366 ///
367 /// Raises the same conditions as the `read` method, for
368 /// each call to its `.next()` method.
369 /// Yields `None` if the condition is handled.
370 pub struct ByteIterator<T> {
371 priv reader: T,
372 }
373
374 impl<R> Decorator<R> for ByteIterator<R> {
375 fn inner(self) -> R { self.reader }
376 fn inner_ref<'a>(&'a self) -> &'a R { &self.reader }
377 fn inner_mut_ref<'a>(&'a mut self) -> &'a mut R { &mut self.reader }
378 }
379
380 impl<'self, R: Reader> Iterator<u8> for ByteIterator<R> {
381 #[inline]
382 fn next(&mut self) -> Option<u8> {
383 self.reader.read_byte()
384 }
385 }
386
387 impl<T: Reader> ReaderByteConversions for T {
388 fn read_le_uint_n_(&mut self, nbytes: uint) -> u64 {
389 assert!(nbytes > 0 && nbytes <= 8);
390
391 let mut val = 0u64;
392 let mut pos = 0;
393 let mut i = nbytes;
394 while i > 0 {
395 val += (self.read_u8_() as u64) << pos;
396 pos += 8;
397 i -= 1;
398 }
399 val
400 }
401
402 fn read_le_int_n_(&mut self, nbytes: uint) -> i64 {
403 extend_sign(self.read_le_uint_n_(nbytes), nbytes)
404 }
405
406 fn read_be_uint_n_(&mut self, nbytes: uint) -> u64 {
407 assert!(nbytes > 0 && nbytes <= 8);
408
409 let mut val = 0u64;
410 let mut i = nbytes;
411 while i > 0 {
412 i -= 1;
413 val += (self.read_u8_() as u64) << i * 8;
414 }
415 val
416 }
417
418 fn read_be_int_n_(&mut self, nbytes: uint) -> i64 {
419 extend_sign(self.read_be_uint_n_(nbytes), nbytes)
420 }
421
422 fn read_le_uint_(&mut self) -> uint {
423 self.read_le_uint_n_(uint::bytes) as uint
424 }
425
426 fn read_le_int_(&mut self) -> int {
427 self.read_le_int_n_(int::bytes) as int
428 }
429
430 fn read_be_uint_(&mut self) -> uint {
431 self.read_be_uint_n_(uint::bytes) as uint
432 }
433
434 fn read_be_int_(&mut self) -> int {
435 self.read_be_int_n_(int::bytes) as int
436 }
437
438 fn read_be_u64_(&mut self) -> u64 {
439 self.read_be_uint_n_(8) as u64
440 }
441
442 fn read_be_u32_(&mut self) -> u32 {
443 self.read_be_uint_n_(4) as u32
444 }
445
446 fn read_be_u16_(&mut self) -> u16 {
447 self.read_be_uint_n_(2) as u16
448 }
449
450 fn read_be_i64_(&mut self) -> i64 {
451 self.read_be_int_n_(8) as i64
452 }
453
454 fn read_be_i32_(&mut self) -> i32 {
455 self.read_be_int_n_(4) as i32
456 }
457
458 fn read_be_i16_(&mut self) -> i16 {
459 self.read_be_int_n_(2) as i16
460 }
461
462 fn read_be_f64_(&mut self) -> f64 {
463 unsafe {
464 cast::transmute::<u64, f64>(self.read_be_u64_())
465 }
466 }
467
468 fn read_be_f32_(&mut self) -> f32 {
469 unsafe {
470 cast::transmute::<u32, f32>(self.read_be_u32_())
471 }
472 }
473
474 fn read_le_u64_(&mut self) -> u64 {
475 self.read_le_uint_n_(8) as u64
476 }
477
478 fn read_le_u32_(&mut self) -> u32 {
479 self.read_le_uint_n_(4) as u32
480 }
481
482 fn read_le_u16_(&mut self) -> u16 {
483 self.read_le_uint_n_(2) as u16
484 }
485
486 fn read_le_i64_(&mut self) -> i64 {
487 self.read_le_int_n_(8) as i64
488 }
489
490 fn read_le_i32_(&mut self) -> i32 {
491 self.read_le_int_n_(4) as i32
492 }
493
494 fn read_le_i16_(&mut self) -> i16 {
495 self.read_le_int_n_(2) as i16
496 }
497
498 fn read_le_f64_(&mut self) -> f64 {
499 unsafe {
500 cast::transmute::<u64, f64>(self.read_le_u64_())
501 }
502 }
503
504 fn read_le_f32_(&mut self) -> f32 {
505 unsafe {
506 cast::transmute::<u32, f32>(self.read_le_u32_())
507 }
508 }
509
510 fn read_u8_(&mut self) -> u8 {
511 match self.read_byte() {
512 Some(b) => b as u8,
513 None => 0
514 }
515 }
516
517 fn read_i8_(&mut self) -> i8 {
518 match self.read_byte() {
519 Some(b) => b as i8,
520 None => 0
521 }
522 }
523
524 }
525
526 impl<T: Writer> WriterByteConversions for T {
527 fn write_int_(&mut self, n: int) {
528 int::to_str_bytes(n, 10u, |bytes| self.write(bytes))
529 }
530
531 fn write_uint_(&mut self, n: uint) {
532 uint::to_str_bytes(n, 10u, |bytes| self.write(bytes))
533 }
534
535 fn write_le_uint_(&mut self, n: uint) {
536 u64_to_le_bytes(n as u64, uint::bytes, |v| self.write(v))
537 }
538
539 fn write_le_int_(&mut self, n: int) {
540 u64_to_le_bytes(n as u64, int::bytes, |v| self.write(v))
541 }
542
543 fn write_be_uint_(&mut self, n: uint) {
544 u64_to_be_bytes(n as u64, uint::bytes, |v| self.write(v))
545 }
546
547 fn write_be_int_(&mut self, n: int) {
548 u64_to_be_bytes(n as u64, int::bytes, |v| self.write(v))
549 }
550
551 fn write_be_u64_(&mut self, n: u64) {
552 u64_to_be_bytes(n, 8u, |v| self.write(v))
553 }
554
555 fn write_be_u32_(&mut self, n: u32) {
556 u64_to_be_bytes(n as u64, 4u, |v| self.write(v))
557 }
558
559 fn write_be_u16_(&mut self, n: u16) {
560 u64_to_be_bytes(n as u64, 2u, |v| self.write(v))
561 }
562
563 fn write_be_i64_(&mut self, n: i64) {
564 u64_to_be_bytes(n as u64, 8u, |v| self.write(v))
565 }
566
567 fn write_be_i32_(&mut self, n: i32) {
568 u64_to_be_bytes(n as u64, 4u, |v| self.write(v))
569 }
570
571 fn write_be_i16_(&mut self, n: i16) {
572 u64_to_be_bytes(n as u64, 2u, |v| self.write(v))
573 }
574
575 fn write_be_f64_(&mut self, f: f64) {
576 unsafe {
577 self.write_be_u64_(cast::transmute(f))
578 }
579 }
580
581 fn write_be_f32_(&mut self, f: f32) {
582 unsafe {
583 self.write_be_u32_(cast::transmute(f))
584 }
585 }
586
587 fn write_le_u64_(&mut self, n: u64) {
588 u64_to_le_bytes(n, 8u, |v| self.write(v))
589 }
590
591 fn write_le_u32_(&mut self, n: u32) {
592 u64_to_le_bytes(n as u64, 4u, |v| self.write(v))
593 }
594
595 fn write_le_u16_(&mut self, n: u16) {
596 u64_to_le_bytes(n as u64, 2u, |v| self.write(v))
597 }
598
599 fn write_le_i64_(&mut self, n: i64) {
600 u64_to_le_bytes(n as u64, 8u, |v| self.write(v))
601 }
602
603 fn write_le_i32_(&mut self, n: i32) {
604 u64_to_le_bytes(n as u64, 4u, |v| self.write(v))
605 }
606
607 fn write_le_i16_(&mut self, n: i16) {
608 u64_to_le_bytes(n as u64, 2u, |v| self.write(v))
609 }
610
611 fn write_le_f64_(&mut self, f: f64) {
612 unsafe {
613 self.write_le_u64_(cast::transmute(f))
614 }
615 }
616
617 fn write_le_f32_(&mut self, f: f32) {
618 unsafe {
619 self.write_le_u32_(cast::transmute(f))
620 }
621 }
622
623 fn write_u8_(&mut self, n: u8) {
624 self.write([n])
625 }
626
627 fn write_i8_(&mut self, n: i8) {
628 self.write([n as u8])
629 }
630 }
631
632 fn extend_sign(val: u64, nbytes: uint) -> i64 {
633 let shift = (8 - nbytes) * 8;
634 (val << shift) as i64 >> shift
635 }
636
637 #[cfg(test)]
638 mod test {
639 use super::ReaderUtil;
640 use option::{Some, None};
641 use cell::Cell;
642 use rt::io::mem::{MemReader, MemWriter};
643 use rt::io::mock::MockReader;
644 use rt::io::{read_error, placeholder_error};
645
646 #[test]
647 fn read_byte() {
648 let mut reader = MemReader::new(~[10]);
649 let byte = reader.read_byte();
650 assert!(byte == Some(10));
651 }
652
653 #[test]
654 fn read_byte_0_bytes() {
655 let mut reader = MockReader::new();
656 let count = Cell::new(0);
657 reader.read = |buf| {
658 do count.with_mut_ref |count| {
659 if *count == 0 {
660 *count = 1;
661 Some(0)
662 } else {
663 buf[0] = 10;
664 Some(1)
665 }
666 }
667 };
668 let byte = reader.read_byte();
669 assert!(byte == Some(10));
670 }
671
672 #[test]
673 fn read_byte_eof() {
674 let mut reader = MockReader::new();
675 reader.read = |_| None;
676 let byte = reader.read_byte();
677 assert!(byte == None);
678 }
679
680 #[test]
681 fn read_byte_error() {
682 let mut reader = MockReader::new();
683 reader.read = |_| {
684 read_error::cond.raise(placeholder_error());
685 None
686 };
687 do read_error::cond.trap(|_| {
688 }).inside {
689 let byte = reader.read_byte();
690 assert!(byte == None);
691 }
692 }
693
694 #[test]
695 fn bytes_0_bytes() {
696 let mut reader = MockReader::new();
697 let count = Cell::new(0);
698 reader.read = |buf| {
699 do count.with_mut_ref |count| {
700 if *count == 0 {
701 *count = 1;
702 Some(0)
703 } else {
704 buf[0] = 10;
705 Some(1)
706 }
707 }
708 };
709 let byte = reader.bytes().next();
710 assert!(byte == Some(10));
711 }
712
713 #[test]
714 fn bytes_eof() {
715 let mut reader = MockReader::new();
716 reader.read = |_| None;
717 let byte = reader.bytes().next();
718 assert!(byte == None);
719 }
720
721 #[test]
722 fn bytes_error() {
723 let mut reader = MockReader::new();
724 reader.read = |_| {
725 read_error::cond.raise(placeholder_error());
726 None
727 };
728 let mut it = reader.bytes();
729 do read_error::cond.trap(|_| ()).inside {
730 let byte = it.next();
731 assert!(byte == None);
732 }
733 }
734
735
736 #[test]
737 fn read_bytes() {
738 let mut reader = MemReader::new(~[10, 11, 12, 13]);
739 let bytes = reader.read_bytes(4);
740 assert!(bytes == ~[10, 11, 12, 13]);
741 }
742
743 #[test]
744 fn read_bytes_partial() {
745 let mut reader = MockReader::new();
746 let count = Cell::new(0);
747 reader.read = |buf| {
748 do count.with_mut_ref |count| {
749 if *count == 0 {
750 *count = 1;
751 buf[0] = 10;
752 buf[1] = 11;
753 Some(2)
754 } else {
755 buf[0] = 12;
756 buf[1] = 13;
757 Some(2)
758 }
759 }
760 };
761 let bytes = reader.read_bytes(4);
762 assert!(bytes == ~[10, 11, 12, 13]);
763 }
764
765 #[test]
766 fn read_bytes_eof() {
767 let mut reader = MemReader::new(~[10, 11]);
768 do read_error::cond.trap(|_| {
769 }).inside {
770 assert!(reader.read_bytes(4) == ~[10, 11]);
771 }
772 }
773
774 #[test]
775 fn push_bytes() {
776 let mut reader = MemReader::new(~[10, 11, 12, 13]);
777 let mut buf = ~[8, 9];
778 reader.push_bytes(&mut buf, 4);
779 assert!(buf == ~[8, 9, 10, 11, 12, 13]);
780 }
781
782 #[test]
783 fn push_bytes_partial() {
784 let mut reader = MockReader::new();
785 let count = Cell::new(0);
786 reader.read = |buf| {
787 do count.with_mut_ref |count| {
788 if *count == 0 {
789 *count = 1;
790 buf[0] = 10;
791 buf[1] = 11;
792 Some(2)
793 } else {
794 buf[0] = 12;
795 buf[1] = 13;
796 Some(2)
797 }
798 }
799 };
800 let mut buf = ~[8, 9];
801 reader.push_bytes(&mut buf, 4);
802 assert!(buf == ~[8, 9, 10, 11, 12, 13]);
803 }
804
805 #[test]
806 fn push_bytes_eof() {
807 let mut reader = MemReader::new(~[10, 11]);
808 let mut buf = ~[8, 9];
809 do read_error::cond.trap(|_| {
810 }).inside {
811 reader.push_bytes(&mut buf, 4);
812 assert!(buf == ~[8, 9, 10, 11]);
813 }
814 }
815
816 #[test]
817 fn push_bytes_error() {
818 let mut reader = MockReader::new();
819 let count = Cell::new(0);
820 reader.read = |buf| {
821 do count.with_mut_ref |count| {
822 if *count == 0 {
823 *count = 1;
824 buf[0] = 10;
825 Some(1)
826 } else {
827 read_error::cond.raise(placeholder_error());
828 None
829 }
830 }
831 };
832 let mut buf = ~[8, 9];
833 do read_error::cond.trap(|_| { } ).inside {
834 reader.push_bytes(&mut buf, 4);
835 }
836 assert!(buf == ~[8, 9, 10]);
837 }
838
839 #[test]
840 #[should_fail]
841 fn push_bytes_fail_reset_len() {
842 // push_bytes unsafely sets the vector length. This is testing that
843 // upon failure the length is reset correctly.
844 let mut reader = MockReader::new();
845 let count = Cell::new(0);
846 reader.read = |buf| {
847 do count.with_mut_ref |count| {
848 if *count == 0 {
849 *count = 1;
850 buf[0] = 10;
851 Some(1)
852 } else {
853 read_error::cond.raise(placeholder_error());
854 None
855 }
856 }
857 };
858 let buf = @mut ~[8, 9];
859 do (|| {
860 reader.push_bytes(&mut *buf, 4);
861 }).finally {
862 // NB: Using rtassert here to trigger abort on failure since this is a should_fail test
863 // FIXME: #7049 This fails because buf is still borrowed
864 //rtassert!(*buf == ~[8, 9, 10]);
865 }
866 }
867
868 #[test]
869 fn read_to_end() {
870 let mut reader = MockReader::new();
871 let count = Cell::new(0);
872 reader.read = |buf| {
873 do count.with_mut_ref |count| {
874 if *count == 0 {
875 *count = 1;
876 buf[0] = 10;
877 buf[1] = 11;
878 Some(2)
879 } else if *count == 1 {
880 *count = 2;
881 buf[0] = 12;
882 buf[1] = 13;
883 Some(2)
884 } else {
885 None
886 }
887 }
888 };
889 let buf = reader.read_to_end();
890 assert!(buf == ~[10, 11, 12, 13]);
891 }
892
893 #[test]
894 #[should_fail]
895 fn read_to_end_error() {
896 let mut reader = MockReader::new();
897 let count = Cell::new(0);
898 reader.read = |buf| {
899 do count.with_mut_ref |count| {
900 if *count == 0 {
901 *count = 1;
902 buf[0] = 10;
903 buf[1] = 11;
904 Some(2)
905 } else {
906 read_error::cond.raise(placeholder_error());
907 None
908 }
909 }
910 };
911 let buf = reader.read_to_end();
912 assert!(buf == ~[10, 11]);
913 }
914
915 #[test]
916 fn test_read_write_le_mem() {
917 let uints = [0, 1, 2, 42, 10_123, 100_123_456, ::u64::max_value];
918
919 let mut writer = MemWriter::new();
920 for i in uints.iter() {
921 writer.write_le_u64_(*i);
922 }
923
924 let mut reader = MemReader::new(writer.inner());
925 for i in uints.iter() {
926 assert!(reader.read_le_u64_() == *i);
927 }
928 }
929
930
931 #[test]
932 fn test_read_write_be() {
933 let uints = [0, 1, 2, 42, 10_123, 100_123_456, ::u64::max_value];
934
935 let mut writer = MemWriter::new();
936 for i in uints.iter() {
937 writer.write_be_u64_(*i);
938 }
939
940 let mut reader = MemReader::new(writer.inner());
941 for i in uints.iter() {
942 assert!(reader.read_be_u64_() == *i);
943 }
944 }
945
946 #[test]
947 fn test_read_be_int_n() {
948 let ints = [::i32::min_value, -123456, -42, -5, 0, 1, ::i32::max_value];
949
950 let mut writer = MemWriter::new();
951 for i in ints.iter() {
952 writer.write_be_i32_(*i);
953 }
954
955 let mut reader = MemReader::new(writer.inner());
956 for i in ints.iter() {
957 // this tests that the sign extension is working
958 // (comparing the values as i32 would not test this)
959 assert!(reader.read_be_int_n_(4) == *i as i64);
960 }
961 }
962
963 #[test]
964 fn test_read_f32() {
965 //big-endian floating-point 8.1250
966 let buf = ~[0x41, 0x02, 0x00, 0x00];
967
968 let mut writer = MemWriter::new();
969 writer.write(buf);
970
971 let mut reader = MemReader::new(writer.inner());
972 let f = reader.read_be_f32_();
973 assert!(f == 8.1250);
974 }
975
976 #[test]
977 fn test_read_write_f32() {
978 let f:f32 = 8.1250;
979
980 let mut writer = MemWriter::new();
981 writer.write_be_f32_(f);
982 writer.write_le_f32_(f);
983
984 let mut reader = MemReader::new(writer.inner());
985 assert!(reader.read_be_f32_() == 8.1250);
986 assert!(reader.read_le_f32_() == 8.1250);
987 }
988
989 }