This repository has been archived on 2021-04-17. You can view files and clone it, but cannot push or open issues or pull requests.
uIRC/tests/parsing.c

82 lines
3.6 KiB
C

#include "../include/uirc.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main(void)
{
IRC_Message results[] = {
{.name.nick = "user", .cmd = NOTICE, .trailing = "ok"},
{.cmd = QUIT},
{.name = {.nick = "nick", .user = "user", .host = "host"}, .cmd = PRIVMSG, .args[0] = "friend", .trailing = "hello there"},
{.cmd = QUIT, .args = {"one", "two", "three", "four", "5", "6", "seven", "8", "nein", "10", "11", "12", "13", "14"}, .trailing = "i'm trailing"}};
char mesg[][513] = {
":user NOTICE :ok",
"QUIT",
":nick!user@host PRIVMSG friend :hello there",
"QUIT one two three four 5 6 seven 8 nein 10 11 12 13 14 i'm trailing"};
IRC_Message parseout;
int res = 0;
for (unsigned long i = 0; i < sizeof(results) / sizeof(IRC_Message); i++) {
memset((void*)&parseout, '\0', sizeof(parseout));
printf("Testing [%lu] --> %s\n", i, mesg[i]);
printf("Tokenize: ");
if ((res = Tok_mesg(mesg[i], &parseout)) <= 0) {
printf("String could not be tokenized. %i\n", res);
return EXIT_FAILURE;
}
printf("OK\n");
const char* const strings[][2] = {
{parseout.trailing, results[i].trailing}, // 0
{parseout.name.nick, results[i].name.nick}, // 1
{parseout.name.host, results[i].name.host}, // 2
{parseout.name.user, results[i].name.user}, // 3
{parseout.tags.account.value, results[i].tags.account.value}, // 4
{parseout.tags.batch.value, results[i].tags.batch.value}, // 5
{parseout.tags.label.value, results[i].tags.label.value}, // 6
{parseout.tags.msgid.value, results[i].tags.msgid.value}, // 7
{parseout.tags.multiline_concat.value, results[i].tags.multiline_concat.value}, // 8
{parseout.tags.time.value, results[i].tags.time.value}, // 9
{parseout.tags.typing.value, results[i].tags.typing.value}, // 10
{parseout.tags.react.value, results[i].tags.react.value}, // 11
{parseout.tags.reply.value, results[i].tags.reply.value} // 12
};
printf("Validation:\n");
printf("\tArguments:\n");
for (int k = 0; i < 14 && results[i].args[k] != NULL; k++) {
printf("\t\t[%i]: ", k);
if (parseout.args[k] == NULL) {
printf("Argument %i is NULL\n", k);
return EXIT_FAILURE;
}
if (strcmp(parseout.args[k], results[i].args[k])) {
printf("Argument %i does not match expected value. Expected %s, got %s instead\n", k, results[i].args[k], parseout.args[k]);
return EXIT_FAILURE;
}
printf("OK %s\n", parseout.args[k]);
}
printf("\tStrings:\n");
for (unsigned long j = 0; j < sizeof(strings) / sizeof(char* [2]); j++) {
if (strings[j][1] != NULL && *strings[j][1] != '\0') {
printf("\t\t[%lu]: ", j);
if (strings[j][0] == NULL || *strings[j][0] == '\0') {
printf("Matching argument is a NULL pointer or zero-lenght --> %s\n", strings[j][0]);
return EXIT_FAILURE;
}
if (strcmp(strings[j][0], strings[j][1])) {
printf("Arguments didn't match at iteration %lu: expected %s but got %s", i, strings[j][1], strings[j][0]);
return EXIT_FAILURE;
}
printf("OK %s\n", strings[j][0]);
}
}
printf("\tCommand: ");
if (parseout.cmd == 0 || parseout.cmd != results[i].cmd) {
printf("Command is not parsed or non-matching. Got %s (%i) and expected %s\n", uirc_ircmd[parseout.cmd], parseout.cmd, uirc_ircmd[results[i].cmd]);
return EXIT_FAILURE;
}
printf("OK\n");
}
return EXIT_SUCCESS;
}