Skip to content

Commit

Permalink
✅ 添加测试,修改检查
Browse files Browse the repository at this point in the history
  • Loading branch information
phidiaLam committed Nov 19, 2024
1 parent f4c89cf commit c096ab0
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion libs/bitmap-convert/src/base70.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const BASE_70: &[u8] =
b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-~._()!*";
// 字符转下标
static CHAR_TO_INDEX: LazyLock<[u8; 127]> = LazyLock::new(|| {
let mut char_to_index: [u8; 127] = [0; 127];
// u8::MAX字符为ÿ,我们不会用到
let mut char_to_index: [u8; 127] = [u8::MAX; 127];
for (i, c) in BASE_70.iter().enumerate() {
char_to_index[*c as usize] = i as u8;
}
Expand Down Expand Up @@ -53,6 +54,9 @@ impl BitmapBase70Conv for Bitmap<256> {
let index = CHAR_TO_INDEX
.get(c as usize)
.ok_or(Error::NotConvertBitmap(string.clone()))?;
if *index == u8::MAX {
return Err(Error::NotConvertBitmap(string.clone()));
}
bytes.push(*index);
}
let value = U256::from_radix_le(&bytes, RADIX)
Expand Down Expand Up @@ -110,4 +114,18 @@ mod tests {

assert_eq!(bitmap, decoded_bitmap);
}

#[test]
fn test_invalid_char() {
let result: Result<Bitmap<256>, _> = BitmapBase70Conv::from_base_70("ugAU#@r!()Mi".to_owned());

assert!(result.is_err());
}

#[test]
fn test_invalid_str() {
let decoded_bitmap: Result<Bitmap<256>, _> =
BitmapBase70Conv::from_base_70("ugAUr!()MiugAUr!()MiugAUr!()MiugAUr!()MiugAUr!()MiugAUr!()MiugAUr!()MiugAUr!()MiugAUr!()Mi".to_owned());
assert!(decoded_bitmap.is_err());
}
}

0 comments on commit c096ab0

Please sign in to comment.