Skip to content

Commit 0481f78

Browse files
authored
Convert more core tests from C++ to C. NFC (#21584)
See #12756
1 parent 2316d8a commit 0481f78

File tree

5 files changed

+31
-34
lines changed

5 files changed

+31
-34
lines changed

test/core/test_sizeof.cpp renamed to test/core/test_sizeof.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ struct A {
1212
};
1313

1414
int main(int argc, const char *argv[]) {
15-
int *a = new int[10];
16-
int *b = new int[1];
17-
int *c = new int[10];
15+
int a[10];
16+
int b[1];
17+
int c[10];
1818
for (int i = 0; i < 10; i++) a[i] = 2;
1919
*b = 5;
2020
for (int i = 0; i < 10; i++) c[i] = 8;
@@ -24,14 +24,11 @@ int main(int argc, const char *argv[]) {
2424
printf("*%d,%d,%d,%d,%d*\n", a[0], a[9], *b, c[0], c[9]);
2525

2626
// Part 2
27-
A as[3] = {{5, 12}, {6, 990}, {7, 2}};
28-
memcpy(&as[0], &as[2], sizeof(A));
27+
struct A as[3] = {{5, 12}, {6, 990}, {7, 2}};
28+
memcpy(&as[0], &as[2], sizeof(struct A));
2929

3030
printf("*%d,%d,%d,%d,%d,%d*\n", as[0].x, as[0].y, as[1].x, as[1].y, as[2].x,
3131
as[2].y);
3232

33-
delete[] a;
34-
delete[] b;
35-
delete[] c;
3633
return 0;
3734
}

test/core/test_sscanf_hex.cpp renamed to test/core/test_sscanf_hex.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,25 @@
44
// found in the LICENSE file.
55

66
#include <stdio.h>
7-
#include <string>
8-
#include <cstdlib>
7+
#include <string.h>
8+
#include <stdlib.h>
99

1010
int main() {
1111
unsigned int a, b;
1212
sscanf("0x12AB 12AB", "%x %x", &a, &b);
1313
printf("%d %d\n", a, b);
1414

15-
std::string hexstr("0102037F00FF");
16-
const char * cstr = hexstr.c_str();
17-
int len = hexstr.length() / 2;
18-
char * tmp_data = new char[len];
15+
const char* hexstr = "0102037F00FF";
16+
int len = strlen(hexstr) / 2;
17+
char* tmp_data = malloc(len);
1918
for (int i = 0; i < len; i++) {
20-
sscanf(cstr, "%2hhx", &tmp_data[i]);
21-
cstr += 2 * sizeof(char);
19+
sscanf(hexstr, "%2hhx", &tmp_data[i]);
20+
hexstr += 2 * sizeof(char);
2221
}
2322

2423
for (int j = 0; j < len; j++) {
2524
printf("%i, ", tmp_data[j]);
2625
}
2726
printf("\n");
28-
delete[] tmp_data;
27+
free(tmp_data);
2928
}

test/core/test_stack_align.cpp renamed to test/core/test_stack_align.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
#define ALIGN(num_bytes) __attribute__((aligned(num_bytes)))
99

10-
struct Aligned {
10+
typedef struct Aligned {
1111
char ALIGN(4) a4;
1212
char ALIGN(8) a8;
1313
char ALIGN(16) a16;
1414
char ALIGN(32) a32;
15-
};
15+
} Aligned;
1616

1717
__attribute__((noinline))
18-
void Test(const void* p, int size) {
19-
printf("align %d: %zu\n", size, reinterpret_cast<size_t>(p) % size);
18+
void Test(const void* p, intptr_t size) {
19+
printf("align %ld: %zu\n", size, (intptr_t)p % size);
2020
}
2121

2222
int main() {
@@ -26,8 +26,8 @@ int main() {
2626
Test(&a.a16, 16);
2727
Test(&a.a32, 32);
2828

29-
int p = reinterpret_cast<size_t>(&a);
30-
printf("base align: %d, %d, %d, %d\n", p%4, p%8, p%16, p%32);
29+
intptr_t p = (intptr_t)&a;
30+
printf("base align: %ld, %ld, %ld, %ld\n", p%4, p%8, p%16, p%32);
3131

3232
return 0;
3333
}

test/core/test_time.cpp renamed to test/core/test_time.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ int main() {
136136

137137
// Verify that mktime updates the tm struct to the correct date if its values are
138138
// out of range by matching against the return value of localtime.
139-
struct tm tm2 { 0 }, tm_local;
139+
struct tm tm2 = { 0 };
140+
struct tm tm_local;
140141
tm2.tm_sec = tm2.tm_min = tm2.tm_hour = tm2.tm_mday = tm2.tm_mon = tm2.tm_wday =
141142
tm2.tm_yday = 1000;
142143
time_t t2 = mktime(&tm2); localtime_r(&t2, &tm_local);
@@ -182,9 +183,9 @@ int main() {
182183
// Verify time() returns reasonable value (between 2011 and 2030).
183184
time_t t4 = 0;
184185
time(&t4);
185-
timespec ts;
186+
struct timespec ts;
186187
assert(clock_gettime(CLOCK_REALTIME, &ts) == 0);
187-
assert(abs(ts.tv_sec - t4) <= 2);
188+
assert(llabs(ts.tv_sec - t4) <= 2);
188189
printf("time: %d\n", t4 > 1309635200ll && t4 < 1893362400ll);
189190

190191
// Verify difftime() calculates accurate time difference.
@@ -243,11 +244,11 @@ int main() {
243244
printf("timespec_get test 5: %d\n", ts.tv_nsec <= 999999999);
244245

245246
// Verify timespec_get() gets similar time value as clock_gettime
246-
timespec ts_timespec_get;
247+
struct timespec ts_timespec_get;
247248
timespec_get(&ts_timespec_get, TIME_UTC);
248-
timespec ts_clock_gettime;
249+
struct timespec ts_clock_gettime;
249250
clock_gettime(CLOCK_REALTIME, &ts_clock_gettime);
250-
printf("timespec_get test 6: %d\n", abs(ts_timespec_get.tv_sec - ts_clock_gettime.tv_sec) <= 2);
251+
printf("timespec_get test 6: %d\n", llabs(ts_timespec_get.tv_sec - ts_clock_gettime.tv_sec) <= 2);
251252

252253
// verify gmtime() and localtime()
253254
check_gmtime_localtime(0);

test/test_core.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ def test_stack(self):
753753
self.do_core_test('test_stack.c')
754754

755755
def test_stack_align(self):
756-
src = test_file('core/test_stack_align.cpp')
756+
src = test_file('core/test_stack_align.c')
757757

758758
def test():
759759
self.do_runf(src, ['''align 4: 0
@@ -1715,7 +1715,7 @@ def test_mod_globalstruct(self):
17151715
self.do_core_test('test_mod_globalstruct.c')
17161716

17171717
def test_sizeof(self):
1718-
self.do_core_test('test_sizeof.cpp')
1718+
self.do_core_test('test_sizeof.c')
17191719

17201720
def test_llvm_used(self):
17211721
self.do_core_test('test_llvm_used.c')
@@ -2625,15 +2625,15 @@ def test_tcgetattr(self):
26252625
self.do_runf('termios/test_tcgetattr.c', 'success')
26262626

26272627
def test_time(self):
2628-
self.do_core_test('test_time.cpp')
2628+
self.do_core_test('test_time.c')
26292629
for tz in ['EST+05EDT', 'UTC+0', 'CET']:
26302630
print('extra tz test:', tz)
26312631
with env_modify({'TZ': tz}):
26322632
# Run the test with different time zone settings if
26332633
# possible. It seems that the TZ environment variable does not
26342634
# work all the time (at least it's not well respected by
26352635
# Node.js on Windows), but it does no harm either.
2636-
self.do_core_test('test_time.cpp')
2636+
self.do_core_test('test_time.c')
26372637

26382638
def test_timeb(self):
26392639
# Confirms they are called in reverse order
@@ -5345,7 +5345,7 @@ def test_sscanf_caps(self):
53455345
self.do_core_test('test_sscanf_caps.c')
53465346

53475347
def test_sscanf_hex(self):
5348-
self.do_core_test('test_sscanf_hex.cpp')
5348+
self.do_core_test('test_sscanf_hex.c')
53495349

53505350
def test_sscanf_float(self):
53515351
self.do_core_test('test_sscanf_float.c')

0 commit comments

Comments
 (0)