TODO: implement

This commit is contained in:
Synth Magic 2024-11-28 23:28:30 +08:00
parent aac3171e9e
commit d7a8e0659e
1 changed files with 34 additions and 135 deletions

169
snippet.c
View File

@ -12,85 +12,24 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <vadefs.h> #include <vadefs.h>
#define MAX_IONUM 100000
// Time Limit (normally 1s) // Time Limit (normally 1s)
// 题目的时限(通常为1秒) // 题目的时限(通常为1秒)
const unsigned int case_millseconds = 1000; const unsigned int case_millseconds = 1000;
bool exception_happens = false;
unsigned int curr_testcase = 0, sum_mark = 0; unsigned int curr_testcase = 0, sum_mark = 0;
size_t cur_output = 0; size_t cur_output = 0;
#define MAX_IONUM 100000
char output_buffer[MAX_IONUM];
#if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN64) #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN64)
#include <windows.h> #include <windows.h>
static LPTOP_LEVEL_EXCEPTION_FILTER prev = NULL; HANDLE child_stdout_r = NULL;
const DWORD EXCEPTIONS[20] = {EXCEPTION_ACCESS_VIOLATION, void create_process() {
EXCEPTION_ARRAY_BOUNDS_EXCEEDED, PROCESS_INFORMATION piProcInfo;
EXCEPTION_DATATYPE_MISALIGNMENT, STARTUPINFO siStartInfo;
EXCEPTION_FLT_DENORMAL_OPERAND, BOOL bSuccess = FALSE;
EXCEPTION_FLT_DIVIDE_BY_ZERO, ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
EXCEPTION_FLT_INEXACT_RESULT, ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
EXCEPTION_FLT_INVALID_OPERATION,
EXCEPTION_FLT_OVERFLOW,
EXCEPTION_FLT_STACK_CHECK,
EXCEPTION_FLT_UNDERFLOW,
EXCEPTION_GUARD_PAGE,
EXCEPTION_ILLEGAL_INSTRUCTION,
EXCEPTION_INT_DIVIDE_BY_ZERO,
EXCEPTION_INT_OVERFLOW,
EXCEPTION_INVALID_DISPOSITION,
EXCEPTION_INVALID_HANDLE,
EXCEPTION_IN_PAGE_ERROR,
EXCEPTION_NONCONTINUABLE_EXCEPTION,
EXCEPTION_PRIV_INSTRUCTION,
EXCEPTION_STACK_OVERFLOW};
// NOT WORKING
LONG WINAPI exception_filter(EXCEPTION_POINTERS *exception_pointers) {
EXCEPTION_RECORD *const exception_record =
exception_pointers->ExceptionRecord;
const DWORD code = exception_record->ExceptionCode;
unsigned int i;
for (i = 0; i < 20; i++) {
if (code == EXCEPTIONS[i]) {
printf("\x1b[31mCatching Exception at %p,This testcase will be marked as "
"Runtime Error\x1b[0m\n",
exception_record->ExceptionAddress);
exception_happens = true;
return EXCEPTION_EXECUTE_HANDLER;
}
}
return EXCEPTION_CONTINUE_SEARCH;
}
FILE *memopen(const char *buf, const char *mode) {
char temppath[MAX_PATH - 13];
if (0 == GetTempPath(sizeof(temppath), temppath)) {
printf("\x1b[31mERROR:While creating tempoary file,Can't get temp "
"path\x1b[0m\n");
return NULL;
}
char filename[MAX_PATH + 1];
if (0 == GetTempFileName(temppath, "TMP", 0, filename)) {
printf("\x1b[31mERROR:While creating tempoary file,Can't get file "
"name\x1b[0m\n");
return NULL;
}
FILE *f;
if ((f = fopen(filename, "w+")) == NULL) {
printf("\x1b[31mERROR:While creating tempoary file,The file '%s' was not "
"opened\x1b[0m\n",
filename);
return NULL;
}
fputs(buf, f);
fclose(f);
FILE *f2;
if ((f2 = fopen(filename, mode)) != NULL)
return f2;
else
return NULL;
} }
#elif defined(__linux__) #elif defined(__linux__)
#define memopen fmemopen #warning not finished
#endif #endif
#if defined(__APPLE__) || defined(__MACH__) || defined(BSD) #if defined(__APPLE__) || defined(__MACH__) || defined(BSD)
#error Unsupported OS #error Unsupported OS
@ -118,73 +57,33 @@ unsigned int crc32f(unsigned char *message) {
} }
return ~crc; return ~crc;
} }
int a_plus_b(int a, int b) { return a + b; }
/**
TESTCASE(1) {
void tprintf(const char *format, ...) {
char *buffer;
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
size_t len = strlen(buffer);
memcpy(output_buffer + cur_output, buffer, sizeof(char) * len);
va_end(args);
cur_output += len;
}
#define tscanf(format, ...) fscanf(in, format, __VA_ARGS__)
void solve(FILE *);
unsigned int run_testcase(const char *input, unsigned int crc32,
unsigned int mark) {
curr_testcase++;
sum_mark += mark;
unsigned int ret = mark;
printf("\x1b[34mJudging Testcase #%d\x1b[0m\n", curr_testcase);
FILE *in_fp = memopen(input, "r+");
if (in_fp == NULL) {
printf("\x1b[31mFATAL ERROR: Cannot Create stdin file,aborting\x1b[0m\n");
exit(1);
} }
#ifdef _WIN32 subprocess:
prev = SetUnhandledExceptionFilter(exception_filter); if result present in time limit * 2 : print crc and time
#endif if not present & still running : TLE
struct timespec then, now; if exit != 0 : RE
clock_gettime(CLOCK_REALTIME, &then); */
solve(in_fp);
clock_gettime(CLOCK_REALTIME, &now); #define BEGIN_TESTCASE switch (atoi(argv[2])) {
#ifdef _WIN32 #define TESTCASE(i, x) \
SetUnhandledExceptionFilter(prev); case i: { \
prev = NULL; x; \
#endif break; \
fclose(in_fp); }
if ((now.tv_sec - then.tv_sec) * 1000 + #define END_TESTCASE \
(now.tv_nsec - then.tv_nsec) / 1000000 >= default: \
case_millseconds) { break; \
printf("\x1b[34mTime Limit Excceed on Testcase #%d\x1b[0m\n", }
curr_testcase); void test() {}
ret = 0; int main(int argc, const char *argv[]) {
} else if (crc32f((unsigned char *)trim(output_buffer)) != crc32) { if (argc == 3) {
printf("\x1b[31mWrong Answer on Testcase #%d\x1b[0m\n", curr_testcase); BEGIN_TESTCASE
ret = 0; TESTCASE(1, printf("1"))
} else if (exception_happens) { END_TESTCASE
printf("\x1b[95mRuntime Error on Testcase #%d\x1b[0m\n", curr_testcase);
ret = 0;
} else { } else {
printf("\x1b[32mAccepted on Testcase #%d\x1b[0m\n", curr_testcase);
} }
memset((void *)output_buffer, 0, MAX_IONUM);
cur_output = 0;
exception_happens = false;
return ret;
}
// 你的代码放到这里
void solve(FILE *in) { int a = 1 / 0; }
int main(void) {
unsigned int total_get = 0;
printf("\x1b[34mStart Judging...\x1b[0m\n");
total_get += run_testcase("1 2", 1842515611, 50);
total_get += run_testcase("114514 1919810", 3586865669, 50);
printf("\x1b[34mTotal testcase %d\nTotal mark %d/%d\x1b[0m", curr_testcase,
total_get, sum_mark);
return 0;
} }