Skip to content

Commit

Permalink
Convert more core tests from C++ to C. NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Mar 21, 2024
1 parent d5ef873 commit 970c5e1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 34 deletions.
13 changes: 5 additions & 8 deletions test/core/test_sizeof.cpp → test/core/test_sizeof.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ struct A {
};

int main(int argc, const char *argv[]) {
int *a = new int[10];
int *b = new int[1];
int *c = new int[10];
int a[10];
int b[1];
int c[10];
for (int i = 0; i < 10; i++) a[i] = 2;
*b = 5;
for (int i = 0; i < 10; i++) c[i] = 8;
Expand All @@ -24,14 +24,11 @@ int main(int argc, const char *argv[]) {
printf("*%d,%d,%d,%d,%d*\n", a[0], a[9], *b, c[0], c[9]);

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

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

delete[] a;
delete[] b;
delete[] c;
return 0;
}
17 changes: 8 additions & 9 deletions test/core/test_sscanf_hex.cpp → test/core/test_sscanf_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@
// found in the LICENSE file.

#include <stdio.h>
#include <string>
#include <cstdlib>
#include <string.h>
#include <stdlib.h>

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

std::string hexstr("0102037F00FF");
const char * cstr = hexstr.c_str();
int len = hexstr.length() / 2;
char * tmp_data = new char[len];
const char* hexstr = "0102037F00FF";
int len = strlen(hexstr) / 2;
char* tmp_data = malloc(len);
for (int i = 0; i < len; i++) {
sscanf(cstr, "%2hhx", &tmp_data[i]);
cstr += 2 * sizeof(char);
sscanf(hexstr, "%2hhx", &tmp_data[i]);
hexstr += 2 * sizeof(char);
}

for (int j = 0; j < len; j++) {
printf("%i, ", tmp_data[j]);
}
printf("\n");
delete[] tmp_data;
free(tmp_data);
}
12 changes: 6 additions & 6 deletions test/core/test_stack_align.cpp → test/core/test_stack_align.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

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

struct Aligned {
typedef struct Aligned {
char ALIGN(4) a4;
char ALIGN(8) a8;
char ALIGN(16) a16;
char ALIGN(32) a32;
};
} Aligned;

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

int main() {
Expand All @@ -26,8 +26,8 @@ int main() {
Test(&a.a16, 16);
Test(&a.a32, 32);

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

return 0;
}
Expand Down
13 changes: 7 additions & 6 deletions test/core/test_time.cpp → test/core/test_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ int main() {

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

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

// Verify timespec_get() gets similar time value as clock_gettime
timespec ts_timespec_get;
struct timespec ts_timespec_get;
timespec_get(&ts_timespec_get, TIME_UTC);
timespec ts_clock_gettime;
struct timespec ts_clock_gettime;
clock_gettime(CLOCK_REALTIME, &ts_clock_gettime);
printf("timespec_get test 6: %d\n", abs(ts_timespec_get.tv_sec - ts_clock_gettime.tv_sec) <= 2);
printf("timespec_get test 6: %d\n", llabs(ts_timespec_get.tv_sec - ts_clock_gettime.tv_sec) <= 2);

// verify gmtime() and localtime()
check_gmtime_localtime(0);
Expand Down
10 changes: 5 additions & 5 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ def test_stack(self):
self.do_core_test('test_stack.c')

def test_stack_align(self):
src = test_file('core/test_stack_align.cpp')
src = test_file('core/test_stack_align.c')

def test():
self.do_runf(src, ['''align 4: 0
Expand Down Expand Up @@ -1715,7 +1715,7 @@ def test_mod_globalstruct(self):
self.do_core_test('test_mod_globalstruct.c')

def test_sizeof(self):
self.do_core_test('test_sizeof.cpp')
self.do_core_test('test_sizeof.c')

def test_llvm_used(self):
self.do_core_test('test_llvm_used.c')
Expand Down Expand Up @@ -2625,15 +2625,15 @@ def test_tcgetattr(self):
self.do_runf('termios/test_tcgetattr.c', 'success')

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

def test_timeb(self):
# Confirms they are called in reverse order
Expand Down Expand Up @@ -5345,7 +5345,7 @@ def test_sscanf_caps(self):
self.do_core_test('test_sscanf_caps.c')

def test_sscanf_hex(self):
self.do_core_test('test_sscanf_hex.cpp')
self.do_core_test('test_sscanf_hex.c')

def test_sscanf_float(self):
self.do_core_test('test_sscanf_float.c')
Expand Down

0 comments on commit 970c5e1

Please sign in to comment.