From 7421c81a22cebd5ea80d2614766c195431b2089e Mon Sep 17 00:00:00 2001 From: Zhuoxun Yang Date: Tue, 17 Oct 2023 22:21:44 +0800 Subject: [PATCH 1/4] tests/expr: sort test cases --- tests/by-util/test_expr.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index d4cca43a28..840e1f3257 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -152,14 +152,20 @@ fn test_and() { .succeeds() .stdout_only("foo\n"); - new_ucmd!().args(&["", "&", "1"]).run().stdout_is("0\n"); - - new_ucmd!().args(&["14", "&", "1"]).run().stdout_is("14\n"); + new_ucmd!() + .args(&["14", "&", "1"]) + .succeeds() + .stdout_only("14\n"); new_ucmd!() .args(&["-14", "&", "1"]) - .run() - .stdout_is("-14\n"); + .succeeds() + .stdout_only("-14\n"); + + new_ucmd!() + .args(&["-1", "&", "10", "/", "5"]) + .succeeds() + .stdout_only("-1\n"); new_ucmd!() .args(&["0", "&", "a", "/", "5"]) @@ -171,10 +177,7 @@ fn test_and() { .run() .stdout_only("0\n"); - new_ucmd!() - .args(&["-1", "&", "10", "/", "5"]) - .succeeds() - .stdout_only("-1\n"); + new_ucmd!().args(&["", "&", "1"]).run().stdout_only("0\n"); } #[test] From 04ab5b010860e290fa0b23b7a4cca47ce934e996 Mon Sep 17 00:00:00 2001 From: Zhuoxun Yang Date: Tue, 17 Oct 2023 22:26:19 +0800 Subject: [PATCH 2/4] tests/expr: add tests for "" --- tests/by-util/test_expr.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index 840e1f3257..28cfcf0ec9 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -143,6 +143,14 @@ fn test_or() { .args(&["12", "|", "9a", "+", "1"]) .succeeds() .stdout_only("12\n"); + + new_ucmd!().args(&["", "|", ""]).run().stdout_only("0\n"); + + new_ucmd!().args(&["", "|", "0"]).run().stdout_only("0\n"); + + new_ucmd!().args(&["", "|", "00"]).run().stdout_only("0\n"); + + new_ucmd!().args(&["", "|", "-0"]).run().stdout_only("0\n"); } #[test] @@ -178,6 +186,8 @@ fn test_and() { .stdout_only("0\n"); new_ucmd!().args(&["", "&", "1"]).run().stdout_only("0\n"); + + new_ucmd!().args(&["", "&", ""]).run().stdout_only("0\n"); } #[test] From 8a6722491746ff7ec805ea058a30de1236836754 Mon Sep 17 00:00:00 2001 From: Zhuoxun Yang Date: Tue, 17 Oct 2023 22:27:10 +0800 Subject: [PATCH 3/4] expr: add assert for `&` --- src/uu/expr/src/syntax_tree.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/uu/expr/src/syntax_tree.rs b/src/uu/expr/src/syntax_tree.rs index e0e786b3a3..2cd2af0b12 100644 --- a/src/uu/expr/src/syntax_tree.rs +++ b/src/uu/expr/src/syntax_tree.rs @@ -470,6 +470,7 @@ fn infix_operator_or(values: &[String]) -> String { } fn infix_operator_and(values: &[String]) -> String { + assert!(values.len() == 2); if value_as_bool(&values[0]) && value_as_bool(&values[1]) { values[0].clone() } else { From d325a952ee1133eaa76cc3d1196a9364c2a539dc Mon Sep 17 00:00:00 2001 From: Zhuoxun Yang Date: Tue, 17 Oct 2023 22:27:47 +0800 Subject: [PATCH 4/4] expr: return "0" for `|` --- src/uu/expr/src/syntax_tree.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/uu/expr/src/syntax_tree.rs b/src/uu/expr/src/syntax_tree.rs index 2cd2af0b12..c55fb0bdc6 100644 --- a/src/uu/expr/src/syntax_tree.rs +++ b/src/uu/expr/src/syntax_tree.rs @@ -464,8 +464,10 @@ fn infix_operator_or(values: &[String]) -> String { assert!(values.len() == 2); if value_as_bool(&values[0]) { values[0].clone() - } else { + } else if value_as_bool(&values[1]) { values[1].clone() + } else { + 0.to_string() } }