Skip to content

Commit 772dee7

Browse files
committed
🛠️ For tests, appropriately cast string literal types (not valid in C17 and below)
1 parent 14f1a30 commit 772dee7

File tree

3 files changed

+121
-83
lines changed

3 files changed

+121
-83
lines changed

source/include/ztd/idk/detail/threads.pthreads.implementation.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535

3636
#include <ztd/thread/version.h>
3737

38+
#define _GNU_SOURCE
39+
#include <pthread.h>
40+
#if ZTD_IS_ON(ZTD_HEADER_PTHREAD_NP_H)
41+
#include <pthread_np.h>
42+
#endif
43+
3844
ZTD_USE(ZTD_C_LANGUAGE_LINKAGE)
3945
ZTD_USE(ZTD_THREAD_API_INTERNAL_LINKAGE)
4046
int __ztdc_pthread_to_thread_error(int __code);

source/ztd/thread/threads.pthreads.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,14 @@ ZTD_USE(ZTD_C_LANGUAGE_LINKAGE)
474474
ZTD_USE(ZTD_THREAD_API_LINKAGE)
475475
int ztdc_thrd_get_mcname(thrd_t __thr, size_t __buffer_size, char* __buffer) {
476476
ztd_char8_t __pivot[ZTD_USE(ZTD_THREAD_INTERMEDIATE_BUFFER_SUGGESTED_BYTE_SIZE)];
477-
const size_t __pivot_size = ztdc_c_array_size(__pivot);
478-
int __res = pthread_getname_np((pthread_t)__thr, (char*)__pivot, __pivot_size);
477+
const size_t __pivot_size = ztdc_c_array_size(__pivot);
478+
const ztd_char8_t* __pivot_ptr = (const ztd_char8_t*)&__pivot[0];
479+
int __res = pthread_getname_np((pthread_t)__thr, (char*)__pivot, __pivot_size);
479480
if (__res != 0) {
480481
return __ztdc_pthread_to_thread_error(__res);
481482
}
482483
size_t __real_pivot_size = ztdc_c_string_ptr_size_limit(__pivot_size, __pivot);
483-
cnc_mcerr __conv_res
484-
= cnc_c8sntomcsn(&__buffer_size, &__buffer, &__real_pivot_size, (const ztd_char8_t**)&__pivot);
484+
cnc_mcerr __conv_res = cnc_c8sntomcsn(&__buffer_size, &__buffer, &__real_pivot_size, &__pivot_ptr);
485485
if (__conv_res == cnc_mcerr_ok) {
486486
return thrd_success;
487487
}
@@ -495,14 +495,14 @@ ZTD_USE(ZTD_C_LANGUAGE_LINKAGE)
495495
ZTD_USE(ZTD_THREAD_API_LINKAGE)
496496
int ztdc_thrd_get_mwcname(thrd_t __thr, size_t __buffer_size, ztd_wchar_t* __buffer) {
497497
ztd_char8_t __pivot[ZTD_USE(ZTD_THREAD_INTERMEDIATE_BUFFER_SUGGESTED_BYTE_SIZE)];
498-
const size_t __pivot_size = ztdc_c_array_size(__pivot);
499-
int __res = pthread_getname_np((pthread_t)__thr, (char*)__pivot, __pivot_size);
498+
const size_t __pivot_size = ztdc_c_array_size(__pivot);
499+
const ztd_char8_t* __pivot_ptr = (const ztd_char8_t*)&__pivot[0];
500+
int __res = pthread_getname_np((pthread_t)__thr, (char*)__pivot, __pivot_size);
500501
if (__res != 0) {
501502
return __ztdc_pthread_to_thread_error(__res);
502503
}
503504
size_t __real_pivot_size = ztdc_c_string_ptr_size_limit(__pivot_size, __pivot);
504-
cnc_mcerr __conv_res
505-
= cnc_c8sntomwcsn(&__buffer_size, &__buffer, &__real_pivot_size, (const ztd_char8_t**)&__pivot);
505+
cnc_mcerr __conv_res = cnc_c8sntomwcsn(&__buffer_size, &__buffer, &__real_pivot_size, &__pivot_ptr);
506506
if (__conv_res == cnc_mcerr_ok) {
507507
return thrd_success;
508508
}

tests/basic_run_time/source/attr.encoded_name.c.cpp

Lines changed: 107 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -39,131 +39,163 @@
3939
#include <type_traits>
4040

4141
TEST_CASE("encoded thread name check", "[thrd][thrd_with_create_attrs][encoded-name]") {
42-
#define MAKE_TEST_BRACKET(prefix, given, expected, expected_prefix) \
43-
using given_char_t = std::remove_cv_t<std::remove_reference_t<decltype(given[0])>>; \
44-
using expected_char_t = std::remove_cv_t<std::remove_reference_t<decltype(expected[0])>>; \
45-
static constexpr const given_char_t thread_name[] = given; \
46-
static constexpr const expected_char_t expected_thread_name[] = expected; \
47-
const constexpr auto thrd_main = [](void* arg) -> int { \
48-
int t_id = *(int*)arg; \
49-
expected_char_t name_buf[128] = {}; \
50-
int name_get_result \
51-
= ztdc_thrd_get_##expected_prefix##name(thrd_current(), ztdc_c_array_size(name_buf), name_buf); \
52-
REQUIRE(name_get_result == thrd_success); \
53-
const expected_char_t* name = name_buf; \
54-
int expected_name_result \
55-
= std::memcmp(name, &expected_thread_name[0], ztdc_c_array_size(expected_thread_name)); \
56-
REQUIRE(expected_name_result == 0); \
57-
thrd_exit(t_id); \
58-
}; \
59-
\
60-
thrd_t t0 = {}; \
61-
\
62-
ztdc_thrd_attr_##prefix##name name_attr = { \
63-
/* format */ \
64-
ztdc_thrd_attr_kind_##prefix##name, \
65-
thread_name, \
66-
}; \
67-
struct ztdc_thrd_attr_priority { \
68-
ztdc_thrd_attr_kind kind; \
69-
int priority; \
70-
} priority_attr = { \
71-
ztdc_thrd_attr_kind_impl_def_priority, \
72-
INT_MAX, \
73-
}; \
74-
\
75-
ztdc_thrd_attr_kind* attrs[] = { \
76-
&priority_attr.kind, \
77-
&name_attr.kind, \
78-
}; \
79-
\
80-
int t0_id = 0xF3; \
81-
ztdc_thrd_create_attrs(&t0, thrd_main, &t0_id, ztdc_c_array_size(attrs), attrs); \
82-
int res0 = 0; \
83-
thrd_join(t0, &res0); \
42+
#define MAKE_TEST_BRACKET(given_prefix, given, given_type, expected_prefix, expected, expected_type) \
43+
using given_char_t = std::remove_cv_t<std::remove_reference_t<decltype(given[0])>>; \
44+
using expected_char_t = std::remove_cv_t<std::remove_reference_t<decltype(expected[0])>>; \
45+
static constexpr const given_char_t thread_name[] = given; \
46+
static constexpr const expected_char_t expected_thread_name[] = expected; \
47+
const constexpr auto thrd_main = [](void* arg) -> int { \
48+
int t_id = *(int*)arg; \
49+
expected_char_t name_buf[128] = {}; \
50+
int name_get_result = ztdc_thrd_get_##expected_prefix##name( \
51+
thrd_current(), ztdc_c_array_size(name_buf), (expected_type*)name_buf); \
52+
REQUIRE(name_get_result == thrd_success); \
53+
const expected_char_t* name = name_buf; \
54+
int expected_name_result \
55+
= std::memcmp(name, &expected_thread_name[0], ztdc_c_array_size(expected_thread_name)); \
56+
REQUIRE(expected_name_result == 0); \
57+
thrd_exit(t_id); \
58+
}; \
59+
\
60+
thrd_t t0 = {}; \
61+
\
62+
ztdc_thrd_attr_##given_prefix##name name_attr = { \
63+
/* format */ \
64+
ztdc_thrd_attr_kind_##given_prefix##name, \
65+
(given_type*)thread_name, \
66+
}; \
67+
struct ztdc_thrd_attr_priority { \
68+
ztdc_thrd_attr_kind kind; \
69+
int priority; \
70+
} priority_attr = { \
71+
ztdc_thrd_attr_kind_impl_def_priority, \
72+
INT_MAX, \
73+
}; \
74+
\
75+
ztdc_thrd_attr_kind* attrs[] = { \
76+
&priority_attr.kind, \
77+
&name_attr.kind, \
78+
}; \
79+
\
80+
int t0_id = 0xF3; \
81+
ztdc_thrd_create_attrs(&t0, thrd_main, &t0_id, ztdc_c_array_size(attrs), attrs); \
82+
int res0 = 0; \
83+
thrd_join(t0, &res0); \
8484
REQUIRE(res0 == 0xF3)
8585

