File: test_binary_array.c

package info (click to toggle)
net-snmp 5.2.3-7etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 24,452 kB
  • ctags: 16,045
  • sloc: ansic: 175,930; perl: 11,814; sh: 11,230; makefile: 5,375; pascal: 62
file content (171 lines) | stat: -rw-r--r-- 3,685 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <net-snmp/net-snmp-config.h>

#if HAVE_IO_H
#include <io.h>
#endif
#include <stdio.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_MALLOC_H
#include <malloc.h>
#endif
#include <sys/types.h>
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif

#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/types.h>
#include <net-snmp/library/snmp_api.h>
#include <net-snmp/library/container.h>
#include <net-snmp/library/container_binary_array.h>
#include <net-snmp/library/tools.h>
#include <net-snmp/library/snmp_assert.h>

#define TEST_SIZE 7

void
print_int(netsnmp_index *i, void *v)
{
    printf("item %p = %ld\n", i, i->oids[0]);
}

int
test_int(void)
{
    oid o1 = 1;
    oid o2 = 2;
    oid o3 = 6;
    oid o4 = 8;
    oid o5 = 9;
    oid ox = 7;
    oid oy = 10;
    netsnmp_index i1,i2,i3,i4,i5,ix,iy, *ip;
    netsnmp_index *a[TEST_SIZE] = { &i1, &i2, &i3, &ix, &i4, &i5, &iy };
    netsnmp_container *c = netsnmp_container_get_binary_array();
    int i;

    c->compare = netsnmp_compare_netsnmp_index;
    
    i1.oids = &o1;
    i2.oids = &o2;
    i3.oids = &o3;
    i4.oids = &o4;
    i5.oids = &o5;
    ix.oids = &ox;
    iy.oids = &oy;
    i1.len = i2.len = i3.len = i4.len = i5.len = ix.len = iy.len = 1;

    printf("Creating container...\n");

    printf("Inserting data...\n");
    CONTAINER_INSERT(c,&i4);
    CONTAINER_INSERT(c,&i2);
    CONTAINER_INSERT(c,&i3);
    CONTAINER_INSERT(c,&i1);
    CONTAINER_INSERT(c,&i5);

    printf("For each...\n");
    CONTAINER_FOR_EACH(c, print_int, NULL);
    printf("Done.\n");

    printf("\n");
    ip = CONTAINER_FIRST(c);
    printf("Find first = %p %ld\n",ip, ip->oids[0]);
    while( ip ) {
        ip = CONTAINER_NEXT(c,ip);
        if(ip)
            printf("Find next = %p %ld\n",ip, ip->oids[0]);
        else
            printf("Find next = %s\n",ip);
    }

    for( i=0; i < TEST_SIZE; ++i) {
        printf("\n");
        ip = CONTAINER_FIND(c,a[i]);
        printf("Find %ld = %p %ld\n", a[i]->oids[0], ip, ip ? ip->oids[0] : 0);
        ip = CONTAINER_NEXT(c,a[i]);
        printf("Next %ld = %p %ld\n", a[i]->oids[0], ip, ip ? ip->oids[0] : 0);
    }

    printf("Done.\n");

    return 0;
}

void
print_string(char *i, void *v)
{
    printf("item %s\n", i);
}

int
my_strcmp(char *lhs, char *rhs)
{
    int rc = strcmp(lhs,rhs);
/*      printf("%s %d %s\n",lhs, rc, rhs); */
    return rc;
}

int
test_string()
{
    const char *o1 = "zebra";
    const char *o2 = "b-two";
    const char *o3 = "b";
    const char *o4 = "cedar";
    const char *o5 = "alpha";
    const char *ox = "dev";
    const char *oy = "aa";
    const char * ip;
    
    const char *a[TEST_SIZE] = { o1, o2, o3, ox, o4, o5, oy };
    netsnmp_container *c = netsnmp_container_get_binary_array();
    int i;

    c->compare = my_strcmp;
    
    printf("Creating container...\n");

    printf("Inserting data...\n");
    CONTAINER_INSERT(c,o4);
    CONTAINER_INSERT(c,o2);
    CONTAINER_INSERT(c,o3);
    CONTAINER_INSERT(c,o1);
    CONTAINER_INSERT(c,o5);
    printf("\n");
    printf("For each...\n");
    CONTAINER_FOR_EACH(c, print_string, NULL);
    printf("Done.\n");

    printf("\n");
    ip = CONTAINER_FIRST(c);
    printf("Find first = %s\n",ip);
    while( ip ) {
        ip = CONTAINER_NEXT(c,ip);
        printf("Find next = %s\n",ip);
    }

    for( i=0; i < TEST_SIZE; ++i) {
        printf("\n");
        ip = CONTAINER_FIND(c,a[i]);
        printf("Find %s = %s\n", a[i], ip);
        ip = CONTAINER_NEXT(c,a[i]);
        printf("Next %s = %s\n", a[i], ip);
    }

    printf("Done.\n");

    return 0;
}

int
main(int argc, char** argv)
{

    test_int();
    test_string();
}