-
Notifications
You must be signed in to change notification settings - Fork 32
asa: rewrite and add tests #426
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // | ||
| // Copyright (c) 2024 Hemi Labs, Inc. | ||
| // | ||
| // This file is part of the posixutils-rs project covered under | ||
| // the MIT License. For the full license text, please see the LICENSE | ||
| // file in the root directory of this project. | ||
| // SPDX-License-Identifier: MIT | ||
| // | ||
|
|
||
| use plib::testing::{run_test, TestPlan}; | ||
|
|
||
| fn asa_test(test_data: &str, expected_output: &str) { | ||
| run_test(TestPlan { | ||
| cmd: String::from("asa"), | ||
| args: vec![], | ||
| stdin_data: String::from(test_data), | ||
| expected_out: String::from(expected_output), | ||
| expected_err: String::from(""), | ||
| expected_exit_code: 0, | ||
| }); | ||
| } | ||
|
|
||
| // Test empty input | ||
| #[test] | ||
| fn asa_empty() { | ||
| asa_test("", ""); | ||
| } | ||
|
|
||
| // Test basic space control character (normal single-spacing) | ||
| #[test] | ||
| fn asa_space_single_line() { | ||
| asa_test(" hello\n", "hello\n"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn asa_space_multiple_lines() { | ||
| asa_test(" line1\n line2\n line3\n", "line1\nline2\nline3\n"); | ||
| } | ||
|
|
||
| // Test '0' control character (double-spacing - blank line before) | ||
| #[test] | ||
| fn asa_zero_first_line() { | ||
| // '0' as first line: outputs blank line, then content | ||
| asa_test("0hello\n", "\nhello\n"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn asa_zero_second_line() { | ||
| // '0' on second line: previous line, blank line, then content | ||
| asa_test(" line1\n0line2\n", "line1\n\nline2\n"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn asa_zero_multiple() { | ||
| asa_test("0first\n0second\n", "\nfirst\n\nsecond\n"); | ||
| } | ||
|
|
||
| // Test '1' control character (form-feed/new page) | ||
| #[test] | ||
| fn asa_one_first_line() { | ||
| // '1' as first line: form-feed, then content | ||
| asa_test("1page1\n", "\x0cpage1\n"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn asa_one_second_line() { | ||
| // '1' on second line: previous line ends, form-feed, then content | ||
| asa_test(" line1\n1page2\n", "line1\n\x0cpage2\n"); | ||
| } | ||
|
|
||
| // Test '+' control character (overprint - carriage return) | ||
| #[test] | ||
| fn asa_plus_overprint() { | ||
| // '+' causes overprint: carriage return instead of newline | ||
| asa_test(" line1\n+over\n", "line1\rover\n"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn asa_plus_multiple_overprint() { | ||
| // Multiple overprints on same logical line | ||
| asa_test(" base\n+mid\n+top\n", "base\rmid\rtop\n"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn asa_plus_first_line() { | ||
| // POSIX: '+' as first character in input is equivalent to space | ||
| asa_test("+first\n", "first\n"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn asa_plus_first_line_then_normal() { | ||
| // '+' as first, then normal lines | ||
| asa_test("+first\n line2\n", "first\nline2\n"); | ||
| } | ||
|
|
||
| // Test '-' control character (triple-spacing - non-POSIX extension) | ||
| #[test] | ||
| fn asa_dash_first_line() { | ||
| asa_test("-content\n", "\n\ncontent\n"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn asa_dash_second_line() { | ||
| asa_test(" line1\n-line2\n", "line1\n\n\nline2\n"); | ||
| } | ||
|
|
||
| // Test other/unknown control characters (treated as space) | ||
| #[test] | ||
| fn asa_other_char() { | ||
| // Unknown control chars treated as space (normal single-spacing) | ||
| asa_test("Xhello\n", "hello\n"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn asa_digit_as_control() { | ||
| // '2' is not a special control, treated as space | ||
| asa_test("2hello\n", "hello\n"); | ||
| } | ||
|
|
||
| // Test empty content (control char only) | ||
| #[test] | ||
| fn asa_space_empty_content() { | ||
| asa_test(" \n", "\n"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn asa_zero_empty_content() { | ||
| asa_test("0\n", "\n\n"); | ||
| } | ||
|
|
||
| // Test mixed control characters | ||
| #[test] | ||
| fn asa_mixed_controls() { | ||
| asa_test( | ||
| " line1\n0double\n1newpage\n+over\n line2\n", | ||
| "line1\n\ndouble\n\x0cnewpage\rover\nline2\n", | ||
| ); | ||
| } | ||
|
|
||
| // Test content without trailing newline (EOF without newline) | ||
| #[test] | ||
| fn asa_no_trailing_newline() { | ||
| asa_test(" hello", "hello\n"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn asa_plus_no_trailing_newline() { | ||
| asa_test(" line1\n+over", "line1\rover\n"); | ||
| } | ||
|
|
||
| // Test lines with only control character (no content, no newline) | ||
| #[test] | ||
| fn asa_control_only_no_newline() { | ||
| asa_test(" ", "\n"); | ||
| } | ||
|
|
||
| // Test complex FORTRAN-style output simulation | ||
| #[test] | ||
| fn asa_fortran_style_report() { | ||
| // Simulate a simple FORTRAN report with page header and data | ||
| let input = "1REPORT TITLE\n \n DATA LINE 1\n DATA LINE 2\n0SECTION 2\n DATA LINE 3\n"; | ||
| let expected = "\x0cREPORT TITLE\n\nDATA LINE 1\nDATA LINE 2\n\nSECTION 2\nDATA LINE 3\n"; | ||
| asa_test(input, expected); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| // SPDX-License-Identifier: MIT | ||
| // | ||
|
|
||
| mod asa; | ||
| mod comm; | ||
| mod csplit; | ||
| mod cut; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The line extraction logic has two issues with multi-byte UTF-8 characters:
1and comparisonline_end > 1, assuming the first character is always single-byte ASCIIraw_line[1..line_end]will panic with a "byte index is not a char boundary" errorWhile the ASA format typically uses ASCII control characters, the code should handle this gracefully. The fix should:
1withch.len_utf8()in the sliceline_end > 1withline_end > ch.len_utf8()Example:
let line = if line_end > ch.len_utf8() { &raw_line[ch.len_utf8()..line_end] } else { "" };