File:
[ELWIX - Embedded LightWeight unIX -] /
libelwix /
example /
test_vars.c
Revision
1.1.1.1 (vendor branch):
download - view:
text,
annotated -
select for diffs -
revision graph
Thu Jan 17 10:05:35 2013 UTC (11 years, 7 months ago) by
misho
Branches:
misho
CVS tags:
elwix1_9,
elwix1_8,
elwix1_7,
elwix1_6,
elwix1_5,
elwix1_4,
elwix1_3,
elwix1_2,
elwix1_1,
ELWIX1_8,
ELWIX1_7,
ELWIX1_6,
ELWIX1_5,
ELWIX1_4,
ELWIX1_3,
ELWIX1_2,
ELWIX1_1,
ELWIX1_0
ELWIX core library
1: /*
2: *
3: * 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
4: *
5: */
6: #include <stdio.h>
7: #include <string.h>
8: #include <elwix.h>
9:
10:
11: static int
12: cmpfunc(const void *a, const void *b)
13: {
14: return strcmp(AIT_GET_STR(*(ait_val_t**) a), AIT_GET_STR(*(ait_val_t**) b));
15: }
16:
17: static void
18: makevars()
19: {
20: ait_val_t *v;
21:
22: v = ait_makeVar(empty);
23: printf("Variable is empty? %d\n", AIT_ISEMPTY(v));
24: ait_freeVar(&v);
25: v = ait_makeVar(ptr, 0x12345678, 42);
26: printf("Variable is ptr=%p len=%u\n", AIT_GET_PTR(v), AIT_LEN(v));
27: ait_freeVar(&v);
28: v = ait_makeVar(data, "TEST DATA !!!", 14);
29: printf("Variable is data=%s len=%u\n", AIT_GET_DATA(v), AIT_LEN(v));
30: ait_freeVar(&v);
31: v = ait_makeVar(buffer, "TEST Buffer !!!", 16);
32: printf("Variable is buf=%s len=%u\n", AIT_GET_BUF(v), AIT_LEN(v));
33: ait_freeVar(&v);
34: v = ait_makeVar(string, "TEST String ...");
35: printf("Variable is str=%s len=%u\n", AIT_GET_STR(v), AIT_LEN(v));
36: ait_freeVar(&v);
37: v = ait_makeVar(blob, 1234, 10000);
38: printf("Variable is blob=%u len=%u\n", AIT_GET_BLOB(v), AIT_LEN(v));
39: ait_freeVar(&v);
40: v = ait_makeVar(f32, 1.0123);
41: printf("Variable is f32=%f\n", AIT_GET_F32(v));
42: ait_freeVar(&v);
43: v = ait_makeVar(f64, 0.00123321);
44: printf("Variable is f64=%f\n", AIT_GET_F64(v));
45: ait_freeVar(&v);
46: v = ait_makeVar(u8, 1);
47: printf("Variable is u8=%hhu\n", AIT_GET_U8(v));
48: ait_freeVar(&v);
49: v = ait_makeVar(u16, 32000);
50: printf("Variable is u16=%hu\n", AIT_GET_U16(v));
51: ait_freeVar(&v);
52: v = ait_makeVar(u32, 0xfedecaa0);
53: printf("Variable is u32=%u\n", AIT_GET_U32(v));
54: ait_freeVar(&v);
55: v = ait_makeVar(u64, 0xffeedccfedecaa01LL);
56: printf("Variable is u64=%llu\n", AIT_GET_U64(v));
57: ait_freeVar(&v);
58: v = ait_makeVar(i8, -1);
59: printf("Variable is i8=%hhd\n", AIT_GET_I8(v));
60: ait_freeVar(&v);
61: v = ait_makeVar(i16, -28888);
62: printf("Variable is i16=%hd\n", AIT_GET_I16(v));
63: ait_freeVar(&v);
64: v = ait_makeVar(i32, -23424234);
65: printf("Variable is i32=%d\n", AIT_GET_I32(v));
66: ait_freeVar(&v);
67: v = ait_makeVar(i64, -123234200000342LL);
68: printf("Variable is i64=%lld\n", AIT_GET_I64(v));
69: ait_freeVar(&v);
70: }
71:
72: static void
73: testcmp()
74: {
75: ait_val_t a, b;
76:
77: AIT_SET_STR(&a, "OHO boho");
78: AIT_SET_STR(&b, "OHO boho");
79: printf("Test equal strings %d\n", ait_cmpVar(&a, &b));
80: AIT_FREE_VAL(&b);
81: AIT_SET_STR(&b, "OHO b0ho");
82: printf("Test not equal strings %d\n", ait_cmpVar(&a, &b));
83: AIT_FREE_VAL(&b);
84: AIT_SET_STR(&b, "OHO b0ho different");
85: printf("Test not equal in length %d\n", ait_cmpVar(&a, &b));
86: AIT_FREE_VAL(&b);
87: AIT_SET_BUF(&b, "OHO", 4);
88: printf("Test not equal in type %d\n", ait_cmpVar(&a, &b));
89: AIT_FREE_VAL(&b);
90: AIT_FREE_VAL(&a);
91: AIT_SET_I16(&a, 5);
92: AIT_SET_I16(&b, 5);
93: printf("Test equal num %d\n", ait_cmpVar(&a, &b));
94: AIT_SET_I16(&b, 2);
95: printf("Test not equal num %d\n", ait_cmpVar(&a, &b));
96: AIT_SET_U8(&b, 9);
97: printf("Test not equal in type %d\n", ait_cmpVar(&a, &b));
98: }
99:
100: int
101: main(int argc, char **argv)
102: {
103: array_t *arr;
104: ait_val_t *v;
105: register int i;
106:
107: if (argc < 2) {
108: makevars();
109: testcmp();
110: return 0;
111: }
112:
113: arr = ait_allocVars((argc - 1 ) / 2);
114: if (!arr) {
115: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
116: return 1;
117: }
118:
119: for (i = 0; i < array_Size(arr); i++) {
120: v = array_Get(arr, i);
121: AIT_KEY(v) = atoi(argv[i * 2 + 1]);
122: AIT_SET_STR(v, argv[i * 2 + 2]);
123: }
124:
125: v = ait_findKeyVars(arr, 7);
126: printf("v=%p\n", v);
127: fflush(stdout);
128: printf("search for key=7 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
129: v = ait_findKeyVars(arr, 23);
130: printf("search for key=23 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
131: v = ait_findKeyVars(arr, 0);
132: printf("search for key=0 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
133: v = ait_findKeyVars(arr, 99);
134: printf("search for key=99 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
135:
136: ait_hashKeyVars(arr);
137:
138: v = ait_getVars(&arr, array_Size(arr));
139: AIT_SET_STR(v, "mauhahahaha ohoho and bottle of rum!");
140: ait_hashVar(v, "Baba Yaga #1");
141:
142: v = ait_findKeyHash(arr, "Baba Yaga #1");
143: printf("search for hash='Baba Yaga #1' %s\n", v ? AIT_GET_LIKE(v, char*) : "");
144:
145: printf("Before sorting::\n");
146: for (i = 0; i < array_Size(arr); i++)
147: printf("%d) KEY=%d %s\n", i, AIT_KEY(array(arr, i, ait_val_t*)),
148: AIT_GET_STR(array(arr, i, ait_val_t*)));
149:
150: ait_sortVarsByKey(arr, 0);
151: printf("After KEY ascend sorting::\n");
152: for (i = 0; i < array_Size(arr); i++)
153: printf("%d) KEY=%d %s\n", i, AIT_KEY(array(arr, i, ait_val_t*)),
154: AIT_GET_STR(array(arr, i, ait_val_t*)));
155:
156: ait_sortVarsByKey(arr, 42);
157: printf("After KEY descend sorting::\n");
158: for (i = 0; i < array_Size(arr); i++)
159: printf("%d) KEY=%d %s\n", i, AIT_KEY(array(arr, i, ait_val_t*)),
160: AIT_GET_STR(array(arr, i, ait_val_t*)));
161:
162: ait_sortVarsByVal(arr, 0, cmpfunc);
163: printf("After string sorting::\n");
164: for (i = 0; i < array_Size(arr); i++)
165: printf("%d) KEY=%d %s\n", i, AIT_KEY(array(arr, i, ait_val_t*)),
166: AIT_GET_STR(array(arr, i, ait_val_t*)));
167:
168: ait_freeVars(&arr);
169: return 0;
170: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>