0
0
mirror of https://github.com/vim/vim.git synced 2025-09-29 04:34:16 -04:00

runtime(c): Add missing syntax test files

These were missing from commit c2a967a.

closes: #16470

Signed-off-by: Doug Kearns <dougkearns@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Doug Kearns
2025-01-18 10:06:59 +01:00
committed by Christian Brabandt
parent 727c567a09
commit 866f229591
10 changed files with 249 additions and 27 deletions

View File

@@ -0,0 +1,11 @@
// C boolean constants
// Source: https://en.cppreference.com/w/c/language/bool_constant
#include <assert.h>
int main()
{
assert(true == 1 && 0 == false);
}

View File

@@ -0,0 +1,40 @@
// C character constants
// Source: https://en.cppreference.com/w/c/language/character_constant
#include <stddef.h>
#include <stdio.h>
#include <uchar.h>
int main (void)
{
printf("constant value \n");
printf("-------- ----------\n");
// integer character constants,
int c1='a'; printf("'a':\t %#010x\n", c1);
int c2='🍌'; printf("'🍌':\t %#010x\n\n", c2); // implementation-defined
// multicharacter constant
int c3='ab'; printf("'ab':\t %#010x\n\n", c3); // implementation-defined
// 16-bit wide character constants
char16_t uc1 = u'a'; printf("'a':\t %#010x\n", (int)uc1);
char16_t uc2 = u'¢'; printf("'¢':\t %#010x\n", (int)uc2);
char16_t uc3 = u''; printf("'猫':\t %#010x\n", (int)uc3);
// implementation-defined (🍌 maps to two 16-bit characters)
char16_t uc4 = u'🍌'; printf("'🍌':\t %#010x\n\n", (int)uc4);
// 32-bit wide character constants
char32_t Uc1 = U'a'; printf("'a':\t %#010x\n", (int)Uc1);
char32_t Uc2 = U'¢'; printf("'¢':\t %#010x\n", (int)Uc2);
char32_t Uc3 = U''; printf("'猫':\t %#010x\n", (int)Uc3);
char32_t Uc4 = U'🍌'; printf("'🍌':\t %#010x\n\n", (int)Uc4);
// wide character constants
wchar_t wc1 = L'a'; printf("'a':\t %#010x\n", (int)wc1);
wchar_t wc2 = L'¢'; printf("'¢':\t %#010x\n", (int)wc2);
wchar_t wc3 = L''; printf("'猫':\t %#010x\n", (int)wc3);
wchar_t wc4 = L'🍌'; printf("'🍌':\t %#010x\n\n", (int)wc4);
}

View File

@@ -0,0 +1,44 @@
// C integer constants
// Source: https://en.cppreference.com/w/c/language/integer_constant
#include <inttypes.h>
#include <stdio.h>
int main(void)
{
printf("123 = %d\n", 123);
printf("0123 = %d\n", 0123);
printf("0x123 = %d\n", 0x123);
printf("12345678901234567890ull = %llu\n", 12345678901234567890ull);
// the type is a 64-bit type (unsigned long long or possibly unsigned long)
// even without a long suffix
printf("12345678901234567890u = %"PRIu64"\n", 12345678901234567890u );
// printf("%lld\n", -9223372036854775808); // Error:
// the value 9223372036854775808 cannot fit in signed long long, which
// is the biggest type allowed for unsuffixed decimal integer constant
printf("%llu\n", -9223372036854775808ull );
// unary minus applied to unsigned value subtracts it from 2^64,
// this gives unsigned 9223372036854775808
printf("%lld\n", -9223372036854775807ll - 1);
// correct way to form signed value -9223372036854775808
}
// The following variables are initialized to the same value:
int d = 42;
int o = 052;
int x = 0x2a;
int X = 0X2A;
int b = 0b101010; // C23
// The following variables are also initialized to the same value:
unsigned long long l1 = 18446744073709550592ull; // C99
unsigned long long l2 = 18'446'744'073'709'550'592llu; // C23
unsigned long long l3 = 1844'6744'0737'0955'0592uLL; // C23
unsigned long long l4 = 184467'440737'0'95505'92LLU; // C23

View File

