Skip to content

Commit 380be77

Browse files
authored
pretty print nums to make output more human readable (#69)
1 parent dbab2bb commit 380be77

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

crates/unigen/src/builder.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,55 @@ pub fn generate_universe(parsed_size: u32) -> Vec<core::Block> {
190190
let total_baryons = generated_universe_length * default_baryons;
191191
let total_quarks = generated_universe_length * default_baryons * quarks_per_baryon;
192192

193-
println!("Atoms: {}", &total_atoms);
194-
println!("Baryons: {}", &total_baryons);
195-
println!("Quarks: {}", &total_quarks);
193+
println!("Atoms: {}", pretty_print_nums(&total_atoms));
194+
println!("Baryons: {}", pretty_print_nums(&total_baryons));
195+
println!("Quarks: {}", pretty_print_nums(&total_quarks));
196196
println!("{}", &separator.magenta().bold());
197197

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

202205
generated_universe
203206
}
204207

208+
fn pretty_print_nums(num: &usize) -> String {
209+
num.to_string()
210+
.as_bytes()
211+
.rchunks(3)
212+
.rev()
213+
.map(std::str::from_utf8)
214+
.collect::<Result<Vec<&str>, _>>()
215+
.unwrap()
216+
.join(",")
217+
}
218+
219+
#[test]
220+
fn it_can_pretty_print_large_numbers() {
221+
let nums: Vec<(usize, String)> = vec![
222+
(4, String::from("4")),
223+
(40, String::from("40")),
224+
(400, String::from("400")),
225+
(4_000, String::from("4,000")),
226+
(40_000, String::from("40,000")),
227+
(400_000, String::from("400,000")),
228+
(4_000_000, String::from("4,000,000")),
229+
(40_000_000, String::from("40,000,000")),
230+
(400_000_000, String::from("400,000,000")),
231+
(4_000_000_000, String::from("4,000,000,000")),
232+
(40_000_000_000, String::from("40,000,000,000")),
233+
(400_000_000_000, String::from("400,000,000,000")),
234+
(4_000_000_000_000, String::from("4,000,000,000,000")),
235+
];
236+
237+
for num in nums {
238+
assert_eq!(pretty_print_nums(&num.0), num.1)
239+
}
240+
}
241+
205242
#[test]
206243
fn it_can_begin() {
207244
let universe = Blocks::initialize_universe(5);

0 commit comments

Comments
 (0)