Annotation of libaitio/example/test_vars.c, revision 1.6
1.2 misho 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 <aitio.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:
1.4 misho 17: static void
18: makevars()
19: {
20: ait_val_t *v;
21:
22: v = io_makeVar(empty);
23: printf("Variable is empty? %d\n", AIT_ISEMPTY(v));
24: io_freeVar(&v);
25: v = io_makeVar(ptr, 0x12345678, 42);
26: printf("Variable is ptr=%p len=%u\n", AIT_GET_PTR(v), AIT_LEN(v));
27: io_freeVar(&v);
28: v = io_makeVar(data, "TEST DATA !!!", 14);
29: printf("Variable is data=%s len=%u\n", AIT_GET_DATA(v), AIT_LEN(v));
30: io_freeVar(&v);
31: v = io_makeVar(buffer, "TEST Buffer !!!", 16);
32: printf("Variable is buf=%s len=%u\n", AIT_GET_BUF(v), AIT_LEN(v));
33: io_freeVar(&v);
34: v = io_makeVar(string, "TEST String ...");
35: printf("Variable is str=%s len=%u\n", AIT_GET_STR(v), AIT_LEN(v));
36: io_freeVar(&v);
37: v = io_makeVar(blob, 1234, 10000);
38: printf("Variable is blob=%u len=%u\n", AIT_GET_BLOB(v), AIT_LEN(v));
39: io_freeVar(&v);
40: v = io_makeVar(f32, 1.0123);
41: printf("Variable is f32=%f\n", AIT_GET_F32(v));
42: io_freeVar(&v);
43: v = io_makeVar(f64, 0.00123321);
44: printf("Variable is f64=%f\n", AIT_GET_F64(v));
45: io_freeVar(&v);
46: v = io_makeVar(u8, 1);
47: printf("Variable is u8=%hhu\n", AIT_GET_U8(v));
48: io_freeVar(&v);
49: v = io_makeVar(u16, 32000);
50: printf("Variable is u16=%hu\n", AIT_GET_U16(v));
51: io_freeVar(&v);
52: v = io_makeVar(u32, 0xfedecaa0);
53: printf("Variable is u32=%u\n", AIT_GET_U32(v));
54: io_freeVar(&v);
55: v = io_makeVar(u64, 0xffeedccfedecaa01LL);
56: printf("Variable is u64=%llu\n", AIT_GET_U64(v));
57: io_freeVar(&v);
58: v = io_makeVar(i8, -1);
59: printf("Variable is i8=%hhd\n", AIT_GET_I8(v));
60: io_freeVar(&v);
61: v = io_makeVar(i16, -28888);
62: printf("Variable is i16=%hd\n", AIT_GET_I16(v));
63: io_freeVar(&v);
64: v = io_makeVar(i32, -23424234);
65: printf("Variable is i32=%d\n", AIT_GET_I32(v));
66: io_freeVar(&v);
67: v = io_makeVar(i64, -123234200000342LL);
68: printf("Variable is i64=%lld\n", AIT_GET_I64(v));
69: io_freeVar(&v);
70: }
71:
1.6 ! misho 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", io_cmpVar(&a, &b));
! 80: AIT_FREE_VAL(&b);
! 81: AIT_SET_STR(&b, "OHO b0ho");
! 82: printf("Test not equal strings %d\n", io_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", io_cmpVar(&a, &b));
! 86: AIT_FREE_VAL(&b);
! 87: AIT_SET_BUF(&b, "OHO", 4);
! 88: printf("Test not equal in type %d\n", io_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", io_cmpVar(&a, &b));
! 94: AIT_SET_I16(&b, 2);
! 95: printf("Test not equal num %d\n", io_cmpVar(&a, &b));
! 96: AIT_SET_U8(&b, 9);
! 97: printf("Test not equal in type %d\n", io_cmpVar(&a, &b));
! 98: }
! 99:
1.2 misho 100: int
101: main(int argc, char **argv)
102: {
103: array_t *arr;
104: ait_val_t *v;
105: register int i;
106:
1.4 misho 107: if (argc < 2) {
108: makevars();
1.6 ! misho 109: testcmp();
1.4 misho 110: return 0;
111: }
112:
1.2 misho 113: arr = io_allocVars((argc - 1 ) / 2);
114: if (!arr) {
115: printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
116: return 1;
117: }
118:
119: for (i = 0; i < io_arraySize(arr); i++) {
120: v = io_arrayGet(arr, i);
121: AIT_KEY(v) = atoi(argv[i * 2 + 1]);
122: AIT_SET_STR(v, argv[i * 2 + 2]);
123: }
124:
125: v = io_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 = io_findKeyVars(arr, 23);
130: printf("search for key=23 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
131: v = io_findKeyVars(arr, 0);
132: printf("search for key=0 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
133: v = io_findKeyVars(arr, 99);
134: printf("search for key=99 %s\n", v ? AIT_GET_LIKE(v, char*) : "");
135:
1.5 misho 136: io_hashKeyVars(arr);
137:
138: v = io_getVars(&arr, io_arraySize(arr));
139: AIT_SET_STR(v, "mauhahahaha ohoho and bottle of rum!");
140: io_hashVar(v, "Baba Yaga #1");
141:
142: v = io_findKeyHash(arr, "Baba Yaga #1");
143: printf("search for hash='Baba Yaga #1' %s\n", v ? AIT_GET_LIKE(v, char*) : "");
144:
1.2 misho 145: printf("Before sorting::\n");
146: for (i = 0; i < io_arraySize(arr); i++)
147: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
148: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
149:
1.3 misho 150: io_sortVarsByKey(arr, 0);
1.2 misho 151: printf("After KEY ascend sorting::\n");
152: for (i = 0; i < io_arraySize(arr); i++)
153: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
154: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
155:
1.3 misho 156: io_sortVarsByKey(arr, 42);
1.2 misho 157: printf("After KEY descend sorting::\n");
158: for (i = 0; i < io_arraySize(arr); i++)
159: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
160: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
161:
1.3 misho 162: io_sortVarsByVal(arr, 0, cmpfunc);
1.2 misho 163: printf("After string sorting::\n");
164: for (i = 0; i < io_arraySize(arr); i++)
165: printf("%d) KEY=%d %s\n", i, AIT_KEY(io_array(arr, i, ait_val_t*)),
166: AIT_GET_STR(io_array(arr, i, ait_val_t*)));
167:
168: io_freeVars(&arr);
169: return 0;
170: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>