Skip to content

Commit

Permalink
more wrapping arithmetic in ascii.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 24, 2020
1 parent e53ff4a commit 9e92a43
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ macro_rules! latin1_alu {
let src_until_alignment = (ALU_ALIGNMENT
- ((src as usize) & ALU_ALIGNMENT_MASK))
& ALU_ALIGNMENT_MASK;
if (dst.add(src_until_alignment) as usize) & ALU_ALIGNMENT_MASK != 0 {
if (dst.wrapping_add(src_until_alignment) as usize) & ALU_ALIGNMENT_MASK != 0 {
break;
}
src_until_alignment
Expand All @@ -300,7 +300,7 @@ macro_rules! latin1_alu {
let dst_until_alignment = (ALU_ALIGNMENT
- ((dst as usize) & ALU_ALIGNMENT_MASK))
& ALU_ALIGNMENT_MASK;
if (src.add(dst_until_alignment) as usize) & ALU_ALIGNMENT_MASK != 0 {
if (src.wrapping_add(dst_until_alignment) as usize) & ALU_ALIGNMENT_MASK != 0 {
break;
}
dst_until_alignment
Expand Down
29 changes: 17 additions & 12 deletions src/big5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,20 @@ mod tests {
// ASCII
encode_big5("\u{0061}\u{0062}", b"\x61\x62");

// Edge cases
encode_big5("\u{9EA6}\u{0061}", b"麦\x61");
encode_big5("\u{2626B}\u{0061}", b"𦉫\x61");
encode_big5("\u{3000}", b"\xA1\x40");
encode_big5("\u{20AC}", b"\xA3\xE1");
encode_big5("\u{4E00}", b"\xA4\x40");
encode_big5("\u{27607}", b"\xC8\xA4");
encode_big5("\u{FFE2}", b"\xC8\xCD");
encode_big5("\u{79D4}", b"\xFE\xFE");

// Not in index
encode_big5("\u{2603}\u{0061}", b"☃\x61");
if !cfg!(miri) { // Miri is too slow
// Edge cases
encode_big5("\u{9EA6}\u{0061}", b"麦\x61");
encode_big5("\u{2626B}\u{0061}", b"𦉫\x61");
encode_big5("\u{3000}", b"\xA1\x40");
encode_big5("\u{20AC}", b"\xA3\xE1");
encode_big5("\u{4E00}", b"\xA4\x40");
encode_big5("\u{27607}", b"\xC8\xA4");
encode_big5("\u{FFE2}", b"\xC8\xCD");
encode_big5("\u{79D4}", b"\xFE\xFE");

// Not in index
encode_big5("\u{2603}\u{0061}", b"☃\x61");
}

// duplicate low bits
encode_big5("\u{203B5}", b"\xFD\x6A");
Expand All @@ -387,6 +389,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_big5_decode_all() {
let input = include_bytes!("test_data/big5_in.txt");
let expectation = include_str!("test_data/big5_in_ref.txt");
Expand All @@ -396,6 +399,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_big5_encode_all() {
let input = include_str!("test_data/big5_out.txt");
let expectation = include_bytes!("test_data/big5_out_ref.txt");
Expand All @@ -406,6 +410,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_big5_encode_from_two_low_surrogates() {
let expectation = b"��";
let mut output = [0u8; 40];
Expand Down
3 changes: 3 additions & 0 deletions src/euc_jp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_jis0208_decode_all() {
let input = include_bytes!("test_data/jis0208_in.txt");
let expectation = include_str!("test_data/jis0208_in_ref.txt");
Expand All @@ -446,6 +447,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_jis0208_encode_all() {
let input = include_str!("test_data/jis0208_out.txt");
let expectation = include_bytes!("test_data/jis0208_out_ref.txt");
Expand All @@ -456,6 +458,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_jis0212_decode_all() {
let input = include_bytes!("test_data/jis0212_in.txt");
let expectation = include_str!("test_data/jis0212_in_ref.txt");
Expand Down
2 changes: 2 additions & 0 deletions src/euc_kr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_euc_kr_decode_all() {
let input = include_bytes!("test_data/euc_kr_in.txt");
let expectation = include_str!("test_data/euc_kr_in_ref.txt");
Expand All @@ -415,6 +416,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_euc_kr_encode_all() {
let input = include_str!("test_data/euc_kr_out.txt");
let expectation = include_bytes!("test_data/euc_kr_out_ref.txt");
Expand Down
2 changes: 2 additions & 0 deletions src/gb18030.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_gb18030_decode_all() {
let input = include_bytes!("test_data/gb18030_in.txt");
let expectation = include_str!("test_data/gb18030_in_ref.txt");
Expand All @@ -721,6 +722,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_gb18030_encode_all() {
let input = include_str!("test_data/gb18030_out.txt");
let expectation = include_bytes!("test_data/gb18030_out_ref.txt");
Expand Down
2 changes: 2 additions & 0 deletions src/iso_2022_jp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_iso_2022_jp_decode_all() {
let input = include_bytes!("test_data/iso_2022_jp_in.txt");
let expectation = include_str!("test_data/iso_2022_jp_in_ref.txt");
Expand All @@ -976,6 +977,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_iso_2022_jp_encode_all() {
let input = include_str!("test_data/iso_2022_jp_out.txt");
let expectation = include_bytes!("test_data/iso_2022_jp_out_ref.txt");
Expand Down
25 changes: 17 additions & 8 deletions src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2231,8 +2231,9 @@ mod tests {

#[test]
fn test_is_utf16_latin1_fail() {
let mut src: Vec<u16> = Vec::with_capacity(256);
src.resize(256, 0);
let len = if cfg!(miri) { 64 } else { 256 }; // Miri is too slow
let mut src: Vec<u16> = Vec::with_capacity(len);
src.resize(len, 0);
for i in 0..src.len() {
src[i] = i as u16;
}
Expand Down Expand Up @@ -2262,8 +2263,9 @@ mod tests {

#[test]
fn test_is_str_latin1_fail() {
let mut src: Vec<u16> = Vec::with_capacity(256);
src.resize(256, 0);
let len = if cfg!(miri) { 32 } else { 256 }; // Miri is too slow
let mut src: Vec<u16> = Vec::with_capacity(len);
src.resize(len, 0);
for i in 0..src.len() {
src[i] = i as u16;
}
Expand All @@ -2280,8 +2282,9 @@ mod tests {

#[test]
fn test_is_utf8_latin1_success() {
let mut src: Vec<u16> = Vec::with_capacity(256);
src.resize(256, 0);
let len = if cfg!(miri) { 64 } else { 256 }; // Miri is too slow
let mut src: Vec<u16> = Vec::with_capacity(len);
src.resize(len, 0);
for i in 0..src.len() {
src[i] = i as u16;
}
Expand All @@ -2297,8 +2300,9 @@ mod tests {

#[test]
fn test_is_utf8_latin1_fail() {
let mut src: Vec<u16> = Vec::with_capacity(256);
src.resize(256, 0);
let len = if cfg!(miri) { 32 } else { 256 }; // Miri is too slow
let mut src: Vec<u16> = Vec::with_capacity(len);
src.resize(len, 0);
for i in 0..src.len() {
src[i] = i as u16;
}
Expand Down Expand Up @@ -3144,6 +3148,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_is_char_bidi_thoroughly() {
for i in 0..0xD800u32 {
let c: char = ::std::char::from_u32(i).unwrap();
Expand All @@ -3156,6 +3161,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_is_utf16_code_unit_bidi_thoroughly() {
for i in 0..0x10000u32 {
let u = i as u16;
Expand All @@ -3167,6 +3173,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_is_str_bidi_thoroughly() {
let mut buf = [0; 4];
for i in 0..0xD800u32 {
Expand All @@ -3186,6 +3193,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_is_utf8_bidi_thoroughly() {
let mut buf = [0; 8];
for i in 0..0xD800u32 {
Expand Down Expand Up @@ -3227,6 +3235,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_is_utf16_bidi_thoroughly() {
let mut buf = [0; 32];
for i in 0..0x10000u32 {
Expand Down
2 changes: 2 additions & 0 deletions src/shift_jis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_shift_jis_decode_all() {
let input = include_bytes!("test_data/shift_jis_in.txt");
let expectation = include_str!("test_data/shift_jis_in_ref.txt");
Expand All @@ -394,6 +395,7 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // Miri is too slow
fn test_shift_jis_encode_all() {
let input = include_str!("test_data/shift_jis_out.txt");
let expectation = include_bytes!("test_data/shift_jis_out_ref.txt");
Expand Down
8 changes: 6 additions & 2 deletions src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use super::*;
pub fn decode(encoding: &'static Encoding, bytes: &[u8], expect: &str) {
let mut vec = Vec::with_capacity(bytes.len() + 32);
let mut string = String::with_capacity(expect.len() + 32);
for i in 0usize..32usize {
let range = if cfg!(miri) { 0usize..4usize } else { 0usize..32usize };
for i in range {
eprintln!("GREPME decode: loop {}", i);
vec.clear();
string.clear();
for j in 0usize..i {
Expand Down Expand Up @@ -44,7 +46,9 @@ fn decode_without_padding_impl(
pub fn encode(encoding: &'static Encoding, str: &str, expect: &[u8]) {
let mut vec = Vec::with_capacity(expect.len() + 32);
let mut string = String::with_capacity(str.len() + 32);
for i in 0usize..32usize {
let range = if cfg!(miri) { 0usize..4usize } else { 0usize..32usize };
for i in range {
eprintln!("GREPME encode: loop {}", i);
vec.clear();
string.clear();
for j in 0usize..i {
Expand Down

0 comments on commit 9e92a43

Please sign in to comment.