HyriseSQLParser/test/sql_grammar_test.cpp

94 lines
2.6 KiB
C++
Raw Normal View History

2014-11-03 23:26:44 +01:00
#include <stdio.h>
#include <string>
#include <chrono>
#include <fstream>
#include <sstream>
#include <vector>
2014-11-03 23:26:44 +01:00
#include "SQLParser.h"
using namespace hsql;
std::vector<std::string> readlines(std::string path) {
std::ifstream infile(path);
std::vector<std::string> lines;
std::string line;
while (std::getline(infile, line)) {
std::istringstream iss(line);
// Skip comments
if (line[0] != '#') {
lines.push_back(line);
}
}
return lines;
}
#define STREQ(a, b) (std::string(a).compare(std::string(b)) == 0)
2014-11-03 23:26:44 +01:00
int main(int argc, char *argv[]) {
if (argc <= 1) {
fprintf(stderr, "Usage: grammar_test [--false] [-f path] query, ...\n");
2014-11-03 23:26:44 +01:00
return -1;
}
2016-02-27 15:22:22 +01:00
bool expectFalse = false;
bool useFile = false;
std::string filePath = "";
// Parse command line arguments
int i = 1;
for (; i < argc; ++i) {
2016-02-27 15:22:22 +01:00
if (STREQ(argv[i], "--false")) expectFalse = true;
else if (STREQ(argv[i], "-f")) {
2016-02-27 15:22:22 +01:00
useFile = true;
filePath = argv[++i];
} else {
break;
}
}
// Read list of queries for this rest
std::vector<std::string> queries;
2016-02-27 15:22:22 +01:00
if (useFile) {
queries = readlines(filePath);
} else {
for (; i < argc; ++i) queries.push_back(argv[i]);
}
2014-11-03 23:57:42 +01:00
2014-11-03 23:26:44 +01:00
// Execute queries
2016-02-27 15:22:22 +01:00
int numFailed = 0;
for (std::string sql : queries) {
2014-11-03 23:26:44 +01:00
// Measuring the parsing time
std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
// Parsing
SQLParserResult* stmt_list = SQLParser::parseSQLString(sql.c_str());
2014-11-03 23:26:44 +01:00
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
2014-11-26 17:45:59 +01:00
double us = elapsed_seconds.count() * 1000 * 1000;
2014-11-03 23:26:44 +01:00
2016-02-27 15:22:22 +01:00
if (expectFalse == stmt_list->isValid) {
2014-11-26 17:45:59 +01:00
printf("\033[0;31m{ failed}\033[0m\n");
2016-02-27 14:45:59 +01:00
printf("\t\033[0;31m%s (L%d:%d)\n\033[0m", stmt_list->errorMsg, stmt_list->errorLine, stmt_list->errorColumn);
2014-11-26 17:45:59 +01:00
printf("\t%s\n", sql.c_str());
2016-02-27 15:22:22 +01:00
numFailed++;
2014-11-03 23:26:44 +01:00
} else {
2016-02-27 15:22:22 +01:00
// TODO: indicate whether expectFalse was set
2014-11-26 17:45:59 +01:00
printf("\033[0;32m{ ok} (%.1fus)\033[0m %s\n", us, sql.c_str());
2014-11-03 23:26:44 +01:00
}
}
2016-02-27 15:22:22 +01:00
if (numFailed == 0) {
2014-12-02 01:27:02 +01:00
printf("\033[0;32m{ ok} \033[0mAll %lu grammar tests completed successfully!\n", queries.size());
2016-02-27 15:48:20 +01:00
return 0;
} else {
2016-02-27 15:22:22 +01:00
fprintf(stderr, "\033[0;31m{ failed} \033[0mSome grammar tests failed! %d out of %lu tests failed!\n", numFailed, queries.size());
2016-02-27 15:48:20 +01:00
return -1;
}
2014-11-03 23:26:44 +01:00
}