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

     1  #include "tests.h"
     2  #include <stdio.h>
     3  #include <stdlib.h>
     4  
     5  #define START_TEST(t) \
     6      diag(#t);         \
     7      test_##t();
     8  
     9  void test_cast()
    10  {
    11      int c[] = { (int)'a', (int)'b' };
    12      is_eq(c[0], 97);
    13  
    14      double d = (double)1;
    15      is_eq(d, 1.0);
    16  }
    17  
    18  void test_castbool()
    19  {
    20      char i1 = (1 == 1);
    21      short i2 = (1 == 1);
    22      int i3 = (1 == 1);
    23      long i4 = (1 == 0);
    24      long long i5 = (1 == 0);
    25  
    26      is_eq((i1 == 1) && (i2 == 1) && (i3 == 1) && (i4 == 0) && (i5 == 0), 1);
    27  }
    28  
    29  void char_overflow()
    30  {
    31      {
    32          char c;
    33          c = -1;
    34          unsigned char u = c;
    35          is_eq(u, 256 - 1);
    36      }
    37      {
    38          char c = -1;
    39          unsigned char u = c;
    40          is_eq(u, 256 - 1);
    41      }
    42      {
    43          char c = (-1);
    44          unsigned char u = c;
    45          is_eq(u, 256 - 1);
    46      }
    47      {
    48          char c = (((-1)));
    49          unsigned char u = c;
    50          is_eq(u, 256 - 1);
    51      }
    52  }
    53  
    54  typedef double* vertex;
    55  void test_vertex()
    56  {
    57      diag("vertex");
    58  
    59      double a[1];
    60      a[0] = 42;
    61      double b[1];
    62      b[0] = 45;
    63  
    64      double dxoa;
    65      vertex triorg = (vertex)(a);
    66      vertex triapex = (vertex)(b);
    67      dxoa = triorg[0] - triapex[0];
    68  
    69      is_eq(dxoa, -3);
    70  }
    71  
    72  static int strlenChar(const char* z)
    73  {
    74      int n = 0;
    75      while (*z) {
    76          if ((0xc0 & *(z++)) != 0x80)
    77              n++;
    78      }
    79      return n;
    80  }
    81  
    82  void test_strCh()
    83  {
    84      char* z = "Hello, c4go\0";
    85      is_eq(strlenChar(z), 11);
    86  }
    87  
    88  void test_bool_to_int()
    89  {
    90      int d = 3;
    91      int x = (d > 0) * 2 / 2 + (d < 10) * 10 + (d == 4) / 2 * 2 + 0 * (d == d);
    92      is_eq(x, 11);
    93  }
    94  
    95  // TODO:
    96  void test_unsafe_pnt()
    97  {
    98      // (otri).orient = (int) ((unsigned long) (ptr) & (unsigned long) 3l);
    99      // {
   100      // long pnt;
   101      // double d = 42.0;
   102      // double *dd = &d;
   103      // long pnt2 = (long) (dd);
   104      // pnt = pnt2;
   105      // double *ddd = pnt;
   106      // is_eq(*ddd, 42);
   107      // (void)pnt;
   108      // }
   109      // {
   110      // int pnt;
   111      // long l = 42;
   112      // pnt = (int)&l;
   113      // long *l_pnt = (long *)(pnt);
   114      // is_eq(*l_pnt,42);
   115      // }
   116      // {
   117      // int pnt;
   118      // long l = 42;
   119      // pnt = (int) (&l);
   120      // long *l_pnt = (long *)(pnt);
   121      // is_eq(*l_pnt,42);
   122      // }
   123      {
   124          void* pnt;
   125          long l = 42;
   126          long* d = &l;
   127          pnt = d;
   128          long l_pnt = 24;
   129          l_pnt = *((long*)(pnt));
   130          is_eq(l_pnt, 42);
   131          (void)pnt;
   132      }
   133      // {
   134      // int pnt;
   135      // long l = 42;
   136      // long *d = &l;
   137      // pnt = (int)(d);
   138      // long *l_pnt = (long *)(pnt);
   139      // is_eq(*l_pnt,42);
   140      // }
   141  }
   142  
   143  void test_init()
   144  {
   145      char ch[4][10] = { "\"", "\n", "\\", "a" };
   146      is_streq(ch[0], "\"");
   147      is_streq(ch[1], "\n");
   148      is_streq(ch[2], "\\");
   149      is_streq(ch[3], "a");
   150  }
   151  
   152  void test_pntindex()
   153  {
   154      int a[10], i;
   155      for (i = 0; i < 10; i++) {
   156          a[i] = i * 2 - 3;
   157      }
   158      int* q;
   159      int* w;
   160      int* e;
   161      w = &a[5];
   162      e = &a[2];
   163  
   164      q = &a[2];
   165      int* res = q + (w - e);
   166      is_eq(*res, 7);
   167  
   168      q = &a[1];
   169      res = (w - e) + q;
   170      is_eq(*res, 5);
   171  
   172      // TODO
   173      // q = &a[2];
   174      // int res2 = *(q + (w - e));
   175      // is_eq(res2, 7);
   176  
   177      (void)q;
   178      (void)w;
   179      (void)e;
   180  }
   181  
   182  int main()
   183  {
   184      plan(44);
   185  
   186      START_TEST(pntindex);
   187      START_TEST(unsafe_pnt);
   188      START_TEST(bool_to_int);
   189      START_TEST(cast);
   190      START_TEST(castbool);
   191      START_TEST(vertex);
   192      START_TEST(strCh);
   193      START_TEST(init);
   194  
   195      {
   196          typedef unsigned int u32;
   197          u32 x = 42;
   198          is_eq(x, 42);
   199          u32 a[10];
   200          a[0] = x;
   201          is_eq(a[0], 42);
   202      }
   203  
   204      {
   205          typedef double u32d;
   206          u32d x = 42.0;
   207          is_eq(x, 42.0);
   208          u32d a[10];
   209          a[0] = x;
   210          is_eq(a[0], 42.0);
   211      }
   212  
   213      {
   214          typedef int integer;
   215          typedef int INTEGER;
   216          integer i = 123;
   217          INTEGER j = 567;
   218          j = i;
   219          i = j;
   220          is_eq(i, 123);
   221          is_eq(j, 123);
   222      }
   223  
   224      double* d = (double*)0;
   225      is_true(d == NULL);
   226      int* i = (int*)0;
   227      is_true(i == NULL);
   228      float* f = (float*)0;
   229      is_true(f == NULL);
   230      char* c = (char*)0;
   231      is_true(c == NULL);
   232  
   233      double* d2 = 0;
   234      is_true(d2 == NULL);
   235      int* i2 = 0;
   236      is_true(i2 == NULL);
   237      float* f2 = 0;
   238      is_true(f2 == NULL);
   239      char* c2 = 0;
   240      is_true(c2 == NULL);
   241  
   242      diag("Calloc with type");
   243      {
   244          double* ddd = (double*)calloc(2, sizeof(double));
   245          is_not_null(ddd);
   246          (void)(ddd);
   247      }
   248      {
   249          double* ddd;
   250          ddd = (double*)calloc(2, sizeof(double));
   251          is_not_null(ddd);
   252          (void)(ddd);
   253      }
   254  
   255      diag("Type convertion from void* to ...");
   256      {
   257          void* ptr2;
   258          int tInt = 55;
   259          ptr2 = &tInt;
   260          is_eq(*(int*)ptr2, 55);
   261          double tDouble = -13;
   262          ptr2 = &tDouble;
   263          is_eq(*(double*)ptr2, -13);
   264          float tFloat = 67;
   265          is_eq(*(float*)(&tFloat), 67);
   266      }
   267      diag("Type convertion from void* to ... in initialization");
   268      {
   269          long tLong = 556;
   270          void* ptr3 = &tLong;
   271          is_eq(*(long*)ptr3, 556);
   272      }
   273      diag("uint16 to bool");
   274      {
   275          unsigned short d1 = 2;
   276          if (!(!(d1))) {
   277              pass("unsigned short to bool")
   278          }
   279          unsigned int d2 = 2;
   280          if (!(!(d2))) {
   281              pass("unsigned int to bool")
   282          }
   283          unsigned long d3 = 2;
   284          if (!(!(d3))) {
   285              pass("unsigned long to bool")
   286          }
   287      }
   288      diag("char to bool");
   289      {
   290          char c = 2;
   291          if (!(!(c))) {
   292              pass("char to bool")
   293          }
   294      }
   295      diag("long to bool");
   296      {
   297          long c = 2;
   298          if (!(!(c))) {
   299              pass("long to bool")
   300          }
   301      }
   302      diag("equal slice");
   303      {
   304          {
   305              char n[10] = "hey";
   306              char m[10] = "boy";
   307              is_true(n != m);
   308          }
   309          {
   310              char* n = "hey";
   311              char* m = "boy";
   312              is_true(n != m);
   313          }
   314      }
   315  
   316      char_overflow();
   317  
   318      done_testing();
   319  }