File:
[ELWIX - Embedded LightWeight unIX -] /
libaitio /
example /
Attic /
test_vars.c
Revision
1.6:
download - view:
text,
annotated -
select for diffs -
revision graph
Wed Sep 19 15:19:53 2012 UTC (11 years, 11 months ago) by
misho
Branches:
MAIN
CVS tags:
io5_0,
io4_1,
io4_0,
io3_9,
io3_8,
IO4_1,
IO4_0,
IO3_9,
IO3_8,
IO3_7,
HEAD
version 3.7
/*
*
* Sorting example: ./test_vars 9 sdf 2 aaa 6 gghfh 7 piuk 6 uaaa 3 abc 1 000 11 mda 0 cccc 0 fiup 23 blah
*
*/
#include <stdio.h>
#include <string.h>
#include <aitio.h>
static int
cmpfunc(const void *a, const void *b)
{
return strcmp(AIT_GET_STR(*(ait_val_t**) a), AIT_GET_STR(*(ait_val_t**) b));
}
static void
makevars()
{
ait_val_t *v;
v = io_makeVar(empty);
printf("Variable is empty? %d\n", AIT_ISEMPTY(v));
io_freeVar(&v);
v = io_makeVar(ptr, 0x12345678, 42);
printf("Variable is ptr=%p len=%u\n", AIT_GET_PTR(v), AIT_LEN(v));
io_freeVar(&v);
v = io_makeVar(data, "TEST DATA !!!", 14);
printf("Variable is data=%s len=%u\n", AIT_GET_DATA(v), AIT_LEN(v));
io_freeVar(&v);
v = io_makeVar(buffer, "TEST Buffer !!!", 16);
printf("Variable is buf=%s len=%u\n", AIT_GET_BUF(v), AIT_LEN(v));
io_freeVar(&v);
v = io_makeVar(string, "TEST String ...");
printf("Variable is str=%s len=%u\n", AIT_GET_STR(v), AIT_LEN(v));
io_freeVar(&v);
v = io_makeVar(blob, 1234, 10000);
printf("Variable is blob=%u len=%u\n", AIT_GET_BLOB(v), AIT_LEN(v));
io_freeVar(&v);
v = io_makeVar(f32, 1.0123);
printf("Variable is f32=%f\n", AIT_GET_F32(v));
io_freeVar(&v);
v = io_makeVar(f64, 0.00123321);
printf("Variable is f64=%f\n", AIT_GET_F64(v));
io_freeVar(&v);
v = io_makeVar(u8, 1);
printf("Variable is u8=%hhu\n", AIT_GET_U8(v));
io_freeVar(&v);
v = io_makeVar(u16, 32000);
printf("Variable is u16=%hu\n", AIT_GET_U16(v));
io_freeVar(&v);
v = io_makeVar(u32, 0xfedecaa0);
printf("Variable is u32=%u\n", AIT_GET_U32(v));
io_freeVar(&v);
v = io_makeVar(u64, 0xffeedccfedecaa01LL);
printf("Variable is u64=%llu\n", AIT_GET_U64(v));
io_freeVar(&v);
v = io_makeVar(i8, -1);
printf("Variable is i8=%hhd\n", AIT_GET_I8(v));
io_freeVar(&v);
v = io_makeVar(i16, -28888);
printf("Variable is i16=%hd\n", AIT_GET_I16(v));
io_freeVar(&v);
v = io_makeVar(i32, -23424234);
printf("Variable is i32=%d\n", AIT_GET_I32(v));
io_freeVar(&v);
v = io_makeVar(i64, -123234200000342LL);
printf("Variable is i64=%lld\n", AIT_GET_I64(v));
io_freeVar(&v);
}
static void
testcmp()
{
ait_val_t a, b;
AIT_SET_STR(&a, "OHO boho");
AIT_SET_STR(&b, "OHO boho");
printf("Test equal strings %d\n", io_cmpVar(&a, &b));
AIT_FREE_VAL(&b);
AIT_SET_STR(&b, "OHO b0ho");
printf("Test not equal strings %d\n", io_cmpVar(&a, &b));
AIT_FREE_VAL(&b);
AIT_SET_STR(&b, "OHO b0ho different");
printf("Test not equal in length %d\n", io_cmpVar(&a, &b));
AIT_FREE_VAL(&b);
AIT_SET_BUF(&b, "OHO", 4);
printf("Test not equal in type %d\n", io_cmpVar(&a, &b));
AIT_FREE_VAL(&b);
AIT_FREE_VAL(&a);
AIT_SET_I16(&a, 5);
AIT_SET_I16(&b, 5);
printf("Test equal num %d\n", io_cmpVar(&a, &b));
AIT_SET_I16(&b, 2);
printf("Test not equal num %d\n", io_cmpVar(&a, &b));
AIT_SET_U8(&b, 9);
printf("Test not equal in type %d\n", io_cmpVar(&a, &b));
}
int
main(int argc, char **argv)
{
array_t *arr;
ait_val_t *v;
register int i;
if (argc < 2) {
makevars();
testcmp();
return 0;
}
arr = io_allocVars((argc - 1 ) / 2);
if (!arr) {
printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
return 1;
}
for (i = 0; i < io_arraySize(arr); i++) {
v = io_arrayGet(arr, i);
AIT_KEY(v) = atoi(argv[i * 2 + 1]);
AIT_SET_STR(v, argv[i * 2 + 2]);
}
v = io_findKeyVars(arr, 7);
printf("v=%p\n", v);
fflush(stdout);
printf("search for key=7 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
v = io_findKeyVars(arr, 23);
printf("search for key=23 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
v = io_findKeyVars(arr, 0);
printf("search for key=0 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
v = io_findKeyVars(arr, 99);
printf("search for key=99 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
io_hashKeyVars(arr);
v = io_getVars(&arr, io_arraySize(arr));
AIT_SET_STR(v, "mauhahahaha ohoho and bottle of rum!");
io_hashVar(v, "Baba Yaga #1");
v = io_findKeyHash(arr, "Baba Yaga #1");
printf("search for hash='Baba Yaga #1' %s\n", v ? AIT_GET_LIKE(v, char*) : "");
printf("Before sorting::\n");
for (i = 0; i < io_arraySize(arr); i++)
printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
AIT_GET_STR(io_array(arr, i, ait_val_t*)));
io_sortVarsByKey(arr, 0);
printf("After KEY ascend sorting::\n");
for (i = 0; i < io_arraySize(arr); i++)
printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
AIT_GET_STR(io_array(arr, i, ait_val_t*)));
io_sortVarsByKey(arr, 42);
printf("After KEY descend sorting::\n");
for (i = 0; i < io_arraySize(arr); i++)
printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
AIT_GET_STR(io_array(arr, i, ait_val_t*)));
io_sortVarsByVal(arr, 0, cmpfunc);
printf("After string sorting::\n");
for (i = 0; i < io_arraySize(arr); i++)
printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
AIT_GET_STR(io_array(arr, i, ait_val_t*)));
io_freeVars(&arr);
return 0;
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>