120 lines
4.8 KiB
C
120 lines
4.8 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Build with the compatibility include directory and -lvcccompat. Run on either
|
|
// emulated CPU; a failed check reports its source line on the CoCo screen.
|
|
#ifdef COCO_HEADER_FIRST
|
|
#include <coco.h>
|
|
#endif
|
|
#include <stdint.h>
|
|
#include <limits.h>
|
|
#include <stddef.h>
|
|
#include <ctype.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <coco.h>
|
|
|
|
#define CHECK(test) do { if (!(test)) return __LINE__; } while (0)
|
|
|
|
struct Pair { uint8_t tag; uint16_t number; };
|
|
|
|
static int checkTokens(void)
|
|
{
|
|
char row[] = "||MARS|RED,,COLD|4||";
|
|
char plain[] = "A B";
|
|
char separators[] = " | ,";
|
|
char untouched[] = "ONE TWO";
|
|
char empty[] = "";
|
|
char *field, *trait;
|
|
// A fresh scan ignores the cursor's prior value. A token points into
|
|
// the original writable line. Splitting it can start a second scan.
|
|
CHECK(strtok_r(row, "|", &field) == row + 2);
|
|
CHECK(strcmp(row + 2, "MARS") == 0);
|
|
char *traits = strtok_r(NULL, "|", &field);
|
|
CHECK(traits == row + 7);
|
|
CHECK(strtok_r(traits, ",", &trait) == traits);
|
|
CHECK(strcmp(traits, "RED") == 0);
|
|
CHECK(strtok(plain, " ") == plain);
|
|
CHECK(strtok_r(NULL, ",", &trait) == traits + 5);
|
|
CHECK(strcmp(traits + 5, "COLD") == 0);
|
|
CHECK(strtok_r(NULL, ",", &trait) == NULL);
|
|
CHECK(strtok_r(NULL, "|", &field) == row + 17);
|
|
CHECK(strcmp(row + 17, "4") == 0);
|
|
CHECK(strtok_r(NULL, "|", &field) == NULL);
|
|
CHECK(strtok_r(NULL, "|", &field) == NULL);
|
|
CHECK(strtok(NULL, " ") == plain + 2);
|
|
|
|
CHECK(strtok_r(empty, ",", &field) == NULL);
|
|
CHECK(strtok_r(separators, " ,|", &field) == NULL);
|
|
CHECK(strtok_r(untouched, "", &field) == untouched);
|
|
CHECK(strcmp(untouched, "ONE TWO") == 0);
|
|
CHECK(strtok_r(NULL, "", &field) == NULL);
|
|
char changed[] = "A,B:C+D";
|
|
CHECK(strtok_r(changed, ",", &field) == changed);
|
|
CHECK(strtok_r(NULL, ":+", &field) == changed + 2);
|
|
CHECK(strcmp(changed + 2, "B") == 0);
|
|
// Starting a new string abandons the remainder of the previous one.
|
|
CHECK(strtok_r(untouched, " ", &field) == untouched);
|
|
CHECK(strtok_r(NULL, " ", &field) == untouched + 4);
|
|
return 0;
|
|
}
|
|
|
|
static int viaList(char *buf, size_t n, const char *fmt, ...)
|
|
{
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
int code = vsnprintf(buf, n, fmt, args);
|
|
va_end(args);
|
|
return code;
|
|
}
|
|
|
|
static int checks(void)
|
|
{
|
|
char box[18], longBuf[42];
|
|
const char raw[2] = { 'A', 'B' };
|
|
CHECK(sizeof(int8_t) == 1 && sizeof(int16_t) == 2 && sizeof(int32_t) == 4);
|
|
CHECK(sizeof(size_t) == 2 && sizeof(ptrdiff_t) == 2 && sizeof(uintptr_t) == sizeof(void *));
|
|
CHECK(offsetof(struct Pair, number) == 1 && CHAR_BIT == 8);
|
|
#ifdef _CMOC_unsigned_char_
|
|
CHECK(CHAR_MIN == 0 && CHAR_MAX == 255);
|
|
#else
|
|
CHECK(CHAR_MIN == -128 && CHAR_MAX == 127);
|
|
#endif
|
|
CHECK(iscntrl(EOF) == 0 && isspace(EOF) == 0 && tolower(EOF) == EOF && toupper(EOF) == EOF);
|
|
CHECK(isblank('\t') && !isblank('\r') && iscntrl(127) && !isprint(127));
|
|
CHECK(isprint(' ') && !isgraph(' ') && isgraph('!') && ispunct('!') && !ispunct('a'));
|
|
CHECK(isxdigit('F') && isxdigit('b') && !isxdigit('G') && islower('b') && isupper('B'));
|
|
strcpy(box, "abcd");
|
|
memmove(box + 1, box, 4);
|
|
box[5] = 0;
|
|
CHECK(strcmp(box, "aabcd") == 0);
|
|
CHECK(strlen("") == 0 && memmove(box, box, 0) == box);
|
|
CHECK(snprintf(box, sizeof(box), NULL) < 0 && box[0] == 0);
|
|
memset(box, '@', sizeof(box));
|
|
CHECK(snprintf(NULL, 0, "%s %c", "CoCo", '3') == 6);
|
|
CHECK(snprintf(box + 1, 0, "abc") == 3 && box[1] == '@');
|
|
CHECK(snprintf(box + 1, 1, "abc") == 3 && box[1] == 0 && box[2] == '@');
|
|
CHECK(snprintf(box + 1, 4, "earth") == 5 && strcmp(box + 1, "ear") == 0);
|
|
CHECK(box[0] == '@' && box[5] == '@');
|
|
CHECK(snprintf(box, 6, "earth") == 5 && strcmp(box, "earth") == 0);
|
|
CHECK(snprintf(box, sizeof(box), "%cX", 0) == 2 && !box[0] && box[1] == 'X' && !box[2]);
|
|
CHECK(viaList(box, sizeof(box), "%04x,%X,%o", 0x1aU, 0xbeefU, 8U) == 12);
|
|
CHECK(!strcmp(box, "001a,BEEF,10"));
|
|
CHECK(snprintf(box, sizeof(box), "%-4.2s|%.0u", raw, 0U) == 5 && strcmp(box, "AB |") == 0);
|
|
CHECK(snprintf(box, sizeof(box), "%05d %4.2d %%", -23, 3) == 12 && !strcmp(box, "-0023 03 %"));
|
|
CHECK(snprintf(longBuf, sizeof(longBuf), "%ld,%lu,%lo", LONG_MIN, ULONG_MAX, ULONG_MAX) == 34);
|
|
CHECK(!strcmp(longBuf, "-2147483648,4294967295,37777777777"));
|
|
CHECK(snprintf(box, sizeof(box), "%d", INT_MIN) == 6 && !strcmp(box, "-32768"));
|
|
CHECK(snprintf(NULL, 0, "%32767d", 1) == INT_MAX);
|
|
CHECK(snprintf(box, 2, "a%32767d", 1) < 0 && !strcmp(box, "a"));
|
|
CHECK(snprintf(box, 3, "x%f") < 0 && !strcmp(box, "x"));
|
|
CHECK(snprintf(box, 3, "x%") < 0 && !strcmp(box, "x"));
|
|
return checkTokens();
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
int failure = checks();
|
|
if (failure) printf("COMPAT FAIL %d\n", failure);
|
|
else printf("COMPAT PASS\n");
|
|
return failure != 0;
|
|
}
|