Debian Codegraph @

tests/by-util/test_expr.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 // spell-checker:ignore Ξ±bcdef ; (people) kkos
7 // spell-checker:ignore aabcccd aabcd aabd abbb abbbd abbcabc abbcac abbcbbbd abbcbd
8 // spell-checker:ignore abbccd abcabc abcac acabc andand bigcmp bignum emptysub
9 // spell-checker:ignore orempty oror bcdef fedcb GHSA xjfw pqfw
10
11 use uutests::new_ucmd;
12
13 #[test]
14 fn test_no_arguments() {
15 new_ucmd!()
16 .fails_with_code(2)
17 .usage_error("missing operand");
18 }
19
20 #[test]
21 fn test_simple_values() {
22 // null or 0 => EXIT_VALUE == 1
23 new_ucmd!().args(&[""]).fails_with_code(1).stdout_only("\n");
24 new_ucmd!()
25 .args(&["0"])
26 .fails_with_code(1)
27 .stdout_only("0\n");
28 new_ucmd!()
29 .args(&["00"])
30 .fails_with_code(1)
31 .stdout_only("00\n");
32 new_ucmd!()
33 .args(&["-0"])
34 .fails_with_code(1)
35 .stdout_only("-0\n");
36
37 // non-null and non-0 => EXIT_VALUE = 0
38 new_ucmd!().args(&["1"]).succeeds().stdout_only("1\n");
39 }
40
41 #[test]
42 fn test_simple_arithmetic() {
43 new_ucmd!()
44 .args(&["1", "+", "1"])
45 .succeeds()
46 .stdout_only("2\n");
47
48 new_ucmd!()
49 .args(&["1", "-", "1"])
50 .fails_with_code(1)
51 .stdout_only("0\n");
52
53 new_ucmd!()
54 .args(&["3", "*", "2"])
55 .succeeds()
56 .stdout_only("6\n");
57
58 new_ucmd!()
59 .args(&["4", "/", "2"])
60 .succeeds()
61 .stdout_only("2\n");
62
63 new_ucmd!()
64 .args(&["4", "=", "2"])
65 .fails_with_code(1)
66 .stdout_only("0\n");
67
68 new_ucmd!()
69 .args(&["4", "=", "4"])
70 .succeeds()
71 .stdout_only("1\n");
72 }
73
74 #[test]
75 fn test_complex_arithmetic() {
76 new_ucmd!()
77 .args(&["9223372036854775807", "+", "9223372036854775807"])
78 .succeeds()
79 .stdout_only("18446744073709551614\n");
80
81 new_ucmd!()
82 .args(&[
83 "92233720368547758076549841651981984981498415651",
84 "%",
85 "922337203685",
86 ])
87 .succeeds()
88 .stdout_only("533691697086\n");
89
90 new_ucmd!()
91 .args(&[
92 "92233720368547758076549841651981984981498415651",
93 "*",
94 "922337203685",
95 ])
96 .succeeds()
97 .stdout_only("85070591730190566808700855121818604965830915152801178873935\n");
98
99 new_ucmd!()
100 .args(&[
101 "92233720368547758076549841651981984981498415651",
102 "-",
103 "922337203685",
104 ])
105 .succeeds()
106 .stdout_only("92233720368547758076549841651981984059161211966\n");
107
108 new_ucmd!()
109 .args(&["9", "/", "0"])
110 .fails()
111 .stderr_only("expr: division by zero\n");
112 }
113
114 #[test]
115 fn test_parenthesis() {
116 new_ucmd!()
117 .args(&["(", "1", "+", "1", ")", "*", "2"])
118 .succeeds()
119 .stdout_only("4\n");
120
121 new_ucmd!()
122 .args(&["1", "(", ")"])
123 .fails_with_code(2)
124 .stderr_only("expr: syntax error: unexpected argument '('\n");
125 }
126
127 #[test]
128 fn test_or() {
129 new_ucmd!()
130 .args(&["0", "|", "foo"])
131 .succeeds()
132 .stdout_only("foo\n");
133
134 new_ucmd!()
135 .args(&["foo", "|", "bar"])
136 .succeeds()
137 .stdout_only("foo\n");
138
139 new_ucmd!()
140 .args(&["14", "|", "1"])
141 .succeeds()
142 .stdout_only("14\n");
143
144 new_ucmd!()
145 .args(&["-14", "|", "1"])
146 .succeeds()
147 .stdout_only("-14\n");
148
149 new_ucmd!()
150 .args(&["1", "|", "a", "/", "5"])
151 .succeeds()
152 .stdout_only("1\n");
153
154 new_ucmd!()
155 .args(&["foo", "|", "a", "/", "5"])
156 .succeeds()
157 .stdout_only("foo\n");
158
159 new_ucmd!()
160 .args(&["0", "|", "10", "/", "5"])
161 .succeeds()
162 .stdout_only("2\n");
163
164 new_ucmd!()
165 .args(&["12", "|", "9a", "+", "1"])
166 .succeeds()
167 .stdout_only("12\n");
168
169 new_ucmd!().args(&["", "|", ""]).fails().stdout_only("0\n");
170
171 new_ucmd!().args(&["", "|", "0"]).fails().stdout_only("0\n");
172
173 new_ucmd!()
174 .args(&["", "|", "00"])
175 .fails()
176 .stdout_only("0\n");
177
178 new_ucmd!()
179 .args(&["", "|", "-0"])
180 .fails()
181 .stdout_only("0\n");
182 }
183
184 #[test]
185 fn test_and() {
186 new_ucmd!()
187 .args(&["foo", "&", "1"])
188 .succeeds()
189 .stdout_only("foo\n");
190
191 new_ucmd!()
192 .args(&["14", "&", "1"])
193 .succeeds()
194 .stdout_only("14\n");
195
196 new_ucmd!()
197 .args(&["-14", "&", "1"])
198 .succeeds()
199 .stdout_only("-14\n");
200
201 new_ucmd!()
202 .args(&["-1", "&", "10", "/", "5"])
203 .succeeds()
204 .stdout_only("-1\n");
205
206 new_ucmd!()
207 .args(&["0", "&", "a", "/", "5"])
208 .fails()
209 .stdout_only("0\n");
210
211 new_ucmd!()
212 .args(&["", "&", "a", "/", "5"])
213 .fails()
214 .stdout_only("0\n");
215
216 new_ucmd!().args(&["", "&", "1"]).fails().stdout_only("0\n");
217
218 new_ucmd!().args(&["", "&", ""]).fails().stdout_only("0\n");
219 }
220
221 #[test]
222 fn test_parenthesized_short_circuit_dead_branches() {
223 new_ucmd!()
224 .args(&["1", "|", "(", "1", "/", "0", ")"])
225 .succeeds()
226 .stdout_only("1\n");
227
228 new_ucmd!()
229 .args(&["0", "&", "(", "1", "/", "0", ")"])
230 .fails_with_code(1)
231 .stdout_only("0\n");
232
233 new_ucmd!()
234 .args(&["1", "|", "(", "0", "&", "(", "1", "/", "0", ")", ")"])
235 .succeeds()
236 .stdout_only("1\n");
237
238 new_ucmd!()
239 .args(&["0", "&", "(", "1", "|", "(", "1", "/", "0", ")", ")"])
240 .fails_with_code(1)
241 .stdout_only("0\n");
242 }
243
244 #[test]
245 fn test_length_fail() {
246 new_ucmd!().args(&["length", "Ξ±bcdef", "1"]).fails();
247 }
248
249 #[test]
250 fn test_length() {
251 new_ucmd!()
252 .args(&["length", "abcdef"])
253 .succeeds()
254 .stdout_only("6\n");
255
256 new_ucmd!()
257 .args(&["abcdef", "length"])
258 .fails_with_code(2)
259 .stderr_only("expr: syntax error: unexpected argument 'length'\n");
260 }
261
262 #[test]
263 fn test_regex_empty() {
264 new_ucmd!().args(&["", ":", ""]).fails().stdout_only("0\n");
265 new_ucmd!()
266 .args(&["abc", ":", ""])
267 .fails()
268 .stdout_only("0\n");
269 }
270
271 #[test]
272 fn test_regex_trailing_backslash() {
273 new_ucmd!()
274 .args(&["\\", ":", "\\\\"])
275 .succeeds()
276 .stdout_only("1\n");
277 new_ucmd!()
278 .args(&["\\", ":", "\\"])
279 .fails()
280 .stderr_only("expr: Trailing backslash\n");
281 new_ucmd!()
282 .args(&["abc\\", ":", "abc\\\\"])
283 .succeeds()
284 .stdout_only("4\n");
285 new_ucmd!()
286 .args(&["abc\\", ":", "abc\\"])
287 .fails()
288 .stderr_only("expr: Trailing backslash\n");
289 }
290
291 #[test]
292 fn test_regex_caret() {
293 new_ucmd!()
294 .args(&["a^b", ":", "a^b"])
295 .succeeds()
296 .stdout_only("3\n");
297 new_ucmd!()
298 .args(&["a^b", ":", "a\\^b"])
299 .succeeds()
300 .stdout_only("3\n");
301 new_ucmd!()
302 .args(&["abc", ":", "^abc"])
303 .succeeds()
304 .stdout_only("3\n");
305 new_ucmd!()
306 .args(&["^abc", ":", "^^abc"])
307 .succeeds()
308 .stdout_only("4\n");
309 new_ucmd!()
310 .args(&["b", ":", "a\\|^b"])
311 .succeeds()
312 .stdout_only("1\n");
313 new_ucmd!()
314 .args(&["ab", ":", "\\(^a\\)b"])
315 .succeeds()
316 .stdout_only("a\n");
317 new_ucmd!()
318 .args(&["^abc", ":", "^abc"])
319 .fails()
320 .stdout_only("0\n");
321 new_ucmd!()
322 .args(&["^^^^^^^^^", ":", "^^^"])
323 .succeeds()
324 .stdout_only("2\n");
325 new_ucmd!()
326 .args(&["ab[^c]", ":", "ab[^c]"])
327 .succeeds()
328 .stdout_only("3\n"); // Matches "ab["
329 new_ucmd!()
330 .args(&["ab[^c]", ":", "ab\\[^c]"])
331 .succeeds()
332 .stdout_only("6\n");
333 // Use `[^x]` to avoid Windows `wild` glob matching the runner's `C:\a` directory
334 new_ucmd!()
335 .args(&["[^x]", ":", "\\[^x]"])
336 .succeeds()
337 .stdout_only("4\n");
338 new_ucmd!()
339 .args(&["\\a", ":", "\\\\[^^]"])
340 .succeeds()
341 .stdout_only("2\n");
342 // Patterns are anchored to the beginning of the pattern "^bc"
343 new_ucmd!()
344 .args(&["abc", ":", "bc"])
345 .fails()
346 .stdout_only("0\n");
347 new_ucmd!()
348 .args(&["^a", ":", "^^[^^]"])
349 .succeeds()
350 .stdout_only("2\n");
351 new_ucmd!()
352 .args(&["abc", ":", "ab[^c]"])
353 .fails()
354 .stdout_only("0\n");
355 }
356
357 #[test]
358 fn test_regex_dollar() {
359 new_ucmd!()
360 .args(&["a$b", ":", "a\\$b"])
361 .succeeds()
362 .stdout_only("3\n");
363 new_ucmd!()
364 .args(&["a", ":", "a$\\|b"])
365 .succeeds()
366 .stdout_only("1\n");
367 new_ucmd!()
368 .args(&["ab", ":", "a\\(b$\\)"])
369 .succeeds()
370 .stdout_only("b\n");
371 new_ucmd!()
372 .args(&["a$c", ":", "a$\\c"])
373 .succeeds()
374 .stdout_only("3\n");
375 new_ucmd!()
376 .args(&["$a", ":", "$a"])
377 .succeeds()
378 .stdout_only("2\n");
379 new_ucmd!()
380 .args(&["a", ":", "a$\\|b"])
381 .succeeds()
382 .stdout_only("1\n");
383 new_ucmd!()
384 .args(&["-5", ":", "-\\{0,1\\}[0-9]*$"])
385 .succeeds()
386 .stdout_only("2\n");
387 new_ucmd!()
388 .args(&["$", ":", "$"])
389 .fails()
390 .stdout_only("0\n");
391 new_ucmd!()
392 .args(&["a$", ":", "a$\\|b"])
393 .fails()
394 .stdout_only("0\n");
395 }
396
397 #[test]
398 fn test_regex_range_quantifier() {
399 new_ucmd!()
400 .args(&["a", ":", "a\\{1\\}"])
401 .succeeds()
402 .stdout_only("1\n");
403 new_ucmd!()
404 .args(&["aaaaaaaaaa", ":", "a\\{1,\\}"])
405 .succeeds()
406 .stdout_only("10\n");
407 new_ucmd!()
408 .args(&["aaa", ":", "a\\{,3\\}"])
409 .succeeds()
410 .stdout_only("3\n");
411 new_ucmd!()
412 .args(&["aa", ":", "a\\{1,3\\}"])
413 .succeeds()
414 .stdout_only("2\n");
415 new_ucmd!()
416 .args(&["aaaa", ":", "a\\{,\\}"])
417 .succeeds()
418 .stdout_only("4\n");
419 new_ucmd!()
420 .args(&["a", ":", "ab\\{,3\\}"])
421 .succeeds()
422 .stdout_only("1\n");
423 new_ucmd!()
424 .args(&["abbb", ":", "ab\\{,3\\}"])
425 .succeeds()
426 .stdout_only("4\n");
427 new_ucmd!()
428 .args(&["abcabc", ":", "\\(abc\\)\\{,\\}"])
429 .succeeds()
430 .stdout_only("abc\n");
431 new_ucmd!()
432 .args(&["a", ":", "a\\{,6\\}"])
433 .succeeds()
434 .stdout_only("1\n");
435 new_ucmd!()
436 .args(&["{abc}", ":", "\\{abc\\}"])
437 .succeeds()
438 .stdout_only("5\n");
439 new_ucmd!()
440 .args(&["a{bc}", ":", "a\\(\\{bc\\}\\)"])
441 .succeeds()
442 .stdout_only("{bc}\n");
443 new_ucmd!()
444 .args(&["{b}", ":", "a\\|\\{b\\}"])
445 .succeeds()
446 .stdout_only("3\n");
447 new_ucmd!()
448 .args(&["{", ":", "a\\|\\{"])
449 .succeeds()
450 .stdout_only("1\n");
451 new_ucmd!()
452 .args(&["{}}}", ":", "\\{\\}\\}\\}"])
453 .succeeds()
454 .stdout_only("4\n");
455 new_ucmd!()
456 .args(&["a{}}}", ":", "a\\{\\}\\}\\}"])
457 .fails()
458 .stderr_only("expr: Invalid content of \\{\\}\n");
459 new_ucmd!()
460 .args(&["ab", ":", "ab\\{\\}"])
461 .fails()
462 .stderr_only("expr: Invalid content of \\{\\}\n");
463 new_ucmd!()
464 .args(&["_", ":", "a\\{12345678901234567890\\}"])
465 .fails()
466 .stderr_only("expr: Regular expression too big\n");
467 new_ucmd!()
468 .args(&["_", ":", "a\\{12345678901234567890,\\}"])
469 .fails()
470 .stderr_only("expr: Regular expression too big\n");
471 new_ucmd!()
472 .args(&["_", ":", "a\\{,12345678901234567890\\}"])
473 .fails()
474 .stderr_only("expr: Regular expression too big\n");
475 new_ucmd!()
476 .args(&["_", ":", "a\\{1,12345678901234567890\\}"])
477 .fails()
478 .stderr_only("expr: Regular expression too big\n");
479 new_ucmd!()
480 .args(&["_", ":", "a\\{1,1234567890abcdef\\}"])
481 .fails()
482 .stderr_only("expr: Invalid content of \\{\\}\n");
483 }
484
485 #[test]
486 fn test_regex_newline() {
487 new_ucmd!()
488 .args(&["line1\nline2\nline3 ", ":", ".*line2.*"])
489 .succeeds()
490 .stdout_only("18\n");
491 }
492
493 #[test]
494 fn test_regex_catastrophic_backtracking() {
495 let input = "a".repeat(30) + "c";
496 new_ucmd!()
497 .args(&[input.as_str(), ":", "\\(a\\+a\\+\\)\\+b"])
498 .fails_with_code(1)
499 .stdout_only("\n");
500 }
501
502 #[test]
503 fn test_regex_leftmost_longest_match_semantics() {
504 new_ucmd!()
505 .args(&["ab", ":", "a\\|ab"])
506 .succeeds()
507 .stdout_only("2\n");
508 }
509
510 #[test]
511 fn test_substr() {
512 new_ucmd!()
513 .args(&["substr", "abc", "1", "1"])
514 .succeeds()
515 .stdout_only("a\n");
516
517 new_ucmd!()
518 .args(&["abc", "substr", "1", "1"])
519 .fails_with_code(2)
520 .stderr_only("expr: syntax error: unexpected argument 'substr'\n");
521 }
522
523 #[test]
524 fn test_builtin_functions_precedence() {
525 new_ucmd!()
526 .args(&["substr", "ab cd", "3", "1", "!=", " "])
527 .fails_with_code(1)
528 .stdout_only("0\n");
529
530 new_ucmd!()
531 .args(&["substr", "ab cd", "3", "1", "=", " "])
532 .succeeds()
533 .stdout_only("1\n");
534
535 new_ucmd!()
536 .args(&["length", "abcd", "!=", "4"])
537 .fails_with_code(1)
538 .stdout_only("0\n");
539
540 new_ucmd!()
541 .args(&["length", "abcd", "=", "4"])
542 .succeeds()
543 .stdout_only("1\n");
544
545 new_ucmd!()
546 .args(&["index", "abcd", "c", "!=", "3"])
547 .fails_with_code(1)
548 .stdout_only("0\n");
549
550 new_ucmd!()
551 .args(&["index", "abcd", "c", "=", "3"])
552 .succeeds()
553 .stdout_only("1\n");
554
555 new_ucmd!()
556 .args(&["match", "abcd", "ab\\(.*\\)", "!=", "cd"])
557 .fails_with_code(1)
558 .stdout_only("0\n");
559
560 new_ucmd!()
561 .args(&["match", "abcd", "ab\\(.*\\)", "=", "cd"])
562 .succeeds()
563 .stdout_only("1\n");
564 }
565
566 #[test]
567 fn test_invalid_substr() {
568 new_ucmd!()
569 .args(&["substr", "abc", "0", "1"])
570 .fails_with_code(1)
571 .stdout_only("\n");
572
573 new_ucmd!()
574 .args(&["substr", "abc", &(usize::MAX.to_string() + "0"), "1"])
575 .fails_with_code(1)
576 .stdout_only("\n");
577
578 new_ucmd!()
579 .args(&["substr", "abc", "0", &(usize::MAX.to_string() + "0")])
580 .fails_with_code(1)
581 .stdout_only("\n");
582 }
583
584 #[test]
585 fn test_escape() {
586 new_ucmd!().args(&["+", "1"]).succeeds().stdout_only("1\n");
587
588 new_ucmd!()
589 .args(&["1", "+", "+", "1"])
590 .succeeds()
591 .stdout_only("2\n");
592
593 new_ucmd!()
594 .args(&["2", "*", "+", "3"])
595 .succeeds()
596 .stdout_only("6\n");
597
598 new_ucmd!()
599 .args(&["(", "1", ")", "+", "1"])
600 .succeeds()
601 .stdout_only("2\n");
602 }
603
604 #[test]
605 fn test_invalid_syntax() {
606 let invalid_syntaxes = [["12", "12"], ["12", "|"], ["|", "12"]];
607
608 for invalid_syntax in invalid_syntaxes {
609 new_ucmd!()
610 .args(&invalid_syntax)
611 .fails_with_code(2)
612 .stderr_contains("syntax error");
613 }
614 }
615
616 #[test]
617 fn test_num_str_comparison() {
618 new_ucmd!()
619 .args(&["1a", "<", "1", "+", "1"])
620 .succeeds()
621 .stdout_is("1\n");
622 }
623
624 #[test]
625 fn test_missing_closing_parenthesis_reports_syntax_error() {
626 new_ucmd!()
627 .args(&["(", "1", "/", "0"])
628 .fails()
629 .stderr_contains("expecting ')' after '0'");
630 }
631
632 #[test]
633 fn test_long_input() {
634 // Giving expr an arbitrary long expression should succeed rather than end with a segfault due to a stack overflow.
635 #[cfg(all(not(windows), not(target_os = "openbsd")))]
636 const MAX_NUMBER: usize = 40000;
637 #[cfg(all(not(windows), not(target_os = "openbsd")))]
638 const RESULT: &str = "800020000\n";
639
640 // On OpenBSD, crash with default MAX_NUMBER
641 #[cfg(target_os = "openbsd")]
642 const MAX_NUMBER: usize = 10000;
643 #[cfg(target_os = "openbsd")]
644 const RESULT: &str = "50005000\n";
645
646 // On windows there is 8192 characters input limit
647 #[cfg(windows)]
648 const MAX_NUMBER: usize = 1300; // 7993 characters (with spaces)
649 #[cfg(windows)]
650 const RESULT: &str = "845650\n";
651
652 let mut args: Vec<String> = vec!["1".to_string()];
653
654 for i in 2..=MAX_NUMBER {
655 args.push('+'.to_string());
656 args.push(i.to_string());
657 }
658
659 new_ucmd!().args(&args).succeeds().stdout_is(RESULT);
660 }
661
662 #[test]
663 fn test_deeply_nested_expression() {
664 // A deeply nested expression should succeed rather than abort due to a
665 // stack overflow in the parser (GHSA-gr9g-xjfw-pqfw).
666 #[cfg(not(windows))]
667 const DEPTH: usize = 10000;
668
669 // On windows there is 8192 characters input limit
670 #[cfg(windows)]
671 const DEPTH: usize = 2000; // 8003 characters (with spaces)
672
673 let mut args: Vec<&str> = vec!["("; DEPTH];
674 args.push("1");
675 args.extend(std::iter::repeat_n(")", DEPTH));
676 new_ucmd!().args(&args).succeeds().stdout_is("1\n");
677 }
678
679 #[test]
680 fn test_deeply_nested_length() {
681 // Keyword operands recurse in the parser just like parentheses do.
682 #[cfg(not(windows))]
683 const DEPTH: usize = 10000;
684
685 // On windows there is 8192 characters input limit
686 #[cfg(windows)]
687 const DEPTH: usize = 1000; // 7002 characters (with spaces)
688
689 let mut args: Vec<&str> = vec!["length"; DEPTH];
690 args.push("1");
691 new_ucmd!().args(&args).succeeds().stdout_is("1\n");
692 }
693
694 /// Test cases for expr arithmetic and string operations
695 mod expr_arithmetic {
696 use uutests::new_ucmd;
697
698 #[test]
699 fn test_addition() {
700 new_ucmd!()
701 .args(&["8", "+", "5"])
702 .succeeds()
703 .stdout_only("13\n");
704 }
705
706 #[test]
707 fn test_subtraction() {
708 new_ucmd!()
709 .args(&["9", "-", "4"])
710 .succeeds()
711 .stdout_only("5\n");
712 }
713
714 #[test]
715 fn test_multiplication() {
716 new_ucmd!()
717 .args(&["7", "*", "6"])
718 .succeeds()
719 .stdout_only("42\n");
720 }
721
722 #[test]
723 fn test_integer_division() {
724 new_ucmd!()
725 .args(&["120", "/", "8"])
726 .succeeds()
727 .stdout_only("15\n");
728 }
729
730 #[test]
731 fn test_modulo_remainder() {
732 new_ucmd!()
733 .args(&["120", "%", "7"])
734 .succeeds()
735 .stdout_only("1\n");
736 }
737
738 #[test]
739 fn test_add_with_negative() {
740 new_ucmd!()
741 .args(&["6", "+", "-4"])
742 .succeeds()
743 .stdout_only("2\n");
744 }
745
746 #[test]
747 fn test_two_negatives_added() {
748 new_ucmd!()
749 .args(&["-3", "+", "-5"])
750 .succeeds()
751 .stdout_only("-8\n");
752 }
753
754 #[test]
755 fn test_double_dash_neg_large() {
756 new_ucmd!()
757 .args(&["--", "-7", "+", "15"])
758 .succeeds()
759 .stdout_only("8\n");
760 }
761
762 #[test]
763 fn test_neg_large_no_dash() {
764 new_ucmd!()
765 .args(&["-7", "+", "15"])
766 .succeeds()
767 .stdout_only("8\n");
768 }
769
770 #[test]
771 fn test_double_dash_neg_small() {
772 new_ucmd!()
773 .args(&["--", "-2", "+", "9"])
774 .succeeds()
775 .stdout_only("7\n");
776 }
777
778 #[test]
779 fn test_neg_small_no_dash() {
780 new_ucmd!()
781 .args(&["-2", "+", "9"])
782 .succeeds()
783 .stdout_only("7\n");
784 }
785
786 #[test]
787 fn test_double_dash_positive() {
788 new_ucmd!()
789 .args(&["--", "3", "+", "6"])
790 .succeeds()
791 .stdout_only("9\n");
792 }
793
794 #[test]
795 fn test_parens_simple_mod() {
796 new_ucmd!()
797 .args(&["(", "120", "%", "7", ")"])
798 .succeeds()
799 .stdout_only("1\n");
800 }
801
802 #[test]
803 fn test_parens_mod_minus() {
804 new_ucmd!()
805 .args(&["(", "120", "%", "7", ")", "-", "8"])
806 .succeeds()
807 .stdout_only("-7\n");
808 }
809
810 #[test]
811 fn test_parens_div_mod_minus() {
812 new_ucmd!()
813 .args(&["18", "/", "(", "120", "%", "7", ")", "-", "6"])
814 .succeeds()
815 .stdout_only("12\n");
816 }
817
818 #[test]
819 fn test_parens_div_nested_mod() {
820 new_ucmd!()
821 .args(&["24", "/", "(", "(", "120", "%", "7", ")", "+", "5", ")"])
822 .succeeds()
823 .stdout_only("4\n");
824 }
825
826 #[test]
827 fn test_parens_add_mod() {
828 new_ucmd!()
829 .args(&["9", "+", "(", "120", "%", "7", ")"])
830 .succeeds()
831 .stdout_only("10\n");
832 }
833
834 #[test]
835 fn test_0bang() {
836 new_ucmd!()
837 .args(&["00", "<", "0!"])
838 .fails()
839 .code_is(1)
840 .stdout_only("0\n");
841 }
842
843 #[test]
844 fn test_00() {
845 new_ucmd!()
846 .args(&["00"])
847 .fails()
848 .code_is(1)
849 .stdout_only("00\n");
850 }
851
852 #[test]
853 fn test_minus0() {
854 new_ucmd!()
855 .args(&["-0"])
856 .fails()
857 .code_is(1)
858 .stdout_only("-0\n");
859 }
860
861 #[test]
862 fn test_andand() {
863 new_ucmd!()
864 .args(&["0", "&", "1", "/", "0"])
865 .fails()
866 .code_is(1)
867 .stdout_only("0\n");
868 }
869
870 #[test]
871 fn test_oror() {
872 new_ucmd!()
873 .args(&["1", "|", "1", "/", "0"])
874 .succeeds()
875 .stdout_only("1\n");
876 }
877
878 #[test]
879 fn test_orempty() {
880 new_ucmd!()
881 .args(&["", "|", ""])
882 .fails()
883 .code_is(1)
884 .stdout_only("0\n");
885 }
886
887 #[test]
888 fn test_fail_a() {
889 new_ucmd!()
890 .args(&["3", "+", "-"])
891 .fails()
892 .code_is(2)
893 .no_stdout()
894 .stderr_contains("non-integer argument");
895 }
896
897 #[test]
898 fn test_bigcmp() {
899 new_ucmd!()
900 .args(&[
901 "--",
902 "-2417851639229258349412352",
903 "<",
904 "2417851639229258349412352",
905 ])
906 .succeeds()
907 .stdout_only("1\n");
908 }
909
910 #[test]
911 fn test_anchor() {
912 new_ucmd!()
913 .args(&["a\nb", ":", "a$"])
914 .fails()
915 .code_is(1)
916 .stdout_only("0\n");
917 }
918
919 #[test]
920 fn test_emptysub() {
921 new_ucmd!()
922 .args(&["a", ":", "\\(b\\)*"])
923 .fails()
924 .code_is(1)
925 .stdout_only("\n");
926 }
927
928 #[test]
929 fn test_bre1() {
930 new_ucmd!()
931 .args(&["abc", ":", "a\\(b\\)c"])
932 .succeeds()
933 .stdout_only("b\n");
934 }
935
936 #[test]
937 fn test_bre2() {
938 new_ucmd!()
939 .args(&["a(", ":", "a("])
940 .succeeds()
941 .stdout_only("2\n");
942 }
943
944 #[test]
945 fn test_bre3() {
946 new_ucmd!()
947 .args(&["_", ":", "a\\("])
948 .fails_with_code(2)
949 .no_stdout()
950 .stderr_contains("Unmatched ( or \\(");
951 }
952
953 #[test]
954 fn test_bre4() {
955 new_ucmd!()
956 .args(&["_", ":", "a\\(b"])
957 .fails_with_code(2)
958 .no_stdout()
959 .stderr_contains("Unmatched ( or \\(");
960 }
961
962 #[test]
963 fn test_bre5() {
964 new_ucmd!()
965 .args(&["a(b", ":", "a(b"])
966 .succeeds()
967 .stdout_only("3\n");
968 }
969
970 #[test]
971 fn test_bre6() {
972 new_ucmd!()
973 .args(&["a)", ":", "a)"])
974 .succeeds()
975 .stdout_only("2\n");
976 }
977
978 #[test]
979 fn test_bre7() {
980 new_ucmd!()
981 .args(&["_", ":", "a\\)"])
982 .fails_with_code(2)
983 .stderr_contains("Unmatched ) or \\)");
984 }
985
986 #[test]
987 fn test_bre8() {
988 new_ucmd!()
989 .args(&["_", ":", "\\)"])
990 .fails_with_code(2)
991 .stderr_contains("Unmatched ) or \\)");
992 }
993
994 #[test]
995 fn test_bre9() {
996 new_ucmd!()
997 .args(&["ab", ":", "a\\(\\)b"])
998 .fails_with_code(1)
999 .stdout_only("\n");
1000 }
1001
1002 #[test]
1003 fn test_bre10() {
1004 new_ucmd!()
1005 .args(&["a^b", ":", "a^b"])
1006 .succeeds()
1007 .stdout_only("3\n");
1008 }
1009
1010 #[test]
1011 fn test_bre11() {
1012 new_ucmd!()
1013 .args(&["a$b", ":", "a$b"])
1014 .succeeds()
1015 .stdout_only("3\n");
1016 }
1017
1018 #[test]
1019 fn test_bre12() {
1020 new_ucmd!()
1021 .args(&["", ":", "\\($\\)\\(^\\)"])
1022 .fails_with_code(1)
1023 .stdout_only("\n");
1024 }
1025
1026 #[test]
1027 fn test_bre13() {
1028 new_ucmd!()
1029 .args(&["b", ":", "a*\\(b$\\)c*"])
1030 .succeeds()
1031 .stdout_only("b\n");
1032 }
1033
1034 #[test]
1035 fn test_bre14() {
1036 new_ucmd!()
1037 .args(&["X|", ":", "X\\(|\\)", ":", "(", "X|", ":", "X\\(|\\)", ")"])
1038 .succeeds()
1039 .stdout_only("1\n");
1040 }
1041
1042 #[test]
1043 fn test_bre15() {
1044 new_ucmd!()
1045 .args(&["X*", ":", "X\\(*\\)", ":", "(", "X*", ":", "X\\(*\\)", ")"])
1046 .succeeds()
1047 .stdout_only("1\n");
1048 }
1049
1050 #[test]
1051 fn test_bre16() {
1052 new_ucmd!()
1053 .args(&["abc", ":", "\\(\\)"])
1054 .fails_with_code(1)
1055 .stdout_only("\n");
1056 }
1057
1058 #[test]
1059 fn test_bre17() {
1060 new_ucmd!()
1061 .args(&["{1}a", ":", "\\(\\{1\\}a\\)"])
1062 .succeeds()
1063 .stdout_only("{1}a\n");
1064 }
1065
1066 #[test]
1067 fn test_bre18() {
1068 new_ucmd!()
1069 .args(&["X*", ":", "X\\(*\\)", ":", "^*"])
1070 .succeeds()
1071 .stdout_only("1\n");
1072 }
1073
1074 #[test]
1075 fn test_bre19() {
1076 new_ucmd!()
1077 .args(&["{1}", ":", "\\{1\\}"])
1078 .succeeds()
1079 .stdout_only("3\n");
1080 }
1081
1082 #[test]
1083 fn test_bre20() {
1084 new_ucmd!()
1085 .args(&["{", ":", "{"])
1086 .succeeds()
1087 .stdout_only("1\n");
1088 }
1089
1090 #[test]
1091 fn test_bre21() {
1092 new_ucmd!()
1093 .args(&["abbcbd", ":", "a\\(b*\\)c\\1d"])
1094 .fails_with_code(1)
1095 .stdout_only("\n");
1096 }
1097
1098 #[test]
1099 fn test_bre22() {
1100 new_ucmd!()
1101 .args(&["abbcbbbd", ":", "a\\(b*\\)c\\1d"])
1102 .fails_with_code(1)
1103 .stdout_only("\n");
1104 }
1105
1106 #[test]
1107 fn test_bre23() {
1108 new_ucmd!()
1109 .args(&["abc", ":", "\\(.\\)\\1"])
1110 .fails_with_code(1)
1111 .stdout_only("\n");
1112 }
1113
1114 #[test]
1115 fn test_bre24() {
1116 new_ucmd!()
1117 .args(&["abbccd", ":", "a\\(\\([bc]\\)\\2\\)*d"])
1118 .succeeds()
1119 .stdout_only("cc\n");
1120 }
1121
1122 #[test]
1123 fn test_bre25() {
1124 new_ucmd!()
1125 .args(&["abbcbd", ":", "a\\(\\([bc]\\)\\2\\)*d"])
1126 .fails_with_code(1)
1127 .stdout_only("\n");
1128 }
1129
1130 #[test]
1131 fn test_bre26() {
1132 new_ucmd!()
1133 .args(&["abbbd", ":", "a\\(\\(b\\)*\\2\\)*d"])
1134 .succeeds()
1135 .stdout_only("bbb\n");
1136 }
1137
1138 #[test]
1139 fn test_bre27() {
1140 new_ucmd!()
1141 .args(&["aabcd", ":", "\\(a\\)\\1bcd"])
1142 .succeeds()
1143 .stdout_only("a\n");
1144 }
1145
1146 #[test]
1147 fn test_bre28() {
1148 new_ucmd!()
1149 .args(&["aabcd", ":", "\\(a\\)\\1bc*d"])
1150 .succeeds()
1151 .stdout_only("a\n");
1152 }
1153
1154 #[test]
1155 fn test_bre29() {
1156 new_ucmd!()
1157 .args(&["aabd", ":", "\\(a\\)\\1bc*d"])
1158 .succeeds()
1159 .stdout_only("a\n");
1160 }
1161
1162 #[test]
1163 fn test_bre30() {
1164 new_ucmd!()
1165 .args(&["aabcccd", ":", "\\(a\\)\\1bc*d"])
1166 .succeeds()
1167 .stdout_only("a\n");
1168 }
1169
1170 #[test]
1171 fn test_bre31() {
1172 new_ucmd!()
1173 .args(&["aabcccd", ":", "\\(a\\)\\1bc*[ce]d"])
1174 .succeeds()
1175 .stdout_only("a\n");
1176 }
1177
1178 #[test]
1179 fn test_bre32() {
1180 new_ucmd!()
1181 .args(&["aabcccd", ":", "\\(a\\)\\1b\\(c\\)*cd"])
1182 .succeeds()
1183 .stdout_only("a\n");
1184 }
1185
1186 #[test]
1187 fn test_bre33() {
1188 new_ucmd!()
1189 .args(&["a*b", ":", "a\\(*\\)b"])
1190 .succeeds()
1191 .stdout_only("*\n");
1192 }
1193
1194 #[test]
1195 fn test_bre34() {
1196 new_ucmd!()
1197 .args(&["ab", ":", "a\\(**\\)b"])
1198 .fails_with_code(1)
1199 .stdout_only("\n");
1200 }
1201
1202 #[test]
1203 fn test_bre35() {
1204 new_ucmd!()
1205 .args(&["ab", ":", "a\\(***\\)b"])
1206 .fails_with_code(1)
1207 .stdout_only("\n");
1208 }
1209
1210 #[test]
1211 fn test_bre36() {
1212 new_ucmd!()
1213 .args(&["*a", ":", "*a"])
1214 .succeeds()
1215 .stdout_only("2\n");
1216 }
1217
1218 #[test]
1219 fn test_bre37() {
1220 new_ucmd!()
1221 .args(&["a", ":", "**a"])
1222 .succeeds()
1223 .stdout_only("1\n");
1224 }
1225
1226 #[test]
1227 fn test_bre38() {
1228 new_ucmd!()
1229 .args(&["a", ":", "***a"])
1230 .succeeds()
1231 .stdout_only("1\n");
1232 }
1233
1234 #[test]
1235 fn test_bre39() {
1236 new_ucmd!()
1237 .args(&["ab", ":", "a\\{1\\}b"])
1238 .succeeds()
1239 .stdout_only("2\n");
1240 }
1241
1242 #[test]
1243 fn test_bre40() {
1244 new_ucmd!()
1245 .args(&["ab", ":", "a\\{1,\\}b"])
1246 .succeeds()
1247 .stdout_only("2\n");
1248 }
1249
1250 #[test]
1251 fn test_bre41() {
1252 new_ucmd!()
1253 .args(&["aab", ":", "a\\{1,2\\}b"])
1254 .succeeds()
1255 .stdout_only("3\n");
1256 }
1257
1258 #[test]
1259 fn test_bre42() {
1260 new_ucmd!()
1261 .args(&["_", ":", "a\\{1"])
1262 .fails_with_code(2)
1263 .no_stdout()
1264 .stderr_contains("Unmatched \\{");
1265 }
1266
1267 #[test]
1268 fn test_bre43() {
1269 new_ucmd!()
1270 .args(&["_", ":", "a\\{1a"])
1271 .fails_with_code(2)
1272 .no_stdout()
1273 .stderr_contains("Unmatched \\{");
1274 }
1275
1276 #[test]
1277 fn test_bre44() {
1278 new_ucmd!()
1279 .args(&["_", ":", "a\\{1a\\}"])
1280 .fails_with_code(2)
1281 .no_stdout()
1282 .stderr_contains("Invalid content of \\{\\}");
1283 }
1284
1285 #[test]
1286 fn test_bre45() {
1287 new_ucmd!()
1288 .args(&["a", ":", "a\\{,2\\}"])
1289 .succeeds()
1290 .stdout_only("1\n");
1291 }
1292
1293 #[test]
1294 fn test_bre46() {
1295 new_ucmd!()
1296 .args(&["a", ":", "a\\{,\\}"])
1297 .succeeds()
1298 .stdout_only("1\n");
1299 }
1300
1301 #[test]
1302 fn test_bre47() {
1303 new_ucmd!()
1304 .args(&["_", ":", "a\\{1,x\\}"])
1305 .fails_with_code(2)
1306 .no_stdout()
1307 .stderr_contains("Invalid content of \\{\\}");
1308 }
1309
1310 #[test]
1311 fn test_bre48() {
1312 new_ucmd!()
1313 .args(&["_", ":", "a\\{1,x"])
1314 .fails_with_code(2)
1315 .no_stdout()
1316 .stderr_contains("Unmatched \\{");
1317 }
1318
1319 #[test]
1320 fn test_bre49() {
1321 new_ucmd!()
1322 .args(&["_", ":", "a\\{32768\\}"])
1323 .fails_with_code(2)
1324 .no_stdout()
1325 .stderr_contains("Regular expression too big\n");
1326 }
1327
1328 #[test]
1329 fn test_bre50() {
1330 new_ucmd!()
1331 .args(&["_", ":", "a\\{1,0\\}"])
1332 .fails_with_code(2)
1333 .no_stdout()
1334 .stderr_contains("Invalid content of \\{\\}");
1335 }
1336
1337 #[test]
1338 fn test_bre51() {
1339 new_ucmd!()
1340 .args(&["acabc", ":", ".*ab\\{0,0\\}c"])
1341 .succeeds()
1342 .stdout_only("2\n");
1343 }
1344
1345 #[test]
1346 fn test_bre52() {
1347 new_ucmd!()
1348 .args(&["abcac", ":", "ab\\{0,1\\}c"])
1349 .succeeds()
1350 .stdout_only("3\n");
1351 }
1352
1353 #[test]
1354 fn test_bre53() {
1355 new_ucmd!()
1356 .args(&["abbcac", ":", "ab\\{0,3\\}c"])
1357 .succeeds()
1358 .stdout_only("4\n");
1359 }
1360
1361 #[test]
1362 fn test_bre54() {
1363 new_ucmd!()
1364 .args(&["abcac", ":", ".*ab\\{1,1\\}c"])
1365 .succeeds()
1366 .stdout_only("3\n");
1367 }
1368
1369 #[test]
1370 fn test_bre55() {
1371 new_ucmd!()
1372 .args(&["abcac", ":", ".*ab\\{1,3\\}c"])
1373 .succeeds()
1374 .stdout_only("3\n");
1375 }
1376
1377 #[test]
1378 fn test_bre56() {
1379 new_ucmd!()
1380 .args(&["abbcabc", ":", ".*ab\\{2,2\\}c"])
1381 .succeeds()
1382 .stdout_only("4\n");
1383 }
1384
1385 #[test]
1386 fn test_bre57() {
1387 new_ucmd!()
1388 .args(&["abbcabc", ":", ".*ab\\{2,4\\}c"])
1389 .succeeds()
1390 .stdout_only("4\n");
1391 }
1392
1393 #[test]
1394 fn test_bre58() {
1395 new_ucmd!()
1396 .args(&["aa", ":", "a\\{1\\}\\{1\\}"])
1397 .succeeds()
1398 .stdout_only("1\n");
1399 }
1400
1401 #[test]
1402 fn test_bre59() {
1403 new_ucmd!()
1404 .args(&["aa", ":", "a*\\{1\\}"])
1405 .succeeds()
1406 .stdout_only("2\n");
1407 }
1408
1409 #[test]
1410 fn test_bre60() {
1411 new_ucmd!()
1412 .args(&["aa", ":", "a\\{1\\}*"])
1413 .succeeds()
1414 .stdout_only("2\n");
1415 }
1416
1417 #[test]
1418 fn test_bre61() {
1419 new_ucmd!()
1420 .args(&["acd", ":", "a\\(b\\)?c\\1d"])
1421 .fails_with_code(1)
1422 .stdout_only("\n");
1423 }
1424
1425 #[test]
1426 fn test_bre62() {
1427 new_ucmd!()
1428 .args(&["--", "-5", ":", "-\\{0,1\\}[0-9]*$"])
1429 .succeeds()
1430 .stdout_only("2\n");
1431 }
1432
1433 #[test]
1434 fn test_fail_c() {
1435 new_ucmd!()
1436 .args::<&str>(&[])
1437 .fails_with_code(2)
1438 .no_stdout()
1439 .stderr_contains("missing operand")
1440 .stderr_contains("Try")
1441 .stderr_contains("for more information");
1442 }
1443
1444 const BIG: &str = "98782897298723498732987928734";
1445 const BIG_P1: &str = "98782897298723498732987928735";
1446 const BIG_SUM: &str = "197565794597446997465975857469";
1447 const BIG_PROD: &str = "9758060798730154302876482828124348356960410232492450771490";
1448
1449 #[test]
1450 fn test_bignum_add() {
1451 new_ucmd!()
1452 .args(&[BIG, "+", "1"])
1453 .succeeds()
1454 .stdout_only(format!("{BIG_P1}\n"));
1455 }
1456
1457 #[test]
1458 fn test_bignum_add1() {
1459 new_ucmd!()
1460 .args(&[BIG, "+", BIG_P1])
1461 .succeeds()
1462 .stdout_only(format!("{BIG_SUM}\n"));
1463 }
1464
1465 #[test]
1466 fn test_bignum_sub() {
1467 new_ucmd!()
1468 .args(&[BIG_P1, "-", "1"])
1469 .succeeds()
1470 .stdout_only(format!("{BIG}\n"));
1471 }
1472
1473 #[test]
1474 fn test_bignum_sub1() {
1475 new_ucmd!()
1476 .args(&[BIG_SUM, "-", BIG])
1477 .succeeds()
1478 .stdout_only(format!("{BIG_P1}\n"));
1479 }
1480
1481 #[test]
1482 fn test_bignum_mul() {
1483 new_ucmd!()
1484 .args(&[BIG_P1, "*", BIG])
1485 .succeeds()
1486 .stdout_only(format!("{BIG_PROD}\n"));
1487 }
1488
1489 #[test]
1490 fn test_bignum_div() {
1491 new_ucmd!()
1492 .args(&[BIG_PROD, "/", BIG])
1493 .succeeds()
1494 .stdout_only(format!("{BIG_P1}\n"));
1495 }
1496
1497 #[test]
1498 fn test_se0() {
1499 new_ucmd!()
1500 .args(&["9", "9"])
1501 .fails_with_code(2)
1502 .no_stdout()
1503 .stderr_contains("syntax error: unexpected argument '9'");
1504 }
1505
1506 #[test]
1507 fn test_se1() {
1508 new_ucmd!()
1509 .args(&["2", "a"])
1510 .fails_with_code(2)
1511 .no_stdout()
1512 .stderr_contains("syntax error: unexpected argument 'a'");
1513 }
1514
1515 #[test]
1516 fn test_se2() {
1517 new_ucmd!()
1518 .args(&["2", "+"])
1519 .fails_with_code(2)
1520 .no_stdout()
1521 .stderr_contains("syntax error: missing argument after '+'");
1522 }
1523
1524 #[test]
1525 fn test_se3() {
1526 new_ucmd!()
1527 .args(&["2", ":"])
1528 .fails_with_code(2)
1529 .no_stdout()
1530 .stderr_contains("syntax error: missing argument after ':'");
1531 }
1532
1533 #[test]
1534 fn test_se4() {
1535 new_ucmd!()
1536 .args(&["length"])
1537 .fails_with_code(2)
1538 .no_stdout()
1539 .stderr_contains("syntax error: missing argument after 'length'");
1540 }
1541
1542 #[test]
1543 fn test_se5() {
1544 new_ucmd!()
1545 .args(&["(", "2"])
1546 .fails_with_code(2)
1547 .no_stdout()
1548 .stderr_contains("syntax error: expecting ')' after '2'");
1549 }
1550
1551 #[test]
1552 fn test_se6() {
1553 new_ucmd!()
1554 .args(&["(", "2", "a"])
1555 .fails_with_code(2)
1556 .no_stdout()
1557 .stderr_contains("syntax error: expecting ')' instead of 'a'");
1558 }
1559 }
1560
1561 /// Test that `expr` correctly detects and handles locales
1562 mod locale_aware {
1563 use uutests::new_ucmd;
1564
1565 #[test]
1566 #[cfg_attr(wasi_runner, ignore = "WASI: no locale data, every locale is C")]
1567 fn test_expr_collating() {
1568 for (loc, code, output) in [
1569 ("C", 0, "1\n"),
1570 ("fr_FR.UTF-8", 1, "0\n"),
1571 ("fr_FR.utf-8", 1, "0\n"),
1572 ("en_US", 1, "0\n"),
1573 ] {
1574 new_ucmd!()
1575 .args(&["50n", ">", "-51"])
1576 .env("LC_ALL", loc)
1577 .run()
1578 .code_is(code)
1579 .stdout_only(output);
1580 }
1581 }
1582 }
1583
1584 /// Tests for multibyte character arithmetic in expr
1585 #[cfg(target_os = "linux")]
1586 mod expr_multibyte_arithmetic {
1587 use uutests::new_ucmd;
1588
1589 use uucore::os_str_from_bytes;
1590
1591 trait AsByteSlice<'a> {
1592 fn into_bytes(self) -> &'a [u8];
1593 }
1594
1595 impl<'a> AsByteSlice<'a> for &'a str {
1596 fn into_bytes(self) -> &'a [u8] {
1597 self.as_bytes()
1598 }
1599 }
1600
1601 impl<'a> AsByteSlice<'a> for &'a [u8] {
1602 fn into_bytes(self) -> &'a [u8] {
1603 self
1604 }
1605 }
1606
1607 impl<'a, const N: usize> AsByteSlice<'a> for &'a [u8; N] {
1608 fn into_bytes(self) -> &'a [u8] {
1609 self
1610 }
1611 }
1612
1613 const EXPRESSION: &[u8] = "\u{6C49}\u{5B57}\u{6D4B}\u{8BD5}".as_bytes(); // 汉字桋试 – 4 chars, 12 bytes
1614
1615 #[derive(Debug, Default, Clone, Copy)]
1616 struct TestCase {
1617 pub locale: &'static str,
1618 pub out: Option<&'static [u8]>,
1619 pub code: i32,
1620 }
1621
1622 impl TestCase {
1623 const FR: Self = Self::new("fr_FR.UTF-8");
1624 const C: Self = Self::new("C");
1625
1626 const fn new(locale: &'static str) -> Self {
1627 Self {
1628 locale,
1629 out: None,
1630 code: 0,
1631 }
1632 }
1633
1634 fn out(mut self, out: impl AsByteSlice<'static>) -> Self {
1635 self.out = Some(out.into_bytes());
1636 self
1637 }
1638
1639 fn code(mut self, code: i32) -> Self {
1640 self.code = code;
1641 self
1642 }
1643 }
1644
1645 fn check_test_case(args: &[&[u8]], tc: &TestCase) {
1646 // The WASI build ships no locale data, so every locale behaves like
1647 // "C", and WASI can only carry valid UTF-8 through argv. Run just the
1648 // cases that don't depend on either.
1649 if std::env::var("UUTESTS_WASM_RUNNER").is_ok()
1650 && (tc.locale != "C" || args.iter().any(|arg| std::str::from_utf8(arg).is_err()))
1651 {
1652 return;
1653 }
1654
1655 let args = args
1656 .iter()
1657 .map(|arg: &&[u8]| os_str_from_bytes(arg).unwrap())
1658 .collect::<Vec<_>>();
1659
1660 let res = new_ucmd!().env("LC_ALL", tc.locale).args(&args).run();
1661
1662 res.code_is(tc.code);
1663
1664 if let Some(out) = tc.out {
1665 let mut out = out.to_owned();
1666 out.push(b'\n');
1667 res.stdout_is_bytes(&out);
1668 } else {
1669 res.no_stdout();
1670 }
1671 }
1672
1673 // LENGTH EXPRESSIONS
1674
1675 // sanity check
1676 #[test]
1677 fn test_mb_length_full() {
1678 let args: &[&[u8]] = &[b"length", b"abcdef"];
1679
1680 let cases = &[TestCase::FR.out("6"), TestCase::C.out("6")];
1681
1682 for tc in cases {
1683 check_test_case(args, tc);
1684 }
1685 }
1686
1687 // A single multibyte character in the beginning of the string \xCE\xB1 is
1688 // UTF-8 for "U+03B1 (2-byte UTF-8 sequence, used as multibyte prefix test)"
1689 #[test]
1690 fn test_mb_length_ascii_prefix() {
1691 let args: &[&[u8]] = &[b"length", b"\xCE\xB1bcdef"];
1692
1693 let cases = &[TestCase::FR.out("6"), TestCase::C.out("7")];
1694
1695 for tc in cases {
1696 check_test_case(args, tc);
1697 }
1698 }
1699
1700 // A single multibyte character in the middle of the string \xCE\xB4 is
1701 // UTF-8 for "U+03B4 (2-byte UTF-8 sequence, used as multibyte middle test)"
1702 #[test]
1703 fn test_mb_length_ascii_middle() {
1704 let args: &[&[u8]] = &[b"length", b"abc\xCE\xB4ef"];
1705
1706 let cases = &[TestCase::FR.out("6"), TestCase::C.out("7")];
1707
1708 for tc in cases {
1709 check_test_case(args, tc);
1710 }
1711 }
1712
1713 // A single multibyte character in the end of the string
1714 #[test]
1715 fn test_mb_length_ascii_suffix() {
1716 let args: &[&[u8]] = &[b"length", b"fedcb\xCE\xB1"];
1717
1718 let cases = &[TestCase::FR.out("6"), TestCase::C.out("7")];
1719
1720 for tc in cases {
1721 check_test_case(args, tc);
1722 }
1723 }
1724
1725 // A invalid multibyte sequence
1726 #[test]
1727 fn test_mb_length_invalid_seq() {
1728 let args: &[&[u8]] = &[b"length", b"\xB1aaa"];
1729
1730 let cases = &[TestCase::FR.out("4"), TestCase::C.out("4")];
1731
1732 for tc in cases {
1733 check_test_case(args, tc);
1734 }
1735 }
1736
1737 // An incomplete multibyte sequence at the end of the string
1738 #[test]
1739 fn test_mb_length_incomplete_end() {
1740 let args: &[&[u8]] = &[b"length", b"aaa\xCE"];
1741
1742 let cases = &[TestCase::FR.out("4"), TestCase::C.out("4")];
1743
1744 for tc in cases {
1745 check_test_case(args, tc);
1746 }
1747 }
1748
1749 // An incomplete multibyte sequence at the end of the string
1750 #[test]
1751 fn test_mb_length_expression() {
1752 let args: &[&[u8]] = &[b"length", EXPRESSION];
1753
1754 let cases = &[TestCase::FR.out("4"), TestCase::C.out("12")];
1755
1756 for tc in cases {
1757 check_test_case(args, tc);
1758 }
1759 }
1760
1761 // INDEX EXPRESSIONS
1762
1763 // sanity check
1764 #[test]
1765 fn test_i1() {
1766 let args: &[&[u8]] = &[b"index", b"abcdef", b"fb"];
1767
1768 let cases = &[TestCase::FR.out("2"), TestCase::C.out("2")];
1769
1770 for tc in cases {
1771 check_test_case(args, tc);
1772 }
1773 }
1774
1775 // Search for a single-octet
1776 #[test]
1777 fn test_i2() {
1778 let args: &[&[u8]] = &[b"index", b"\xCE\xB1bc\xCE\xB4ef", b"b"];
1779
1780 let cases = &[TestCase::FR.out("2"), TestCase::C.out("3")];
1781
1782 for tc in cases {
1783 check_test_case(args, tc);
1784 }
1785 }
1786
1787 #[test]
1788 fn test_i3() {
1789 let args: &[&[u8]] = &[b"index", b"\xCE\xB1bc\xCE\xB4ef", b"f"];
1790
1791 let cases = &[TestCase::FR.out("6"), TestCase::C.out("8")];
1792
1793 for tc in cases {
1794 check_test_case(args, tc);
1795 }
1796 }
1797
1798 // Search for multibyte character.
1799 // In the C locale, the search string is treated as two octets.
1800 // the first of them (\xCE) matches the first octet of the input string.
1801 #[test]
1802 fn test_i4() {
1803 let args: &[&[u8]] = &[b"index", b"\xCE\xB1bc\xCE\xB4ef", b"\xCE\xB4"];
1804
1805 let cases = &[TestCase::FR.out("4"), TestCase::C.out("1")];
1806
1807 for tc in cases {
1808 check_test_case(args, tc);
1809 }
1810 }
1811
1812 // Invalid multibyte sequence in the input string, treated as a single
1813 // octet.
1814 #[test]
1815 fn test_i5() {
1816 let args: &[&[u8]] = &[b"index", b"\xCEbc\xCE\xB4ef", b"\xCE\xB4"];
1817
1818 let cases = &[TestCase::FR.out("4"), TestCase::C.out("1")];
1819
1820 for tc in cases {
1821 check_test_case(args, tc);
1822 }
1823 }
1824
1825 // Invalid multibyte sequence in the search string, treated as a single
1826 // octet. In multibyte locale, there should be no match, expr returns and
1827 // prints zero, and terminates with exit-code 1 (as per POSIX).
1828 #[test]
1829 fn test_i6() {
1830 let args: &[&[u8]] = &[b"index", b"\xCE\xB1bc\xCE\xB4ef", b"\xB4"];
1831
1832 let cases = &[TestCase::FR.out("0").code(1), TestCase::C.out("6")];
1833
1834 for tc in cases {
1835 check_test_case(args, tc);
1836 }
1837 }
1838
1839 // Edge-case: invalid multibyte sequence BOTH in the input string and in
1840 // the search string: expr should find a match.
1841 #[test]
1842 fn test_i7() {
1843 let args: &[&[u8]] = &[b"index", b"\xCE\xB1bc\xB4ef", b"\xB4"];
1844
1845 let cases = &[TestCase::FR.out("4")];
1846
1847 for tc in cases {
1848 check_test_case(args, tc);
1849 }
1850 }
1851
1852 // SUBSTR EXPRESSIONS
1853
1854 // sanity check
1855 #[test]
1856 fn test_s1() {
1857 let args: &[&[u8]] = &[b"substr", b"abcdef", b"2", b"3"];
1858
1859 let cases = &[TestCase::FR.out("bcd"), TestCase::C.out("bcd")];
1860
1861 for tc in cases {
1862 check_test_case(args, tc);
1863 }
1864 }
1865
1866 #[test]
1867 fn test_s2() {
1868 let args: &[&[u8]] = &[b"substr", b"\xCE\xB1bc\xCE\xB4ef", b"1", b"1"];
1869
1870 let cases = &[TestCase::FR.out(b"\xCE\xB1"), TestCase::C.out(b"\xCE")];
1871
1872 for tc in cases {
1873 check_test_case(args, tc);
1874 }
1875 }
1876
1877 #[test]
1878 fn test_s3() {
1879 let args: &[&[u8]] = &[b"substr", b"\xCE\xB1bc\xCE\xB4ef", b"3", b"2"];
1880
1881 let cases = &[TestCase::FR.out(b"c\xCE\xB4"), TestCase::C.out("bc")];
1882
1883 for tc in cases {
1884 check_test_case(args, tc);
1885 }
1886 }
1887
1888 #[test]
1889 fn test_s4() {
1890 let args: &[&[u8]] = &[b"substr", b"\xCE\xB1bc\xCE\xB4ef", b"4", b"1"];
1891
1892 let cases = &[TestCase::FR.out(b"\xCE\xB4"), TestCase::C.out("c")];
1893
1894 for tc in cases {
1895 check_test_case(args, tc);
1896 }
1897 }
1898
1899 #[test]
1900 fn test_s5() {
1901 let args: &[&[u8]] = &[b"substr", b"\xCE\xB1bc\xCE\xB4ef", b"4", b"2"];
1902
1903 let cases = &[TestCase::FR.out(b"\xCE\xB4e"), TestCase::C.out(b"c\xCE")];
1904
1905 for tc in cases {
1906 check_test_case(args, tc);
1907 }
1908 }
1909
1910 #[test]
1911 fn test_s6() {
1912 let args: &[&[u8]] = &[b"substr", b"\xCE\xB1bc\xCE\xB4ef", b"6", b"1"];
1913
1914 let cases = &[TestCase::FR.out(b"f"), TestCase::C.out(b"\xB4")];
1915
1916 for tc in cases {
1917 check_test_case(args, tc);
1918 }
1919 }
1920
1921 #[test]
1922 fn test_s7() {
1923 let args: &[&[u8]] = &[b"substr", b"\xCE\xB1bc\xCE\xB4ef", b"7", b"1"];
1924
1925 let cases = &[TestCase::FR.out("").code(1), TestCase::C.out(b"e")];
1926
1927 for tc in cases {
1928 check_test_case(args, tc);
1929 }
1930 }
1931
1932 #[test]
1933 fn test_s8() {
1934 let args: &[&[u8]] = &[b"substr", b"\xCE\xB1bc\xB4ef", b"3", b"3"];
1935
1936 let cases = &[TestCase::FR.out(b"c\xB4e"), TestCase::C.out(b"bc\xB4")];
1937
1938 for tc in cases {
1939 check_test_case(args, tc);
1940 }
1941 }
1942
1943 // MATCH EXPRESSIONS
1944
1945 // sanity check
1946 #[test]
1947 fn test_m1() {
1948 let args: &[&[u8]] = &[b"match", b"abcdef", b"ab"];
1949
1950 let cases = &[TestCase::FR.out("2"), TestCase::C.out("2")];
1951
1952 for tc in cases {
1953 check_test_case(args, tc);
1954 }
1955 }
1956 #[test]
1957 fn test_m2() {
1958 let args: &[&[u8]] = &[b"match", b"abcdef", b"\\(ab\\)"];
1959
1960 let cases = &[TestCase::FR.out("ab"), TestCase::C.out("ab")];
1961
1962 for tc in cases {
1963 check_test_case(args, tc);
1964 }
1965 }
1966
1967 // The regex engine should match the '.' to the first multibyte character.
1968 #[test]
1969 fn test_m3() {
1970 let args: &[&[u8]] = &[b"match", b"\xCE\xB1bc\xCE\xB4ef", b".bc"];
1971
1972 let cases = &[TestCase::FR.out("3"), TestCase::C.out("0").code(1)];
1973
1974 for tc in cases {
1975 check_test_case(args, tc);
1976 }
1977 }
1978
1979 // The opposite of the previous test: two dots should only match the two
1980 // octets in single-byte locale.
1981 #[test]
1982 fn test_m4() {
1983 let args: &[&[u8]] = &[b"match", b"\xCE\xB1bc\xCE\xB4ef", b"..bc"];
1984
1985 let cases = &[TestCase::FR.out("0").code(1), TestCase::C.out("4")];
1986
1987 for tc in cases {
1988 check_test_case(args, tc);
1989 }
1990 }
1991
1992 // Match with grouping - a single dot should return the two octets
1993 #[test]
1994 fn test_m5() {
1995 let args: &[&[u8]] = &[b"match", b"\xCE\xB1bc\xCE\xB4ef", b"\\(.b\\)c"];
1996
1997 let cases = &[TestCase::FR.out(b"\xCE\xB1b"), TestCase::C.out("").code(1)];
1998
1999 for tc in cases {
2000 check_test_case(args, tc);
2001 }
2002 }
2003
2004 // Invalid multibyte sequences - regex should not match in multibyte locale
2005 // (POSIX requirement)
2006 #[test]
2007 fn test_m6() {
2008 let args: &[&[u8]] = &[b"match", b"\xCEbc\xCE\xB4ef", b"\\(.\\)"];
2009
2010 let cases = &[TestCase::FR.out("").code(1), TestCase::C.out(b"\xCE")];
2011
2012 for tc in cases {
2013 check_test_case(args, tc);
2014 }
2015 }
2016
2017 // Character classes: in the multibyte case, the regex engine understands
2018 // there is a single multibyte character in the brackets.
2019 // In the single byte case, the regex engine sees two octets in the
2020 // character class ('\xCE' and '\xB1') - and it matches the first one.
2021 #[test]
2022 fn test_m7() {
2023 let args: &[&[u8]] = &[b"match", b"\xCE\xB1bc\xCE\xB4ef", b"\\(.\\)"];
2024
2025 let cases = &[TestCase::FR.out(b"\xCE\xB1"), TestCase::C.out(b"\xCE")];
2026
2027 for tc in cases {
2028 check_test_case(args, tc);
2029 }
2030 }
2031 }
2032
2033 #[test]
2034 #[cfg_attr(wasi_runner, ignore = "WASI: no locale data, every locale is C")]
2035 fn test_emoji_operations() {
2036 new_ucmd!()
2037 .args(&["πŸš€", "=", "πŸš€"])
2038 .succeeds()
2039 .stdout_only("1\n");
2040
2041 new_ucmd!()
2042 .args(&["πŸš€", "!=", "πŸš€"])
2043 .fails()
2044 .stdout_only("0\n");
2045
2046 new_ucmd!()
2047 .args(&["πŸš€", "=", "🧨"])
2048 .fails()
2049 .stdout_only("0\n");
2050
2051 new_ucmd!()
2052 .args(&["length", "πŸ¦€πŸš€πŸŽ―"])
2053 .env("LC_ALL", "fr_FR.UTF-8")
2054 .succeeds()
2055 .stdout_only("3\n");
2056
2057 new_ucmd!()
2058 .args(&["🌍", "!=", "🌎"])
2059 .succeeds()
2060 .stdout_only("1\n");
2061 }
2062
2063 #[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))]
2064 mod diagnostics {
2065 use super::*;
2066 #[cfg(unix)]
2067 #[test]
2068 fn test_snippet_points_at_the_unexpected_argument() {
2069 let result = new_ucmd!()
2070 .terminal_sim_stderr()
2071 .args(&["6", "+", "7", "spare"])
2072 .fails_with_code(2);
2073
2074 // The whole report: the `expr: ` prefix of the plain form, the
2075 // expression echoed back, a caret on `spare`, and the quoting advice.
2076 assert_eq!(
2077 result.stderr_as_displayed(),
2078 "\
2079 expr: syntax error: unexpected argument 'spare'
2080 ╭─[ expr:1:7 ]
2081 β”‚
2082 1 β”‚ 6 + 7 spare
2083 β”‚ ──┬──
2084 β”‚ ╰──── the expression was already complete here
2085 β”‚
2086 β”‚ Help: the shell may have expanded an operator; quote it as '*' or escape it as \\*
2087 ───╯"
2088 );
2089 }
2090
2091 #[cfg(unix)]
2092 #[test]
2093 fn test_snippet_points_at_the_dangling_operator() {
2094 let result = new_ucmd!()
2095 .terminal_sim_stderr()
2096 .args(&["6", "*"])
2097 .fails_with_code(2);
2098
2099 assert_eq!(
2100 result.stderr_as_displayed(),
2101 "\
2102 expr: syntax error: missing argument after '*'
2103 ╭─[ expr:1:3 ]
2104 β”‚
2105 1 β”‚ 6 *
2106 β”‚ ─
2107 β”‚
2108 β”‚ Help: every operator needs a value on both sides
2109 ───╯"
2110 );
2111 }
2112
2113 #[cfg(unix)]
2114 #[test]
2115 fn test_snippet_points_at_the_non_integer_operand() {
2116 let result = new_ucmd!()
2117 .terminal_sim_stderr()
2118 .args(&["oops", "+", "4"])
2119 .fails_with_code(2);
2120
2121 assert_eq!(
2122 result.stderr_as_displayed(),
2123 "\
2124 expr: non-integer argument
2125 ╭─[ expr:1:1 ]
2126 β”‚
2127 1 β”‚ oops + 4
2128 β”‚ ────
2129 β”‚
2130 β”‚ Help: arithmetic operators need integers; use = or != to compare strings instead
2131 ───╯"
2132 );
2133 }
2134
2135 #[cfg(unix)]
2136 #[test]
2137 fn test_unclosed_parenthesis_points_at_the_last_operand() {
2138 let result = new_ucmd!()
2139 .terminal_sim_stderr()
2140 .args(&["(", "6", "+", "7"])
2141 .fails_with_code(2);
2142
2143 // No help line here: the label alone says what should follow.
2144 assert_eq!(
2145 result.stderr_as_displayed(),
2146 "\
2147 expr: syntax error: expecting ')' after '7'
2148 ╭─[ expr:1:7 ]
2149 β”‚
2150 1 β”‚ ( 6 + 7
2151 β”‚ ─
2152 ───╯"
2153 );
2154 }
2155
2156 #[cfg(unix)]
2157 #[test]
2158 fn test_errors_without_a_position_stay_plain() {
2159 // Division by zero is raised once the expression is already parsed and
2160 // carries no operand, so there is nothing to point at. The terminal
2161 // turns `\n` into `\r\n`, which is undone before comparing.
2162 let result = new_ucmd!()
2163 .terminal_sim_stderr()
2164 .args(&["6", "/", "0"])
2165 .fails_with_code(2);
2166 assert_eq!(
2167 result.stderr_str().replace("\r\n", "\n"),
2168 "expr: division by zero\n"
2169 );
2170 // An empty expression keeps its usage hint.
2171 new_ucmd!()
2172 .terminal_sim_stderr()
2173 .fails_with_code(2)
2174 .stderr_contains("missing operand")
2175 .stderr_contains("for more information");
2176 }
2177
2178 #[cfg(unix)]
2179 #[test]
2180 fn test_snippet_points_at_the_operand_that_failed_not_its_twin() {
2181 // Both operands read `a`, but only the right-hand one of `+` failed
2182 // arithmetic; the caret must not drift to the first occurrence, a
2183 // perfectly valid string operand of `=`.
2184 let result = new_ucmd!()
2185 .terminal_sim_stderr()
2186 .args(&["a", "=", "2", "+", "a"])
2187 .fails_with_code(2);
2188
2189 assert_eq!(
2190 result.stderr_as_displayed(),
2191 "\
2192 expr: non-integer argument
2193 ╭─[ expr:1:9 ]
2194 β”‚
2195 1 β”‚ a = 2 + a
2196 β”‚ ─
2197 β”‚
2198 β”‚ Help: arithmetic operators need integers; use = or != to compare strings instead
2199 ───╯"
2200 );
2201 }
2202
2203 #[cfg(unix)]
2204 #[test]
2205 fn test_computed_operand_stays_plain() {
2206 // The failing operand is the result of `substr`, not an argument, so
2207 // there is nothing to point at and the plain message is kept.
2208 let result = new_ucmd!()
2209 .terminal_sim_stderr()
2210 .args(&["substr", "abc", "1", "2", "+", "1"])
2211 .fails_with_code(2);
2212 assert_eq!(
2213 result.stderr_str().replace("\r\n", "\n"),
2214 "expr: non-integer argument\n"
2215 );
2216 }
2217
2218 #[test]
2219 fn test_plain_message_is_the_default() {
2220 // The test harness pipes stderr, so the report must not appear.
2221 new_ucmd!()
2222 .args(&["6", "+", "7", "spare"])
2223 .fails_with_code(2)
2224 .stderr_is("expr: syntax error: unexpected argument 'spare'\n");
2225 }
2226 }
2227
2228 #[test]
2229 #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))]
2230 fn test_exit_with_3_write_error() {
2231 let dev_full = std::fs::OpenOptions::new()
2232 .write(true)
2233 .open("/dev/full")
2234 .unwrap();
2235
2236 new_ucmd!()
2237 .arg("2 + 2")
2238 .set_stdout(dev_full)
2239 .fails_with_code(3)
2240 .stderr_is("expr: No space left on device\n");
2241 }