File:  [ELWIX - Embedded LightWeight unIX -] / libelwix / example / test_sarray.c
Revision 1.2: 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

#include <stdio.h>
#include <string.h>
#include <elwix.h>

int
main(int argc, char **argv)
{
	sarr_t *a = NULL;
	array_t *ar;
	int i;
	void *d = "MEOWeoeoeoeoe!!!";

	a = sarr_Init(10, 4);
	if (!a) {
		printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
		return 1;
	}

	switch (argc) {
		case 4:
			sarr_Set(a, 10, argv[3]);
		case 3:
			sarr_Set(a, 6, argv[2]);
			sarr_Set(a, 4, argv[2]);
		case 2:
			sarr_Set(a, 3, argv[1]);
			sarr_Set(a, 1, argv[1]);
	}

	printf(">>> a[1]=%s a[2]=%s a[3]=%s a[4]=%s a[6]=%s a[10]=%s\n", 
			sarr(a, 1, char*), sarr(a, 2, char*), 
			sarr(a, 3, char*), sarr(a, 4, char*), 
			sarr(a, 6, char*), sarr(a, 10, char*));
	printf(">>> array size=%d\n", sarr_Size(a));

	sarr_Del(a, 6);
	sarr_Del(a, 9);
	sarr_Del(a, 10);
	printf(">>> a[1]=%s a[2]=%s a[3]=%s a[4]=%s a[6]=%s a[10]=%s\n", 
			sarr(a, 1, char*), sarr(a, 2, char*), 
			sarr(a, 3, char*), sarr(a, 4, char*), 
			sarr(a, 6, char*), sarr(a, 10, char*));
	printf(">>> array size=%d\n", sarr_Size(a));

	sarr_Set(a, 240, d);
	printf(">>> array size=%d s[240]=%s\n", sarr_Size(a), (char*) sarr_Get2(a, 240));
	sarr_Set(a, 240, d);
	printf(">>> array size=%d s[240]=%s\n", sarr_Size(a), sarr(a, 240, char*));
	printf(">>> array size=%d s[240]=%s\n", sarr_Size(a), (char*) sarr_Get2(a, 240));

	sarr_Vacuum(a);
	printf("vacuum:: a[1]=%s a[2]=%s a[3]=%s a[4]=%s a[6]=%s a[10]=%s\n", 
			sarr(a, 1, char*), sarr(a, 2, char*), 
			sarr(a, 3, char*), sarr(a, 4, char*), 
			sarr(a, 6, char*), sarr(a, 10, char*));
	printf("vacuum:: array size=%d\n", sarr_Size(a));

	sarr_Grow(a, 20);
	printf("grow:: array size=%d\n", sarr_Size(a));
	printf("grow:: a[1]=%s a[2]=%s a[3]=%s a[4]=%s a[6]=%s a[10]=%s\n", 
			sarr(a, 1, char*), sarr(a, 2, char*), 
			sarr(a, 3, char*), sarr(a, 4, char*), 
			sarr(a, 6, char*), sarr(a, 10, char*));

	sarr_Grow(a, 1);
	sarr_Grow(a, 2);
	printf("shrink:: array size=%d\n", sarr_Size(a));
	printf("shrink:: a[1]=%s a[2]=%s\n", sarr(a, 1, char*), sarr(a, 2, char*));

	ar = sarr_sarr2array(&a, 1);
	if (!ar) {
		printf("Error:: in convert sarr2array #%d - %s\n", elwix_GetErrno(), elwix_GetError());
	} else
		for (i = 0; i < array_Size(ar); i++)
			printf("arrGet(%d)=%s\n", i, array(ar, i, char*));
	//sarr_Destroy(&a);

	a = sarr_array2sarr(&ar, 4, 0);
	if (!a) {
		printf("Error:: in convert array2sarr #%d - %s\n", elwix_GetErrno(), elwix_GetError());
	} else
		for (i = 0; i < sarr_Size(a); i++)
			printf("sarrGet(%d)=%s\n", i, sarr(a, i + 1, char*));

	array_Destroy(&ar);
	sarr_Destroy(&a);
	return 0;
}

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