Repository: dpretet/svut (language: python)

Issue:
Improve `ASSERT` Macro to Handle Operator Precedence More Intuitively
# Description
The current implementation of the `ASSERT` macro requires extra parentheses to handle operator precedence correctly, such as:
``` system verilog
`ASSERT((a===b));  // Correct
```
Without these parentheses, expressions may lead to incorrect logic due to operator precedence rules.

For example, a developer might mistakenly write:
``` system verilog
`ASSERT(a === b);  // Wrong
```
This could be misinterpreted by the compiler as:
``` system verilog
if (!a === b)  // Wrong behavior
```
Instead of the intended:
``` system verilog
if (!(a === b))  // Correct logic when using parentheses.
```


# Proposed Solution:

https://github.com/dpretet/svut/blob/d2b4d6229049574b8d420164edb757bb41b554e4/svut/svut_h.sv#L159 
Use a pair of parentheses to wrap `exp` such as:
``` system verilog
if (!(exp)) begin
```

# Benefits:

1. Prevents potential logical errors due to misunderstood operator precedence.
1. Improves developer productivity by making the ASSERT macro behavior more intuitive.
1. Reduces debugging time caused by precedence-related bugs.

Thanks for your work on this framework, it definitely helps me a lot.

Best Regards


Discussion / hints:
Hello,

That looks to be definitely a good improvement 😄 It's true I faced sometimes this problem, and adding these extra parentheses makes the code weird to read. I have to do a release with a fix for issue #28, a pull request for that proposal is welcome 🤗 

Thanks for your feedback and using this flow !

Best,
Damien
Sure, I will submit a pull request for this issue in a few days.
Thanks for your reply.

Fix patch:
diff --git a/svut/svut_h.sv b/svut/svut_h.sv
index edef5e5..ec02883 100644
--- a/svut/svut_h.sv
+++ b/svut/svut_h.sv
@@ -129,7 +129,7 @@ endfunction
 `define FAIL_IF_NOT(exp, message="") \
     svut_status = 0; \
     svut_msg = create_msg("FAIL_IF_NOT", message); \
-    if (!exp) begin \
+    if (!(exp)) begin \
         `ERROR(svut_msg); \
         svut_status = 1; \
     end
@@ -156,7 +156,7 @@ endfunction
 `define ASSERT(exp, message="") \
     svut_status = 0; \
     svut_msg = create_msg("ASSERT", message); \
-    if (!exp) begin \
+    if (!(exp)) begin \
         `ERROR(svut_msg); \
         svut_status = 1; \
     end
diff --git a/svut/template.sv b/svut/template.sv
index cc4a961..c17e907 100644
--- a/svut/template.sv
+++ b/svut/template.sv
@@ -51,7 +51,7 @@ ${module_inst}
     //    - `FAIL_IF_EQUAL(aSignal, 23):       Increment error counter if evaluation is equal
     //    - `FAIL_IF_NOT_EQUAL(aSignal, 45):   Increment error counter if evaluation is not equal
     //    - `ASSERT(aSignal):                  Increment error counter if evaluation is not true
-    //    - `ASSERT((aSignal == 0)):           Increment error counter if evaluation is not true
+    //    - `ASSERT(aSignal == 0):           Increment error counter if evaluation is not true
     //
     //  Available flag:
     //


Test patch:
diff --git a/test/Adder_KO_testsuite.sv b/test/Adder_KO_testsuite.sv
index d5c732d..1a167b2 100644
--- a/test/Adder_KO_testsuite.sv
+++ b/test/Adder_KO_testsuite.sv
@@ -95,6 +95,8 @@ module Adder_unit_test_KO;
         `FAIL_IF_NOT_EQUAL(out, 8'd0);
         `INFO("Test FAIL_IF_NOT_EQUAL");
         `FAIL_IF_EQUAL(out, 8'd1);
+        `INFO("Test ASSERT");
+        `ASSERT(inc === 1'b1);
 
         `ERROR("Test finished");
 
diff --git a/test/Adder_OK_testsuite.sv b/test/Adder_OK_testsuite.sv
index 3b8c307..4cc95ae 100644
--- a/test/Adder_OK_testsuite.sv
+++ b/test/Adder_OK_testsuite.sv
@@ -98,6 +98,8 @@ module Adder_unit_test_OK;
         `FAIL_IF_EQUAL(out, 8'd0);
         `INFO("Test FAIL_IF_NOT_EQUAL");
         `FAIL_IF_NOT_EQUAL(out, 8'd1);
+        `INFO("Test ASSERT");
+        `ASSERT(inc === 1'b0);
 
         `SUCCESS("Test finished");
 
diff --git a/test/testsuite_run.sh b/test/testsuite_run.sh
index 54be071..795a848 100644
--- a/test/testsuite_run.sh
+++ b/test/testsuite_run.sh
@@ -20,7 +20,7 @@ test_run_ko_testsuite() { #@test
 test_run_ko_testsuite_error_count() { #@test
 
     run exe_ko_to_log
-    error_num=9
+    error_num=10
     [ $(grep -c "ERROR:" log) -eq "$error_num" ]
 }