net-snmp 5.7
test_binary_array.c
00001 #include <net-snmp/net-snmp-config.h>
00002 
00003 #if HAVE_IO_H
00004 #include <io.h>
00005 #endif
00006 #include <stdio.h>
00007 #if HAVE_STDLIB_H
00008 #include <stdlib.h>
00009 #endif
00010 #if HAVE_MALLOC_H
00011 #include <malloc.h>
00012 #endif
00013 #include <sys/types.h>
00014 #if HAVE_STRING_H
00015 #include <string.h>
00016 #else
00017 #include <strings.h>
00018 #endif
00019 
00020 #include <net-snmp/net-snmp-includes.h>
00021 #include <net-snmp/types.h>
00022 #include <net-snmp/library/snmp_api.h>
00023 #include <net-snmp/library/container.h>
00024 #include <net-snmp/library/container_binary_array.h>
00025 #include <net-snmp/library/tools.h>
00026 #include <net-snmp/library/snmp_assert.h>
00027 
00028 #define TEST_SIZE 7
00029 
00030 void
00031 print_int(netsnmp_index *i, void *v)
00032 {
00033     printf("item %p = %ld\n", i, i->oids[0]);
00034 }
00035 
00036 int
00037 test_int(void)
00038 {
00039     oid o1 = 1;
00040     oid o2 = 2;
00041     oid o3 = 6;
00042     oid o4 = 8;
00043     oid o5 = 9;
00044     oid ox = 7;
00045     oid oy = 10;
00046     netsnmp_index i1,i2,i3,i4,i5,ix,iy, *ip;
00047     netsnmp_index *a[TEST_SIZE] = { &i1, &i2, &i3, &ix, &i4, &i5, &iy };
00048     netsnmp_container *c = netsnmp_container_get_binary_array();
00049     int i;
00050 
00051     c->compare = netsnmp_compare_netsnmp_index;
00052     
00053     i1.oids = &o1;
00054     i2.oids = &o2;
00055     i3.oids = &o3;
00056     i4.oids = &o4;
00057     i5.oids = &o5;
00058     ix.oids = &ox;
00059     iy.oids = &oy;
00060     i1.len = i2.len = i3.len = i4.len = i5.len = ix.len = iy.len = 1;
00061 
00062     printf("Creating container...\n");
00063 
00064     printf("Inserting data...\n");
00065     CONTAINER_INSERT(c,&i4);
00066     CONTAINER_INSERT(c,&i2);
00067     CONTAINER_INSERT(c,&i3);
00068     CONTAINER_INSERT(c,&i1);
00069     CONTAINER_INSERT(c,&i5);
00070 
00071     printf("For each...\n");
00072     CONTAINER_FOR_EACH(c, print_int, NULL);
00073     printf("Done.\n");
00074 
00075     printf("\n");
00076     ip = CONTAINER_FIRST(c);
00077     printf("Find first = %p %ld\n",ip, ip->oids[0]);
00078     while( ip ) {
00079         ip = CONTAINER_NEXT(c,ip);
00080         if(ip)
00081             printf("Find next = %p %ld\n",ip, ip->oids[0]);
00082         else
00083             printf("Find next = %s\n",ip);
00084     }
00085 
00086     for( i=0; i < TEST_SIZE; ++i) {
00087         printf("\n");
00088         ip = CONTAINER_FIND(c,a[i]);
00089         printf("Find %ld = %p %ld\n", a[i]->oids[0], ip, ip ? ip->oids[0] : 0);
00090         ip = CONTAINER_NEXT(c,a[i]);
00091         printf("Next %ld = %p %ld\n", a[i]->oids[0], ip, ip ? ip->oids[0] : 0);
00092     }
00093 
00094     printf("Done.\n");
00095 
00096     return 0;
00097 }
00098 
00099 void
00100 print_string(char *i, void *v)
00101 {
00102     printf("item %s\n", i);
00103 }
00104 
00105 int
00106 my_strcmp(char *lhs, char *rhs)
00107 {
00108     int rc = strcmp(lhs,rhs);
00109 /*      printf("%s %d %s\n",lhs, rc, rhs); */
00110     return rc;
00111 }
00112 
00113 int
00114 test_string()
00115 {
00116     const char *o1 = "zebra";
00117     const char *o2 = "b-two";
00118     const char *o3 = "b";
00119     const char *o4 = "cedar";
00120     const char *o5 = "alpha";
00121     const char *ox = "dev";
00122     const char *oy = "aa";
00123     const char * ip;
00124     
00125     const char *a[TEST_SIZE] = { o1, o2, o3, ox, o4, o5, oy };
00126     netsnmp_container *c = netsnmp_container_get_binary_array();
00127     int i;
00128 
00129     c->compare = my_strcmp;
00130     
00131     printf("Creating container...\n");
00132 
00133     printf("Inserting data...\n");
00134     CONTAINER_INSERT(c,o4);
00135     CONTAINER_INSERT(c,o2);
00136     CONTAINER_INSERT(c,o3);
00137     CONTAINER_INSERT(c,o1);
00138     CONTAINER_INSERT(c,o5);
00139     printf("\n");
00140     printf("For each...\n");
00141     CONTAINER_FOR_EACH(c, print_string, NULL);
00142     printf("Done.\n");
00143 
00144     printf("\n");
00145     ip = CONTAINER_FIRST(c);
00146     printf("Find first = %s\n",ip);
00147     while( ip ) {
00148         ip = CONTAINER_NEXT(c,ip);
00149         printf("Find next = %s\n",ip);
00150     }
00151 
00152     for( i=0; i < TEST_SIZE; ++i) {
00153         printf("\n");
00154         ip = CONTAINER_FIND(c,a[i]);
00155         printf("Find %s = %s\n", a[i], ip);
00156         ip = CONTAINER_NEXT(c,a[i]);
00157         printf("Next %s = %s\n", a[i], ip);
00158     }
00159 
00160     printf("Done.\n");
00161 
00162     return 0;
00163 }
00164 
00165 int
00166 main(int argc, char** argv)
00167 {
00168 
00169     test_int();
00170     test_string();
00171 }