Skip to content
Snippets Groups Projects
Verified Commit 85626f9f authored by Recolic Keghart's avatar Recolic Keghart
Browse files

> Manual commit: Finished expr test gen.

U201614531
recolic
Linux RECOLICPC 5.4.2-arch1-1 #1 SMP PREEMPT Thu, 05 Dec 2019 12:29:40 +0000 x86_64 GNU/Linux
 01:48:48 up 3 days,  9:07,  1 user,  load average: 0.83, 1.31, 1.24
69a22b1db808ab4796bd38efb2e0838c972396e5
parent 9de1dd73
No related branches found
No related tags found
No related merge requests found
......@@ -105,7 +105,6 @@ uint32_t expr(char *e, bool *success) {
return 0;
}
/* TODO: Insert codes to evaluate the expression. */
try {
auto res = parse_one(std::string(e));
*success = true;
......
APP=gen-expr
$(APP): gen-expr.c
gcc -O2 -Wall -Werror -o $@ $<
$(APP): gen-expr.cc
g++ -O2 -Wall -o $@ $< -g
.PHONY: clean
clean:
......
......@@ -5,14 +5,45 @@
#include <assert.h>
#include <string.h>
#include <string>
#include <array>
#include <random>
#include <chrono>
static std::mt19937 rand_generator(std::chrono::system_clock::now().time_since_epoch().count());
#define RANDFLOAT ((float)(rand_generator() % (1<<30)) / (float)(1<<30))
static std::string do_gen_rand_expr(int depth) {
if(depth > 64) {
return std::to_string(rand() % 100);
}
if(RANDFLOAT < 0.4) {
auto op = std::array<std::string, 16> {"+", "-", "*", "==", "!=", "&&"} [rand() % 6];
return do_gen_rand_expr(depth+1) + op + do_gen_rand_expr(depth+1);
}
if(RANDFLOAT < 0.4) {
return "(" + do_gen_rand_expr(depth+1) + ")";
}
if(RANDFLOAT < 0.2) {
auto tmp = do_gen_rand_expr(depth+1);
if(tmp[0] != '-') tmp = '-' + tmp;
return tmp;
}
return std::to_string(rand() % 10000);
}
#undef RANDFLOAT
// this should be enough
static char buf[65536];
static inline void gen_rand_expr() {
buf[0] = '\0';
auto res = do_gen_rand_expr(0);
while(res.size() > 512 || res.size() < 16)
res = do_gen_rand_expr(0);
strcpy(buf, res.c_str());
}
static char code_buf[65536];
static char *code_format =
static const char *code_format =
"#include <stdio.h>\n"
"int main() { "
" unsigned result = %s; "
......@@ -21,8 +52,6 @@ static char *code_format =
"}";
int main(int argc, char *argv[]) {
int seed = time(0);
srand(seed);
int loop = 1;
if (argc > 1) {
sscanf(argv[1], "%d", &loop);
......@@ -38,6 +67,7 @@ int main(int argc, char *argv[]) {
fputs(code_buf, fp);
fclose(fp);
// One test one compile: extremely slow, unusable.
int ret = system("gcc .code.c -o .expr");
if (ret != 0) continue;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment