woj-sandbox/test.c

116 lines
2.3 KiB
C
Raw Permalink Normal View History

2022-10-02 14:09:25 +08:00
#include "utils/log.h"
2024-01-01 17:28:39 +08:00
#include <stdio.h>
2022-10-02 16:06:27 +08:00
#include <stdlib.h>
#include <unistd.h>
2022-10-02 14:09:25 +08:00
2024-01-01 17:28:39 +08:00
#define LOG_AND_EXECUTE(func) \
do { \
LOG_INFO("Testing " #func); \
do_##func(); \
} while (0)
#pragma GCC push_options
#pragma GCC optimize("O0")
void do_tle() {
for (volatile int i = 0; i != -1; i++) asm("nop");
}
void do_re1() {
char *p = NULL;
*p = 'a';
printf("%c", *p);
}
void do_re2() {
int a = 0;
int b = 2 / a;
printf("%d", b);
}
void do_mle1() {
for (volatile int i = 0; i != -1; i++) {
int *p = malloc(1);
if (p == NULL) {
LOG_INFO("OK: malloc failed: i=%d", i);
break;
2022-10-20 15:44:03 +08:00
}
2022-10-02 16:06:27 +08:00
}
2024-01-01 17:28:39 +08:00
}
// void do_mle2() {
// static int a[1 << 30];
// printf("%d", a[0]);
// }
void do_block1() {
int a;
for (volatile int i = 0; i != -1; i++) scanf("%d", &a);
printf("%d", a);
}
void do_block2() { sleep(10); }
void do_nproc() {
while (1) {
pid_t pid = fork();
if (pid == -1) {
LOG_INFO("OK: fork failed");
break;
} else if (pid == 0)
LOG_ERR("FAIL: Child process, pid = %d", getpid());
else
LOG_ERR("FAIL: Parent process, pid = %d", pid);
}
}
2022-10-02 16:06:27 +08:00
2024-04-27 21:22:04 +08:00
void do_fsize() {
FILE *fp = fopen("test.dat", "w");
if (fp == NULL) {
LOG_ERR("FAIL: fopen failed");
return;
}
for (int i = 0; i < 1 << 30; i++) {
if (fputc('a', fp) == EOF) {
LOG_INFO("OK: fputc failed: i=%d", i);
break;
}
}
fclose(fp);
}
2024-01-01 17:28:39 +08:00
#pragma GCC pop_options
2022-10-02 16:06:27 +08:00
2024-01-01 17:28:39 +08:00
int main() {
int op;
scanf("%d", &op);
2022-10-02 16:06:27 +08:00
2024-01-01 17:28:39 +08:00
switch (op) {
case 1: LOG_AND_EXECUTE(tle); break;
case 2: LOG_AND_EXECUTE(re1); break;
case 3: LOG_AND_EXECUTE(re2); break;
case 4: LOG_AND_EXECUTE(mle1); break;
// case 5: LOG_AND_EXECUTE(mle2); break;
case 6: LOG_AND_EXECUTE(block1); break;
case 7: LOG_AND_EXECUTE(block2); break;
case 8: LOG_AND_EXECUTE(nproc); break;
2024-04-27 21:22:04 +08:00
case 9: LOG_AND_EXECUTE(fsize); break;
2024-01-01 17:28:39 +08:00
default: {
LOG_INFO("NO TEST SPECIFIED");
break;
}
}
2022-10-09 00:06:07 +08:00
// destroy stdin, stdout, stderr
close(0);
close(1);
close(2);
stdin = NULL;
stdout = NULL;
stderr = NULL;
2022-10-02 14:09:25 +08:00
return 0;
}