mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
dict syntax; index & update mod parser; psyc-mode for emacs
This commit is contained in:
parent
eaeab8c226
commit
71866659f9
31 changed files with 2063 additions and 691 deletions
|
@ -3,7 +3,7 @@ DEBUG = 2
|
|||
CFLAGS = -I../include -I../src -Wall -std=c99 ${OPT}
|
||||
LDFLAGS = -L../lib
|
||||
LOADLIBES = -lpsyc -lm
|
||||
TARGETS = test_psyc test_psyc_speed test_parser test_match test_render test_text var_routing var_type uniform_parse test_list test_table test_packet_id method
|
||||
TARGETS = test_psyc test_psyc_speed test_parser test_match test_render test_text var_routing var_type uniform_parse test_packet_id test_index test_update method
|
||||
O = test.o
|
||||
WRAPPER =
|
||||
DIET = diet
|
||||
|
@ -47,10 +47,13 @@ test: ${TARGETS}
|
|||
./test_text
|
||||
./var_routing
|
||||
./var_type
|
||||
./method
|
||||
./uniform_parse
|
||||
./test_list
|
||||
./test_table
|
||||
# ./test_list
|
||||
# ./test_table
|
||||
./test_packet_id
|
||||
./test_index
|
||||
./test_update
|
||||
x=0; for f in packets/[0-9]*; do echo ">> $$f"; ./test_psyc -f $$f | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x
|
||||
x=0; for f in packets/[0-9]*; do echo ">> $$f"; ./test_psyc -rf $$f | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x
|
||||
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
:_source psyc://foo.example.com/
|
||||
:_target psyc://bar.example.com/
|
||||
0
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
:_source psyc://foo.example.com/
|
||||
:_target psyc://bar.example.com/
|
||||
|
||||
:_foo 0
|
||||
_message_private
|
||||
OHAI
|
||||
|
|
|
@ -1,18 +1,17 @@
|
|||
=_source psyc://foo/~bar
|
||||
:_target psyc://bar/~baz
|
||||
=_list_foo |foo|bar|baz
|
||||
:_tag sch1828hu3r2cm
|
||||
=_context psyc://foo/~bar
|
||||
|
||||
=_foo bar baz
|
||||
=_abc_def 11 ghi jkl
|
||||
|
||||
xq
|
||||
=_list_bar 36 3 foo|3 bar|7 foo
|
||||
bar|11 foo
|
||||
bar
|
||||
baz
|
||||
:_foo_bar yay
|
||||
_message_foo_bar
|
||||
ohai there!
|
||||
\o/
|
||||
=_list_xxx |=_foo|=_bar|=_baz|4 abc
|
||||
=_list_foo |=_foo|=_bar ||=_baz|
|
||||
=_list_foo _test|3 foo|=_color:4 blue|=_nick bar
|
||||
=_list_bar 43 |3 foo|=_color:4 blue|=_nick bar|8
|
||||
foo|bar
|
||||
=_list_bar 43 |3 foo|=_color:4 blue|=_nick bar|9
|
||||
foo|bar
|
||||
=_list_a _type| elem1| elem2| elem3
|
||||
=_list_b |=_type1 elem1|=_type2 elem2|=_type3 elem3
|
||||
=_list_members _uniform| psyc://example.net/~alice| psyc://example.org/~bob
|
||||
=_list_topic |9 democracy|3 now
|
||||
=_list_c | foo| bar|6 foobar|3 baz|=_int 234|=_time 1234
|
||||
=_list_foo |=_int 234|=_time 1234|=_picture:3 \o/|7 \oXoXo/
|
||||
_test_list
|
||||
|
|
||||
|
|
7
test/packets/02-list2
Normal file
7
test/packets/02-list2
Normal file
|
@ -0,0 +1,7 @@
|
|||
:_source psyc://foo/~bar
|
||||
:_target psyc://bar/~baz
|
||||
|
||||
=_list_foo _test|3 foo|=_color:4 blue|=_nick bar
|
||||
=_list_bar |3 foo|=_color:4 blue|=_nick bar
|
||||
_test
|
||||
|
|
11
test/packets/03-dict
Normal file
11
test/packets/03-dict
Normal file
|
@ -0,0 +1,11 @@
|
|||
=_context psyc://foo/~bar
|
||||
|
||||
=_dict _type{4 key1}6 value1{key2} value2{key3}6 value3{key4} value4
|
||||
=_dict_example {4 key1}=_type1:6 value1{key2}=_type2 value2{key3}6 value3{key4} value4
|
||||
=_struct_member |=_nick|=_picture
|
||||
=_dict_owners {psyc://example.net/~alice}{psyc://example.org/~bob}
|
||||
=_dict_members {psyc://example.net/~alice}=_struct_member | alice| \o/{psyc://example.org/~bob}=_struct_member | bob| \oXo/
|
||||
=_dict_members {25 psyc://example.net/~alice}=_struct_member:12 | alice| \o/{psyc://example.org/~bob}=_struct_member | bob| \oXo/
|
||||
=_dict_members _struct_member{25 psyc://example.net/~alice}12 | alice| \o/{psyc://example.org/~bob} | bob| \oXo/
|
||||
_test_dict
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
=_source psyc://foo/~bar
|
||||
:_target psyc://bar/~baz
|
||||
=_list_foo |foo|bar|baz
|
||||
:_tag sch1828hu3r2cm
|
||||
|
||||
?_test ignored
|
||||
=_foo bar baz
|
||||
=_abc_def 11 ghi jkl
|
||||
|
||||
xq
|
||||
=_list_bar 36 3 foo|3 bar|7 foo
|
||||
bar|11 foo
|
||||
b|r
|
||||
baz
|
||||
:_foo_bar yay
|
||||
=_amount_x 10
|
||||
_message_foo_bar
|
||||
ohai there!
|
||||
\o/
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
:_list_understand_modules |_state|_fragments|_context
|
||||
:_list_understand_modules | _state| _fragments| _context
|
||||
|
||||
_request_features
|
||||
|
|
||||
|
|
87
test/test_index.c
Normal file
87
test/test_index.c
Normal file
|
@ -0,0 +1,87 @@
|
|||
#include <psyc.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <lib.h>
|
||||
|
||||
uint8_t verbose;
|
||||
|
||||
int
|
||||
test_index (const char *buf, size_t buflen)
|
||||
{
|
||||
char *res = malloc(buflen * 2);
|
||||
size_t reslen = 0;
|
||||
|
||||
int ret, reti, len = 0;
|
||||
PsycString idx;
|
||||
PsycParseIndexState state;
|
||||
psyc_parse_index_state_init(&state);
|
||||
psyc_parse_index_buffer_set(&state, buf, buflen);
|
||||
|
||||
if (verbose)
|
||||
printf(">> %.*s\n", (int)buflen, buf);
|
||||
|
||||
do {
|
||||
ret = reti = psyc_parse_index(&state, &idx);
|
||||
len = 0;
|
||||
|
||||
switch (ret) {
|
||||
case PSYC_PARSE_INDEX_LIST_LAST:
|
||||
ret = 0;
|
||||
case PSYC_PARSE_INDEX_LIST:
|
||||
len = sprintf(res + reslen, "#%ld", idx.length);
|
||||
break;
|
||||
case PSYC_PARSE_INDEX_STRUCT_LAST:
|
||||
ret = 0;
|
||||
case PSYC_PARSE_INDEX_STRUCT:
|
||||
len = sprintf(res + reslen, ".%.*s", PSYC_S2ARGP(idx));
|
||||
break;
|
||||
case PSYC_PARSE_INDEX_DICT:
|
||||
if (state.elemlen_found)
|
||||
len = sprintf(res + reslen, "{%ld %.*s}",
|
||||
idx.length, PSYC_S2ARGP(idx));
|
||||
else
|
||||
len = sprintf(res + reslen, "{%.*s}", PSYC_S2ARGP(idx));
|
||||
break;
|
||||
case PSYC_PARSE_INDEX_END:
|
||||
ret = 0;
|
||||
break;
|
||||
default:
|
||||
ret = -1;
|
||||
}
|
||||
if (verbose)
|
||||
printf("%2d: %.*s\n", reti, len, res + reslen);
|
||||
reslen += len;
|
||||
} while (ret > 0);
|
||||
|
||||
if (reslen != buflen || memcmp(res, buf, buflen) != 0) {
|
||||
printf("ERROR: got\n\[%.*s] (%ld) instead of\n[%.*s] (%ld)\n",
|
||||
(int)reslen, res, reslen, (int)buflen, buf, buflen);
|
||||
ret = 1;
|
||||
} else
|
||||
ret = 0;
|
||||
|
||||
free(res);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
verbose = argc > 1;
|
||||
|
||||
if (test_index(PSYC_C2ARG("#1{foo}._bar")) != 0)
|
||||
return 1;
|
||||
|
||||
if (test_index(PSYC_C2ARG("{3 foo}._bar#0")) != 0)
|
||||
return 2;
|
||||
|
||||
if (test_index(PSYC_C2ARG("{foo}#2._bar")) != 0)
|
||||
return 3;
|
||||
|
||||
if (test_index(PSYC_C2ARG("._bar#1{3 foo}")) != 0)
|
||||
return 4;
|
||||
|
||||
printf("test_index passed all tests.\n");
|
||||
return 0; // passed all tests
|
||||
}
|
|
@ -17,21 +17,25 @@ main (int argc, char **argv)
|
|||
|
||||
PsycParseListState listState;
|
||||
PsycList list_text, list_bin;
|
||||
PsycString elems_text[NELEMS], elems_bin[NELEMS], elem;
|
||||
PsycElem elems_text[NELEMS], elems_bin[NELEMS];
|
||||
PsycString type, value;
|
||||
char buf_text[NELEMS * 200], buf_bin[NELEMS * 200], *elems[NELEMS], **elems2 = NULL;
|
||||
|
||||
struct timeval start, end;
|
||||
|
||||
char *text = "1234567890abcdefghijklmnopqrstuvwxyz-._ "
|
||||
"1234567890abcdefghijklmnopqrstuvwxyz-._ "
|
||||
"1234567890abcdefghijklmnopqrstuvwxyz-._ "
|
||||
"1234567890";
|
||||
char *bin = "1234567890|abcdefghijklmnopqrstuvwxyz|_\n"
|
||||
"1234567890|abcdefghijklmnopqrstuvwxyz|_\n"
|
||||
"1234567890|abcdefghijklmnopqrstuvwxyz|_\n"
|
||||
"1234567890";
|
||||
|
||||
for (i=0; i<NELEMS; i++)
|
||||
elems_text[i] = PSYC_C2STR("1234567890abcdefghijklmnopqrstuvwxyz-._ "
|
||||
"1234567890abcdefghijklmnopqrstuvwxyz-._ "
|
||||
"1234567890abcdefghijklmnopqrstuvwxyz-._ "
|
||||
"1234567890");
|
||||
elems_text[i] = PSYC_ELEM_V(text, strlen(text));
|
||||
for (i=0; i<NELEMS; i++)
|
||||
elems_bin[i] = PSYC_C2STR("1234567890|abcdefghijklmnopqrstuvwxyz|_\n"
|
||||
"1234567890|abcdefghijklmnopqrstuvwxyz|_\n"
|
||||
"1234567890|abcdefghijklmnopqrstuvwxyz|_\n"
|
||||
"1234567890");
|
||||
elems_bin[i] = PSYC_ELEM_V(bin, strlen(bin));
|
||||
|
||||
psyc_list_init(&list_text, elems_text, PSYC_NUM_ELEM(elems_text),
|
||||
PSYC_LIST_NO_LENGTH);
|
||||
|
@ -54,13 +58,14 @@ main (int argc, char **argv)
|
|||
psyc_parse_list_buffer_set(&listState, buf_text, list_text.length);
|
||||
i = 0;
|
||||
do {
|
||||
ret = psyc_parse_list(&listState, &elem);
|
||||
ret = psyc_parse_list(&listState, &type, &value);
|
||||
switch (ret) {
|
||||
case PSYC_PARSE_LIST_END:
|
||||
ret = 0;
|
||||
case PSYC_PARSE_LIST_ELEM:
|
||||
if (verbose)
|
||||
printf("|%d: %.*s... (%ld)\n", i, 10, elem.data, elem.length);
|
||||
printf("|%ld:%.*s %.*s...\n", value.length,
|
||||
(int)type.length, type.data, 10, value.data);
|
||||
//elems[i] = malloc(elem.length);
|
||||
//memcpy(&elems[i++], elem.data, elem.length);
|
||||
break;
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
#include <psyc/packet.h>
|
||||
#include <psyc/render.h>
|
||||
|
||||
uint8_t verbose;
|
||||
|
||||
int
|
||||
packet_id (char *context, size_t contextlen,
|
||||
char *source, size_t sourcelen,
|
||||
|
@ -13,16 +15,22 @@ packet_id (char *context, size_t contextlen,
|
|||
char *fragment, size_t fragmentlen,
|
||||
char *result, size_t resultlen)
|
||||
{
|
||||
size_t idlen = psyc_packet_id_length(contextlen, sourcelen, targetlen,
|
||||
counterlen, fragmentlen);
|
||||
PsycList list;
|
||||
PsycElem elems[PSYC_PACKET_ID_ELEMS];
|
||||
memset(&list, 0, sizeof(PsycList));
|
||||
memset(elems, 0, sizeof(PsycElem) * PSYC_PACKET_ID_ELEMS);
|
||||
|
||||
psyc_packet_id(&list, elems, context, contextlen,
|
||||
source, sourcelen, target, targetlen,
|
||||
counter, counterlen, fragment, fragmentlen);
|
||||
|
||||
size_t idlen = list.length;
|
||||
char *id = malloc(idlen);
|
||||
psyc_render_packet_id(context, contextlen,
|
||||
source, sourcelen,
|
||||
target, targetlen,
|
||||
counter, counterlen,
|
||||
fragment, fragmentlen,
|
||||
id, idlen);
|
||||
printf("%.*s\n", (int)idlen, id);
|
||||
|
||||
psyc_render_list(&list, id, idlen);
|
||||
|
||||
if (verbose)
|
||||
printf("[%.*s]\n", (int)idlen, id);
|
||||
int ret = idlen == resultlen && memcmp(result, id, idlen) == 0;
|
||||
free(id);
|
||||
return ret;
|
||||
|
@ -31,13 +39,16 @@ packet_id (char *context, size_t contextlen,
|
|||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
verbose = argc > 1;
|
||||
|
||||
if (!packet_id(PSYC_C2ARG(""),
|
||||
PSYC_C2ARG("psyc://example.net/~alice"),
|
||||
PSYC_C2ARG("psyc://example.net/~bob"),
|
||||
PSYC_C2ARG("1337"),
|
||||
PSYC_C2ARG("42"),
|
||||
PSYC_C2ARG("||psyc://example.net/~alice|psyc://example.net/~bob"
|
||||
"|1337|42")))
|
||||
PSYC_C2ARG("|| psyc://example.net/~alice"
|
||||
"| psyc://example.net/~bob"
|
||||
"| 1337| 42")))
|
||||
return 1;
|
||||
|
||||
if (!packet_id(PSYC_C2ARG("psyc://example.net/@bar"),
|
||||
|
@ -45,8 +56,9 @@ main (int argc, char **argv)
|
|||
PSYC_C2ARG(""),
|
||||
PSYC_C2ARG("1337"),
|
||||
PSYC_C2ARG("42"),
|
||||
PSYC_C2ARG("|psyc://example.net/@bar|psyc://example.net/~alice|"
|
||||
"|1337|42")))
|
||||
PSYC_C2ARG("| psyc://example.net/@bar"
|
||||
"| psyc://example.net/~alice|"
|
||||
"| 1337| 42")))
|
||||
return 2;
|
||||
|
||||
if (!packet_id(PSYC_C2ARG("psyc://example.net/@bar"),
|
||||
|
@ -54,8 +66,9 @@ main (int argc, char **argv)
|
|||
PSYC_C2ARG("psyc://example.net/~alice"),
|
||||
PSYC_C2ARG("1337"),
|
||||
PSYC_C2ARG("42"),
|
||||
PSYC_C2ARG("|psyc://example.net/@bar||psyc://example.net/~alice"
|
||||
"|1337|42")))
|
||||
PSYC_C2ARG("| psyc://example.net/@bar|"
|
||||
"| psyc://example.net/~alice"
|
||||
"| 1337| 42")))
|
||||
return 3;
|
||||
|
||||
if (!packet_id(PSYC_C2ARG("psyc://example.net/@bar"),
|
||||
|
@ -63,7 +76,7 @@ main (int argc, char **argv)
|
|||
PSYC_C2ARG(""),
|
||||
PSYC_C2ARG(""),
|
||||
PSYC_C2ARG(""),
|
||||
PSYC_C2ARG("|psyc://example.net/@bar||||")))
|
||||
PSYC_C2ARG("| psyc://example.net/@bar||||")))
|
||||
return 4;
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -12,7 +12,7 @@ main (int argc, char **argv)
|
|||
uint8_t verbose = argc > 2 && memchr(argv[2], (int)'v', strlen(argv[2]));
|
||||
int idx, ret;
|
||||
char buffer[2048], oper;
|
||||
PsycString name, value, elem;
|
||||
PsycString name, value, type;
|
||||
PsycParseState state;
|
||||
PsycParseListState listState;
|
||||
|
||||
|
@ -62,12 +62,13 @@ main (int argc, char **argv)
|
|||
psyc_parse_list_state_init(&listState);
|
||||
psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(value));
|
||||
|
||||
while ((ret = psyc_parse_list(&listState, &elem))) {
|
||||
while ((ret = psyc_parse_list(&listState, &type, &value))) {
|
||||
switch (ret) {
|
||||
case PSYC_PARSE_LIST_END:
|
||||
case PSYC_PARSE_LIST_ELEM:
|
||||
if (verbose)
|
||||
printf("|%.*s\n", (int)elem.length, elem.data);
|
||||
printf("|%.*s %.*s\n", (int)type.length, type.data,
|
||||
(int)value.length, value.data);
|
||||
break;
|
||||
default:
|
||||
printf("Error while parsing list: %i\n", ret);
|
||||
|
|
128
test/test_psyc.c
128
test/test_psyc.c
|
@ -8,9 +8,6 @@
|
|||
#include <sys/socket.h>
|
||||
|
||||
#include <psyc.h>
|
||||
#include <psyc/parse.h>
|
||||
#include <psyc/render.h>
|
||||
#include <psyc/syntax.h>
|
||||
|
||||
#include "test.c"
|
||||
|
||||
|
@ -71,10 +68,11 @@ test_input (int i, char *recvbuf, size_t nbytes)
|
|||
PsycPacket *packet = &packets[i];
|
||||
|
||||
char oper;
|
||||
PsycString name, value, elem;
|
||||
PsycString name, value, type;
|
||||
PsycString *pname = NULL, *pvalue = NULL;
|
||||
PsycModifier *mod = NULL;
|
||||
PsycParseListState listState;
|
||||
PsycParseListState lstate;
|
||||
PsycParseDictState dstate;
|
||||
size_t len;
|
||||
|
||||
// Set buffer with data for the parser.
|
||||
|
@ -283,37 +281,131 @@ test_input (int i, char *recvbuf, size_t nbytes)
|
|||
oper = 0;
|
||||
name.length = 0;
|
||||
value.length = 0;
|
||||
type.length = 0;
|
||||
|
||||
if (psyc_var_is_list(PSYC_S2ARG(*pname))) {
|
||||
switch (psyc_var_type(PSYC_S2ARG(*pname))) {
|
||||
case PSYC_TYPE_LIST:
|
||||
if (verbose >= 2)
|
||||
printf("## LIST START\n");
|
||||
|
||||
psyc_parse_list_state_init(&listState);
|
||||
psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(*pvalue));
|
||||
psyc_parse_list_state_init(&lstate);
|
||||
psyc_parse_list_buffer_set(&lstate, PSYC_S2ARG(*pvalue));
|
||||
|
||||
do {
|
||||
retl = psyc_parse_list(&listState, &elem);
|
||||
retl = psyc_parse_list(&lstate, &type, &value);
|
||||
switch (retl) {
|
||||
case PSYC_PARSE_LIST_END:
|
||||
case PSYC_PARSE_LIST_TYPE:
|
||||
if (verbose >= 2)
|
||||
printf("## LIST TYPE: %.*s\n", (int)type.length, type.data);
|
||||
break;
|
||||
case PSYC_PARSE_LIST_ELEM_START:
|
||||
case PSYC_PARSE_LIST_ELEM_LAST:
|
||||
retl = 0;
|
||||
case PSYC_PARSE_LIST_ELEM:
|
||||
if (verbose >= 2) {
|
||||
printf("|%.*s\n", (int)elem.length, elem.data);
|
||||
if (ret == PSYC_PARSE_LIST_END)
|
||||
printf("## LIST END");
|
||||
printf("|%.*s [%.*s]", (int)type.length, type.data,
|
||||
(int)value.length, value.data);
|
||||
if (retl == PSYC_PARSE_LIST_ELEM_START)
|
||||
printf(" ...");
|
||||
printf("\n");
|
||||
if (ret == PSYC_PARSE_LIST_ELEM_LAST)
|
||||
printf("## LAST ELEM\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case PSYC_PARSE_LIST_ELEM_CONT:
|
||||
retl = 0;
|
||||
case PSYC_PARSE_LIST_ELEM_END:
|
||||
if (verbose >= 2) {
|
||||
printf("... [%.*s]", (int)value.length, value.data);
|
||||
if (retl == PSYC_PARSE_LIST_ELEM_CONT)
|
||||
printf(" ...");
|
||||
printf("\n");
|
||||
}
|
||||
break;
|
||||
case PSYC_PARSE_LIST_END:
|
||||
retl = 0;
|
||||
if (verbose >= 2)
|
||||
printf("## LIST END\n");
|
||||
break;
|
||||
default:
|
||||
printf("# Error while parsing list: %i\n", retl);
|
||||
ret = retl = -1;
|
||||
}
|
||||
}
|
||||
while (retl > 0);
|
||||
} while (retl > 0);
|
||||
break;
|
||||
case PSYC_TYPE_DICT:
|
||||
if (verbose >= 2)
|
||||
printf("## DICT START\n");
|
||||
|
||||
psyc_parse_dict_state_init(&dstate);
|
||||
psyc_parse_dict_buffer_set(&dstate, PSYC_S2ARG(*pvalue));
|
||||
|
||||
do {
|
||||
retl = psyc_parse_dict(&dstate, &type, &value);
|
||||
switch (retl) {
|
||||
case PSYC_PARSE_DICT_TYPE:
|
||||
if (verbose >= 2)
|
||||
printf("## DICT TYPE: %.*s\n", (int)type.length, type.data);
|
||||
break;
|
||||
case PSYC_PARSE_DICT_KEY_START:
|
||||
retl = 0;
|
||||
case PSYC_PARSE_DICT_KEY:
|
||||
if (verbose >= 2) {
|
||||
printf("{[%.*s]", (int)value.length, value.data);
|
||||
if (retl == PSYC_PARSE_DICT_KEY_START)
|
||||
printf(" ...");
|
||||
printf("\n");
|
||||
}
|
||||
break;
|
||||
case PSYC_PARSE_DICT_KEY_CONT:
|
||||
retl = 0;
|
||||
case PSYC_PARSE_DICT_KEY_END:
|
||||
if (verbose >= 2) {
|
||||
printf("... [%.*s]", (int)value.length, value.data);
|
||||
if (retl == PSYC_PARSE_DICT_KEY_CONT)
|
||||
printf(" ...");
|
||||
printf("\n");
|
||||
}
|
||||
break;
|
||||
case PSYC_PARSE_DICT_VALUE_START:
|
||||
case PSYC_PARSE_DICT_VALUE_LAST:
|
||||
retl = 0;
|
||||
case PSYC_PARSE_DICT_VALUE:
|
||||
if (verbose >= 2) {
|
||||
printf("}%.*s [%.*s]", (int)type.length, type.data,
|
||||
(int)value.length, value.data);
|
||||
if (retl == PSYC_PARSE_DICT_VALUE_START)
|
||||
printf(" ...");
|
||||
printf("\n");
|
||||
if (ret == PSYC_PARSE_DICT_VALUE_LAST)
|
||||
printf("## LAST VALUE\n");
|
||||
}
|
||||
break;
|
||||
case PSYC_PARSE_DICT_VALUE_CONT:
|
||||
retl = 0;
|
||||
case PSYC_PARSE_DICT_VALUE_END:
|
||||
if (verbose >= 2) {
|
||||
printf("... [%.*s]", (int)value.length, value.data);
|
||||
if (retl == PSYC_PARSE_DICT_VALUE_CONT)
|
||||
printf(" ...");
|
||||
printf("\n");
|
||||
}
|
||||
break;
|
||||
case PSYC_PARSE_DICT_END:
|
||||
retl = 0;
|
||||
printf("## DICT END\n");
|
||||
break;
|
||||
default:
|
||||
printf("# Error while parsing dict: %i\n", retl);
|
||||
ret = retl = -1;
|
||||
}
|
||||
} while (retl > 0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
while (ret > 0);
|
||||
} while (ret > 0);
|
||||
|
||||
if (progress)
|
||||
r = write(1, " ", 1);
|
||||
|
|
|
@ -1,16 +1,15 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include <lib.h>
|
||||
#include <psyc/render.h>
|
||||
#include <psyc/syntax.h>
|
||||
#include <psyc.h>
|
||||
|
||||
#define myUNI "psyc://10.100.1000/~ludwig"
|
||||
|
||||
/* example renderer generating a presence packet */
|
||||
int
|
||||
testPresence (char *avail, int availlen,
|
||||
char *desc, int desclen,
|
||||
char *rendered, uint8_t verbose)
|
||||
test_presence (char *avail, int availlen,
|
||||
char *desc, int desclen,
|
||||
char *rendered, uint8_t verbose)
|
||||
{
|
||||
PsycModifier routing[1];
|
||||
psyc_modifier_init(&routing[0], PSYC_OPERATOR_SET,
|
||||
|
@ -42,7 +41,7 @@ testPresence (char *avail, int availlen,
|
|||
}
|
||||
|
||||
int
|
||||
testList (const char *rendered, uint8_t verbose)
|
||||
test_list (const char *rendered, uint8_t verbose)
|
||||
{
|
||||
PsycModifier routing[2];
|
||||
psyc_modifier_init(&routing[0], PSYC_OPERATOR_SET,
|
||||
|
@ -52,23 +51,21 @@ testList (const char *rendered, uint8_t verbose)
|
|||
PSYC_C2ARG("_context"),
|
||||
PSYC_C2ARG(myUNI), PSYC_MODIFIER_ROUTING);
|
||||
|
||||
PsycString elems_text[] = {
|
||||
PSYC_C2STR("foo"),
|
||||
PSYC_C2STR("bar"),
|
||||
PSYC_C2STR("baz"),
|
||||
PsycElem elems_text[] = {
|
||||
PSYC_ELEM_V("foo", 3),
|
||||
PSYC_ELEM_V("bar", 3),
|
||||
PSYC_ELEM_V("baz", 3),
|
||||
};
|
||||
|
||||
PsycString elems_bin[] = {
|
||||
PSYC_C2STR("foo"),
|
||||
PSYC_C2STR("b|r"),
|
||||
PSYC_C2STR("baz\nqux"),
|
||||
PsycElem elems_bin[] = {
|
||||
PSYC_ELEM_V("foo", 3),
|
||||
PSYC_ELEM_V("b|r", 3),
|
||||
PSYC_ELEM_V("baz\nqux", 7),
|
||||
};
|
||||
|
||||
PsycList list_text, list_bin;
|
||||
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);
|
||||
psyc_list_init(&list_text, elems_text, PSYC_NUM_ELEM(elems_text));
|
||||
psyc_list_init(&list_bin, elems_bin, PSYC_NUM_ELEM(elems_bin));
|
||||
|
||||
char buf_text[32], buf_bin[32];
|
||||
psyc_render_list(&list_text, buf_text, sizeof(buf_text));
|
||||
|
@ -77,10 +74,12 @@ testList (const char *rendered, uint8_t verbose)
|
|||
PsycModifier entity[2];
|
||||
psyc_modifier_init(&entity[0], PSYC_OPERATOR_SET,
|
||||
PSYC_C2ARG("_list_text"),
|
||||
buf_text, list_text.length, list_text.flag);
|
||||
buf_text, list_text.length,
|
||||
PSYC_MODIFIER_CHECK_LENGTH);
|
||||
psyc_modifier_init(&entity[1], PSYC_OPERATOR_SET,
|
||||
PSYC_C2ARG("_list_binary"),
|
||||
buf_bin, list_bin.length, list_bin.flag);
|
||||
buf_bin, list_bin.length,
|
||||
PSYC_MODIFIER_CHECK_LENGTH);
|
||||
|
||||
PsycPacket packet;
|
||||
psyc_packet_init(&packet, routing, PSYC_NUM_ELEM(routing),
|
||||
|
@ -102,21 +101,21 @@ main (int argc, char **argv)
|
|||
{
|
||||
uint8_t verbose = argc > 1;
|
||||
|
||||
if (testPresence(PSYC_C2ARG("_here"), PSYC_C2ARG("I'm omnipresent right now"), "\
|
||||
if (test_presence(PSYC_C2ARG("_here"), PSYC_C2ARG("I'm omnipresent right now"), "\
|
||||
:_context\t" myUNI "\n\
|
||||
\n\
|
||||
97\n\
|
||||
=_degree_availability\t_here\n\
|
||||
=_description_presence\tI'm omnipresent right now\n\
|
||||
=_description_presence 25\tI'm omnipresent right now\n\
|
||||
_notice_presence\n\
|
||||
|\n", verbose))
|
||||
return 1;
|
||||
|
||||
if (testList("\
|
||||
if (test_list("\
|
||||
:_source psyc://10.100.1000/~ludwig\n\
|
||||
:_context psyc://10.100.1000/~ludwig\n\
|
||||
85\n\
|
||||
:_list_text |foo|bar|baz\n\
|
||||
:_list_binary 21 3 foo|3 b|r|7 baz\n\
|
||||
90\n\
|
||||
:_list_text 15 | foo| bar| baz\n\
|
||||
:_list_binary 20 | foo|3 b|r| baz\n\
|
||||
qux\n\
|
||||
_test_list\n\
|
||||
list test\n\
|
||||
|
|
140
test/test_update.c
Normal file
140
test/test_update.c
Normal file
|
@ -0,0 +1,140 @@
|
|||
#include <psyc.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <lib.h>
|
||||
|
||||
uint8_t verbose;
|
||||
|
||||
int
|
||||
test_update (const char *buf, size_t buflen,
|
||||
const char *r_idx, size_t r_idxlen, char r_oper,
|
||||
const char *r_typ, size_t r_typlen,
|
||||
const char *r_val, size_t r_vallen)
|
||||
{
|
||||
char *idx = malloc(r_idxlen * 2);
|
||||
char *typ = NULL, *val = NULL;
|
||||
size_t idxlen = 0, typlen = 0, vallen = 0;
|
||||
|
||||
int ret, reti, len = 0;
|
||||
char oper;
|
||||
PsycString value;
|
||||
PsycParseUpdateState state;
|
||||
psyc_parse_update_state_init(&state);
|
||||
psyc_parse_update_buffer_set(&state, buf, buflen);
|
||||
|
||||
if (verbose)
|
||||
printf(">> %.*s\n", (int)buflen, buf);
|
||||
|
||||
do {
|
||||
ret = reti = psyc_parse_update(&state, &oper, &value);
|
||||
len = 0;
|
||||
|
||||
switch (ret) {
|
||||
case PSYC_PARSE_INDEX_LIST:
|
||||
len = sprintf(idx + idxlen, "#%ld", value.length);
|
||||
break;
|
||||
case PSYC_PARSE_INDEX_STRUCT:
|
||||
len = sprintf(idx + idxlen, ".%.*s", PSYC_S2ARGP(value));
|
||||
break;
|
||||
case PSYC_PARSE_INDEX_DICT:
|
||||
if (state.elemlen_found)
|
||||
len = sprintf(idx + idxlen, "{%ld %.*s}",
|
||||
value.length, PSYC_S2ARGP(value));
|
||||
else
|
||||
len = sprintf(idx + idxlen, "{%.*s}", PSYC_S2ARGP(value));
|
||||
break;
|
||||
case PSYC_PARSE_UPDATE_TYPE_END:
|
||||
ret = 0;
|
||||
case PSYC_PARSE_UPDATE_TYPE:
|
||||
typ = value.data;
|
||||
typlen = value.length;
|
||||
if (verbose)
|
||||
printf("%c[%.*s]\n", oper, PSYC_S2ARGP(value));
|
||||
break;
|
||||
case PSYC_PARSE_UPDATE_VALUE:
|
||||
val = value.data;
|
||||
vallen = value.length;
|
||||
ret = 0;
|
||||
if (verbose)
|
||||
printf("[%.*s]\n", PSYC_S2ARGP(value));
|
||||
break;
|
||||
case PSYC_PARSE_UPDATE_END:
|
||||
ret = 0;
|
||||
break;
|
||||
default:
|
||||
ret = -1;
|
||||
}
|
||||
if (verbose && len)
|
||||
printf("%2d: %.*s\n", reti, len, idx + idxlen);
|
||||
idxlen += len;
|
||||
} while (ret > 0);
|
||||
|
||||
if (ret == 0) {
|
||||
if (idxlen != r_idxlen || oper != r_oper
|
||||
|| typlen != r_typlen || vallen != r_vallen
|
||||
|| memcmp(r_idx, idx, idxlen) != 0
|
||||
|| memcmp(r_typ, typ, typlen) != 0
|
||||
|| memcmp(r_val, val, vallen) != 0) {
|
||||
printf("ERROR: got\n\[%.*s] %c[%.*s]:[%ld] [%.*s] instead of\n[%.*s]\n",
|
||||
(int)idxlen, idx, oper, (int)typlen, typ, vallen, (int)vallen, val,
|
||||
(int)buflen, buf);
|
||||
ret = 1;
|
||||
} else
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
free(idx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
verbose = argc > 1;
|
||||
|
||||
if (test_update(PSYC_C2ARG("#1{foo}._bar =_foo:3 bar"),
|
||||
PSYC_C2ARG("#1{foo}._bar"), '=',
|
||||
PSYC_C2ARG("_foo"),
|
||||
PSYC_C2ARG("bar")) != 0)
|
||||
return 1;
|
||||
|
||||
if (test_update(PSYC_C2ARG("{3 foo}._bar#0 +:3 baz"),
|
||||
PSYC_C2ARG("{3 foo}._bar#0"), '+',
|
||||
PSYC_C2ARG(""),
|
||||
PSYC_C2ARG("baz")) != 0)
|
||||
return 2;
|
||||
|
||||
if (test_update(PSYC_C2ARG("{foo}#2._bar - 1337"),
|
||||
PSYC_C2ARG("{foo}#2._bar"), '-',
|
||||
PSYC_C2ARG(""),
|
||||
PSYC_C2ARG("1337")) != 0)
|
||||
return 3;
|
||||
|
||||
if (test_update(PSYC_C2ARG("._bar#1{3 foo} ="),
|
||||
PSYC_C2ARG("._bar#1{3 foo}"), '=',
|
||||
PSYC_C2ARG(""),
|
||||
PSYC_C2ARG("")) != 0)
|
||||
return 4;
|
||||
|
||||
if (test_update(PSYC_C2ARG("#1{3 foo}._bar =_list"),
|
||||
PSYC_C2ARG("#1{3 foo}._bar"), '=',
|
||||
PSYC_C2ARG("_list"),
|
||||
PSYC_C2ARG("")) != 0)
|
||||
return 5;
|
||||
|
||||
if (test_update(PSYC_C2ARG("#1{3 foo}._bar =_list "),
|
||||
PSYC_C2ARG("#1{3 foo}._bar"), '=',
|
||||
PSYC_C2ARG("_list"),
|
||||
PSYC_C2ARG("")) != 0)
|
||||
return 6;
|
||||
|
||||
if (test_update(PSYC_C2ARG("#1{3 foo}._bar =_list"),
|
||||
PSYC_C2ARG("#1{3 foo}._bar"), '=',
|
||||
PSYC_C2ARG("_list"),
|
||||
PSYC_C2ARG("")) != 0)
|
||||
return 7;
|
||||
|
||||
printf("test_update passed all tests.\n");
|
||||
return 0; // passed all tests
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue