Skip to content

Commit

Permalink
Fix for format_tools for overflow subtract (#1490)
Browse files Browse the repository at this point in the history
* Refert previous fix

* Add test

* Add test

* Add fix

* Revert wrong fix

* Add correct test

* Add fix
  • Loading branch information
InAnYan authored Nov 14, 2024
1 parent c055473 commit 9370621
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
15 changes: 3 additions & 12 deletions module/core/format_tools/src/format/output_format/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,10 @@ impl TableOutputFormat for Table

write!( c.buf, "{}", cell_prefix )?;

println!( "icol : {icol} | irow : {irow} | width : {width} | cell_width : {cell_width} | slice.len() : {}", slice.len() );
// println!( "icol : {icol} | irow : {irow} | width : {width} | cell_width : {cell_width} | slice.len() : {}", slice.len() );

let lspaces = if cell_width > width {
0
} else {
( width - cell_width ) / 2
};

let rspaces = if (cell_width > width) || (slice.len() > cell_width) {
0
} else {
( width - cell_width + 1 ) / 2 + cell_width - slice.len()
};
let lspaces = ( width - cell_width ) / 2;
let rspaces = ( width - cell_width + 1 ) / 2 + cell_width - slice.chars().count();

// println!( "icol : {icol} | irow : {irow} | width : {width} | cell_width : {cell_width} | lspaces : {lspaces} | rspaces : {rspaces}" );

Expand Down
2 changes: 2 additions & 0 deletions module/core/format_tools/src/format/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ pub mod own
Context,
Printer,
InputExtract,
RowDescriptor,
ColDescriptor,
};

}
Expand Down
22 changes: 22 additions & 0 deletions module/core/format_tools/tests/inc/format_table_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use the_module::
filter,
print,
output_format,
print::{ InputExtract, RowDescriptor, ColDescriptor },
TableOutputFormat,
filter::LineType
};

use std::
Expand Down Expand Up @@ -326,3 +329,22 @@ fn filter_row_callback()
//

// xxx : implement test for vector of vectors

//

#[ test ]
fn no_subtract_with_overflow()
{
let test_objects = test_object::test_objects_gen_with_unicode();

let format = output_format::Table::default();
let printer = print::Printer::with_format( &format );

let as_table = AsTable::new( &test_objects );
let mut output = String::new();
let mut context = print::Context::new( &mut output, printer );

let result = the_module::TableFormatter::fmt( &as_table, &mut context );

assert!( result.is_ok() );
}
14 changes: 14 additions & 0 deletions module/core/format_tools/tests/inc/test_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,17 @@ pub fn test_objects_gen() -> Vec< TestObject >
]

}

pub fn test_objects_gen_with_unicode() -> Vec< TestObject >
{
vec!
[
TestObject
{
id : "Юнікод".to_string(),
created_at : 100,
file_ids : vec![],
tools : None,
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use the_module::
WithDebug,
WithDisplay,
// the_module::to_string_with_fallback::Ref,
to_string_with_fallback,
to_string_with_fallback
};

use std::
{
// fmt,
fmt,
// collections::HashMap,
borrow::Cow,
};
Expand Down

0 comments on commit 9370621

Please sign in to comment.