8686
SECTION("c8/u8/ascii") {
87-
MAKE_TEST_BRACKET(c8, u8"meow?!", u8"meow?!", c8);
87+
MAKE_TEST_BRACKET(c8, u8"meow?!", ztd_char8_t, c8, u8"meow?!", ztd_char8_t);
8888
}
8989
SECTION("c16/u/ascii") {
90-
MAKE_TEST_BRACKET(c16, u"meow?!", u8"meow?!", c8);
90+
MAKE_TEST_BRACKET(c16, u"meow?!", ztd_char16_t, c8, u8"meow?!", ztd_char8_t);
9191
}
9292
SECTION("c32/U/ascii") {
93-
MAKE_TEST_BRACKET(c32, U"meow?!", u8"meow?!", c8);
93+
MAKE_TEST_BRACKET(c32, U"meow?!", ztd_char32_t, c8, u8"meow?!", ztd_char8_t);
9494
}
9595
SECTION("mc//ascii") {
96-
MAKE_TEST_BRACKET(mc, "meow?!", u8"meow?!", c8);
96+
MAKE_TEST_BRACKET(mc, "meow?!", ztd_char_t, c8, u8"meow?!", ztd_char8_t);
9797
}
9898
SECTION("mwc/L/ascii") {
99-
MAKE_TEST_BRACKET(mwc, L"meow?!", u8"meow?!", c8);
99+
MAKE_TEST_BRACKET(mwc, L"meow?!", ztd_wchar_t, c8, u8"meow?!", ztd_char8_t);
100100
}
101101
SECTION("c8/u8/unicode") {
102-
MAKE_TEST_BRACKET(
103-
c8, u8"\u300E\U0001F49A yay! \U0001F49A\u300F", u8"\u300E\U0001F49A yay! \U0001F49A\u300F", c8);
102+
MAKE_TEST_BRACKET(c8, u8"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char8_t, c8,
103+
u8"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char8_t);
104104
}
105105
SECTION("c16/u/unicode") {
106-
MAKE_TEST_BRACKET(
107-
c16, u"\u300E\U0001F49A yay! \U0001F49A\u300F", u8"\u300E\U0001F49A yay! \U0001F49A\u300F", c8);
106+
MAKE_TEST_BRACKET(c16, u"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char16_t, c8,
107+
u8"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char8_t);
108108
}
109109
SECTION("c32/U/unicode") {
110-
MAKE_TEST_BRACKET(
111-
c32, U"\u300E\U0001F49A yay! \U0001F49A\u300F", u8"\u300E\U0001F49A yay! \U0001F49A\u300F", c8);
110+
MAKE_TEST_BRACKET(c32, U"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char32_t, c8,
111+
u8"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char8_t);
112112
}
113113

114114
SECTION("c8/u8/ascii") {
115-
MAKE_TEST_BRACKET(c8, u8"meow?!", u"meow?!", c16);
115+
MAKE_TEST_BRACKET(c8, u8"meow?!", ztd_char8_t, c16, u"meow?!", ztd_char16_t);
116116
}
117117
SECTION("c16/u/ascii") {
118-
MAKE_TEST_BRACKET(c16, u"meow?!", u"meow?!", c16);
118+
MAKE_TEST_BRACKET(c16, u"meow?!", ztd_char16_t, c16, u"meow?!", ztd_char16_t);
119119
}
120120
SECTION("c32/U/ascii") {
121-
MAKE_TEST_BRACKET(c32, U"meow?!", u"meow?!", c16);
121+
MAKE_TEST_BRACKET(c32, U"meow?!", ztd_char32_t, c16, u"meow?!", ztd_char16_t);
122122
}
123123
SECTION("mc//ascii") {
124-
MAKE_TEST_BRACKET(mc, "meow?!", u"meow?!", c16);
124+
MAKE_TEST_BRACKET(mc, "meow?!", ztd_char_t, c16, u"meow?!", ztd_char16_t);
125125
}
126126
SECTION("mwc/L/ascii") {
127-
MAKE_TEST_BRACKET(mwc, L"meow?!", u"meow?!", c16);
127+
MAKE_TEST_BRACKET(mwc, L"meow?!", ztd_wchar_t, c16, u"meow?!", ztd_char16_t);
128128
}
129129
SECTION("c8/u8/unicode") {
130-
MAKE_TEST_BRACKET(
131-
c8, u8"\u300E\U0001F49A yay! \U0001F49A\u300F", u"\u300E\U0001F49A yay! \U0001F49A\u300F", c16);
130+
MAKE_TEST_BRACKET(c8, u8"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char8_t, c16,
131+
u"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char16_t);
132132
}
133133
SECTION("c16/u/unicode") {
134-
MAKE_TEST_BRACKET(
135-
c16, u"\u300E\U0001F49A yay! \U0001F49A\u300F", u"\u300E\U0001F49A yay! \U0001F49A\u300F", c16);
134+
MAKE_TEST_BRACKET(c16, u"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char16_t, c16,
135+
u"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char16_t);
136136
}
137137
SECTION("c32/U/unicode") {
138-
MAKE_TEST_BRACKET(
139-
c32, U"\u300E\U0001F49A yay! \U0001F49A\u300F", u"\u300E\U0001F49A yay! \U0001F49A\u300F", c16);
138+
MAKE_TEST_BRACKET(c32, U"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char32_t, c16,
139+
u"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char16_t);
140140
}
141141

142142
SECTION("c8/u8/ascii") {
143-
MAKE_TEST_BRACKET(c8, u8"meow?!", U"meow?!", c32);
143+
MAKE_TEST_BRACKET(c8, u8"meow?!", ztd_char8_t, c32, U"meow?!", ztd_char32_t);
144144
}
145145
SECTION("c16/u/ascii") {
146-
MAKE_TEST_BRACKET(c16, u"meow?!", U"meow?!", c32);
146+
MAKE_TEST_BRACKET(c16, u"meow?!", ztd_char16_t, c32, U"meow?!", ztd_char32_t);
147147
}
148148
SECTION("c32/U/ascii") {
149-
MAKE_TEST_BRACKET(c32, U"meow?!", U"meow?!", c32);
149+
MAKE_TEST_BRACKET(c32, U"meow?!", ztd_char32_t, c32, U"meow?!", ztd_char32_t);
150150
}
151151
SECTION("mc//ascii") {
152-
MAKE_TEST_BRACKET(mc, "meow?!", U"meow?!", c32);
152+
MAKE_TEST_BRACKET(mc, "meow?!", ztd_char_t, c32, U"meow?!", ztd_char32_t);
153153
}
154154
SECTION("mwc/L/ascii") {
155-
MAKE_TEST_BRACKET(mwc, L"meow?!", U"meow?!", c32);
155+
MAKE_TEST_BRACKET(mwc, L"meow?!", ztd_wchar_t, c32, U"meow?!", ztd_char32_t);
156156
}
157157
SECTION("c8/u8/unicode") {
158-
MAKE_TEST_BRACKET(
159-
c8, u8"\u300E\U0001F49A yay! \U0001F49A\u300F", U"\u300E\U0001F49A yay! \U0001F49A\u300F", c32);
158+
MAKE_TEST_BRACKET(c8, u8"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char8_t, c32,
159+
U"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char32_t);
160160
}
161161
SECTION("c16/u/unicode") {
162-
MAKE_TEST_BRACKET(
163-
c16, u"\u300E\U0001F49A yay! \U0001F49A\u300F", U"\u300E\U0001F49A yay! \U0001F49A\u300F", c32);
162+
MAKE_TEST_BRACKET(c16, u"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char16_t, c32,
163+
U"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char32_t);
164164
}
165165
SECTION("c32/U/unicode") {
166-
MAKE_TEST_BRACKET(
167-
c32, U"\u300E\U0001F49A yay! \U0001F49A\u300F", U"\u300E\U0001F49A yay! \U0001F49A\u300F", c32);
166+
MAKE_TEST_BRACKET(c32, U"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char32_t, c32,
167+
U"\u300E\U0001F49A yay! \U0001F49A\u300F", ztd_char32_t);
168+
}
169+
170+
SECTION("c8/u8/ascii") {
171+
MAKE_TEST_BRACKET(c8, u8"meow?!", ztd_char8_t, mc, "meow?!", ztd_char_t);
172+
}
173+
SECTION("c16/u/ascii") {
174+
MAKE_TEST_BRACKET(c16, u"meow?!", ztd_char16_t, mc, "meow?!", ztd_char_t);
175+
}
176+
SECTION("c32/U/ascii") {
177+
MAKE_TEST_BRACKET(c32, U"meow?!", ztd_char32_t, mc, "meow?!", ztd_char_t);
178+
}
179+
SECTION("mc//ascii") {
180+
MAKE_TEST_BRACKET(mc, "meow?!", ztd_char_t, mc, "meow?!", ztd_char_t);
181+
}
182+
SECTION("mwc/L/ascii") {
183+
MAKE_TEST_BRACKET(mwc, L"meow?!", ztd_wchar_t, mc, "meow?!", ztd_char_t);
184+
}
185+
186+
SECTION("c8/u8/ascii") {
187+
MAKE_TEST_BRACKET(c8, u8"meow?!", ztd_char8_t, mwc, L"meow?!", ztd_wchar_t);
188+
}
189+
SECTION("c16/u/ascii") {
190+
MAKE_TEST_BRACKET(c16, u"meow?!", ztd_char16_t, mwc, L"meow?!", ztd_wchar_t);
191+
}
192+
SECTION("c32/U/ascii") {
193+
MAKE_TEST_BRACKET(c32, U"meow?!", ztd_char32_t, mwc, L"meow?!", ztd_wchar_t);
194+
}
195+
SECTION("mc//ascii") {
196+
MAKE_TEST_BRACKET(mc, "meow?!", ztd_char_t, mwc, L"meow?!", ztd_wchar_t);
197+
}
198+
SECTION("mwc/L/ascii") {
199+
MAKE_TEST_BRACKET(mwc, L"meow?!", ztd_wchar_t, mwc, L"meow?!", ztd_wchar_t);
168200
}
169201
}

0 commit comments

Comments
 (0)