[TCT][packaging/scripts/src/templates][NonACR][apply do-while for the assert macros]
authortaeyoung <ty317.kim@samsung.com>
Mon, 19 Sep 2016 04:38:12 +0000 (13:38 +0900)
committertaeyoung <ty317.kim@samsung.com>
Mon, 19 Sep 2016 04:49:28 +0000 (13:49 +0900)
The macros just have "if" statements. Thus the macros
can occur incorrect operation. The following is the example.
The "else" cannot operate properly since there are two "if"
in front of the "else". Thus do-while needs to be added.

   if (condition)
      assert_eq(ret, 0);
   else
      assert_eq(ret, 1);

===>>

   if (condition)
      if (ret != 0) {
         do something;
      }
   else
      if (ret != 1) {
         do something;
      }

Change-Id: I435f15f18aa1e01ad5ddce314dcbda8e0b9fbc0b
Signed-off-by: taeyoung <ty317.kim@samsung.com>
src/common/assert.h

index 3a9603f5b8b739104dbfef81667f23243e2824cd..25621ff3c30cc1e1dbcd5e5b468706c0910bc7d9 100755 (executable)
 #include <stdlib.h>
 
 #define assert(exp) \
-    if (!(exp)) { \
-        fprintf(stderr, \
-            "\\n[TCT][%s][Line : %d] Assert Fail; Following expression is not true: %s\\n", __FILE__, __LINE__, #exp); \
-        return 1; \
-    }
+    do { \
+        if (!(exp)) { \
+            fprintf(stderr, \
+                "\\n[TCT][%s][Line : %d] Assert Fail; Following expression is not true: %s\\n", \
+                __FILE__, __LINE__, #exp); \
+            return 1; \
+        } \
+    } while (0);
 
 #define assert_eq(var, ref) \
-    if (var != ref) { \
-        fprintf(stderr, \
-            "\\n[TCT][%s][Line : %d] Assert fail; Values (%s == 0x%x) and (%s == 0x%x) are not equal\\n", __FILE__, __LINE__, #var, (int)var, #ref, (int)ref); \
-        return 1; \
-    }
+    do { \
+        if (var != ref) { \
+            fprintf(stderr, \
+                "\\n[TCT][%s][Line : %d] Assert fail; Values (%s == 0x%x) and (%s == 0x%x) are not equal\\n", \
+                __FILE__, __LINE__, #var, (int)var, #ref, (int)ref); \
+            return 1; \
+        } \
+    } while (0);
 
 #define assert_neq(var, ref) \
-   if (var == ref) { \
-        fprintf(stderr, \
-            "\\n[TCT][%s][Line : %d] Assert fail; Values (%s == 0x%x) and (%s == 0x%x) are equal\\n", __FILE__, __LINE__,  #var, (int)var, #ref, (int)ref); \
-        return 1; \
-    }
+    do { \
+        if (var == ref) { \
+            fprintf(stderr, \
+                "\\n[TCT][%s][Line : %d] Assert fail; Values (%s == 0x%x) and (%s == 0x%x) are equal\\n", \
+                __FILE__, __LINE__,  #var, (int)var, #ref, (int)ref); \
+            return 1; \
+        } \
+    } while (0);
 
 #define assert_gt(var, ref) \
-    if (var <= ref) { \
-        fprintf(stderr, \
-            "\\n[TCT][%s][Line : %d] Assert fail; Values (%s == 0x%x) is not greater than (%s == 0x%x)\\n", __FILE__, __LINE__,  #var, (int)var, #ref, (int)ref); \
-        return 1; \
-    }
+    do { \
+        if (var <= ref) { \
+            fprintf(stderr, \
+                "\\n[TCT][%s][Line : %d] Assert fail; Values (%s == 0x%x) is not greater than (%s == 0x%x)\\n", \
+            __FILE__, __LINE__,  #var, (int)var, #ref, (int)ref); \
+            return 1; \
+        } \
+    } while (0);
 
 #define assert_geq(var, ref) \
-    if (var < ref) { \
-        fprintf(stderr, \
-            "\\n[TCT][%s][Line : %d] Assert fail; Values (%s == 0x%x) is not greater than or equal to (%s == 0x%x)\\n", __FILE__, __LINE__,  #var, (int)var, #ref, (int)ref); \
-        return 1; \
-    }
+    do { \
+        if (var < ref) { \
+            fprintf(stderr, \
+                "\\n[TCT][%s][Line : %d] Assert fail; Values (%s == 0x%x) is not greater than or equal to (%s == 0x%x)\\n", \
+                __FILE__, __LINE__,  #var, (int)var, #ref, (int)ref); \
+            return 1; \
+        } \
+    } while (0);
 
 #define assert_lt(var, ref) \
-    if (var >= ref) { \
-        fprintf(stderr, \
-            "\\n[TCT][%s][Line : %d] Assert fail; Values (%s == 0x%x) is not lower than (%s == 0x%x)\\n", __FILE__, __LINE__,  #var, (int)var, #ref, (int)ref); \
-        return 1; \
-    }
+    do { \
+        if (var >= ref) { \
+            fprintf(stderr, \
+                "\\n[TCT][%s][Line : %d] Assert fail; Values (%s == 0x%x) is not lower than (%s == 0x%x)\\n", \
+                __FILE__, __LINE__,  #var, (int)var, #ref, (int)ref); \
+            return 1; \
+        } \
+    } while (0);
 
 #define assert_leq(var, ref) \
-    if (var > ref) { \
-        fprintf(stderr, \
-            "\\n[TCT][%s][Line : %d] Assert fail; Values (%s == 0x%x) is not lower than or equal to (%s == 0x%x)\\n", __FILE__, __LINE__,  #var, (int)var, #ref, (int)ref); \
-        return 1; \
-    }
+    do { \
+        if (var > ref) { \
+            fprintf(stderr, \
+                "\\n[TCT][%s][Line : %d] Assert fail; Values (%s == 0x%x) is not lower than or equal to (%s == 0x%x)\\n", \
+            __FILE__, __LINE__,  #var, (int)var, #ref, (int)ref); \
+            return 1; \
+        } \
+    } while (0);
 
 
 #endif //  _ASSERT_H_