and added files
[bcm963xx.git] / userapps / opensource / net-snmp / snmplib / test_binary_array.c
1 #include <net-snmp/net-snmp-config.h>
2
3 #if HAVE_IO_H
4 #include <io.h>
5 #endif
6 #include <stdio.h>
7 #if HAVE_STDLIB_H
8 #include <stdlib.h>
9 #endif
10 #if HAVE_MALLOC_H
11 #include <malloc.h>
12 #endif
13 #include <sys/types.h>
14 #if HAVE_STRING_H
15 #include <string.h>
16 #else
17 #include <strings.h>
18 #endif
19
20 #include <net-snmp/net-snmp-includes.h>
21 #include <net-snmp/types.h>
22 #include <net-snmp/library/snmp_api.h>
23 #include <net-snmp/library/container.h>
24 #include <net-snmp/library/container_binary_array.h>
25 #include <net-snmp/library/tools.h>
26 #include <net-snmp/library/snmp_assert.h>
27
28 #define TEST_SIZE 7
29
30 void
31 print_int(netsnmp_index *i, void *v)
32 {
33     printf("item %p = %ld\n", i, i->oids[0]);
34 }
35
36 int
37 test_int(void)
38 {
39     oid o1 = 1;
40     oid o2 = 2;
41     oid o3 = 6;
42     oid o4 = 8;
43     oid o5 = 9;
44     oid ox = 7;
45     oid oy = 10;
46     netsnmp_index i1,i2,i3,i4,i5,ix,iy, *ip;
47     netsnmp_index *a[TEST_SIZE] = { &i1, &i2, &i3, &ix, &i4, &i5, &iy };
48     netsnmp_container *c = netsnmp_container_get_binary_array();
49     int i;
50
51     c->compare = netsnmp_compare_netsnmp_index;
52     
53     i1.oids = &o1;
54     i2.oids = &o2;
55     i3.oids = &o3;
56     i4.oids = &o4;
57     i5.oids = &o5;
58     ix.oids = &ox;
59     iy.oids = &oy;
60     i1.len = i2.len = i3.len = i4.len = i5.len = ix.len = iy.len = 1;
61
62     printf("Creating container...\n");
63
64     printf("Inserting data...\n");
65     CONTAINER_INSERT(c,&i4);
66     CONTAINER_INSERT(c,&i2);
67     CONTAINER_INSERT(c,&i3);
68     CONTAINER_INSERT(c,&i1);
69     CONTAINER_INSERT(c,&i5);
70
71     printf("For each...\n");
72     CONTAINER_FOR_EACH(c, print_int, NULL);
73     printf("Done.\n");
74
75     printf("\n");
76     ip = CONTAINER_FIRST(c);
77     printf("Find first = %p %ld\n",ip, ip->oids[0]);
78     while( ip ) {
79         ip = CONTAINER_NEXT(c,ip);
80         if(ip)
81             printf("Find next = %p %ld\n",ip, ip->oids[0]);
82         else
83             printf("Find next = %s\n",ip);
84     }
85
86     for( i=0; i < TEST_SIZE; ++i) {
87         printf("\n");
88         ip = CONTAINER_FIND(c,a[i]);
89         printf("Find %ld = %p %ld\n", a[i]->oids[0], ip, ip ? ip->oids[0] : 0);
90         ip = CONTAINER_NEXT(c,a[i]);
91         printf("Next %ld = %p %ld\n", a[i]->oids[0], ip, ip ? ip->oids[0] : 0);
92     }
93
94     printf("Done.\n");
95
96     return 0;
97 }
98
99 void
100 print_string(char *i, void *v)
101 {
102     printf("item %s\n", i);
103 }
104
105 int
106 my_strcmp(char *lhs, char *rhs)
107 {
108     int rc = strcmp(lhs,rhs);
109 /*      printf("%s %d %s\n",lhs, rc, rhs); */
110     return rc;
111 }
112
113 int
114 test_string()
115 {
116     const char *o1 = "zebra";
117     const char *o2 = "b-two";
118     const char *o3 = "b";
119     const char *o4 = "cedar";
120     const char *o5 = "alpha";
121     const char *ox = "dev";
122     const char *oy = "aa";
123     const char * ip;
124     
125     const char *a[TEST_SIZE] = { o1, o2, o3, ox, o4, o5, oy };
126     netsnmp_container *c = netsnmp_container_get_binary_array();
127     int i;
128
129     c->compare = my_strcmp;
130     
131     printf("Creating container...\n");
132
133     printf("Inserting data...\n");
134     CONTAINER_INSERT(c,o4);
135     CONTAINER_INSERT(c,o2);
136     CONTAINER_INSERT(c,o3);
137     CONTAINER_INSERT(c,o1);
138     CONTAINER_INSERT(c,o5);
139     printf("\n");
140     printf("For each...\n");
141     CONTAINER_FOR_EACH(c, print_string, NULL);
142     printf("Done.\n");
143
144     printf("\n");
145     ip = CONTAINER_FIRST(c);
146     printf("Find first = %s\n",ip);
147     while( ip ) {
148         ip = CONTAINER_NEXT(c,ip);
149         printf("Find next = %s\n",ip);
150     }
151
152     for( i=0; i < TEST_SIZE; ++i) {
153         printf("\n");
154         ip = CONTAINER_FIND(c,a[i]);
155         printf("Find %s = %s\n", a[i], ip);
156         ip = CONTAINER_NEXT(c,a[i]);
157         printf("Next %s = %s\n", a[i], ip);
158     }
159
160     printf("Done.\n");
161
162     return 0;
163 }
164
165 int
166 main(int argc, char** argv)
167 {
168
169     test_int();
170     test_string();
171 }