| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use std::borrow::ToOwned; |
| 8 |
use types::{RawDecoder, RawEncoder}; |
| 9 |
|
| 10 |
pub struct TestResult<'a, Output: 'a + ?Sized + ToOwned> { |
| 11 |
pub expected_return: (usize, Option<isize>), |
| 12 |
pub expected_push: &'a Output, |
| 13 |
pub actual_return: (usize, Option<isize>), |
| 14 |
pub actual_push: Output::Owned, |
| 15 |
} |
| 16 |
|
| 17 |
pub trait Testable { |
| 18 |
type Input: ?Sized; |
| 19 |
type Output: ?Sized + ToOwned; |
| 20 |
|
| 21 |
fn process_feed_ok<'a>(&mut self, processed: &Self::Input, unprocessed: &Self::Input, |
| 22 |
output: &'a Self::Output) -> TestResult<'a, Self::Output>; |
| 23 |
fn process_feed_err<'a>(&mut self, backup: isize, |
| 24 |
processed: &Self::Input, problem: &Self::Input, remaining: &Self::Input, |
| 25 |
output: &'a Self::Output) -> TestResult<'a, Self::Output>; |
| 26 |
fn process_finish_ok<'a>(&mut self, output: &'a Self::Output) -> TestResult<'a, Self::Output>; |
| 27 |
fn process_finish_err<'a>(&mut self, backup: isize, |
| 28 |
output: &'a Self::Output) -> TestResult<'a, Self::Output>; |
| 29 |
} |
| 30 |
|
| 31 |
impl Testable for RawDecoder { |
| 32 |
type Input = [u8]; |
| 33 |
type Output = str; |
| 34 |
|
| 35 |
fn process_feed_ok<'a>(&mut self, processed: &[u8], unprocessed: &[u8], |
| 36 |
output: &'a str) -> TestResult<'a, str> { |
| 37 |
let mut input = Vec::with_capacity(processed.len() + unprocessed.len()); |
| 38 |
input.extend(processed.iter().cloned()); |
| 39 |
input.extend(unprocessed.iter().cloned()); |
| 40 |
|
| 41 |
let mut buf = String::new(); |
| 42 |
let (nprocessed, err) = self.raw_feed(&input, &mut buf); |
| 43 |
TestResult { |
| 44 |
expected_return: (processed.len(), None), |
| 45 |
expected_push: output, |
| 46 |
actual_return: (nprocessed, err.map(|e| e.upto)), |
| 47 |
actual_push: buf, |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
fn process_feed_err<'a>(&mut self, backup: isize, processed: &[u8], problem: &[u8], |
| 52 |
remaining: &[u8], output: &'a str) -> TestResult<'a, str> { |
| 53 |
let mut input = Vec::with_capacity(processed.len() + problem.len() + remaining.len()); |
| 54 |
input.extend(processed.iter().cloned()); |
| 55 |
input.extend(problem.iter().cloned()); |
| 56 |
input.extend(remaining.iter().cloned()); |
| 57 |
|
| 58 |
let mut buf = String::new(); |
| 59 |
let (nprocessed, err) = self.raw_feed(&input[-backup as usize..], &mut buf); |
| 60 |
TestResult { |
| 61 |
expected_return: (processed.len(), Some(processed.len() as isize + |
| 62 |
problem.len() as isize + backup)), |
| 63 |
expected_push: output, |
| 64 |
actual_return: (nprocessed, err.map(|e| e.upto)), |
| 65 |
actual_push: buf, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
fn process_finish_ok<'a>(&mut self, output: &'a str) -> TestResult<'a, str> { |
| 70 |
let mut buf = String::new(); |
| 71 |
let err = self.raw_finish(&mut buf); |
| 72 |
TestResult { |
| 73 |
expected_return: (0, None), |
| 74 |
expected_push: output, |
| 75 |
actual_return: (0, err.map(|e| e.upto)), |
| 76 |
actual_push: buf, |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
fn process_finish_err<'a>(&mut self, backup: isize, output: &'a str) -> TestResult<'a, str> { |
| 81 |
let mut buf = String::new(); |
| 82 |
let err = self.raw_finish(&mut buf); |
| 83 |
TestResult { |
| 84 |
expected_return: (0, Some(backup)), |
| 85 |
expected_push: output, |
| 86 |
actual_return: (0, err.map(|e| e.upto)), |
| 87 |
actual_push: buf, |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
impl Testable for RawEncoder { |
| 93 |
type Input = str; |
| 94 |
type Output = [u8]; |
| 95 |
|
| 96 |
fn process_feed_ok<'a>(&mut self, processed: &str, unprocessed: &str, |
| 97 |
output: &'a [u8]) -> TestResult<'a, [u8]> { |
| 98 |
let mut input = String::with_capacity(processed.len() + unprocessed.len()); |
| 99 |
input.push_str(processed); |
| 100 |
input.push_str(unprocessed); |
| 101 |
|
| 102 |
let mut buf = Vec::new(); |
| 103 |
let (nprocessed, err) = self.raw_feed(&input, &mut buf); |
| 104 |
TestResult { |
| 105 |
expected_return: (processed.len(), None), |
| 106 |
expected_push: output, |
| 107 |
actual_return: (nprocessed, err.map(|e| e.upto)), |
| 108 |
actual_push: buf, |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
fn process_feed_err<'a>(&mut self, backup: isize, processed: &str, problem: &str, |
| 113 |
remaining: &str, output: &'a [u8]) -> TestResult<'a, [u8]> { |
| 114 |
let mut input = String::with_capacity(processed.len() + problem.len() + remaining.len()); |
| 115 |
input.push_str(processed); |
| 116 |
input.push_str(problem); |
| 117 |
input.push_str(remaining); |
| 118 |
|
| 119 |
let mut buf = Vec::new(); |
| 120 |
let (nprocessed, err) = self.raw_feed(&input[-backup as usize..], &mut buf); |
| 121 |
TestResult { |
| 122 |
expected_return: (processed.len(), Some(processed.len() as isize + |
| 123 |
problem.len() as isize + backup)), |
| 124 |
expected_push: output, |
| 125 |
actual_return: (nprocessed, err.map(|e| e.upto)), |
| 126 |
actual_push: buf, |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
fn process_finish_ok<'a>(&mut self, output: &'a [u8]) -> TestResult<'a, [u8]> { |
| 131 |
let mut buf = Vec::new(); |
| 132 |
let err = self.raw_finish(&mut buf); |
| 133 |
TestResult { |
| 134 |
expected_return: (0, None), |
| 135 |
expected_push: output, |
| 136 |
actual_return: (0, err.map(|e| e.upto)), |
| 137 |
actual_push: buf, |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
fn process_finish_err<'a>(&mut self, backup: isize, output: &'a [u8]) -> TestResult<'a, [u8]> { |
| 142 |
let mut buf = Vec::new(); |
| 143 |
let err = self.raw_finish(&mut buf); |
| 144 |
TestResult { |
| 145 |
expected_return: (0, Some(backup)), |
| 146 |
expected_push: output, |
| 147 |
actual_return: (0, err.map(|e| e.upto)), |
| 148 |
actual_push: buf, |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
macro_rules! assert_expected { |
| 154 |
($result:expr, $func:expr, $filter:expr) => ({ |
| 155 |
use testutils::Testable; |
| 156 |
match $result { |
| 157 |
result => { |
| 158 |
assert!(result.expected_return == result.actual_return, |
| 159 |
"{} should return {:?}, but instead returned {:?}", |
| 160 |
$func, $filter(result.expected_return), $filter(result.actual_return)); |
| 161 |
assert!(&result.expected_push[..] == &result.actual_push[..], |
| 162 |
"{} should push {:?}, but instead pushed {:?}", |
| 163 |
$func, result.expected_push, result.actual_push); |
| 164 |
} |
| 165 |
} |
| 166 |
}); |
| 167 |
} |
| 168 |
|
| 169 |
macro_rules! assert_feed_ok { |
| 170 |
($this:expr, $processed:expr, $unprocessed:expr, $output:expr) => ( |
| 171 |
assert_expected!($this.process_feed_ok(&$processed, &$unprocessed, &$output), |
| 172 |
"raw_feed", |r| r) |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
macro_rules! assert_feed_err { |
| 177 |
($this:expr, $backup:expr, $processed:expr, $problem:expr, $remaining:expr, $output:expr) => ( |
| 178 |
assert_expected!($this.process_feed_err($backup, &$processed, &$problem, &$remaining, |
| 179 |
&$output), |
| 180 |
"raw_feed", |r| r) |
| 181 |
); |
| 182 |
($this:expr, $processed:expr, $problem:expr, $remaining:expr, $output:expr) => ( |
| 183 |
assert_feed_err!($this, 0, $processed, $problem, $remaining, $output) |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
macro_rules! assert_finish_ok { |
| 188 |
($this:expr, $output:expr) => ( |
| 189 |
assert_expected!($this.process_finish_ok(&$output), |
| 190 |
"raw_finish", |r: (usize, Option<isize>)| r.0) |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
macro_rules! assert_finish_err { |
| 195 |
($this:expr, $backup:expr, $output:expr) => ( |
| 196 |
assert_expected!($this.process_finish_err($backup, &$output), |
| 197 |
"raw_finish", |r: (usize, Option<isize>)| r.0) |
| 198 |
); |
| 199 |
($this:expr, $output:expr) => ( |
| 200 |
assert_finish_err!($this, 0, $output) |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
pub static ASCII_TEXT: &'static str = |
| 209 |
"English is a West Germanic language that was first spoken in early medieval England \ |
| 210 |
and is now a global lingua franca. It is spoken as a first language by \ |
| 211 |
the majority populations of several sovereign states, including the United Kingdom, \ |
| 212 |
the United States, Canada, Australia, Ireland, New Zealand and a number of Caribbean nations; \ |
| 213 |
and it is an official language of almost 60 sovereign states. It is the third-most-common \ |
| 214 |
native language in the world, after Mandarin Chinese and Spanish. It is widely learned as \ |
| 215 |
a second language and is an official language of the European Union, many Commonwealth \ |
| 216 |
countries and the United Nations, as well as in many world organisations."; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
pub static KOREAN_TEXT: &'static str = |
| 223 |
"한국어(韓國語)는 주로 한반도(韓半島)와 한민족(韓民族) 거주 지역에서 쓰이는 언어로, \ |
| 224 |
대한민국에서는 한국어, 한국말이라고 부르고, 조선민주주의인민공화국과 중국, 일본에서는 \ |
| 225 |
조선어(朝鮮語), 조선말이라고 불린다. 우즈베키스탄, 러시아 등 구 소련의 고려인들 사이에서는 \ |
| 226 |
고려말(高麗語)로 불린다. 19세기 중반 이후 한반도와 주변 정세의 혼란, 20세기 전반 \ |
| 227 |
일본 제국주의의 침략, 20세기 후반 대한민국의 해외 이민에 의해 중국 동북 지방, 일본, \ |
| 228 |
러시아 연해주와 사할린, 우즈베키스탄, 미국, 캐나다, 오스트레일리아, 필리핀, 베트남, 브라질 등 \ |
| 229 |
세계 곳곳에 한민족이 이주하면서 한국어가 쓰이고 있다. 한국어 쓰는 인구는 전 세계를 통틀어 \ |
| 230 |
약 8천250만 명으로 추산된다."; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
pub static JAPANESE_TEXT: &'static str = |
| 237 |
"日本語(にほんご、にっぽんご)とは、主に日本国内や日本人同士の間で使われている言語である。\ |
| 238 |
日本は法令によって「公用語」を規定していないが、法令その他の公用文は日本語で記述され、\ |
| 239 |
各種法令(裁判所法第74条、会社計算規則第57条、特許法施行規則第2条など)において\ |
| 240 |
日本語を用いることが定められるなど事実上の公用語となっており、学校教育の「国語」でも\ |
| 241 |
教えられる。使用人口について正確な統計はないが、日本国内の人口、および日本国外に住む\ |
| 242 |
日本人や日系人、日本がかつて統治した地域の一部の住民など、約1億3千万人以上と考えられる。\ |
| 243 |
統計によって前後する可能性はあるが、この数は世界の母語話者数で上位10位以内に入る人数である。"; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
pub static SIMPLIFIED_CHINESE_TEXT: &'static str = |
| 250 |
"汉语,又称中文、华语(东南亚)、国语(中华民国国语)、中国语(日本、韩国等),\ |
| 251 |
其他名称有汉文(通常指文言文)、华文、唐文、唐话、中国话等,是属汉藏语系的分析语,具有声调。\ |
| 252 |
汉语的文字系统——汉字是一种意音文字,表意的同时也具一定的表音功能。\ |
| 253 |
汉语包含书面语以及口语两部分,古代书面汉语称为文言文,现代书面汉语一般指使用现代标准汉语语法,\ |
| 254 |
词汇的中文通行文体。目前全球有六分之一人口使用汉语作为母语。现代汉语书面语高度统一,\ |
| 255 |
口语则有官话、粤语、吴语、湘语、赣语、客家语、闽语等七种主要汉语言\ |
| 256 |
(也有人认为晋语和(或)徽语和(或)平话(广西平话)也应为独立汉语言,\ |
| 257 |
也有其他人认为闽语其实是一个语族,下辖闽南语、闽东语、闽中语以及莆仙语,\ |
| 258 |
国际标准化组织即持此观点,部分资料将其中的一至六种也算成单独的汉语言,\ |
| 259 |
这就是八至十三种汉语言的由来)。"; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
pub static TRADITIONAL_CHINESE_TEXT: &'static str = |
| 266 |
"漢語,又稱中文、華語(東南亞)、國語(中華民國國語)、中國語(日本、韓國等),\ |
| 267 |
其他名稱有漢文(通常指文言文)、華文、唐文、唐話、中國話等,是屬漢藏語系的分析語,具有聲調。\ |
| 268 |
漢語的文字系統——漢字是一種意音文字,表意的同時也具一定的表音功能。\ |
| 269 |
漢語包含書面語以及口語兩部分,古代書面漢語稱為文言文,現代書面漢語一般指使用現代標準漢語語法,\ |
| 270 |
詞彙的中文通行文體。目前全球有六分之一人口使用漢語作為母語。現代漢語書面語高度統一,\ |
| 271 |
口語則有官話、粵語、吳語、湘語、贛語、客家語、閩語等七種主要漢語言\ |
| 272 |
(也有人認為晉語和(或)徽語和(或)平話(廣西平話)也應為獨立漢語言,\ |
| 273 |
也有其他人認為閩語其實是一個語族,下轄閩南語、閩東語、閩中語以及莆仙語,\ |
| 274 |
國際標準化組織即持此觀點,部分資料將其中的一至六種也算成單獨的漢語言,\ |
| 275 |
這就是八至十三種漢語言的由來)。"; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
pub static INVALID_UTF8_TEXT: &'static [u8] = include_bytes!("examples/UTF-8-test.txt"); |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
pub fn get_external_bench_data() -> Vec<u8> { |
| 287 |
use std::env; |
| 288 |
use std::io::Read; |
| 289 |
use std::fs::File; |
| 290 |
use std::path::Path; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
static LONGER_TEXT: &'static [u8] = include_bytes!("examples/outer-space-treaty.html"); |
| 295 |
|
| 296 |
match env::var("EXTERNAL_BENCH_DATA") { |
| 297 |
Ok(path) => { |
| 298 |
let path = Path::new(&path); |
| 299 |
let mut file = File::open(&path).ok().expect("cannot read an external bench data"); |
| 300 |
let mut ret = Vec::new(); |
| 301 |
file.read_to_end(&mut ret).ok().expect("cannot read an external bench data"); |
| 302 |
ret |
| 303 |
} |
| 304 |
Err(..) => { |
| 305 |
LONGER_TEXT.to_vec() |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|