Attention une mise à jour du service Gitlab va être effectuée le mardi 30 novembre entre 17h30 et 18h00. Cette mise à jour va générer une interruption du service dont nous ne maîtrisons pas complètement la durée mais qui ne devrait pas excéder quelques minutes. Cette mise à jour intermédiaire en version 14.0.12 nous permettra de rapidement pouvoir mettre à votre disposition une version plus récente.

test-p99-c11.c 20.3 KB
Newer Older
(no author)'s avatar
(no author) committed
1 2 3 4
/* This may look like nonsense, but it really is -*- mode: C -*-              */
/*                                                                            */
/* Except for parts copied from previous work and as explicitly stated below, */
/* the author and copyright holder for this work is                           */
5
/* all rights reserved,  2011-2013 Jens Gustedt, INRIA, France                */
(no author)'s avatar
(no author) committed
6 7 8 9 10 11 12
/*                                                                            */
/* This file is free software; it is part of the P99 project.                 */
/* You can redistribute it and/or modify it under the terms of the QPL as     */
/* given in the file LICENSE. It is distributed without any warranty;         */
/* without even the implied warranty of merchantability or fitness for a      */
/* particular purpose.                                                        */
/*                                                                            */
13
#include "p99_threads.h"
14 15
#include <stdbool.h>
#include <stdlib.h>
Jens Gustedt's avatar
Jens Gustedt committed
16
#include "p99_compiler.h"
17
#include "p99_generic.h"
18
#include "p99_atomic.h"
19
#include "p99_clib.h"
Jens Gustedt's avatar
Jens Gustedt committed
20 21 22 23

static_assert(true, "everything is alright");
// static_assert(false, "all goes wrong");

24 25
//_Alignas(double)
int a;
26 27 28 29 30

_Alignas(16) int b;

alignas(16) int c;

31 32
//alignas(double)
int d;
33 34

alignas(alignof(double)) int e;
Jens Gustedt's avatar
Jens Gustedt committed
35

36 37 38 39 40 41
// three tentative definitions of the same variable
unsigned char f[_Alignof(double)];
unsigned char f[__alignof(double)];
unsigned char f[__alignof__(double)];

int * g = P99_GENERIC(a, (void*)&d, (int, &a), (unsigned int, &a));
42
//int * h = _Generic(a, default: (void*)&d, int: &a, unsigned int: &a);
43

44 45 46 47
#define TYPESTR(T) (T, #T)

#define SIGNEDTYPE_(T, X) (unsigned T, (signed T)(X))

(no author)'s avatar
(no author) committed
48 49 50 51 52 53 54
#define SSIZE_T(X)                                             \
P99_GENERIC(sizeof(1),                                         \
            ,                                                  \
            SIGNEDTYPE_(char, X),                              \
            SIGNEDTYPE_(short, X),                             \
            SIGNEDTYPE_(int, X),                               \
            SIGNEDTYPE_(long, X),                              \
55 56
            SIGNEDTYPE_(long long, X))

57 58 59
float funcf(float a, float b) {
  return a;
}
60
void funcf_2nd_arg_error(float);
61 62 63
double funcd(double a, double b) {
  return a;
}
64 65
void funcd_2nd_arg_error(double);

66 67
#define check(x, y)                                                  \
  (*(_Generic((x), float:  (float*){ 0 }, double: (double*){ 0 })    \
68 69
     = &_Generic((y), float:  (float){ y }, double: (double){ y })))

70 71 72 73
#define func(x, y)                                                            \
  _Generic((x),                                                               \
           float:  _Generic((y), float:  funcf, double: funcf_2nd_arg_error), \
           double: _Generic((y), double: funcd, float:  funcd_2nd_arg_error)  \
74 75
           )(x, check(x, y))

76 77
#define check2(x, y)                                                          \
  (*(P99_GENERIC((x), , (float,  (float*){ 0 }), (double, (double*){ 0 }))    \
78 79
     = &P99_GENERIC((y), , (float,  (float){ y }), (double, (double){ y }))))

