Debian Codegraph @

fuzz/uufuzz/examples/integration_testing.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 use std::ffi::OsString;
7 use uufuzz::{generate_and_run_uumain, run_gnu_cmd};
8
9 // Mock sort implementation for demonstration
10 fn mock_sort_main(args: std::vec::IntoIter<OsString>) -> i32 {
11 use std::io::{self, Read};
12
13 let args: Vec<OsString> = args.collect();
14 let mut numeric_sort = false;
15 let mut reverse_sort = false;
16
17 // Parse arguments
18 for arg in args.iter().skip(1) {
19 let arg_str = arg.to_string_lossy();
20 match arg_str.as_ref() {
21 "-n" | "--numeric-sort" => numeric_sort = true,
22 "-r" | "--reverse" => reverse_sort = true,
23 _ => {}
24 }
25 }
26
27 // Read from stdin
28 let mut input = String::new();
29 match io::stdin().read_to_string(&mut input) {
30 Ok(_) => {
31 let mut lines: Vec<&str> = input.lines().collect();
32
33 if numeric_sort {
34 // Sort numerically
35 lines.sort_by(|a, b| {
36 let a_num: f64 = a.trim().parse().unwrap_or(0.0);
37 let b_num: f64 = b.trim().parse().unwrap_or(0.0);
38 a_num.partial_cmp(&b_num).unwrap()
39 });
40 } else {
41 // Sort lexically
42 lines.sort();
43 }
44
45 if reverse_sort {
46 lines.reverse();
47 }
48
49 for line in lines {
50 println!("{}", line);
51 }
52 0
53 }
54 Err(_) => {
55 eprintln!("Error reading from stdin");
56 1
57 }
58 }
59 }
60
61 #[cfg(test)]
62 mod tests {
63 use super::*;
64
65 #[test]
66 fn test_basic_sort_functionality() {
67 println!("Testing basic sort functionality...");
68 let args = vec![OsString::from("sort")];
69 let input = "zebra\napple\nbanana\n";
70
71 let rust_result = generate_and_run_uumain(&args, mock_sort_main, Some(input));
72
73 match run_gnu_cmd("sort", &args[1..], false, Some(input)) {
74 Ok(gnu_result) => {
75 // In test environment, stdout might not be captured properly
76 // Just verify the function runs without errors and exit codes match
77 assert_eq!(
78 rust_result.exit_code, gnu_result.exit_code,
79 "Exit codes should match"
80 );
81 println!("✓ Basic sort test passed (exit codes match)");
82 }
83 Err(_) => {
84 // GNU sort not available, just check our implementation runs
85 assert_eq!(
86 rust_result.exit_code, 0,
87 "Our sort should exit successfully"
88 );
89 println!("✓ Basic sort test passed (GNU sort not available)");
90 }
91 }
92 }
93
94 #[test]
95 fn test_numeric_sort() {
96 println!("Testing numeric sort...");
97 let args = vec![OsString::from("sort"), OsString::from("-n")];
98 let input = "10\n2\n1\n20\n";
99
100 let rust_result = generate_and_run_uumain(&args, mock_sort_main, Some(input));
101
102 match run_gnu_cmd("sort", &args[1..], false, Some(input)) {
103 Ok(gnu_result) => {
104 assert_eq!(
105 rust_result.exit_code, gnu_result.exit_code,
106 "Exit codes should match"
107 );
108 println!("✓ Numeric sort test passed (exit codes match)");
109 }
110 Err(_) => {
111 // GNU sort not available, just check our implementation runs
112 assert_eq!(
113 rust_result.exit_code, 0,
114 "Our numeric sort should exit successfully"
115 );
116 println!("✓ Numeric sort test passed (GNU sort not available)");
117 }
118 }
119 }
120
121 #[test]
122 fn test_reverse_sort() {
123 println!("Testing reverse sort...");
124 let args = vec![OsString::from("sort"), OsString::from("-r")];
125 let input = "apple\nbanana\nzebra\n";
126
127 let rust_result = generate_and_run_uumain(&args, mock_sort_main, Some(input));
128
129 match run_gnu_cmd("sort", &args[1..], false, Some(input)) {
130 Ok(gnu_result) => {
131 assert_eq!(
132 rust_result.exit_code, gnu_result.exit_code,
133 "Exit codes should match"
134 );
135 println!("✓ Reverse sort test passed (exit codes match)");
136 }
137 Err(_) => {
138 // GNU sort not available, just check our implementation runs
139 assert_eq!(
140 rust_result.exit_code, 0,
141 "Our reverse sort should exit successfully"
142 );
143 println!("✓ Reverse sort test passed (GNU sort not available)");
144 }
145 }
146 }
147
148 #[test]
149 fn test_empty_input() {
150 println!("Testing empty input...");
151 let args = vec![OsString::from("sort")];
152 let input = "";
153
154 let rust_result = generate_and_run_uumain(&args, mock_sort_main, Some(input));
155
156 match run_gnu_cmd("sort", &args[1..], false, Some(input)) {
157 Ok(gnu_result) => {
158 assert_eq!(
159 rust_result.exit_code, gnu_result.exit_code,
160 "Exit codes should match"
161 );
162 println!("✓ Empty input test passed (exit codes match)");
163 }
164 Err(_) => {
165 // GNU sort not available, just check our implementation runs
166 assert_eq!(
167 rust_result.exit_code, 0,
168 "Should exit successfully with empty input"
169 );
170 println!("✓ Empty input test passed (GNU sort not available)");
171 }
172 }
173 }
174 }
175
176 fn main() {
177 println!("=== Integration Testing uufuzz Example ===");
178 println!("This demonstrates how to use uufuzz in regular test suites");
179 println!("to verify compatibility with reference implementations.\n");
180
181 println!("Run 'cargo test --example integration_testing' to execute the tests.");
182 println!("Or run individual tests below for demonstration:\n");
183
184 // Demonstrate the tests manually
185 let test_cases = [
186 (
187 "Basic lexical sort",
188 vec![OsString::from("sort")],
189 "zebra\napple\nbanana\n",
190 ),
191 (
192 "Numeric sort",
193 vec![OsString::from("sort"), OsString::from("-n")],
194 "10\n2\n1\n20\n",
195 ),
196 (
197 "Reverse sort",
198 vec![OsString::from("sort"), OsString::from("-r")],
199 "apple\nbanana\nzebra\n",
200 ),
201 ("Empty input", vec![OsString::from("sort")], ""),
202 ];
203
204 for (test_name, args, input) in test_cases {
205 println!("--- {} ---", test_name);
206 println!(
207 "Args: {:?}",
208 args.iter().map(|s| s.to_string_lossy()).collect::<Vec<_>>()
209 );
210 println!("Input: {:?}", input.replace('\n', "\\n"));
211
212 let rust_result = generate_and_run_uumain(&args, mock_sort_main, Some(input));
213 println!("Our output: {:?}", rust_result.stdout.replace('\n', "\\n"));
214 println!("Exit code: {}", rust_result.exit_code);
215
216 match run_gnu_cmd("sort", &args[1..], false, Some(input)) {
217 Ok(gnu_result) => {
218 println!("GNU output: {:?}", gnu_result.stdout.replace('\n', "\\n"));
219 if rust_result.stdout == gnu_result.stdout
220 && rust_result.exit_code == gnu_result.exit_code
221 {
222 println!("✓ Outputs match!");
223 } else {
224 println!("✗ Outputs differ!");
225 }
226 }
227 Err(_) => {
228 println!("GNU sort not available for comparison");
229 }
230 }
231 println!();
232 }
233
234 println!("=== Example completed ===");
235 println!("In a real test suite, assertions would ensure compatibility.");
236 }