2014-10-09 01:30:22 +02:00
/*
* sql_tests . cpp
*/
2014-10-16 15:54:51 +02:00
# include "SQLParser.h"
2014-10-09 01:30:22 +02:00
# include <stdio.h>
# include <string>
# include <cassert>
2014-10-17 16:25:55 +02:00
# include <thread>
2014-10-22 16:48:02 +02:00
# include <time.h>
2014-10-09 01:30:22 +02:00
# define STREQUALS(str1, str2) std::string(str1).compare(std::string(str2)) == 0
# define ASSERT(cond) if (!(cond)) { fprintf(stderr, "failed! Assertion (" #cond ")\n"); return; }
# define ASSERT_STR(STR1, STR2) ASSERT(STREQUALS(STR1, STR2));
void SelectTest1 ( ) {
2014-10-22 17:20:50 +02:00
printf ( " Test: SelectTest1... " ) ; fflush ( stdout ) ;
2014-10-09 01:30:22 +02:00
2014-10-22 16:01:25 +02:00
const char * sql = " SELECT age, name, address from table WHERE age < 12.5; " ;
2014-10-09 04:46:25 +02:00
Statement * sqlStatement = SQLParser : : parseSQL ( sql ) ;
ASSERT ( sqlStatement ! = NULL ) ;
2014-10-20 22:33:36 +02:00
ASSERT ( sqlStatement - > type = = eSelect ) ;
2014-10-09 01:30:22 +02:00
2014-10-09 04:46:25 +02:00
SelectStatement * stmt = ( SelectStatement * ) sqlStatement ;
2014-10-09 01:30:22 +02:00
2014-10-20 22:33:36 +02:00
ASSERT ( stmt - > select_list - > size ( ) = = 3 ) ;
ASSERT_STR ( stmt - > select_list - > at ( 0 ) - > name , " age " ) ;
ASSERT_STR ( stmt - > select_list - > at ( 1 ) - > name , " name " ) ;
ASSERT_STR ( stmt - > select_list - > at ( 2 ) - > name , " address " ) ;
2014-10-09 01:30:22 +02:00
2014-10-20 22:33:36 +02:00
ASSERT ( stmt - > from_table ! = NULL ) ;
ASSERT ( stmt - > from_table - > type = = eTableName ) ;
ASSERT_STR ( stmt - > from_table - > table_names - > at ( 0 ) , " table " ) ;
2014-10-09 04:46:25 +02:00
// WHERE
2014-10-20 22:33:36 +02:00
ASSERT ( stmt - > where_clause ! = NULL ) ;
ASSERT ( stmt - > where_clause - > expr - > type = = eExprColumnRef ) ;
ASSERT_STR ( stmt - > where_clause - > expr - > name , " age " ) ;
2014-10-22 16:30:45 +02:00
ASSERT ( stmt - > where_clause - > pred_type = = SQL_LESS ) ;
2014-10-20 22:33:36 +02:00
ASSERT ( stmt - > where_clause - > expr2 - > type = = eExprLiteralFloat ) ;
ASSERT ( stmt - > where_clause - > expr2 - > float_literal = = 12.5 ) ;
2014-10-09 01:30:22 +02:00
printf ( " passed! \n " ) ;
}
void SelectTest2 ( ) {
2014-10-22 17:20:50 +02:00
printf ( " Test: SelectTest2... " ) ; fflush ( stdout ) ;
2014-10-09 01:30:22 +02:00
2014-10-22 17:18:43 +02:00
const char * sql = " SELECT * FROM (SELECT age FROM table, table2); " ;
2014-10-09 01:30:22 +02:00
Statement * stmt = SQLParser : : parseSQL ( sql ) ;
ASSERT ( stmt ! = NULL ) ;
2014-10-20 22:33:36 +02:00
ASSERT ( stmt - > type = = eSelect ) ;
2014-10-09 01:30:22 +02:00
SelectStatement * select = ( SelectStatement * ) stmt ;
2014-10-22 17:18:43 +02:00
ASSERT ( select - > select_list - > size ( ) = = 1 ) ;
ASSERT_STR ( select - > select_list - > at ( 0 ) - > name , " * " ) ;
2014-10-09 01:30:22 +02:00
2014-10-20 22:33:36 +02:00
ASSERT ( select - > from_table ! = NULL ) ;
ASSERT ( select - > from_table - > type = = eTableSelect ) ;
ASSERT ( select - > from_table - > stmt ! = NULL ) ;
ASSERT ( select - > from_table - > stmt - > select_list - > size ( ) = = 1 ) ;
ASSERT_STR ( select - > from_table - > stmt - > from_table - > table_names - > at ( 0 ) , " table " ) ;
ASSERT_STR ( select - > from_table - > stmt - > from_table - > table_names - > at ( 1 ) , " table2 " ) ;
2014-10-09 01:30:22 +02:00
printf ( " passed! \n " ) ;
}
2014-10-20 22:33:36 +02:00
uint parse_count = 0 ;
uint conflicts = 0 ;
2014-10-17 16:25:55 +02:00
void SelectTest3 ( bool print ) {
2014-10-22 17:20:50 +02:00
if ( print ) { printf ( " Test: SelectTest3... " ) ; fflush ( stdout ) ; }
2014-10-09 01:30:22 +02:00
const char * sql = " SELECT name, AVG(age) FROM table GROUP BY name " ;
2014-10-17 16:25:55 +02:00
parse_count + + ;
2014-10-09 01:30:22 +02:00
Statement * stmt = SQLParser : : parseSQL ( sql ) ;
2014-10-17 16:25:55 +02:00
2014-10-20 22:33:36 +02:00
if ( parse_count ! = 1 ) conflicts + + ;
2014-10-17 16:25:55 +02:00
parse_count - - ;
2014-10-09 01:30:22 +02:00
ASSERT ( stmt ! = NULL ) ;
2014-10-20 22:33:36 +02:00
ASSERT ( stmt - > type = = eSelect ) ;
2014-10-09 01:30:22 +02:00
SelectStatement * select = ( SelectStatement * ) stmt ;
2014-10-20 22:33:36 +02:00
ASSERT ( select - > select_list - > size ( ) = = 2 ) ;
2014-10-09 01:30:22 +02:00
2014-10-20 22:33:36 +02:00
ASSERT ( select - > select_list - > at ( 0 ) - > type = = eExprColumnRef ) ;
ASSERT ( select - > select_list - > at ( 1 ) - > type = = eExprFunctionRef ) ;
ASSERT_STR ( " name " , select - > select_list - > at ( 0 ) - > name ) ;
2014-10-09 01:30:22 +02:00
2014-10-20 22:33:36 +02:00
ASSERT ( select - > group_by ! = NULL ) ;
ASSERT ( select - > group_by - > size ( ) = = 1 ) ;
ASSERT_STR ( " name " , select - > group_by - > at ( 0 ) - > name ) ;
2014-10-09 01:30:22 +02:00
2014-10-17 16:25:55 +02:00
if ( print ) printf ( " passed! \n " ) ;
2014-10-09 01:30:22 +02:00
}
2014-10-17 16:25:55 +02:00
/** Multithread Test **/
void multithreadTest ( int numberOfRuns , int id ) {
for ( int n = 0 ; n < numberOfRuns ; + + n ) {
SelectTest3 ( false ) ;
}
}
void ThreadSafeTest ( uint numThreads , uint runsPerThread ) {
2014-10-22 16:48:02 +02:00
printf ( " Multithread-Test... " ) ;
2014-10-20 22:33:36 +02:00
conflicts = 0 ;
2014-10-17 16:25:55 +02:00
std : : thread * threads = new std : : thread [ numThreads ] ;
for ( int n = 0 ; n < numThreads ; + + n ) {
threads [ n ] = std : : thread ( multithreadTest , runsPerThread , n ) ;
}
for ( int n = 0 ; n < numThreads ; + + n ) {
threads [ n ] . join ( ) ;
}
2014-10-20 22:33:36 +02:00
printf ( " there were %u concurrent parses... " , conflicts ) ;
2014-10-17 16:25:55 +02:00
printf ( " finished! \n " ) ;
}
2014-10-09 01:30:22 +02:00
2014-10-22 16:48:02 +02:00
/** Performance Test **/
void Benchmark1 ( uint numRuns ) {
printf ( " Benchmarking... " ) ;
clock_t start , end ;
2014-10-22 17:20:50 +02:00
const char * sql = " SELECT SUM(age), name, address FROM (SELECT age FROM (SELECT age FROM (SELECT age FROM table, table2))) WHERE income > 10 GROUP BY age; " ;
2014-10-22 16:48:02 +02:00
start = clock ( ) ;
for ( uint n = 0 ; n < numRuns ; + + n ) {
Statement * stmt = SQLParser : : parseSQL ( sql ) ;
}
end = clock ( ) ;
long diff = end - start ;
printf ( " Total Time: %ld ticks (~%.4fms) \n " , diff , 1000.0 * diff / CLOCKS_PER_SEC ) ;
printf ( " Time per exec: ~%.2f ticks (~%.4fms) \n " , ( double ) diff / numRuns , ( 1000.0 * diff / numRuns ) / CLOCKS_PER_SEC ) ;
printf ( " Benchmarking done! \n " ) ;
}
2014-10-09 01:30:22 +02:00
int main ( int argc , char * argv [ ] ) {
printf ( " \n ###################################### \n " ) ;
printf ( " ## Running all tests... \n \n " ) ;
SelectTest1 ( ) ;
SelectTest2 ( ) ;
2014-10-17 16:25:55 +02:00
SelectTest3 ( true ) ;
ThreadSafeTest ( 10 , 1000 ) ;
2014-10-22 16:48:02 +02:00
Benchmark1 ( 500000 ) ;
2014-10-09 01:30:22 +02:00
printf ( " \n ## Finished running all tests... \n " ) ;
printf ( " ###################################### \n " ) ;
return 0 ;
2014-10-20 22:24:06 +02:00
}