github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/if.c (about)

     1  #include "tests.h"
     2  #include <stdio.h>
     3  #include <stdlib.h>
     4  
     5  int d(int v)
     6  {
     7      return 2 * v;
     8  }
     9  
    10  int main()
    11  {
    12      plan(27);
    13  
    14      int x = 1;
    15  
    16      // empty if body
    17      if (x == 0)
    18          ;
    19      else if (x != 0) {
    20          pass("ok: empty if body");
    21      }
    22  
    23      // Without else
    24      if (x == 1)
    25          pass("%s", "x is equal to one");
    26  
    27      if (x == 1) {
    28          pass("%s", "x is equal to one");
    29      }
    30  
    31      // With else
    32      if (x != 1) {
    33          fail("%s", "x is not equal to one");
    34      } else {
    35          pass("%s", "x is equal to one");
    36      }
    37  
    38      if (NULL) {
    39          fail("%s", "NULL is zero");
    40      } else {
    41          pass("%s", "NULL is not zero");
    42      }
    43  
    44      if (!NULL) {
    45          pass("%s", "Invert : ! NULL is zero");
    46      } else {
    47          fail("%s", "Invert : ! NULL is not zero");
    48      }
    49  
    50      diag("Operation inside function");
    51      int ii = 5;
    52      if ((ii = d(ii)) != (-1)) {
    53          is_eq(ii, 10)
    54      }
    55  
    56      diag("if - else");
    57      {
    58          int a = 10;
    59          int b = 5;
    60          int c = 0;
    61          if (a < b) {
    62          } else if (b > c) {
    63              pass("ok");
    64          }
    65      }
    66  
    67      diag("null");
    68      {
    69          char c = 'r';
    70          char* ch = &c;
    71          if (ch != NULL) {
    72              pass("null test 1")
    73          }
    74          if (NULL != ch) {
    75              pass("null test 2")
    76          }
    77          if (NULL != NULL) {
    78              fail("null test 3")
    79          }
    80          ch = NULL;
    81          if (ch == NULL) {
    82              pass("null test 4")
    83          }
    84          if (NULL == ch) {
    85              pass("null test 5")
    86          }
    87          if (NULL == NULL) {
    88              pass("null test 6")
    89          }
    90      }
    91  
    92      diag("Bool to int");
    93      if ((9 != 0) * 2) {
    94          pass("ok");
    95      }
    96  
    97      diag("pointer in if");
    98      {
    99          typedef struct rowA {
   100              unsigned int* p;
   101          } rowB;
   102          rowB vv;
   103          vv.p = NULL;
   104          rowB* r = &vv;
   105          if (r->p == NULL) {
   106              pass("pointer test 1");
   107          }
   108          if (!r->p) {
   109              pass("pointer test 2");
   110          }
   111          if (r->p) {
   112              fail("pointer 3")
   113          }
   114      }
   115  
   116      diag("equal on if paren");
   117      {
   118          int p = 45;
   119          int y = 12;
   120          if (!(!(y = p, y))) {
   121              pass("case 1");
   122          }
   123          if (!(!(y = p))) {
   124              pass("case 2");
   125          }
   126      }
   127  
   128      diag("if equal");
   129      {
   130          int x = 0;
   131          int y = 5;
   132          if ((x = y) == 0) {
   133              fail("if equal");
   134          }
   135          if ((x = 0) == 0) {
   136              is_true(x == 0);
   137          }
   138          int l[5];
   139          for (int i = 0; i < 5; i++) {
   140              l[i] = i;
   141          }
   142          is_true(x == 0);
   143          is_true(y == 5);
   144          int s = 2;
   145          if ((l[0] = y - s) == 3) {
   146              is_true(l[0] == 3);
   147          }
   148          if ((l[1] = l[4] - s) == 2) {
   149              is_true(l[1] == 2);
   150          }
   151      }
   152  
   153      int esct = 0;
   154      int esc = 5;
   155      if (esct += esc) {
   156          pass("ok");
   157      } else {
   158          fail("fail");
   159      }
   160      (void)(esc);
   161      (void)(esct);
   162  
   163      int* y = NULL;
   164      if ((y = (int*)calloc(1, sizeof(int))) != NULL) {
   165          pass("y");
   166      } else {
   167          fail("y");
   168      }
   169  
   170      struct yS {
   171          int* c;
   172      };
   173      struct yS y2;
   174      if ((y2.c = (int*)calloc(1, sizeof(char))) != NULL) {
   175          pass("y2");
   176      } else {
   177          fail("y2");
   178      }
   179  
   180      {
   181          diag("ssize_t to bool");
   182          ssize_t len = 1;
   183          if (len) {
   184              pass("ok");
   185          }
   186      }
   187  
   188      done_testing();
   189  }