80 81
#define func2(x, y)                                                                         \
  P99_GENERIC((x), ,                                                                        \
82
              (float,  P99_GENERIC((y), , (float,  funcf), (double, funcf_2nd_arg_error))), \
83
              (double, P99_GENERIC((y), , (double, funcd), (float,  funcd_2nd_arg_error)))  \
84 85
              )(x, check2(x, y))

Jens Gustedt's avatar
Jens Gustedt committed
86 87 88 89 90 91 92
noreturn
void stop(void) {
  abort();
}

thread_local int ret = EXIT_SUCCESS;

93 94 95 96 97
#define print_feature(X) if (p99_has_feature(X)) printf("has feature %s\n", #X)
#define print_attribute(X) if (p99_has_attribute(X)) printf("has attribute %s\n", #X)
#define print_extension(X) if (p99_has_extension(X)) printf("has extension %s\n", #X)
#define print_builtin(X) if (p99_has_builtin(X)) printf("has builtin %s\n", #X)

98 99 100
unsigned globA = P99_GENERIC(globA, ,(unsigned, 1));   // should work and not give diagnostic
//unsigned globB = P99_GENERIC(globB, ,(signed, 1));     // shouldn't work and give a diagnostic

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#define LOCALEPRINTSTR(NAME)                    \
do {                                            \
  if (l->NAME && l->NAME[0])                    \
    printf("locale " #NAME ":\t%s\n",           \
           l->NAME);                            \
 } while (0)

