Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pretty print nums to make output more human readable #69

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions crates/unigen/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,55 @@ pub fn generate_universe(parsed_size: u32) -> Vec<core::Block> {
let total_baryons = generated_universe_length * default_baryons;
let total_quarks = generated_universe_length * default_baryons * quarks_per_baryon;

println!("Atoms: {}", &total_atoms);
println!("Baryons: {}", &total_baryons);
println!("Quarks: {}", &total_quarks);
println!("Atoms: {}", pretty_print_nums(&total_atoms));
println!("Baryons: {}", pretty_print_nums(&total_baryons));
println!("Quarks: {}", pretty_print_nums(&total_quarks));
println!("{}", &separator.magenta().bold());

let total_objects = &total_atoms + &total_baryons + &total_quarks;
println!("Total objects in memory: {}", &total_objects);
println!(
"Total high-level objects in memory: {}",
pretty_print_nums(&total_objects)
);
println!("{}", &separator.green().bold());

generated_universe
}

fn pretty_print_nums(num: &usize) -> String {
num.to_string()
.as_bytes()
.rchunks(3)
.rev()
.map(std::str::from_utf8)
.collect::<Result<Vec<&str>, _>>()
.unwrap()
.join(",")
}

#[test]
fn it_can_pretty_print_large_numbers() {
let nums: Vec<(usize, String)> = vec![
(4, String::from("4")),
(40, String::from("40")),
(400, String::from("400")),
(4_000, String::from("4,000")),
(40_000, String::from("40,000")),
(400_000, String::from("400,000")),
(4_000_000, String::from("4,000,000")),
(40_000_000, String::from("40,000,000")),
(400_000_000, String::from("400,000,000")),
(4_000_000_000, String::from("4,000,000,000")),
(40_000_000_000, String::from("40,000,000,000")),
(400_000_000_000, String::from("400,000,000,000")),
(4_000_000_000_000, String::from("4,000,000,000,000")),
];

for num in nums {
assert_eq!(pretty_print_nums(&num.0), num.1)
}
}

#[test]
fn it_can_begin() {
let universe = Blocks::initialize_universe(5);
Expand Down