Esempio n. 1
0
static void test_logic_do()
{
    P_COMPARE(run_stmt("do {} while (false);"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("do { if (X == f(Y)) Y = a; else X = f(Y); } while (X !== f(a));"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("do { fail; } while (true);"), P_RESULT_FAIL);
    P_COMPARE(run_stmt_error("do { throw(a); } while (true);", "a"), P_RESULT_ERROR);
    P_COMPARE(run_stmt_error("do {} while (throw(b));", "b"), P_RESULT_ERROR);
    P_COMPARE(run_stmt("do { if (Y == c) X = b; else X = a; Y = c; } while (X !== b);"), P_RESULT_FAIL);
    P_COMPARE(run_stmt("do [X] { if (Y == c) X = b; else X = a; Y = c; } while (X !== b);"), P_RESULT_TRUE);
}
Esempio n. 2
0
static void test_logic_while()
{
    P_COMPARE(run_stmt("while (false) {}"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("while (X !== f(a)) { if (X == f(Y)) Y = a; else X = f(Y); }"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("while (true) { fail; }"), P_RESULT_FAIL);
    P_COMPARE(run_stmt_error("while (true) { throw(a); }", "a"), P_RESULT_ERROR);
    P_COMPARE(run_stmt_error("while (throw(b)) {}", "b"), P_RESULT_ERROR);
    P_COMPARE(run_stmt("while (X !== b) { if (Y == c) X = b; else X = a; Y = c; }"), P_RESULT_FAIL);
    P_COMPARE(run_stmt("while [X] (Z !== d) { if (Y == c) { X = b; Z = d; } else { X = a; } Y = c; }"), P_RESULT_TRUE);
}
Esempio n. 3
0
static void test_logic_for()
{
    P_COMPARE(run_stmt("for (X in []) {}"), P_RESULT_TRUE);
    P_COMPARE(run_stmt_error("for (X in Y) {}", "instantiation_error"), P_RESULT_ERROR);
    P_COMPARE(run_stmt_error("for (X in [a, b, c |Y]) {}", "instantiation_error"), P_RESULT_ERROR);
    P_COMPARE(run_stmt_error("for (X in [a, b, c |f(d)]) {}", "type_error(list, f(d))"), P_RESULT_ERROR);
    P_COMPARE(run_stmt_error("for (X in f(d)) {}", "type_error(list, f(d))"), P_RESULT_ERROR);
    P_COMPARE(run_stmt("for (X in [a, b]) { atom(X); }"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("for (X in [a, b]) { X == a; }"), P_RESULT_FAIL);
    P_COMPARE(run_stmt_error("for (X in [a, b]) { throw(c); }", "c"), P_RESULT_ERROR);
    P_COMPARE(run_stmt("for (X in [a, b]) { Y = X; }"), P_RESULT_FAIL);
    P_COMPARE(run_stmt("for [Y] (X in [a, b]) { Y = X; }"), P_RESULT_TRUE);
}
Esempio n. 4
0
static void test_logic_if_stmt()
{
    P_COMPARE(run_stmt("if (atom(a)) atom(b); else atom(X);"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("if (atom(a)) atom(X); else atom(c);"), P_RESULT_FAIL);
    P_COMPARE(run_stmt("if (atom(X)) atom(X); else atom(c);"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("if (!, atom(X)) atom(a); else atom(c);"), P_RESULT_FAIL);
    P_COMPARE(run_stmt("if (commit, atom(a)) atom(a); else atom(X);"), P_RESULT_TRUE);
    P_COMPARE(run_stmt_error("if (call(X)) atom(a); else atom(X);", "instantiation_error"), P_RESULT_ERROR);
    P_COMPARE(run_stmt_error("if (X) atom(a); else atom(X);", "instantiation_error"), P_RESULT_ERROR);

    P_COMPARE(run_stmt("if (atom(a)) atom(b);"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("if (atom(X)) atom(Y);"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("if (!, atom(X)) atom(b);"), P_RESULT_FAIL);
    P_COMPARE(run_stmt("if (commit, atom(a)) atom(b);"), P_RESULT_TRUE);
}
Esempio n. 5
0
static void test_logic_switch()
{
    P_COMPARE(run_stmt("switch (a) {}"), P_RESULT_FAIL);
    P_COMPARE(run_stmt("switch (a) { default: true; }"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("switch (a) { case X: Y = b; } X == a; Y == b;"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("switch (f(a)) { case g(X): case f(X): Y = b; } X == a; Y == b;"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("switch (f(a)) { case g(X): Y = c; case f(X): Y = b; } X == a; Y == b;"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("switch (f(a)) { case g(X): Y = c; case f(X): Y = b; case Z: Y = d; } X == a; Y == b;"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("switch (f(a)) { case g(X): Y = c; case h(X): Y = b; default: Y = d; } var(X); Y == d;"), P_RESULT_TRUE);
}
Esempio n. 6
0
static void test_logic_catch()
{
    P_COMPARE(run_goal_error("throw(a)", "a"), P_RESULT_ERROR);
    P_COMPARE(run_goal("catch(throw(a), X, Y = caught), Y == caught"), P_RESULT_TRUE);
    P_COMPARE(run_goal("catch(atom(a), X, Y = caught), Y !== caught"), P_RESULT_TRUE);
    P_COMPARE(run_goal_error("catch(throw(a), b, Y = caught)", "a"), P_RESULT_ERROR);
    P_COMPARE(run_goal_error("catch(call(1.5), b, Y = caught)", "type_error(callable, 1.5)"), P_RESULT_ERROR);
    P_COMPARE(run_goal("catch(throw(a), X, fail)"), P_RESULT_FAIL);
    P_COMPARE(run_goal("catch(atom(a), X, fail)"), P_RESULT_TRUE);
    P_COMPARE(run_goal_error("catch(throw(a), X, throw(b))", "b"), P_RESULT_ERROR);
    P_COMPARE(run_goal("catch(catch(throw(a), X, throw(b)), Z, Y = caught), Y == caught"), P_RESULT_TRUE);

    P_COMPARE(run_stmt("try { throw(a); } catch(X) { Y = caught; } Y == caught;"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("try { atom(a); } catch(X) { Y = caught; } Y !== caught;"), P_RESULT_TRUE);
    P_COMPARE(run_stmt_error("try { throw(a); } catch(b) { Y = caught; }", "a"), P_RESULT_ERROR);
    P_COMPARE(run_stmt_error("try { call(1.5); } catch(b) { Y = caught; }", "type_error(callable, 1.5)"), P_RESULT_ERROR);
    P_COMPARE(run_stmt("try { throw(a); } catch(X) { fail; }"), P_RESULT_FAIL);
    P_COMPARE(run_stmt("try { atom(a); } catch(X) { fail; }"), P_RESULT_TRUE);
    P_COMPARE(run_stmt_error("try { throw(a); } catch(X) { throw(b); }", "b"), P_RESULT_ERROR);
    P_COMPARE(run_stmt_error("try { throw(a); } catch(X) { throw(b); } catch(Z) { Y = caught; }", "b"), P_RESULT_ERROR);
    P_COMPARE(run_stmt("try { throw(a); } catch(b) { throw(b); } catch(Z) { Y = caught; }; Y == caught;"), P_RESULT_TRUE);
    P_COMPARE(run_stmt("try { try { throw(a); } catch(X) { throw(b); } } catch(Z) { Y = caught; } Y == caught;"), P_RESULT_TRUE);

    P_COMPARE(run_stmt_error("X = f(d); throw(type_error(list, X));", "type_error(list, f(d))"), P_RESULT_ERROR);

    P_COMPARE(run_goal("catch(true, X, fail), throw(t)"), P_RESULT_ERROR);
    P_COMPARE(run_goal("catch(throw(t), X, fail)"), P_RESULT_FAIL);
    P_COMPARE(run_goal_error("catch(throw(t), X, throw(u))", "u"), P_RESULT_ERROR);
}
Esempio n. 7
0
void * mysql_thread(int tid) {
	std::mt19937 mt_rand(time(0));

	thread_data_t *THD;
	THD=GloThrData[tid];

	// in this version, each mysql thread has just ONE connection
	// for now we use blocking API
	MYSQL *mysql;

	//MYSQL_STMT **stmt;

	// we intialize the local mapping : MySQL_STMTs_local()
	MySQL_STMTs_local *local_stmts=new MySQL_STMTs_local();

	// we initialize a MYSQL structure
	THD->mysql = mysql_init(NULL);
	mysql=THD->mysql;

	char buff[128];
	unsigned int bl=0;

	// we establish a connection to the database
	if (!mysql_real_connect(mysql,"127.0.0.1",USER,"",SCHEMA,3306,NULL,0)) {
		fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(mysql));
		exit(EXIT_FAILURE);
	}
	int i;

	// array of (MYSQL_STMT *) ; we don't use it in this version
	//stmt=(MYSQL_STMT **)malloc(sizeof(MYSQL_STMT*)*NUMPREP);
	
	MYSQL_STMT *stmt;
	{
	cpu_timer t;
	// in this loop we create only some the prepared statements
	for (i=0; i<NUMPREP/100; i++) {
		sprintf(buff,"SELECT %u + ?",(uint32_t)mt_rand()%NUMPRO);
		bl=strlen(buff);
		uint64_t hash=local_stmts->compute_hash(0,(char *)USER,(char *)SCHEMA,buff,bl);
		MySQL_STMT_Global_info *a=GloMyStmt->find_prepared_statement_by_hash(hash);
		if (a==NULL) { // no prepared statement was found in global
			stmt = mysql_stmt_init(mysql);
			if (!stmt) {
				fprintf(stderr, " mysql_stmt_init(), out of memory\n");
				exit(EXIT_FAILURE);
			}
			if (mysql_stmt_prepare(stmt, buff, bl)) { // the prepared statement is created
				fprintf(stderr, " mysql_stmt_prepare(), failed: %s\n" , mysql_stmt_error(stmt));
				exit(EXIT_FAILURE);
			}
			MySQL_STMT_Global_info *stmt_info=NULL;
			stmt_info=GloMyStmt->add_prepared_statement(0,(char *)USER,(char *)SCHEMA,buff,bl,stmt);
			uint32_t stmid=stmt_info->statement_id;
			if (NUMPRO < 32)
				fprintf(stdout, "SERVER_statement_id=%lu , PROXY_statement_id=%u\n", stmt->stmt_id, stmid);
			local_stmts->insert(stmid,stmt);
			}
	}
	fprintf(stdout, "Prepared statements: %u client, %u proxy/server. ", i, GloMyStmt->total_prepared_statements());
	fprintf(stdout, "Created in: ");
	}
	{
		unsigned int founds=0;
		cpu_timer t;
		for (i=0; i<NUMPREP*LOOPS; i++) {
			sprintf(buff,"SELECT %u + ?",(uint32_t)mt_rand()%NUMPRO);
			bl=strlen(buff);
			//uint64_t hash=local_stmts->compute_hash(0,(char *)USER,(char *)SCHEMA,buff,bl);
			//MySQL_STMT_Global_info *a=GloMyStmt->find_prepared_statement_by_hash(hash);
			//if (a) founds++;
		}
		fprintf(stdout, "Computed %u random strings in: ", i);
	}
	{
		unsigned int founds=0;
		cpu_timer t;
		for (i=0; i<NUMPREP*LOOPS; i++) {
			sprintf(buff,"SELECT %u + ?",(uint32_t)mt_rand()%NUMPRO);
			bl=strlen(buff);
			uint64_t hash=local_stmts->compute_hash(0,(char *)USER,(char *)SCHEMA,buff,bl);
			//MySQL_STMT_Global_info *a=GloMyStmt->find_prepared_statement_by_hash(hash);
			//if (a) founds++;
		}
		fprintf(stdout, "Computed %u hashes in: ", i);
	}
	{
		unsigned int founds=0;
		cpu_timer t;
		for (i=0; i<NUMPREP*LOOPS; i++) {
			sprintf(buff,"SELECT %u + ?",(uint32_t)mt_rand()%NUMPRO);
			bl=strlen(buff);
			uint64_t hash=local_stmts->compute_hash(0,(char *)USER,(char *)SCHEMA,buff,bl);
			MySQL_STMT_Global_info *a=GloMyStmt->find_prepared_statement_by_hash(hash);
			if (a) founds++;
		}
		fprintf(stdout, "Found    %u prepared statements searching by hash in: ", founds);
	}

	{
		unsigned int founds=0;
		unsigned int created=0;
		unsigned int executed=0;
		cpu_timer t;
		for (i=0; i<NUMPREP*LOOPS; i++) {
			sprintf(buff,"SELECT %u + ?",(uint32_t)mt_rand()%NUMPRO);
			bl=strlen(buff);
			uint64_t hash=local_stmts->compute_hash(0,(char *)USER,(char *)SCHEMA,buff,bl);
			MySQL_STMT_Global_info *a=GloMyStmt->find_prepared_statement_by_hash(hash);
			if (a) {
				// we have a prepared statement, we can run it
				MYSQL_STMT *stm=local_stmts->find(a->statement_id);
				if (stm) { // the statement exists in local
					run_stmt(stm,(uint32_t)mt_rand());
					founds++;
					executed++;
					local_stmts->erase(a->statement_id);
				} else { // the statement doesn't exist locally
					stmt = mysql_stmt_init(mysql);	
					if (!stmt) {
						fprintf(stderr, " mysql_stmt_init(), out of memory\n");
						exit(EXIT_FAILURE);
					}
					if (mysql_stmt_prepare(stmt, buff, bl)) { // the prepared statement is created
						fprintf(stderr, " mysql_stmt_prepare(), failed: %s\n" , mysql_stmt_error(stmt));
						exit(EXIT_FAILURE);
					}
					local_stmts->insert(a->statement_id,stmt);
					run_stmt(stmt,(uint32_t)mt_rand());
					created++;
					executed++;
					local_stmts->erase(a->statement_id);
				}
			} else { // no prepared statement was found in global
				stmt = mysql_stmt_init(mysql);	
				if (!stmt) {
					fprintf(stderr, " mysql_stmt_init(), out of memory\n");
					exit(EXIT_FAILURE);
				}
				if (mysql_stmt_prepare(stmt, buff, bl)) { // the prepared statement is created
					fprintf(stderr, " mysql_stmt_prepare(), failed: %s\n" , mysql_stmt_error(stmt));
					exit(EXIT_FAILURE);
				}
				MySQL_STMT_Global_info *stmt_info=NULL;
				stmt_info=GloMyStmt->add_prepared_statement(0,(char *)USER,(char *)SCHEMA,buff,bl,stmt);
				uint32_t stmid=stmt_info->statement_id;
				if (NUMPRO < 32)
					fprintf(stdout, "SERVER_statement_id=%lu , PROXY_statement_id=%u\n", stmt->stmt_id, stmid);
				local_stmts->insert(stmid,stmt);
				run_stmt(stmt,(uint32_t)mt_rand());
				created++;
				executed++;
				local_stmts->erase(stmid);
			}
		}
		fprintf(stdout, "Found %u , created %u and executed %u prepared statements in: ", founds, created, executed);
	}
/*
	{
		// for comparison, we run also queries in TEXT protocol
		cpu_timer t;
		for (i=0; i<NUMPREP*LOOPS; i++) {
			sprintf(buff,"SELECT %u + %u",i,(uint32_t)mt_rand()%NUMPRO);
			bl=strlen(buff);
			int rc=mysql_real_query(mysql,buff,bl);
			if (rc) {
				fprintf(stderr, " mysql_real_query(), failed: %s\n" , mysql_error(mysql));
				exit(EXIT_FAILURE);
			}
			MYSQL_RES *res=mysql_store_result(mysql);
			if (res==NULL) {
				fprintf(stderr, " mysql_store_result(), failed: %s\n" , mysql_error(mysql));
				exit(EXIT_FAILURE);
			}
			mysql_free_result(res);
		}
		fprintf(stdout, "Executed %u queries in: ", i);
	}
	return 0;
*/
}