void test_parse_with_prefix() { struct raw_event* raw; raw = lst_parse(":prefix TEST This is a message test with prefix"); mu_assert(s_eq(raw->prefix, "prefix"), "test_parse_with_prefix: prefix should be 'prefix'"); mu_assert(s_eq(raw->type, "TEST"), "test_parse_with_prefix: type should be 'TEST'"); mu_assert(raw->num_params == 7, "test_parse_with_prefix: there should be 7 parameters"); evt_raw_destroy(raw); /* Cleanup */ }
void test_parse_with_prefix_and_last_param() { char* last_param; struct raw_event* raw; raw = lst_parse(":prefix TEST This is a message test with a :last parameter"); last_param = raw->params[raw->num_params - 1]; mu_assert(s_eq(raw->prefix, "prefix"), "test_parse_with_prefix_and_last_param: prefix should be 'prefix'"); mu_assert(s_eq(raw->type, "TEST"), "test_parse_with_prefix_and_last_param: type should be 'TEST'"); mu_assert(raw->num_params == 8, "test_parse_with_prefix_and_last_param: there should be 8 parameters"); mu_assert(s_eq(last_param, "last parameter"), "test_parse_with_prefix_and_last_param: last parameter should be 'last parameter'"); evt_raw_destroy(raw); /* Cleanup */ }
void test_parse_with_prefix_only_last_param() { char* last_param; struct raw_event* raw; raw = lst_parse(":prefix TEST :only last parameter"); last_param = raw->params[raw->num_params - 1]; mu_assert(s_eq(raw->prefix, "prefix"), "test_parse_with_prefix_only_last_param: prefix should be 'prefix'"); mu_assert(s_eq(raw->type, "TEST"), "test_parse_with_prefix_only_last_param: type should be 'TEST'"); mu_assert(raw->num_params == 1, "test_parse_with_prefix_only_last_param: there should be 1 parameters"); mu_assert(s_eq(last_param, "only last parameter"), "test_parse_with_prefix_only_last_param: last parameter should be 'only last parameter'"); evt_raw_destroy(raw); /* Cleanup */ }
/// Parse `name = value`. static bool parseNameValuePair(ASTSetQuery::Change & change, IParser::Pos & pos, IParser::Pos end, IParser::Pos & max_parsed_pos, Expected & expected) { ParserIdentifier name_p; ParserLiteral value_p; ParserWhiteSpaceOrComments ws; ParserString s_eq("="); ASTPtr name; ASTPtr value; ws.ignore(pos, end); if (!name_p.parse(pos, end, name, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!s_eq.ignore(pos, end, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!value_p.parse(pos, end, value, max_parsed_pos, expected)) return false; ws.ignore(pos, end); change.name = typeid_cast<const ASTIdentifier &>(*name).name; change.value = typeid_cast<const ASTLiteral &>(*value).value; return true; }
void test_irc_nick() { char msg[READ_BUF]; irc_nick("circus"); read_mock(msg); mu_assert(s_eq(msg, "NICK circus\r\n"), "test_irc_nick: msg should be 'NICK circus\\r\\n'"); }
void test_irc_topic() { char msg[READ_BUF]; irc_topic("#circus", "The topic"); read_mock(msg); mu_assert(s_eq(msg, "TOPIC #circus :The topic\r\n"), "test_irc_topic: msg should be 'TOPIC #circus :The topic\\r\\n'"); }
void test_irc_pong() { char msg[READ_BUF]; irc_pong("irc.freenode.net"); read_mock(msg); mu_assert(s_eq(msg, "PONG irc.freenode.net\r\n"), "test_irc_pong: msg should be 'PONG irc.freenode.net\\r\\n'"); }
void test_irc_names() { char msg[READ_BUF]; irc_names("#circus"); read_mock(msg); mu_assert(s_eq(msg, "NAMES #circus\r\n"), "test_irc_names: msg should be 'NAMES #circus\\r\\n'"); }
void test_irc_channel_unset() { char msg[READ_BUF]; irc_channel_unset("#circus", CH_PRIVATE | CH_SECRET); read_mock(msg); mu_assert(s_eq(msg, "MODE #circus -ps\r\n"), "test_irc_channel_unset: msg should be 'MODE #circus -ps\\r\\n'"); }
void test_irc_part() { char msg[READ_BUF]; irc_part("#circus"); read_mock(msg); mu_assert(s_eq(msg, "PART #circus\r\n"), "test_irc_part: msg should be 'PART #circus\\r\\n'"); }
void test_irc_raw() { char msg[READ_BUF]; irc_raw("prefix", "type", "message"); read_mock(msg); mu_assert(s_eq(msg, ":prefix type message\r\n"), "test_irc_raw: msg should be ':prefix type message\\r\\n'"); }
void test_irc_join_pass() { char msg[READ_BUF]; irc_join_pass("#circus", "foo"); read_mock(msg); mu_assert(s_eq(msg, "JOIN #circus foo\r\n"), "test_irc_join_pass: msg should be 'JOIN #circus foo\\r\\n'"); }
void test_irc_join() { char msg[READ_BUF]; irc_join("#circus"); read_mock(msg); mu_assert(s_eq(msg, "JOIN #circus\r\n"), "test_irc_join: msg should be 'JOIN #circus\\r\\n'"); }
void test_irc_quit() { char msg[READ_BUF]; irc_quit("Bye"); read_mock(msg); mu_assert(s_eq(msg, "QUIT :Bye\r\n"), "test_irc_quit: msg should be 'QUIT :Bye\\r\\n'"); }
void test_irc_invite() { char msg[READ_BUF]; irc_invite("circus", "#circus"); read_mock(msg); mu_assert(s_eq(msg, "INVITE circus #circus\r\n"), "test_irc_invite: msg should be 'INVITE circus #circus\\r\\n'"); }
void test_irc_user_unset() { char msg[READ_BUF]; irc_user_unset("circus", USR_INVISIBLE | USR_OPERATOR); read_mock(msg); mu_assert(s_eq(msg, "MODE circus -io\r\n"), "test_irc_user_unset: msg should be 'MODE circus -io\\r\\n'"); }
void test_irc_channel_key() { char msg[READ_BUF]; irc_channel_key("#circus", "Foo"); read_mock(msg); mu_assert(s_eq(msg, "MODE #circus +k Foo\r\n"), "test_irc_channel_key: msg should be 'MODE #circus +k Foo\\r\\n'"); }
void test_irc_limit() { char msg[READ_BUF]; irc_limit("#circus", 10); read_mock(msg); mu_assert(s_eq(msg, "MODE #circus +l 10\r\n"), "test_irc_limit: msg should be 'MODE #circus +l 10\\r\\n'"); }
void test_irc_list() { char msg[READ_BUF]; irc_list(); read_mock(msg); mu_assert(s_eq(msg, "LIST\r\n"), "test_irc_list: msg should be 'LIST\\r\\n'"); }
void test_irc_ban_list() { char msg[READ_BUF]; irc_ban_list("#circus"); read_mock(msg); mu_assert(s_eq(msg, "MODE #circus +b\r\n"), "test_irc_ban_list: msg should be 'MODE #circus +b\\r\\n'"); }
void test_irc_unban() { char msg[READ_BUF]; irc_unban("#circus", "*!*@*"); read_mock(msg); mu_assert(s_eq(msg, "MODE #circus -b *!*@*\r\n"), "test_irc_unban: msg should be 'MODE #circus -b *!*@*\\r\\n'"); }
void test_irc_kick() { char msg[READ_BUF]; irc_kick("#circus", "circus", "Foo"); read_mock(msg); mu_assert(s_eq(msg, "KICK #circus circus :Foo\r\n"), "test_irc_kick: msg should be 'KICK #circus circus :Foo\\r\\n'"); }
void test_irc_devoice() { char msg[READ_BUF]; irc_devoice("#circus", "circus"); read_mock(msg); mu_assert(s_eq(msg, "MODE #circus -v circus\r\n"), "test_irc_devoice: msg should be 'MODE #circus -v circus\\r\\n'"); }
void test_irc_user() { char msg[READ_BUF]; irc_user("Circus", "Circus IRC"); read_mock(msg); mu_assert(s_eq(msg, "USER Circus hostname server :Circus IRC\r\n"), "test_irc_user: msg should be 'USER Circus hostname server :Circus IRC\\r\\n'"); }
void test_irc_message() { char msg[READ_BUF]; irc_message("#circus", "Foo bar"); read_mock(msg); mu_assert(s_eq(msg, "PRIVMSG #circus :Foo bar\r\n"), "test_irc_message: msg should be 'PRIVMSG #circus :Foo bar\\r\\n'"); }
void test_parse() { struct raw_event* raw; raw = lst_parse("TEST This is a message test without prefix"); mu_assert(raw->prefix == NULL, "test_parse: prefix should be NULL"); mu_assert(s_eq(raw->type, "TEST"), "test_parse: type should be TEST"); mu_assert(raw->num_params == 7, "test_parse: there should be 7 parameters"); evt_raw_destroy(raw); /* Cleanup */ }
ListEvent evt_list(struct raw_event *raw) { ListEvent event; event.timestamp = &raw->timestamp; event.channel = NULL; event.num_users = 0; event.topic = NULL; event.finished = s_eq(raw->type, RPL_LISTEND); if (!event.finished) { event.channel = raw->params[1]; event.num_users = atoi(raw->params[2]); event.topic = raw->params[3]; } return event; }
NamesEvent evt_names(struct raw_event *raw) { char* token, *next = NULL; NamesEvent event; event.timestamp = &raw->timestamp; event.finished = s_eq(raw->type, RPL_ENDOFNAMES); event.channel = raw->params[event.finished? 1 : 2]; event.num_names = 0; if (!event.finished) { token = strtok_r(raw->params[3], " ", &next); while(token != NULL) { event.names[event.num_names++] = token; token = strtok_r(NULL, " ", &next); } } return event; }
bool ParserInsertQuery::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected) { Pos begin = pos; ParserWhiteSpaceOrComments ws; ParserString s_insert("INSERT", true, true); ParserString s_into("INTO", true, true); ParserString s_dot("."); ParserString s_id("ID"); ParserString s_eq("="); ParserStringLiteral id_p; ParserString s_values("VALUES", true, true); ParserString s_format("FORMAT", true, true); ParserString s_select("SELECT", true, true); ParserString s_lparen("("); ParserString s_rparen(")"); ParserIdentifier name_p; ParserList columns_p(std::make_unique<ParserCompoundIdentifier>(), std::make_unique<ParserString>(","), false); ASTPtr database; ASTPtr table; ASTPtr columns; ASTPtr format; ASTPtr select; ASTPtr id; /// Данные для вставки const char * data = nullptr; ws.ignore(pos, end); /// INSERT INTO if (!s_insert.ignore(pos, end, max_parsed_pos, expected) || !ws.ignore(pos, end) || !s_into.ignore(pos, end, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!name_p.parse(pos, end, table, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (s_dot.ignore(pos, end, max_parsed_pos, expected)) { database = table; if (!name_p.parse(pos, end, table, max_parsed_pos, expected)) return false; ws.ignore(pos, end); } ws.ignore(pos, end); if (s_id.ignore(pos, end, max_parsed_pos, expected)) { if (!s_eq.ignore(pos, end, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!id_p.parse(pos, end, id, max_parsed_pos, expected)) return false; } ws.ignore(pos, end); /// Есть ли список столбцов if (s_lparen.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!columns_p.parse(pos, end, columns, max_parsed_pos, expected)) return false; ws.ignore(pos, end); if (!s_rparen.ignore(pos, end, max_parsed_pos, expected)) return false; } ws.ignore(pos, end); Pos before_select = pos; /// VALUES или FORMAT или SELECT if (s_values.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); data = pos; pos = end; } else if (s_format.ignore(pos, end, max_parsed_pos, expected)) { ws.ignore(pos, end); if (!name_p.parse(pos, end, format, max_parsed_pos, expected)) return false; /// Данные начинаются после первого перевода строки, если такой есть, или после всех пробельных символов, иначе. ParserWhiteSpaceOrComments ws_without_nl(false); ws_without_nl.ignore(pos, end); if (pos != end && *pos == ';') throw Exception("You have excessive ';' symbol before data for INSERT.\n" "Example:\n\n" "INSERT INTO t (x, y) FORMAT TabSeparated\n" "1\tHello\n" "2\tWorld\n" "\n" "Note that there is no ';' in first line.", ErrorCodes::SYNTAX_ERROR); if (pos != end && *pos == '\n') ++pos; data = pos; pos = end; } else if (s_select.ignore(pos, end, max_parsed_pos, expected)) { pos = before_select; ParserSelectQuery select_p; select_p.parse(pos, end, select, max_parsed_pos, expected); } else { expected = "VALUES or FORMAT or SELECT"; return false; } auto query = std::make_shared<ASTInsertQuery>(StringRange(begin, data ? data : pos)); node = query; if (database) query->database = typeid_cast<ASTIdentifier &>(*database).name; query->table = typeid_cast<ASTIdentifier &>(*table).name; if (id) query->insert_id = safeGet<const String &>(typeid_cast<ASTLiteral &>(*id).value); if (format) query->format = typeid_cast<ASTIdentifier &>(*format).name; query->columns = columns; query->select = select; query->data = data != end ? data : NULL; query->end = end; if (columns) query->children.push_back(columns); if (select) query->children.push_back(select); return true; }
int main(int argc, char** argv) { int error = 1; printf("Set Testing Framework\n"); printf("\n"); // Test equality first so we can use it in later tests printf("Phase 0: Testing equality methods.\n"); { set *s1 = malloc(sizeof(set)); set *s2 = malloc(sizeof(set)); int numbers[8] = { -1, 5, 8, 2, 3, 5, 9, 10 }; int order1 [8] = { 0, 7, 3, 2, 4, 5, 6, 1 }; int order2 [8] = { 6, 2, 7, 3, 1, 0, 5, 4 }; int i; for (i = 0; i < 8; i++) { s_add(s1, numbers[order1[i]]); s_add(s2, numbers[order2[i]]); } bool eq = s_eq(s1, s2); if (!eq) { return error; } printf("Equal sets are equal.\n"); error++; bool seq = s_subseteq(s1, s2) && s_subseteq(s2, s1); if (!seq) { return error; } printf("Subset works both ways.\n"); error++; } printf("\n"); printf("Phase I: Testing single-set operations.\n"); { set *s = malloc(sizeof(set)); printf("Set created.\n"); error++; printf("Adding numbers.\n"); s_add(s, 1); s_add(s, 0); s_add(s, 6); s_add(s, 6); s_add(s, 1); s_add(s, -1); // set is now { 1, 0, 6, -1 } if (s_size(s) != 4) { return error; } printf("Size correct.\n"); error++; printf("Removing numbers.\n"); s_remove(s, 3); // nothing s_remove(s, 1); // -1 s_remove(s, 6); // -2 // set is now { 0, -1 } if (s_size(s) != 2) { return error; } printf("Size still correct.\n"); error++; printf("Testing s_contains.\n"); { bool okay = true; okay &= s_contains(s, 0); okay &= (!s_contains(s, 1)); okay &= s_contains(s, -1); okay &+ (!s_contains(s, 8)); if (!okay) { return error; } } printf("s_contains works as intended.\n"); error++; } printf("\n"); printf("Phase II: Testing Multi-Set Operations\n"); { set *s1 = malloc(sizeof(set)), *s2 = malloc(sizeof(set)); printf("Two sets created.\n"); error++; s_add(s1, 1); s_add(s1, 3); s_add(s1, 7); s_add(s2, 5); s_add(s2, -2); s_add(s2, 3); printf("Two sets filled.\n"); error++; { set *sr_union = s_union(s1, s2); printf("Union created.\n"); error++; int shouldBe[5] = { -2, 1, 3, 5, 7 }; int size = sizeof(shouldBe) / sizeof(int); if (s_size(sr_union) != size) { node *n = sr_union -> first; while (n != NULL ) { printf("%d ", n -> value); n = n -> next; } printf("\n"); printf("Size is %d but should be %d!\n", s_size(sr_union), size); return error; } printf("Size correct (getting good at this).\n"); error++; int i; for (i = 0; i < size; i++) { if (!s_contains(sr_union, shouldBe[i])) { return error; } } printf("Union set contains all required elements.\n"); error++; }{ set *sr_isect = s_isect(s1, s2); printf("Intersection created.\n"); error++; int shouldBe[1] = { 3 }; int size = sizeof(shouldBe) / sizeof(int); if (s_size(sr_isect) != size) { return error; } printf("Size correct (again).\n"); error++; int i; for (i = 0; i < size; i++) { if (!s_contains(sr_isect, shouldBe[i])) { return error; } } printf("Intersection set contains all required elements.\n"); error++; }{ set *sr_diff = s_diff(s1, s2); printf("Difference created.\n"); error++; int shouldBe[2] = { 1, 7 }; int size = sizeof(shouldBe) / sizeof(int); if (s_size(sr_diff) != size) { return error; } printf("Size correct (miraculously).\n"); error++; int i; for (i = 0; i < size; i++) { if (!s_contains(sr_diff, shouldBe[i])) { return error; } } printf("Difference set contains all required elements.\n"); error++; } } printf("\n"); printf("Phase III: Testing Specification"); { set *s = malloc(sizeof(set)); printf("Set created.\n"); error++; int list[6] = { -2, -1, 1, 4, 8, 9 }; int size = sizeof(list) / sizeof(int); int i; for (i = 0; i < size; i++) { s_add(s, list[i]); } printf("Set filled.\n"); error++; printf("Testing specification with p(x) = (x >= 3).\n"); { set *sr_p1 = s_spec(s, p1); printf("Specification complete.\n"); error++; int expected[3] = { 4, 8, 9 }; int expectedSize = sizeof(expected) / sizeof(int); if (s_size(sr_p1) != expectedSize) { return error; } printf("Specification size correct - phew!\n"); error++; int j; for (j = 0; j < expectedSize; j++) { if (!s_contains(sr_p1, expected[j])) { return error; } } printf("Set contains all required elements.\n"); } printf("Testing specification with p(x) = (x %% 2 == 0).\n"); { set *sr_p1 = s_spec(s, p2); printf("Specification complete.\n"); error++; int expected[3] = { -2, 4, 8 }; int expectedSize = sizeof(expected) / sizeof(int); if (s_size(sr_p1) != expectedSize) { return error; } printf("Specification size correct; what a surprise.\n"); error++; int j; for (j = 0; j < expectedSize; j++) { if (!s_contains(sr_p1, expected[j])) { return error; } } printf("Set contains all required elements.\n"); } } printf("\n"); printf("Phase IV: Testing Image\n"); { set *s = malloc(sizeof(set)); set *control = malloc(sizeof(set)); set *control2 = malloc(sizeof(set)); // image with predicate lambda x: x >= 3 int toadd[6] = { -1, 3, 5, 2, 0, 1 }; int expected[6] = { 3, 11, 27, 6, 2, 3 }; int expected2[6] = { 11, 27 }; int i; for (i = 0; i< 6; i++) { s_add(s, toadd[i]); s_add(control, expected[i]); if (i < 2) s_add(control2, expected2[i]); } set *image = s_fullimage(s, f); bool eq = s_eq(image, control); if (!eq) { return error; } printf("Full image set matches control set.\n"); error++; set *pimage = s_image(s, p1, f); bool peq = s_eq(pimage, control2); if (!peq) { return error; } printf("Partial image set matches control set.\n"); error++; } printf("\n"); printf("Completed without error.\n"); return 0; }