1 // Take a look at the license at the top of the repository in the LICENSE file. 2 3 #[cfg(test)] 4 mod tests { 5 use crate::prelude::*; 6 use crate::MemoryInputStream; 7 use futures_util::io::{AsyncBufReadExt, AsyncReadExt}; 8 use glib::Bytes; 9 use std::error::Error; 10 11 #[test] new()12 fn new() { 13 let strm = MemoryInputStream::new(); 14 let ret = strm.skip(1, crate::NONE_CANCELLABLE); 15 assert!(!ret.is_err()); 16 assert_eq!(ret.unwrap(), 0); 17 18 let mut buf = vec![0; 10]; 19 let ret = strm.read(&mut buf, crate::NONE_CANCELLABLE).unwrap(); 20 assert_eq!(ret, 0); 21 } 22 23 #[test] from_bytes()24 fn from_bytes() { 25 let b = Bytes::from_owned(vec![1, 2, 3]); 26 let strm = MemoryInputStream::from_bytes(&b); 27 let mut buf = vec![0; 10]; 28 let ret = strm.read(&mut buf, crate::NONE_CANCELLABLE).unwrap(); 29 assert_eq!(ret, 3); 30 assert_eq!(buf[0], 1); 31 assert_eq!(buf[1], 2); 32 assert_eq!(buf[2], 3); 33 34 let ret = strm.skip(10, crate::NONE_CANCELLABLE).unwrap(); 35 assert_eq!(ret, 0); 36 } 37 38 #[test] add_bytes()39 fn add_bytes() { 40 let strm = MemoryInputStream::new(); 41 let b = Bytes::from_owned(vec![1, 2, 3]); 42 strm.add_bytes(&b); 43 let mut buf = vec![0; 10]; 44 let ret = strm.read(&mut buf, crate::NONE_CANCELLABLE).unwrap(); 45 assert_eq!(ret, 3); 46 assert_eq!(buf[0], 1); 47 assert_eq!(buf[1], 2); 48 assert_eq!(buf[2], 3); 49 50 let ret = strm.skip(10, crate::NONE_CANCELLABLE).unwrap(); 51 assert_eq!(ret, 0); 52 } 53 54 #[test] read_async_future()55 fn read_async_future() { 56 use futures_util::future::TryFutureExt; 57 58 let c = glib::MainContext::new(); 59 60 let buf = vec![0; 10]; 61 let b = glib::Bytes::from_owned(vec![1, 2, 3]); 62 let strm = MemoryInputStream::from_bytes(&b); 63 64 let res = c 65 .block_on( 66 strm.read_async_future(buf, glib::PRIORITY_DEFAULT) 67 .map_err(|(_buf, err)| err) 68 .map_ok(move |(mut buf, len)| { 69 buf.truncate(len); 70 buf 71 }), 72 ) 73 .unwrap(); 74 75 assert_eq!(res, vec![1, 2, 3]); 76 } 77 78 #[test] async_read()79 fn async_read() { 80 async fn run() -> Result<(), Box<dyn Error>> { 81 let b = Bytes::from_owned(vec![1, 2, 3]); 82 83 // Adapter is big enough to read everything in one read 84 let mut read = MemoryInputStream::from_bytes(&b).into_async_buf_read(8); 85 let mut buf = [0u8; 4]; 86 assert_eq!(read.read(&mut buf).await?, 3); 87 assert_eq!(buf, [1, 2, 3, 0]); 88 assert_eq!(read.read(&mut buf).await?, 0); 89 90 let mut read = MemoryInputStream::from_bytes(&b).into_async_buf_read(8); 91 let mut buf = [0u8; 1]; 92 assert_eq!(read.read(&mut buf).await?, 1); 93 assert_eq!(buf, [1]); 94 assert_eq!(read.read(&mut buf).await?, 1); 95 assert_eq!(buf, [2]); 96 assert_eq!(read.read(&mut buf).await?, 1); 97 assert_eq!(buf, [3]); 98 assert_eq!(read.read(&mut buf).await?, 0); 99 100 // Adapter is NOT big enough to read everything in one read 101 let mut read = MemoryInputStream::from_bytes(&b).into_async_buf_read(2); 102 let mut buf = [0u8; 4]; 103 assert_eq!(read.read(&mut buf).await?, 2); 104 assert_eq!(buf, [1, 2, 0, 0]); 105 assert_eq!(read.read(&mut buf).await?, 1); 106 assert_eq!(buf[0], 3); 107 assert_eq!(read.read(&mut buf).await?, 0); 108 109 let mut read = MemoryInputStream::from_bytes(&b).into_async_buf_read(2); 110 let mut buf = [0u8; 1]; 111 assert_eq!(read.read(&mut buf).await?, 1); 112 assert_eq!(buf, [1]); 113 assert_eq!(read.read(&mut buf).await?, 1); 114 assert_eq!(buf, [2]); 115 assert_eq!(read.read(&mut buf).await?, 1); 116 assert_eq!(buf, [3]); 117 assert_eq!(read.read(&mut buf).await?, 0); 118 119 Ok(()) 120 } 121 122 let main_context = glib::MainContext::new(); 123 main_context.block_on(run()).unwrap(); 124 } 125 126 #[test] async_buf_read()127 fn async_buf_read() { 128 async fn run() -> Result<(), Box<dyn Error>> { 129 let b = Bytes::from_owned(vec![1, 2, 3]); 130 // Adapter is big enough to read everything in one read 131 let mut read = MemoryInputStream::from_bytes(&b).into_async_buf_read(16); 132 let mut buf = String::new(); 133 assert_eq!(read.read_line(&mut buf).await?, 3); 134 assert_eq!(buf.as_bytes(), [1, 2, 3]); 135 assert_eq!(read.read_line(&mut buf).await?, 0); 136 137 // Adapter is NOT big enough to read everything in one read 138 let mut read = MemoryInputStream::from_bytes(&b).into_async_buf_read(2); 139 let mut buf = String::new(); 140 assert_eq!(read.read_line(&mut buf).await?, 3); 141 assert_eq!(buf.as_bytes(), [1, 2, 3]); 142 assert_eq!(read.read_line(&mut buf).await?, 0); 143 144 Ok(()) 145 } 146 147 let main_context = glib::MainContext::new(); 148 main_context.block_on(run()).unwrap(); 149 } 150 } 151