Skip to content

Commit

Permalink
test: more tests for big integers and strings
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Oct 11, 2024
1 parent 6aa8c53 commit c2d8612
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,30 @@ mod tests {
assert_eq!(bencode_to_json_unchecked(b"i-1e"), "-1".to_string());
}

#[test]
fn positive_integer_greater_than_i64_max() {
let big_positive_integer = i64::MAX.to_string() + "1";

let bencoded_big_positive_integer = format!("i{big_positive_integer}e");

assert_eq!(
bencode_to_json_unchecked(bencoded_big_positive_integer.as_bytes()),
big_positive_integer
);
}

#[test]
fn negative_integer_smaller_than_i64_min() {
let big_negative_integer = i64::MIN.to_string() + "1";

let bencoded_big_negative_integer = format!("i{big_negative_integer}e");

assert_eq!(
bencode_to_json_unchecked(bencoded_big_negative_integer.as_bytes()),
big_negative_integer
);
}

#[test]
fn it_should_fail_when_it_finds_an_invalid_byte() {
let int_with_invalid_byte = b"iae";
Expand All @@ -363,7 +387,10 @@ mod tests {
}

mod strings {
use crate::test::bencode_to_json_unchecked;
use crate::{
test::{bencode_to_json_unchecked, bencoded_string_with_repeated_byte},
to_bencode,
};

#[test]
fn length_can_contain_leading_zeros() {
Expand Down Expand Up @@ -391,6 +418,25 @@ mod tests {
);
}

#[test]
fn big_utf8_string() {
let big_string = "a".repeat(1_000_000);

assert_eq!(
bencode_to_json_unchecked(&to_bencode(&big_string)),
format!(r#""{big_string}""#)
);
}

#[test]
fn big_non_utf8_string() {
let big_non_utf8_string = bencoded_string_with_repeated_byte(b'\xFF', 1_000_000);

let expected = format!(r#""<hex>{}</hex>""#, "ff".repeat(1_000_000));

assert_eq!(bencode_to_json_unchecked(&big_non_utf8_string), expected);
}

#[test]
fn ending_with_bencode_end_char() {
assert_eq!(bencode_to_json_unchecked(b"1:e"), r#""e""#.to_string());
Expand Down
26 changes: 26 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,29 @@ pub(crate) fn generate_n_nested_empty_json_objects(n: usize) -> String {

object
}

#[cfg(test)]
/// Generates a bencoded string with a repeated byte.
///
/// This function creates a bencoded string where the string value consists of a
/// repeated byte.
///
/// # Arguments
///
/// * `byte` - The byte to repeat in the string value.
/// * `n` - The number of times to repeat the byte.
///
/// # Returns
///
/// A `Vec<u8>` containing the bencoded string.
pub(crate) fn bencoded_string_with_repeated_byte(byte: u8, n: usize) -> Vec<u8> {
let string_length = b"1000000".to_vec();
let string_value = vec![byte; n];

let mut bencoded_string = Vec::new();
bencoded_string.extend_from_slice(&string_length);
bencoded_string.push(b':'); // Length/value separator
bencoded_string.extend_from_slice(&string_value);

bencoded_string
}

0 comments on commit c2d8612

Please sign in to comment.