File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / example / test_vars.c
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Mon Apr 15 10:42:26 2024 UTC (7 months, 1 week ago) by misho
Branches: MAIN
CVS tags: elwix6_6, elwix6_5, elwix6_4, elwix6_3, elwix6_2, elwix6_1, elwix5_12, HEAD, ELWIX6_5, ELWIX6_4, ELWIX6_2, ELWIX6_1, ELWIX6_0, ELWIX5_11
Version 5.11

/*
 *
 * 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 <stdlib.h>
#include <string.h>
#include <elwix.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 = ait_makeVar(empty);
	printf("Variable is empty? %d\n", AIT_ISEMPTY(v));
	ait_freeVar(&v);
	v = ait_makeVar(ptr, 0x12345678, 42);
	printf("Variable is ptr=%p len=%u\n", AIT_GET_PTR(v), AIT_LEN(v));
	ait_freeVar(&v);
	v = ait_makeVar(data, "TEST DATA !!!", 14);
	printf("Variable is data=%s len=%u\n", AIT_GET_DATA(v), AIT_LEN(v));
	ait_freeVar(&v);
	v = ait_makeVar(buffer, "TEST Buffer !!!", 16);
	printf("Variable is buf=%s len=%u\n", AIT_GET_BUF(v), AIT_LEN(v));
	ait_freeVar(&v);
	v = ait_makeVar(string, "TEST String ...");
	printf("Variable is str=%s len=%u\n", AIT_GET_STR(v), AIT_LEN(v));
	ait_freeVar(&v);
	v = ait_makeVar(blob, 1234, 10000);
	printf("Variable is blob=%u len=%u\n", AIT_GET_BLOB(v), AIT_LEN(v));
	ait_freeVar(&v);
	v = ait_makeVar(f32, 1.0123);
	printf("Variable is f32=%f\n", AIT_GET_F32(v));
	ait_freeVar(&v);
	v = ait_makeVar(f64, 0.00123321);
	printf("Variable is f64=%f\n", AIT_GET_F64(v));
	ait_freeVar(&v);
	v = ait_makeVar(u8, 1);
	printf("Variable is u8=%hhu\n", AIT_GET_U8(v));
	ait_freeVar(&v);
	v = ait_makeVar(u16, 32000);
	printf("Variable is u16=%hu\n", AIT_GET_U16(v));
	ait_freeVar(&v);
	v = ait_makeVar(u32, 0xfedecaa0);
	printf("Variable is u32=%u\n", AIT_GET_U32(v));
	ait_freeVar(&v);
	v = ait_makeVar(u64, 0xffeedccfedecaa01LL);
	printf("Variable is u64=%lu\n", AIT_GET_U64(v));
	ait_freeVar(&v);
	v = ait_makeVar(i8, -1);
	printf("Variable is i8=%hhd\n", AIT_GET_I8(v));
	ait_freeVar(&v);
	v = ait_makeVar(i16, -28888);
	printf("Variable is i16=%hd\n", AIT_GET_I16(v));
	ait_freeVar(&v);
	v = ait_makeVar(i32, -23424234);
	printf("Variable is i32=%d\n", AIT_GET_I32(v));
	ait_freeVar(&v);
	v = ait_makeVar(i64, -123234200000342LL);
	printf("Variable is i64=%ld\n", AIT_GET_I64(v));
	ait_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", ait_cmpVar(&a, &b));
	AIT_FREE_VAL(&b);
	AIT_SET_STR(&b, "OHO b0ho");
	printf("Test not equal strings %d\n", ait_cmpVar(&a, &b));
	AIT_FREE_VAL(&b);
	AIT_SET_STR(&b, "OHO b0ho different");
	printf("Test not equal in length %d\n", ait_cmpVar(&a, &b));
	AIT_FREE_VAL(&b);
	AIT_SET_BUF(&b, "OHO", 4);
	printf("Test not equal in type %d\n", ait_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", ait_cmpVar(&a, &b));
	AIT_SET_I16(&b, 2);
	printf("Test not equal num %d\n", ait_cmpVar(&a, &b));
	AIT_SET_U8(&b, 9);
	printf("Test not equal in type %d\n", ait_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 = ait_allocVars((argc - 1 ) / 2);
	if (!arr) {
		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
		return 1;
	}

	for (i = 0; i < array_Size(arr); i++) {
		v = array_Get(arr, i);
		AIT_KEY(v) = atoi(argv[i * 2 + 1]);
		AIT_SET_STR(v, argv[i * 2 + 2]);
	}

	printf("Reside %zu bytes into variables array\n", ait_resideVars(arr));

	v = ait_findKeyVars(arr, 7);
	printf("v=%p\n", v);
	fflush(stdout);
	printf("search for key=7 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
	v = ait_findKeyVars(arr, 23);
	printf("search for key=23 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
	v = ait_findKeyVars(arr, 0);
	printf("search for key=0 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
	v = ait_findKeyVars(arr, 99);
	printf("search for key=99 %s\n", v ? AIT_GET_LIKE(v, char*) : "");

	ait_hashKeyVars(arr);

	v = ait_getVars(&arr, array_Size(arr));
	AIT_SET_STR(v, "mauhahahaha ohoho and bottle of rum!");
	ait_hashVar(v, "Baba Yaga #1");

	v = ait_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 < array_Size(arr); i++)
		printf("%d) KEY=%d %s\n", i, AIT_KEY(array(arr, i, ait_val_t*)), 
				AIT_GET_STR(array(arr, i, ait_val_t*)));

	ait_sortVarsByKey(arr, 0);
	printf("After KEY ascend sorting::\n");
	for (i = 0; i < array_Size(arr); i++)
		printf("%d) KEY=%d %s\n", i, AIT_KEY(array(arr, i, ait_val_t*)), 
				AIT_GET_STR(array(arr, i, ait_val_t*)));

	ait_sortVarsByKey(arr, 42);
	printf("After KEY descend sorting::\n");
	for (i = 0; i < array_Size(arr); i++)
		printf("%d) KEY=%d %s\n", i, AIT_KEY(array(arr, i, ait_val_t*)), 
				AIT_GET_STR(array(arr, i, ait_val_t*)));

	ait_sortVarsByVal(arr, 0, cmpfunc);
	printf("After string sorting::\n");
	for (i = 0; i < array_Size(arr); i++)
		printf("%d) KEY=%d %s\n", i, AIT_KEY(array(arr, i, ait_val_t*)), 
				AIT_GET_STR(array(arr, i, ait_val_t*)));

	ait_freeVars(&arr);
	return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>