#define LOCALEPRINTGRP(NAME)                    \
  do {                                          \
    if (l->NAME && l->NAME[0]) {                \
      fputs("locale " #NAME ":\t", stdout);     \
      for (unsigned i=0; l->NAME[i]; ++i)       \
        printf("\t%d", (int)l->NAME[i]);        \
      fputs("\t0\n", stdout);                   \
    }                                           \
  } while (0)

#define LOCALEPRINTCHAR(NAME)                   \
do {                                            \
  if (l->NAME != CHAR_MAX)                      \
    printf("locale " #NAME ":\t%d\n",           \
           (int)l->NAME);                       \
 } while (0)

void localeprint(void) {
  struct lconv* l = localeconv();
  P99_SEP(LOCALEPRINTSTR,
          decimal_point,
          thousands_sep,
          mon_decimal_point,
          mon_thousands_sep,
          positive_sign,
          negative_sign,
          currency_symbol,
          int_curr_symbol);
  P99_SEP(LOCALEPRINTGRP,
          grouping,
          mon_grouping);
  P99_SEP(LOCALEPRINTCHAR,
          frac_digits,
          p_cs_precedes,
          n_cs_precedes,
          p_sep_by_space,
          n_sep_by_space,
          p_sign_posn,
          n_sign_posn,
          int_frac_digits,
          int_p_cs_precedes,
          int_n_cs_precedes,
          int_p_sep_by_space,
          int_n_sep_by_space,
          int_p_sign_posn,
          int_n_sign_posn);
}

156
int main(int argc, char* argv[]) {
157 158 159 160 161 162 163 164 165
  char const*loc = setlocale(LC_ALL, 0);
  printf("initial locale:\t%s%s\n",
         loc,
         (loc && !strcmp(loc, "C")) ? "" : " (should have been \"C\")");
  localeprint();
  loc = setlocale(LC_ALL, "");
  loc = setlocale(LC_ALL, 0);
  printf("locale now:\t%s\n", loc);
  localeprint();
166 167 168
  struct timespec now_timespec = { 0 };
  timespec_get(&now_timespec, TIME_UTC);
  double now_exact = now_timespec.tv_sec + now_timespec.tv_nsec * 1E-9;
169
  time_t const now = time(0);
Jens Gustedt's avatar
Jens Gustedt committed
170
  time_t const epoch = { 0 };
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  timespec_get(&now_timespec, TIME_UTC);
  now_exact += now_timespec.tv_sec + now_timespec.tv_nsec * 1E-9;
  now_exact /= 2.0;
  double time_grain = difftime(now+1, now);
  printf("time_t, size is %zu bit (%s), granularity is %g seconds\n",
         sizeof(time_t)*CHAR_BIT,
         P99_GENERIC((time_t)0, "<unknown>",
                     (double, "double"),
                     (float, "float"),
                     (int, "int"),
                     (unsigned, "unsigned"),
                     (long, "long"),
                     (unsigned long, "unsigned long"),
                     (long long, "long"),
                     (unsigned long long, "unsigned long long")),
         time_grain);
  double now_loose = difftime(now, epoch);

  if ((now_exact > now_loose) && ((now_exact - now_loose) < 1.0))
    printf("time_t and timespec seem to use the same epoch\n");
  else {
    printf("time_t is at %f, timespec at %f, difference %f\n",
           now_loose, now_exact,
           now_exact - now_loose);
  }

  time_t inv_min = 0;
  time_t inv_max = now;
  struct tm * inv_time = gmtime(&(time_t const){ -1, });
  if (inv_time) {
    printf("(time_t)-1 is a valid time corresponding to %s", asctime(inv_time));
    inv_time = gmtime(&(time_t const){ INT_MIN, });
    if (inv_time) {
      inv_time = gmtime(&(time_t const){ LONG_MIN, });
      if (!inv_time) {
        inv_min = LONG_MIN;
      }
    } else {
      inv_min = INT_MIN;
    }
  } else {
    inv_min = -1;
  }
  inv_time = gmtime(&(time_t const){ INT_MAX, });
  if (inv_time) {
    struct tm * inv_time = gmtime(&(time_t const){ LONG_MAX, });
    if (!inv_time) {
      inv_max = LONG_MAX;
    }
  } else {
    inv_max = INT_MAX;
  }

  time_t val_min = 0;
  time_t val_max = now;

  while (inv_min+1 < val_min) {
    time_t t3 = (inv_min + val_min)/2;
    inv_time = gmtime(&t3);
    if (inv_time) val_min = t3;
    else inv_min = t3;
  }
  inv_time = gmtime(&val_min);
  if (inv_time) {
    char const * asc = asctime(inv_time);
    if (asc) P99_PRINTF("smallest valid time_t %s, corresponds to %s", val_min, asc);
    else P99_PRINTF("smallest valid time_t %s, is not printable, year is %s\n", val_min, inv_time->tm_year+1900L);
    asc = asctime(&(struct tm){ .tm_year = -900, .tm_mday = 1});
    if (asc) printf("smallest valid struct tm for asctime, corresponds to %s", asc);
    else printf("smallest struct tm for asctime is not printable\n");
    char tc[26];
    if (!asctime_s(tc, sizeof tc, &(struct tm){ .tm_year = -1900, .tm_mday = 1}))
      printf("smallest valid struct tm for asctime_s, corresponds to %s", tc);
    else printf("smallest struct tm for asctime_s is not printable\n");
  }


  while (inv_max > val_max+1) {
    time_t t3 = (inv_max/2 + val_max/2);
    inv_time = gmtime(&t3);
    if (inv_time) val_max = t3;
    else inv_max = t3;
  }
  inv_time = gmtime(&val_max);
  if (inv_time) {
    char const * asc = asctime(inv_time);
    if (asc) P99_PRINTF("largest valid time_t %s, corresponds to %s", val_max, asc);
    else P99_PRINTF("largest valid time_t %s, is not printable, year is %s\n", val_max, inv_time->tm_year+1900L);
    asc = asctime(&(struct tm){ .tm_year = 9999-1900, .tm_mon = 11, .tm_mday = 31,
          .tm_hour = 23, .tm_min = 59, .tm_sec = 59, });
    if (asc) printf("largest valid struct tm, corresponds to %s", asc);
    else printf("largest valid struct tm is not printable\n");
  }

Jens Gustedt's avatar
Jens Gustedt committed
265
  char * date_c = ctime(&now);
Jens Gustedt's avatar
Jens Gustedt committed
266
  char date[26];
Jens Gustedt's avatar
Jens Gustedt committed
267
  ctime_s(date, sizeof date, &now);
268 269 270
  if (!strcmp(date, date_c)) printf("current date:\t%s", date);
  else printf("warning:localtime and localtime_s are not equal\n\t%s\t%s", date_c, date);

Jens Gustedt's avatar
Jens Gustedt committed
271 272
  date_c = ctime(&epoch);
  ctime_s(date, sizeof date, &epoch);
273 274 275
  if (!strcmp(date, date_c)) printf("epoch local:\t%s", date);
  else printf("warning:local epoch are not equal\n\t%s\t%s", date_c, date);

276
  struct tm tm = *gmtime(&epoch);
277 278 279 280 281 282 283 284 285 286 287 288 289 290
  date_c = asctime(&tm);
  printf("epoch UTC:\t%s", date_c);
  tm = *gmtime(&now);
  date_c = asctime(&tm);
  gmtime_s(&now, &tm);
  asctime_s(date, sizeof date, &tm);
  if (!strcmp(date, date_c)) printf("UTC date:\t%s", date);
  else printf("warning:gmtime and gmtime_s are not equal\n\t%s\t%s", date_c, date);

  tm.tm_mday = 29;
  tm.tm_mon = 1;
  tm.tm_year = 100;
  mktime(&tm);
  asctime_s(date, sizeof date, &tm);
Jens Gustedt's avatar
Jens Gustedt committed
291
  printf("leap year:\t%s", date);
292 293 294
  tm.tm_year = 0;
  mktime(&tm);
  asctime_s(date, sizeof date, &tm);
Jens Gustedt's avatar
Jens Gustedt committed
295
  printf("no leap year:\t%s", date);
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

  localtime_s(&now, &tm);
  tm.tm_mon = 0;
  tm.tm_mday = 1;
  tm.tm_isdst = -1;
  time_t jan1 = mktime(&tm);
  if (tm.tm_isdst > 0) {
    tm.tm_mon = 0;
    tm.tm_mday = 1;
    tm.tm_isdst = 0;
    time_t jan1_dst = mktime(&tm);
    printf("DST difference on Jan 1, %u: %g minutes\n", (tm.tm_year + 1900), difftime(jan1_dst, jan1)/60.0);
  }
  tm.tm_mon = 6;
  tm.tm_mday = 1;
  tm.tm_isdst = -1;
  time_t jul1 = mktime(&tm);
  if (tm.tm_isdst > 0) {
    tm.tm_mon = 6;
    tm.tm_mday = 1;
    tm.tm_isdst = 0;
    time_t jul1_dst = mktime(&tm);
    printf("DST difference on Jul 1, %u: %g minutes\n", (tm.tm_year + 1900), difftime(jul1_dst, jul1)/60.0);
  }
320 321 322 323 324 325 326 327
  tm = *localtime(&now);
  char wide[256];
  strftime(wide, sizeof wide, "%c", &tm);
  printf("locale time (%s):\t%s\n", setlocale(LC_TIME, 0), wide);
  strftime(wide, sizeof wide, "%Ec", &tm);
  printf("alternative time (%s):\t%s\n", setlocale(LC_TIME, 0), wide);
  strerror_s(wide, sizeof wide, EDOM);
  printf("some locale error:\t%s\n", wide);
328

329 330 331 332 333 334 335 336 337 338 339
  print_attribute(deprecated);
  print_attribute(weak);
  print_attribute(weakref);
  print_extension(c_generic_selections);
  print_feature(c_alignas);
  print_feature(c_alignof);
  print_feature(c_inline);
  print_feature(c_noreturn);
  print_feature(gnu_alignof);
  print_feature(gnu_thread_local);
  print_feature(statement_expression);
340 341 342 343 344 345 346 347 348 349 350 351 352 353
  //print_feature(__typeof);
  print_feature(topeof);
  print_builtin(__sync_val_compare_and_swap);
  printf("list of default unsigned types is " P99_STRINGIFY(P00_UINT_TYPE_LIST) "\n");
  printf("list of atomic lock-free types is " P99_STRINGIFY(P99_ATOMIC_LOCK_FREE_TYPES) "\n");
  printf("atomic lock-free macros: %d %d %d %d %d %d %d\n",
         ATOMIC_BOOL_LOCK_FREE,
         ATOMIC_CHAR_LOCK_FREE,
         ATOMIC_SHORT_LOCK_FREE,
         ATOMIC_INT_LOCK_FREE,
         ATOMIC_LONG_LOCK_FREE,
         ATOMIC_LLONG_LOCK_FREE,
         ATOMIC_POINTER_LOCK_FREE
        );
354 355 356 357 358 359 360 361 362 363 364
  //P99_GENERIC(1.0, ,(int, P99_NOP)); // shouldn't work
  P99_GENERIC(1, ,(int, P99_NOP));   // should work and not give diagnostic
  func2(1.0f, 2.0f);  // should work, but doesn't
  //func2(1.0f, 2.0); // shouldn't work
  //func2(1.0,  2.0f);  // shouldn't work
  func2(1.0,  2.0); // should work
  //func2(1,  2.0); // shouldn't work
  /* func(1.0f, 2.0f);  // should work, but doesn't */
  /* func(1.0f, 2.0); // shouldn't work */
  /* func(1.0,  2.0f);  // shouldn't work */
  /* func(1.0,  2.0); // should work, but doesn't */
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
  if (P99_SIZE_INDICATOR(sizeof(int), 1, 2, 4, 8)) {
    printf("sizeof int is one of 1, 2, 4, 8\n");
  } else {
    printf("sizeof int is weird\n");
  }
  /* Test both branches of P99_SIZE_INDICATOR */
  if (!P99_SIZE_INDICATOR(sizeof(char[3]), 1, 2, 4, 8)
      && P99_SIZE_INDICATOR(sizeof(char[3]), 3)) {
    printf("sizeof char[3] seems ok\n");
  } else {
    printf("sizeof char[3] is weird\n");
  }
  /* Test the minmax value for P99_SIZE_INDICATOR */
  if (P99_SIZE_INDICATOR(UINT16_MAX, 1, 2, 4, 8)) {
    printf("UINT16_MAX (%" PRIu16 ") is weird\n", UINT16_MAX);
  } else {
    printf("UINT16_MAX (%" PRIu16 ") seems ok\n", UINT16_MAX);
  }
383 384 385 386 387 388 389 390
  printf("size_t is %s\n",
         P99_GENERIC(sizeof(1),
                     ,
                     TYPESTR(unsigned char),
                     TYPESTR(unsigned short),
                     TYPESTR(unsigned int),
                     TYPESTR(unsigned long),
                     TYPESTR(unsigned long long)
(no author)'s avatar
(no author) committed
391
                    ));
392 393 394 395 396 397 398 399 400

  printf("ssize_t is %s\n",
         P99_GENERIC(SSIZE_T(1),
                     ,
                     TYPESTR(signed char),
                     TYPESTR(signed short),
                     TYPESTR(signed int),
                     TYPESTR(signed long),
                     TYPESTR(signed long long)
(no author)'s avatar
(no author) committed
401
                    ));
402

403 404 405 406
  unsigned Arr[7] = { 0 };
  printf("array length is %zu, pointer is %zu\n",
         P99_OBJLEN(Arr, unsigned, signed, float, double),
         P99_OBJLEN(&Arr[0], unsigned, signed, float, double)
(no author)'s avatar
(no author) committed
407
        );
408 409 410 411
  double Ard[7] = { 0 };
  printf("array length is %zu, pointer is %zu\n",
         P99_OBJLEN(Ard, unsigned, signed, float, double),
         P99_OBJLEN(&Ard[0], unsigned, signed, float, double)
(no author)'s avatar
(no author) committed
412
        );
413 414 415 416
  char Arc[7] = { 0 };
  printf("array length is %zu, pointer is %zu\n",
         P99_OBJLEN(Arc, char, unsigned, signed),
         P99_OBJLEN(&Arc[0], char, unsigned, signed)
(no author)'s avatar
(no author) committed
417
        );
418 419 420 421
  unsigned char Aruc[7] = { 0 };
  printf("array length is %zu, pointer is %zu\n",
         P99_OBJLEN(Aruc, unsigned char, signed),
         P99_OBJLEN(&Aruc[0], unsigned char, signed)
(no author)'s avatar
(no author) committed
422
        );
423 424 425 426
  signed char Arsc[7] = { 0 };
  printf("array length is %zu, pointer is %zu\n",
         P99_OBJLEN(Arsc, signed char, unsigned),
         P99_OBJLEN(&Arsc[0], signed char, unsigned)
(no author)'s avatar
(no author) committed
427
        );
428
  char volatile aChar = 'a';
429 430 431 432 433
  P99_PRINTF("print generic from float %s, long double complex %s, a pointer %s, int %s, uint128 %s (%s), _Bool %s and char %s\n",
             5.3f,
             CMPLXL(54, 1E-181),
             Ard,
             argc,
434
#ifdef P99X_UINT128_MAX
435 436
             (p99x_uint128)1020304050607 * 100000,
             (p99x_uint128)0x0,
437 438 439 440
#else
             UINT64_C(1020304050607) * 100000,
             UINT64_C(0),
#endif
441 442
             (bool)(argc > 1),
             aChar);
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

  double base[25] = { 0 };
  double * somewhere = base + 13;
  double * elsewhere = base + 25;
  P99_PRINTF("checking generic range test, pointers: base=%s, base1=%s, inside=%s, outside=%s\n",
             P99_IN_RANGE(base, base, 25 * sizeof base[0]),
             P99_IN_RANGE(base + 1, base, 25 * sizeof base[0]),
             P99_IN_RANGE(somewhere, base, 25 * sizeof base[0]),
             P99_IN_RANGE(elsewhere, base, 25 * sizeof base[0]));
  P99_PRINTF("checking generic range test, integers: base=%s, inside=%s, upper=%s, outside=%s\n",
             P99_IN_RANGE(1, 1u, 7),
             P99_IN_RANGE(5, 1u, 7),
             P99_IN_RANGE(8, 1u, 7),
             P99_IN_RANGE(28, 1, 7));
  P99_PRINTF("checking generic range test, floats: base=%s, inside=%s, upper=%s, outside=%s\n",
             P99_IN_RANGE(1.0, 1u, 7.f),
             P99_IN_RANGE(5.0, 1u, 7.f),
             P99_IN_RANGE(8.0, 1u, 7),
             P99_IN_RANGE(28.0, 1, 7));
Jens Gustedt's avatar
Jens Gustedt committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
  static char nullptr0[P99_IS_NULLPTR_CONSTANT(0)+1];
  static char nullptr1[P99_IS_NULLPTR_CONSTANT((void*)0)+1];
  enum p00_nullptr_enum { p00_nullptr };
  static char nullptr2[P99_IS_NULLPTR_CONSTANT(p00_nullptr)+1];
  static char nullptr3[P99_IS_NULLPTR_CONSTANT((enum p00_nullptr_enum)0)+1];
  static char nullptr4[P99_IS_NULLPTR_CONSTANT((void const*)0)+1];
  static char nullptr5[P99_IS_NULLPTR_CONSTANT((void*)1)+1];
  static void *const ptr = 0;
  static char nullptr6[P99_IS_NULLPTR_CONSTANT(ptr)+1];
  printf("Testing for null pointer constant.\n"
         "\tshould all be true:\t0 (%s),"
         " (void*)0 (%s),\n"
         "\t\t\t\tenumeration constant (%s),"
         " constant of enum type (%s)\n"
         "\tshould all be false:\t(void const*)0 (%s),"
         " (void*)1 (%s),"
         " void*const ptr (%s)\n",
         bool_getname(2 == sizeof nullptr0),
         bool_getname(2 == sizeof nullptr1),
         bool_getname(2 == sizeof nullptr2),
         bool_getname(2 == sizeof nullptr3),
         bool_getname(2 == sizeof nullptr4),
         bool_getname(2 == sizeof nullptr5),
         bool_getname(2 == sizeof nullptr6)
         );

  static char integral_constant0[P99_IS_INTEGRAL_CONSTANT(7)+1];
  enum p00_integral_constant_enum { p00_integral_constant = 1 };
  static char integral_constant1[P99_IS_INTEGRAL_CONSTANT(p00_integral_constant)+1];
  static char integral_constant2[P99_IS_INTEGRAL_CONSTANT((enum p00_integral_constant_enum)1)+1];
  static char integral_constant3[P99_IS_INTEGRAL_CONSTANT(7.0)+1];
  static char integral_constant4[P99_IS_INTEGRAL_CONSTANT(!!0.0)+1];
  static int const val = 0;
  static char integral_constant5[P99_IS_INTEGRAL_CONSTANT(val)+1];
  static char integral_constant6[P99_IS_INTEGRAL_CONSTANT((char*)1)+1];
  /* may produce a warning or error*/
  static char integral_constant7[P99_IS_INTEGRAL_CONSTANT((void*)0)+1];
  printf("Testing for integral constant.\n"
         "\tshould all be true:"
         "\t7 (%s),"
         "\n"
         "\t\t\t\tenumeration constant (%s),"
         "\tconstant of enum type (%s)\n"
         "\tshould all be false:"
         "\t7.0 (%s),"
         "\t!!0.0 (%s),"
         "\tint const val (%s),\n"
         "\t\t\t\t(char*)1 (%s),"
         "\t(void*)0 (%s),\n",
         bool_getname(2 == sizeof integral_constant0),
         bool_getname(2 == sizeof integral_constant1),
         bool_getname(2 == sizeof integral_constant2),
         bool_getname(2 == sizeof integral_constant3),
         bool_getname(2 == sizeof integral_constant4),
         bool_getname(2 == sizeof integral_constant5),
         bool_getname(2 == sizeof integral_constant6),
         bool_getname(2 == sizeof integral_constant7)
         );

  printf("NULL\t\t(" P99_STRINGIFY(NULL) ")\t\tis %sa null pointer.\n",
         P99_IS_NULLPTR(NULL) ? "" : "not ");
#undef NULL
#define NULL UINTPTR_C(0)

  printf("redefined to\t(" P99_STRINGIFY(NULL) ")\tis %sa null pointer.\n",
         P99_IS_NULLPTR(NULL) ? "" : "not ");

#undef NULL
#define NULL ((void*)0)

  printf("redefined to\t(" P99_STRINGIFY(NULL) ")\t\tis %sa null pointer.\n",
         P99_IS_NULLPTR(NULL) ? "" : "not ");

Jens Gustedt's avatar
Jens Gustedt committed
535 536 537 538 539 540 541 542 543 544 545
  char line[] = "this is a, long text -- it has some special charActers\nnewlines?";
  puts("--- end ---");

  do {
    printf("parsing: %s\n", line);
    char *ptr = 0;
    rsize_t s1max = sizeof line;
    for(char * tok;
        s1max && (tok = strtok_s(ptr ? 0 : line, &s1max, "A-\n? ,", &ptr));)
      puts(tok);
    tmpnam_s(line, sizeof line);
546
    puts(line);
Jens Gustedt's avatar
Jens Gustedt committed
547 548
  }  while(gets_s(line, sizeof line) && line[0]);

Jens Gustedt's avatar
Jens Gustedt committed
549 550
  return ret;
}