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

     1  #include "tests.h"
     2  #include <stdio.h>
     3  
     4  #define START_TEST(t) \
     5      diag(#t);         \
     6      test_##t();
     7  
     8  void print_bool(int tr, int fl)
     9  {
    10      if (tr) {
    11          printf("print_bool tr is true\n");
    12      } else {
    13          printf("print_bool tr is false\n");
    14      }
    15      if (fl) {
    16          printf("print_bool fl is true\n");
    17      } else {
    18          printf("print_bool fl is false\n");
    19      }
    20      if (tr != 0 && fl != 0) {
    21          pass("test != 0");
    22      }
    23      if (tr == 0 && fl == 0) {
    24          pass("test == 0");
    25      }
    26  }
    27  
    28  void ok()
    29  {
    30      pass("success");
    31  }
    32  
    33  void f()
    34  {
    35      fail("need fix");
    36  }
    37  
    38  #define not_c_type(type)                                      \
    39      diag("------------------");                               \
    40      {                                                         \
    41          diag(#type);                                          \
    42          type a;                                               \
    43          int p;                                                \
    44          diag("not for C type : zero value");                  \
    45          a = 0;                                                \
    46          if (!a) {                                             \
    47              printf("a1\n");                                   \
    48              ok();                                             \
    49          } else {                                              \
    50              printf("a2\n");                                   \
    51              f();                                              \
    52          }                                                     \
    53          diag("not-not for C type : zero value");              \
    54          a = 0;                                                \
    55          if (!(!a)) {                                          \
    56              printf("a3\n");                                   \
    57              f();                                              \
    58          } else {                                              \
    59              printf("a4\n");                                   \
    60              ok();                                             \
    61          }                                                     \
    62          diag("for C type : function");                        \
    63          print_bool(!a, !(!a));                                \
    64          diag("for C type : assign");                          \
    65          p = !a;                                               \
    66          if (p) {                                              \
    67              printf("p1\n");                                   \
    68          } else {                                              \
    69              printf("p2\n");                                   \
    70          }                                                     \
    71          diag("not for C type : non-zero positive value");     \
    72          a = 42;                                               \
    73          if (!a) {                                             \
    74              printf("a5\n");                                   \
    75          } else {                                              \
    76              printf("a6\n");                                   \
    77          }                                                     \
    78          diag("for C type : function");                        \
    79          print_bool(!a, !(!a));                                \
    80          diag("for C type : assign");                          \
    81          p = !a;                                               \
    82          if (p) {                                              \
    83              printf("p1\n");                                   \
    84          } else {                                              \
    85              printf("p2\n");                                   \
    86          }                                                     \
    87          diag("not-not for C type : non-zero positive value"); \
    88          a = 42;                                               \
    89          if (!(!a)) {                                          \
    90              printf("a7\n");                                   \
    91          } else {                                              \
    92              printf("a8\n");                                   \
    93          }                                                     \
    94          diag("not for C type : non-zero negative value");     \
    95          a = -42;                                              \
    96          if (!a) {                                             \
    97              printf("a5\n");                                   \
    98          } else {                                              \
    99              printf("a6\n");                                   \
   100          }                                                     \
   101          diag("not-not for C type : non-zero negative value"); \
   102          a = -42;                                              \
   103          if (!(!a)) {                                          \
   104              printf("a7\n");                                   \
   105          } else {                                              \
   106              printf("a8\n");                                   \
   107          }                                                     \
   108          diag("for C type : function");                        \
   109          print_bool(!a, !(!a));                                \
   110          diag("for C type : assign");                          \
   111          p = !a;                                               \
   112          if (p) {                                              \
   113              printf("p1\n");                                   \
   114          } else {                                              \
   115              printf("p2\n");                                   \
   116          }                                                     \
   117          diag("for C type : zero value");                      \
   118          a = 0;                                                \
   119          if (a) {                                              \
   120              printf("a9\n");                                   \
   121          } else {                                              \
   122              printf("a10\n");                                  \
   123          }                                                     \
   124          diag("for C type : positive value");                  \
   125          a = 15;                                               \
   126          if (a) {                                              \
   127              printf("a11\n");                                  \
   128          } else {                                              \
   129              printf("a12\n");                                  \
   130          }                                                     \
   131          diag("for C type : negative value");                  \
   132          a = -15;                                              \
   133          if (a) {                                              \
   134              printf("a13\n");                                  \
   135          } else {                                              \
   136              printf("a14\n");                                  \
   137          }                                                     \
   138          diag("for C type : assign");                          \
   139          p = !a;                                               \
   140          if (p) {                                              \
   141              printf("p1\n");                                   \
   142          } else {                                              \
   143              printf("p2\n");                                   \
   144          }                                                     \
   145      }
   146  
   147  void test_c_types()
   148  {
   149      not_c_type(char);
   150      not_c_type(double);
   151      not_c_type(float);
   152      not_c_type(short);
   153      not_c_type(int);
   154      not_c_type(long);
   155      not_c_type(long double);
   156      not_c_type(long long);
   157      not_c_type(signed char);
   158  }
   159  
   160  #define not_c_pointer(type)                  \
   161      diag("------------------");              \
   162      {                                        \
   163          diag(#type);                         \
   164          type* a = NULL;                      \
   165          int p;                               \
   166          diag("for C pointer:  null");        \
   167          if (a) {                             \
   168              printf("a1\n");                  \
   169          } else {                             \
   170              printf("a2\n");                  \
   171          }                                    \
   172          diag("for C pointer: not null");     \
   173          if (!a) {                            \
   174              printf("a2\n");                  \
   175          } else {                             \
   176              printf("a3\n");                  \
   177          }                                    \
   178          diag("for C pointer: not-not null"); \
   179          if (!(!a)) {                         \
   180              printf("a4\n");                  \
   181          } else {                             \
   182              printf("a5\n");                  \
   183          }                                    \
   184          diag("for C type : function");       \
   185          print_bool(!a, !(!a));               \
   186          p = !a;                              \
   187          if (p) {                             \
   188              printf("p1\n");                  \
   189          } else {                             \
   190              printf("p2\n");                  \
   191          }                                    \
   192          type b = 42;                         \
   193          a = &b;                              \
   194          diag("for C pointer:  null");        \
   195          if (a) {                             \
   196              printf("a11\n");                 \
   197          } else {                             \
   198              printf("a12\n");                 \
   199          }                                    \
   200          diag("for C pointer: not null");     \
   201          if (!a) {                            \
   202              printf("a12\n");                 \
   203          } else {                             \
   204              printf("a13\n");                 \
   205          }                                    \
   206          diag("for C pointer: not-not null"); \
   207          if (!(!a)) {                         \
   208              printf("a14\n");                 \
   209          } else {                             \
   210              printf("a15\n");                 \
   211          }                                    \
   212          diag("for C type : function");       \
   213          print_bool(!a, !(!a));               \
   214          p = !a;                              \
   215          if (p) {                             \
   216              printf("p1\n");                  \
   217          } else {                             \
   218              printf("p2\n");                  \
   219          }                                    \
   220      }
   221  
   222  #define not_c_struct(type)                   \
   223      diag("------------------");              \
   224      {                                        \
   225          diag(#type);                         \
   226          type* a = NULL;                      \
   227          int p;                               \
   228          diag("for C pointer:  null");        \
   229          if (a) {                             \
   230              printf("a1\n");                  \
   231          } else {                             \
   232              printf("a2\n");                  \
   233          }                                    \
   234          diag("for C pointer: not null");     \
   235          if (!a) {                            \
   236              printf("a2\n");                  \
   237          } else {                             \
   238              printf("a3\n");                  \
   239          }                                    \
   240          diag("for C pointer: not-not null"); \
   241          if (!(!a)) {                         \
   242              printf("a4\n");                  \
   243          } else {                             \
   244              printf("a5\n");                  \
   245          }                                    \
   246          diag("for C type : function");       \
   247          print_bool(!a, !(!a));               \
   248          p = !a;                              \
   249          if (p) {                             \
   250              printf("p1\n");                  \
   251          } else {                             \
   252              printf("p2\n");                  \
   253          }                                    \
   254          type b;                              \
   255          a = &b;                              \
   256          diag("for C pointer:  null");        \
   257          if (a) {                             \
   258              printf("a11\n");                 \
   259          } else {                             \
   260              printf("a12\n");                 \
   261          }                                    \
   262          diag("for C pointer: not null");     \
   263          if (!a) {                            \
   264              printf("a12\n");                 \
   265          } else {                             \
   266              printf("a13\n");                 \
   267          }                                    \
   268          diag("for C pointer: not-not null"); \
   269          if (!(!a)) {                         \
   270              printf("a14\n");                 \
   271          } else {                             \
   272              printf("a15\n");                 \
   273          }                                    \
   274          diag("for C type : function");       \
   275          print_bool(!a, !(!a));               \
   276          p = !a;                              \
   277          if (p) {                             \
   278              printf("p1\n");                  \
   279          } else {                             \
   280              printf("p2\n");                  \
   281          }                                    \
   282      }
   283  
   284  void test_c_pointers()
   285  {
   286      not_c_pointer(char);
   287      not_c_pointer(double);
   288      not_c_pointer(float);
   289      not_c_pointer(int);
   290      not_c_pointer(long double);
   291      not_c_pointer(long long);
   292      not_c_pointer(signed char);
   293      not_c_pointer(unsigned long);
   294  }
   295  
   296  struct str {
   297      int i;
   298  };
   299  union un {
   300      int i;
   301      double d;
   302  };
   303  
   304  void test_c_struct()
   305  {
   306      not_c_struct(struct str);
   307      not_c_struct(union un);
   308  }
   309  
   310  void test_c_function()
   311  {
   312      void (*a)(void);
   313      a = NULL;
   314      int p;
   315      diag("for C pointer:  null");
   316      if (a) {
   317          printf("a1\n");
   318      } else {
   319          printf("a2\n");
   320      }
   321      diag("for C pointer: not null");
   322      if (!a) {
   323          printf("a2\n");
   324      } else {
   325          printf("a3\n");
   326      }
   327      diag("for C pointer: not-not null");
   328      if (!(!a)) {
   329          printf("a4\n");
   330      } else {
   331          printf("a5\n");
   332      }
   333      diag("for C type : function");
   334      print_bool(!a, !(!a));
   335      p = !a;
   336      if (p) {
   337          printf("p1\n");
   338      } else {
   339          printf("p2\n");
   340      }
   341      a = test_c_pointers;
   342      diag("for C pointer:  null");
   343      if (a) {
   344          printf("a11\n");
   345      } else {
   346          printf("a12\n");
   347      }
   348      diag("for C pointer: not null");
   349      if (!a) {
   350          printf("a12\n");
   351      } else {
   352          printf("a13\n");
   353      }
   354      diag("for C pointer: not-not null");
   355      if (!(!a)) {
   356          printf("a14\n");
   357      } else {
   358          printf("a15\n");
   359      }
   360      diag("for C type : function");
   361      print_bool(!a, !(!a));
   362      p = !a;
   363      if (p) {
   364          printf("p1\n");
   365      } else {
   366          printf("p2\n");
   367      }
   368  }
   369  
   370  int main()
   371  {
   372      plan(18);
   373  
   374      START_TEST(c_types);
   375      START_TEST(c_pointers);
   376      START_TEST(c_struct);
   377      START_TEST(c_function);
   378  
   379      done_testing();
   380  }