|
|
1.1 ! root 1: /* The Plum Hall Validation Suite for C ! 2: * Unpublished copyright (c) 1986-1991, Chiron Systems Inc and Plum Hall Inc. ! 3: * VERSION: 4 ! 4: * DATE: 1993-01-01 ! 5: * The "ANSI" mode of this suite corresponds to official ANSI C, X3.159-1989. ! 6: * As per your license agreement, your distribution is not to be moved or copied outside the Designated Site ! 7: * without specific permission from Plum Hall Inc. ! 8: */ ! 9: ! 10: #include "flags.h" ! 11: #ifndef SKIP32 ! 12: /* ! 13: * 3.2 - Conversions. ! 14: */ ! 15: #include "defs.h" ! 16: ! 17: static void c3_2_1_1(); ! 18: static void c3_2_1_2(); ! 19: static void c3_2_1_3(); ! 20: static void c3_2_1_4(); ! 21: static void c3_2_1_5(); ! 22: static void c3_2_2_1(); ! 23: static void c3_2_2_2(); ! 24: static void c3_2_2_3(); ! 25: ! 26: ! 27: void c3_2() ! 28: { ! 29: Filename = "c32.c"; ! 30: c3_2_1_1(); ! 31: c3_2_1_2(); ! 32: c3_2_1_3(); ! 33: c3_2_1_4(); ! 34: c3_2_1_5(); ! 35: c3_2_2_1(); ! 36: c3_2_2_2(); ! 37: c3_2_2_3(); ! 38: } ! 39: ! 40: /* ! 41: * 3.2.1.1 - Characters and integers ! 42: */ ! 43: static void c3_2_1_1() ! 44: { ! 45: char c; ! 46: short ss; ! 47: #if ANSI ! 48: signed char sc; ! 49: #endif ! 50: ! 51: ! 52: ! 53: #if ANSI /* 3.2.1.1 (cont.) */ ! 54: UCHAR uc; ! 55: USHORT us; ! 56: #endif ! 57: ! 58: #if ANSI ! 59: /* check signed and unsigned integral promotions */ ! 60: sc = ivalue(4); ! 61: ss = ivalue(5); ! 62: dequals(__LINE__, (double)(sc-5), -1.); ! 63: dequals(__LINE__, (double)(ss-6), -1.); ! 64: uc = ivalue(4); ! 65: us = ivalue(5); ! 66: if (MAX_UCHAR <= MAX_INT) ! 67: dequals(__LINE__, (double)(uc-5), -1.); ! 68: else ! 69: dequals(__LINE__, (double)(uc-5), (double)MAX_UINT); ! 70: ! 71: if (MAX_USHORT <= MAX_INT) ! 72: dequals(__LINE__, (double)(us-6), -1.); ! 73: else ! 74: dequals(__LINE__, (double)(us-6), (double)MAX_UINT); ! 75: ! 76: #endif ! 77: ! 78: /* sign of "plain" char is implem-def */ ! 79: #if SIGNED_CHAR ! 80: c = ivalue(-1); ! 81: iequals(__LINE__, c, -1); ! 82: #endif ! 83: #if ANSI ! 84: #if UNSIGNED_CHAR ! 85: c = ~0u; ! 86: iequals(__LINE__, c, MAX_UCHAR); ! 87: #endif /* UNSIGNED_CHAR */ ! 88: /* demotion to shorter signed */ ! 89: sc = ivalue(-1); ! 90: iequals(__LINE__, sc, -1); ! 91: #endif /* ANSI */ ! 92: ss = ivalue(-1); ! 93: iequals(__LINE__, ss, -1); ! 94: ! 95: #if ANSI ! 96: uc = ~0u; ! 97: checkthat(__LINE__, uc == MAX_UCHAR); ! 98: us = ~0u; ! 99: checkthat(__LINE__, us == MAX_USHORT); ! 100: #endif ! 101: } ! 102: ! 103: /* ! 104: * 3.2.1.2 - Signed and unsigned integers ! 105: */ ! 106: static void c3_2_1_2() ! 107: { ! 108: #if ANSI ! 109: signed int si; ! 110: int i; ! 111: signed short int ssi; ! 112: short int ss; ! 113: signed long int sl; ! 114: long int l; ! 115: signed int *psi = &i; /* allowable, compatible type */ ! 116: signed short int *pssi = &ss; ! 117: signed long int *psli = &l; ! 118: ULONG ul; ! 119: struct { ! 120: unsigned ubf2 : 2; ! 121: signed sbf2 : 2; ! 122: int ibf2 : 2; ! 123: } bf; ! 124: UCHAR uc=ivalue(4); ! 125: USHORT us=ivalue(5); ! 126: unsigned int ui; ! 127: ! 128: /* each of the pairs are compatible types */ ! 129: iequals(__LINE__, sizeof(signed int), sizeof(int)); ! 130: iequals(__LINE__, sizeof(signed short int), sizeof(short int)); ! 131: iequals(__LINE__, sizeof(signed long int), sizeof(long int)); ! 132: ! 133: /* when unsigned integers are converted to representable signed values, the value is unchanged. */ ! 134: uc = lvalue(MAX_UCHAR); ! 135: sl = uc; ! 136: /* allow for the rare case where sizeof(UCHAR) == sizeof(long) */ ! 137: if (sizeof(sl) > sizeof(uc)) ! 138: dequals(__LINE__, (double)sl, (double)MAX_UCHAR); ! 139: us = lvalue(MAX_USHORT); ! 140: sl = us; ! 141: /* allow for the rare case where sizeof(USHORT) == sizeof(long) */ ! 142: if (sizeof(sl) > sizeof(us)) ! 143: dequals(__LINE__, (double)sl, (double)MAX_USHORT); ! 144: ! 145: checkthat(__LINE__, (unsigned char)(long)(UCHAR_MAX+1+ivalue(2)) == 2); ! 146: checkthat(__LINE__, (unsigned long)(short)ivalue(-2) == (ULONG_MAX-1)); ! 147: bf.ubf2 = ivalue(-1); ! 148: iequals(__LINE__, bf.ubf2, 3); ! 149: bf.sbf2 = ivalue(-1); ! 150: iequals(__LINE__, bf.sbf2, -1); ! 151: bf.ibf2 = ivalue(-1); ! 152: checkthat(__LINE__, bf.ibf2 == -1 || bf.ibf2 == 3); ! 153: ! 154: /* signed to unsigned of equal or greater length: if non-negative, value unchanged */ /* 3.2.1.2 (cont.) */ ! 155: ssi = ivalue(4); ! 156: ui = ssi; ! 157: checkthat(__LINE__, ui == 4); ! 158: /* signed to unsigned of equal or greater length: if negative, converted modulo "largest plus one" */ ! 159: ssi = ivalue(-1); ! 160: ul = (unsigned long) ivalue(ssi); ! 161: checkthat(__LINE__, ul == MAX_ULONG); ! 162: us = ssi; ! 163: checkthat(__LINE__, us == MAX_USHORT); ! 164: ! 165: /* integer demoted to smaller unsigned: modulo "largest plus one" */ ! 166: sl = ivalue(-3); ! 167: uc = sl; ! 168: iequals(__LINE__, uc, MAX_UCHAR-2); ! 169: #endif ! 170: } ! 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: * 3.2.1.3 - floating and integral ! 205: */ ! 206: static void c3_2_1_3() ! 207: { ! 208: #if ANSI ! 209: float f; ! 210: double d; ! 211: char c; ! 212: short ss; ! 213: int si; ! 214: long sl; ! 215: unsigned int ui; ! 216: unsigned char uc; ! 217: unsigned short us; ! 218: unsigned long ul; ! 219: signed char sc; ! 220: enum { a, b } en; ! 221: struct { unsigned u : 7; signed i : 7; } su; ! 222: ! 223: /* conversion from floating->integral truncates positive numbers */ ! 224: f = dvalue(99.999); ! 225: c = f; iequals(__LINE__, c, ivalue(99)); ! 226: sc = f; iequals(__LINE__, sc, ivalue(99)); ! 227: uc = f; iequals(__LINE__, uc, ivalue(99)); ! 228: ss = f; iequals(__LINE__, ss, ivalue(99)); ! 229: us = f; iequals(__LINE__, us, ivalue(99)); ! 230: si = f; iequals(__LINE__, si, ivalue(99)); ! 231: ui = f; iequals(__LINE__, ui, ivalue(99)); ! 232: en = f; iequals(__LINE__, en, ivalue(99)); ! 233: su.u = f; iequals(__LINE__, su.u, ivalue(99)); ! 234: su.i = 33.333; iequals(__LINE__, su.i, ivalue(33)); ! 235: sl = f; lequals(__LINE__, sl, lvalue(99L)); ! 236: ul = f; lequals(__LINE__, ul, lvalue(99L)); ! 237: d = dvalue(99.999); ! 238: c = d; iequals(__LINE__, c, ivalue(99)); ! 239: sc = d; iequals(__LINE__, sc, ivalue(99)); ! 240: uc = d; iequals(__LINE__, uc, ivalue(99)); ! 241: ss = d; iequals(__LINE__, ss, ivalue(99)); ! 242: us = d; iequals(__LINE__, us, ivalue(99)); ! 243: si = d; iequals(__LINE__, si, ivalue(99)); ! 244: ui = d; iequals(__LINE__, ui, ivalue(99)); ! 245: en = d; iequals(__LINE__, en, ivalue(99)); ! 246: su.u = d; iequals(__LINE__, su.u, ivalue(99)); ! 247: su.i = 33.333; iequals(__LINE__, su.i, ivalue(33)); ! 248: sl = d; lequals(__LINE__, sl, lvalue(99L)); ! 249: ul = d; lequals(__LINE__, ul, lvalue(99L)); ! 250: #endif ! 251: } ! 252: ! 253: /* ! 254: * 3.2.1.4 - Floating types ! 255: */ ! 256: static void c3_2_1_4() ! 257: { ! 258: float f; ! 259: double d; ! 260: #if ANSI ! 261: long double l; ! 262: #endif ! 263: ! 264: /* float->double, etc. has no value change */ ! 265: f = dvalue(1.23456789012345); ! 266: d = dvalue(f); ! 267: dequals(__LINE__, f, d); ! 268: #if ANSI ! 269: l = dvalue(f); ! 270: ldequals(__LINE__, (long double)f, l); ! 271: d = dvalue(1.23456789012345); ! 272: l = d; ! 273: ldequals(__LINE__, (long double)d, l); ! 274: #endif ! 275: ! 276: d = dvalue(.25); ! 277: f = dvalue(d); ! 278: checkthat(__LINE__, f == .25); ! 279: ! 280: /* conversion downward has undefined behavior if not representable */ ! 281: ! 282: } ! 283: ! 284: ! 285: ! 286: ! 287: ! 288: ! 289: ! 290: ! 291: ! 292: ! 293: ! 294: ! 295: ! 296: ! 297: ! 298: ! 299: ! 300: ! 301: ! 302: ! 303: /* ! 304: * 3.2.1.5 - Usual arithmetic conversions ! 305: * These will be checked in greater detail in 'expressions'. ! 306: */ ! 307: static void c3_2_1_5() ! 308: { ! 309: #if ANSI ! 310: long double ld = dvalue(3.0); ! 311: signed char sc = ivalue(4); ! 312: char c = ivalue(5); ! 313: int si = ivalue(6); ! 314: short ss = ivalue(7); ! 315: long sl = ivalue(8); ! 316: unsigned int ui = ivalue(9); ! 317: float f = ivalue(10); ! 318: double d = ivalue(11); ! 319: UCHAR uc = ivalue(12); ! 320: USHORT us = ivalue(13); ! 321: ULONG ul = ivalue(14); ! 322: ! 323: /* if either argument is long double, that is the type of the result */ ! 324: ldequals(__LINE__, ld+sc, 7.0L); ! 325: ! 326: /* if either argument is double, that is the type of the result */ ! 327: dequals(__LINE__, d+f, 21.0); ! 328: dequals(__LINE__, d+si, 17.0); ! 329: dequals(__LINE__, (double)f+si, 16.0); ! 330: ! 331: /* otherwise, if either float, then float */ ! 332: iequals(__LINE__, sizeof(d+c), sizeof(double)); ! 333: iequals(__LINE__, sizeof(f+c), sizeof(float)); ! 334: /* otherwise, int promotions are performed. Then, following rules: */ ! 335: ! 336: /* if either unsigned long int, then that */ ! 337: ldequals(__LINE__, (long double)(ss-ul), (long double)(MAX_ULONG-6)); ! 338: /* otherwise, if long vs unsigned int: */ ! 339: if (UINT_MAX <= LONG_MAX) /* if unsigned int can be held in long, then long */ ! 340: ldequals(__LINE__, sl-ui, -1); ! 341: else /* if unsigned int cannot be held in long, then unsigned long */ ! 342: ldequals(__LINE__, sl-ui, MAX_ULONG); ! 343: /* otherwise, if either long int, then that */ ! 344: ldequals(__LINE__, (long double)(si-sl), (long double)(-2)); ! 345: iequals(__LINE__, sizeof(si-sl), sizeof(long)); ! 346: ! 347: /* otherwise, if either unsigned int, then that */ ! 348: ldequals(__LINE__, (long double)(ss-ui), (long double)(MAX_UINT - 1)); ! 349: /* integral widening has been tested in 3.2.1.1 */ ! 350: #endif ! 351: } ! 352: ! 353: /* ! 354: * 3.2.2.1 - Lvalues and function designators ! 355: */ ! 356: #if ANSI ! 357: static char iarray[6] = "abcdef"; ! 358: #else ! 359: static char iarray[6] = "abcde"; ! 360: #endif ! 361: static void c3_2_2_1() ! 362: { ! 363: char *p; ! 364: ! 365: /* the name of an array is equivalent to a pointer to the first element */ ! 366: aequals(__LINE__, iarray, &iarray[0]); ! 367: ! 368: /* except for use in 'sizeof' */ ! 369: iequals(__LINE__, sizeof(iarray), 6 * sizeof(char)); ! 370: #if ANSI ! 371: /* ... or as operand of & */ ! 372: { ! 373: char (*pa)[6] = &iarray; ! 374: ! 375: checkthat(__LINE__, pa == &iarray); ! 376: } ! 377: #endif ! 378: ! 379: /* or except as a string literal initializer */ ! 380: iequals(__LINE__, iarray[0], 'a'); ! 381: iequals(__LINE__, iarray[1], 'b'); ! 382: iequals(__LINE__, iarray[2], 'c'); ! 383: iequals(__LINE__, iarray[3], 'd'); ! 384: iequals(__LINE__, iarray[4], 'e'); ! 385: iequals(__LINE__, iarray[5], ANSI ? 'f' : '\0'); ! 386: ! 387: /* similarly we can overaddress and check a pointer */ ! 388: for (p = iarray; p < &iarray[6]; ++p) ! 389: ; ! 390: aequals(__LINE__, p, iarray+6); ! 391: ! 392: /* function name takes on type 'pointer to function returning type' */ ! 393: checkthat(__LINE__, c3_2_2_1 != 0); ! 394: #if ANSI ! 395: checkthat(__LINE__, &c3_2_2_1 == c3_2_2_1); ! 396: #endif ! 397: } ! 398: ! 399: ! 400: ! 401: ! 402: ! 403: /* ! 404: * 3.2.2.2 - Void ! 405: */ ! 406: static void c3_2_2_2() ! 407: { ! 408: static int iterations = 0; ! 409: ! 410: if (iterations++ == 0) ! 411: #if ANSI8709 ! 412: (void)c3_2_2_2(); ! 413: #else ! 414: c3_2_2_2(); ! 415: #endif ! 416: else ! 417: { ! 418: (void)1; ! 419: (void)iequals(__LINE__, 1, ivalue(1)); ! 420: (void)(iterations + 1); ! 421: iequals(__LINE__, 7, ((void)3, ivalue(7))); ! 422: (void)++iterations; ! 423: return; ! 424: } ! 425: iequals(__LINE__, iterations, 3); ! 426: } ! 427: ! 428: ! 429: ! 430: ! 431: ! 432: ! 433: ! 434: ! 435: ! 436: ! 437: ! 438: ! 439: ! 440: ! 441: ! 442: ! 443: ! 444: ! 445: ! 446: ! 447: ! 448: ! 449: ! 450: ! 451: ! 452: ! 453: /* ! 454: * 3.2.2.3 - Pointers ! 455: */ ! 456: static void c3_2_2_3() ! 457: { ! 458: long li, *pl = (long *)avalue(&li); ! 459: double d, *pd = (double *)avalue(&d); ! 460: char *pc; ! 461: ! 462: int (*ipf)(); ! 463: #if ANSI ! 464: /* pointer to void can be converted to any ptr-to-object */ ! 465: void *gp; ! 466: ! 467: gp = avalue(pl); ! 468: aequals(__LINE__, pl, gp); ! 469: #endif ! 470: #if ANSI8706 ! 471: { ! 472: struct unknown *pu; ! 473: ! 474: pu = avalue(gp); ! 475: gp = avalue(pu); ! 476: pu = avalue(gp); ! 477: aequals(__LINE__, gp, pu); ! 478: } ! 479: #endif ! 480: ! 481: /* A pointer can be converted to one of less strict alignment ! 482: * and back again without loss of information. (See 3.3.4.) ! 483: */ ! 484: li = ivalue(1234); pc = (char *)pl; do_nothing(&pc, &pl); pl = (long *)pc; ! 485: lequals(__LINE__, *pl, 1234L); ! 486: ! 487: d = dvalue(3.1416); pc = (char *)pd; do_nothing(&pc, &pd); pd = (double *)pc; ! 488: dequals(__LINE__, *pd, 3.1416); ! 489: ! 490: /* null pointers */ ! 491: pc = 0; ! 492: do_nothing(&pc); ! 493: checkthat(__LINE__, pc == 0); ! 494: pd = 0; ! 495: do_nothing(&pd); ! 496: checkthat(__LINE__, pd == 0); ! 497: pd = &d; /* again */ ! 498: ! 499: ! 500: ! 501: ! 502: ! 503: #if ANSI8712 /* 3.2.2.3 (cont.) */ ! 504: { ! 505: void *gp; ! 506: struct unknown *pu, *pu2; ! 507: const long *pcl; ! 508: ! 509: /* a ptr-to-void may be converted to/from a ptr to any object type */ ! 510: li = ivalue(1234); gp = pl; do_nothing(&gp, &pl); pl = gp; ! 511: lequals(__LINE__, *pl, 1234L); ! 512: ! 513: d = dvalue(3.1416); gp = pd; do_nothing(&gp, &pd); pd = gp; ! 514: dequals(__LINE__, *pd, 3.1416); ! 515: ! 516: /* a ptr-to-void may be converted to/from a ptr to any incomplete type */ ! 517: pu = (struct unknown *)pl; gp = pu; pu2 = gp; ! 518: checkthat(__LINE__, pu == pu2); ! 519: ! 520: /* qualifiers do not affect comparisons */ ! 521: pcl = (const long *)pl; ! 522: checkthat(__LINE__, pcl == pl); ! 523: ! 524: /* null pointer can be e.g. 0L or (void *)0 */ ! 525: pc = 0L; ! 526: do_nothing(&pc); ! 527: checkthat(__LINE__, pc == 0L); ! 528: pd = 0L; ! 529: do_nothing(&pd); ! 530: checkthat(__LINE__, pd == 0L); ! 531: ! 532: pc = (void *)0; ! 533: do_nothing(&pc); ! 534: checkthat(__LINE__, pc == (void *)0); ! 535: pd = (void *)0; ! 536: do_nothing(&pd); ! 537: checkthat(__LINE__, pd == (void *)0); ! 538: ! 539: /* two null pointers, thru different cast sequences, compare equal */ ! 540: checkthat(__LINE__, (char *)(long *)pc == (char *)(float *)pd); ! 541: } ! 542: #endif ! 543: } ! 544: #else /* if SKIP32 */ ! 545: ! 546: void c3_2() { pr_skip("c3_2: SKIPPED ENTIRELY\n"); } ! 547: #endif /* SKIP32 */ ! 548: ! 549: /* ! 550: * Section 3.2.2.4 (const and volatile) has been moved to 3.5.3 ! 551: * See c35.c ! 552: */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.