@@ -0,0 +1,40 @@
// C preprocessor - conditional inclusion
// Source: https://en.cppreference.com/w/c/preprocessor/conditional
#define ABCD 2
#include <stdio.h>
int main(void)
{
#ifdef ABCD
printf("1: yes\n");
#else
printf("1: no\n");
#endif
#ifndef ABCD
printf("2: no1\n");
#elif ABCD == 2
printf("2: yes\n");
#else
printf("2: no2\n");
#endif
#if !defined(DCBA) && (ABCD < 2 * 4 - 3)
printf("3: yes\n");
#endif
// C23 directives #elifdef/#elifndef
#ifdef CPU
printf("4: no1\n");
#elifdef GPU
printf("4: no2\n");
#elifndef RAM
printf("4: yes\n"); // selected in C23 mode, may be selected in pre-C23 mode
#else
printf("4: no3\n"); // may be selected in pre-C23 mode
#endif
}

View File

@@ -0,0 +1,31 @@
// C preprocessor - binary resource inclusion
// Source: https://en.cppreference.com/w/c/preprocessor/embed
#include <stdint.h>
#include <stdio.h>
const uint8_t image_data[] = {
#embed "image.png"
};
const char message[] = {
#embed "message.txt" if_empty('M', 'i', 's', 's', 'i', 'n', 'g', '\n')
,'\0' // null terminator
};
void dump(const uint8_t arr[], size_t size)
{
for (size_t i = 0; i != size; ++i)
printf("%02X%c", arr[i], (i + 1) % 16 ? ' ' : '\n');
puts("");
}
int main()
{
puts("image_data[]:");
dump(image_data, sizeof image_data);
puts("message[]:");
dump((const uint8_t*)message, sizeof message);
}

View File

@@ -0,0 +1,19 @@
// C preprocessor - diagnostic directives
// Source: https://en.cppreference.com/w/c/preprocessor/error
#if __STDC__ != 1
# error "Not a standard compliant compiler"
#endif
#if __STDC_VERSION__ >= 202311L
# warning "Using #warning as a standard feature"
#endif
#include <stdio.h>
int main (void)
{
printf("The compiler used conforms to the ISO C Standard !!");
}

View File

@@ -0,0 +1,57 @@
// C string literals
// Source: https://en.cppreference.com/w/c/language/string_literal
#include <inttypes.h>
#include <locale.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <uchar.h>
int main(void)
{
char s1[] = "a猫🍌"; // or "a\u732B\U0001F34C"
#if __STDC_VERSION__ >= 202311L
char8_t
#else
char
#endif
s2[] = u8"a猫🍌";
char16_t s3[] = u"a猫🍌";
char32_t s4[] = U"a猫🍌";
wchar_t s5[] = L"a猫🍌";
setlocale(LC_ALL, "en_US.utf8");
printf(" \"%s\" is a char[%zu] holding { ", s1, sizeof s1 / sizeof *s1);
for(size_t n = 0; n < sizeof s1 / sizeof *s1; ++n)
printf("0x%02X ", +(unsigned char)s1[n]);
puts("}");
printf(
#if __STDC_VERSION__ >= 202311L
"u8\"%s\" is a char8_t[%zu] holding { "
#else
"u8\"%s\" is a char[%zu] holding { "
#endif
, s2, sizeof s2 / sizeof *s2);
for(size_t n = 0; n < sizeof s2 / sizeof *s2; ++n)
#if __STDC_VERSION__ >= 202311L
printf("0x%02X ", s2[n]);
#else
printf("0x%02X ", +(unsigned char)s2[n]);
#endif
puts("}");
printf(" u\"a猫🍌\" is a char16_t[%zu] holding { ", sizeof s3 / sizeof *s3);
for(size_t n = 0; n < sizeof s3 / sizeof *s3; ++n)
printf("0x%04" PRIXLEAST16" ", s3[n]);
puts("}");
printf(" U\"a猫🍌\" is a char32_t[%zu] holding { ", sizeof s4 / sizeof *s4);
for(size_t n = 0; n < sizeof s4 / sizeof *s4; ++n)
printf("0x%08" PRIXLEAST32" ", s4[n]);
puts("}");
printf(" L\"%ls\" is a wchar_t[%zu] holding { ", s5, sizeof s5 / sizeof *s5);
for(size_t n = 0; n < sizeof s5 / sizeof *s5; ++n)
printf("0x%08X ", (unsigned)s5[n]);
puts("}");
}