1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00
libpsyc/test/test_render.c

122 lines
3.7 KiB
C
Raw Normal View History

#include <stdio.h>
2011-05-08 21:40:26 +00:00
#include <lib.h>
#include <psyc/render.h>
#include <psyc/syntax.h>
2011-04-22 09:50:13 +00:00
#define myUNI "psyc://10.100.1000/~ludwig"
/* example renderer generating a presence packet */
int testPresence (const char *avail, int availlen,
const char *desc, int desclen,
const char *rendered, uint8_t verbose)
2011-04-25 19:57:03 +00:00
{
2011-11-03 13:27:01 +00:00
PsycModifier routing[1];
2011-11-09 18:01:05 +00:00
psyc_modifier_init(&routing[0], PSYC_OPERATOR_SET,
2011-11-03 13:27:01 +00:00
PSYC_C2ARG("_context"),
PSYC_C2ARG(myUNI), PSYC_MODIFIER_ROUTING);
PsycModifier entity[2];
// presence is to be assigned permanently in distributed state
2011-11-09 18:01:05 +00:00
psyc_modifier_init(&entity[0], PSYC_OPERATOR_ASSIGN,
2011-11-03 13:27:01 +00:00
PSYC_C2ARG("_degree_availability"),
avail, availlen, PSYC_MODIFIER_CHECK_LENGTH);
2011-11-09 18:01:05 +00:00
psyc_modifier_init(&entity[1], PSYC_OPERATOR_ASSIGN,
2011-11-03 13:27:01 +00:00
PSYC_C2ARG("_description_presence"),
desc, desclen, PSYC_MODIFIER_CHECK_LENGTH);
PsycPacket packet;
psyc_packet_init(&packet, routing, PSYC_NUM_ELEM(routing),
entity, PSYC_NUM_ELEM(entity),
PSYC_C2ARG("_notice_presence"),
NULL, 0,
PSYC_PACKET_CHECK_LENGTH);
2011-04-25 19:57:03 +00:00
char buffer[512];
psyc_render(&packet, buffer, sizeof(buffer));
2011-04-26 15:23:06 +00:00
if (verbose)
printf("%.*s\n", (int)packet.length, buffer);
2011-04-29 21:11:48 +00:00
return strncmp(rendered, buffer, packet.length);
2011-04-22 09:50:13 +00:00
}
int testList (const char *rendered, uint8_t verbose)
2011-04-26 15:23:06 +00:00
{
2011-11-03 13:27:01 +00:00
PsycModifier routing[2];
2011-11-09 18:01:05 +00:00
psyc_modifier_init(&routing[0], PSYC_OPERATOR_SET,
2011-11-03 13:27:01 +00:00
PSYC_C2ARG("_source"),
PSYC_C2ARG(myUNI), PSYC_MODIFIER_ROUTING);
2011-11-09 18:01:05 +00:00
psyc_modifier_init(&routing[1], PSYC_OPERATOR_SET,
2011-11-03 13:27:01 +00:00
PSYC_C2ARG("_context"),
PSYC_C2ARG(myUNI), PSYC_MODIFIER_ROUTING);
2011-04-26 15:23:06 +00:00
2011-10-31 19:26:47 +00:00
PsycString elems_text[] = {
2011-04-26 15:23:06 +00:00
PSYC_C2STR("foo"),
PSYC_C2STR("bar"),
PSYC_C2STR("baz"),
};
2011-10-31 19:26:47 +00:00
PsycString elems_bin[] = {
2011-04-26 15:23:06 +00:00
PSYC_C2STR("foo"),
PSYC_C2STR("b|r"),
PSYC_C2STR("baz\nqux"),
};
2011-10-31 19:26:47 +00:00
PsycList list_text, list_bin;
2011-11-03 13:27:01 +00:00
psyc_list_init(&list_text, elems_text, PSYC_NUM_ELEM(elems_text), PSYC_LIST_CHECK_LENGTH);
psyc_list_init(&list_bin, elems_bin, PSYC_NUM_ELEM(elems_bin), PSYC_LIST_CHECK_LENGTH);
2011-04-26 15:23:06 +00:00
char buf_text[32], buf_bin[32];
2011-10-31 19:04:16 +00:00
psyc_render_list(&list_text, buf_text, sizeof(buf_text));
psyc_render_list(&list_bin, buf_bin, sizeof(buf_bin));
2011-04-26 15:23:06 +00:00
2011-11-03 13:27:01 +00:00
PsycModifier entity[2];
2011-11-09 18:01:05 +00:00
psyc_modifier_init(&entity[0], PSYC_OPERATOR_SET,
2011-11-03 13:27:01 +00:00
PSYC_C2ARG("_list_text"),
buf_text, list_text.length, list_text.flag);
2011-11-09 18:01:05 +00:00
psyc_modifier_init(&entity[1], PSYC_OPERATOR_SET,
2011-11-03 13:27:01 +00:00
PSYC_C2ARG("_list_binary"),
buf_bin, list_bin.length, list_bin.flag);
PsycPacket packet;
psyc_packet_init(&packet, routing, PSYC_NUM_ELEM(routing),
entity, PSYC_NUM_ELEM(entity),
PSYC_C2ARG("_test_list"),
PSYC_C2ARG("list test"),
PSYC_PACKET_CHECK_LENGTH);
2011-04-26 15:23:06 +00:00
char buffer[512];
psyc_render(&packet, buffer, sizeof(buffer));
if (verbose)
printf("%.*s\n", (int)packet.length, buffer);
2011-04-26 15:23:06 +00:00
return strncmp(rendered, buffer, packet.length);
}
int main (int argc, char **argv) {
2011-04-26 15:23:06 +00:00
uint8_t verbose = argc > 1;
if (testPresence(PSYC_C2ARG("_here"), PSYC_C2ARG("I'm omnipresent right now"), "\
2011-04-25 21:58:07 +00:00
:_context\t" myUNI "\n\
2011-04-25 21:49:27 +00:00
\n\
=_degree_availability\t_here\n\
=_description_presence\tI'm omnipresent right now\n\
_notice_presence\n\
2011-04-26 15:23:06 +00:00
|\n", verbose))
return 1;
2011-04-26 15:23:06 +00:00
if (testList("\
:_source psyc://10.100.1000/~ludwig\n\
:_context psyc://10.100.1000/~ludwig\n\
85\n\
2011-04-26 15:23:06 +00:00
:_list_text |foo|bar|baz\n\
:_list_binary 21 3 foo|3 b|r|7 baz\n\
qux\n\
_test_list\n\
list test\n\
|\n", verbose))
return 2;
2011-04-26 15:23:06 +00:00
puts("psyc_render passed all tests.");
return 0;
2011-04-22 09:50:13 +00:00
}