2 * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #define LENARR (1<<10)
50 exstr_cmp(const void *a, const void *b) {
51 return ((EXMPL*)a)->id - ((EXMPL*)b)->id;
58 exstr_free( void *a ) {
60 if ( ((EXMPL*)a)->value ) free( ((EXMPL*)a)->value );
66 main(int argn, char *argv[]) {
73 if ( PS_init( &sobj, LIMIT, exstr_cmp, exstr_free ) ) {
74 fprintf(stderr,"No memory\n");
78 printf("Test insert:\n");
79 for(i=0;i<LENARR;i++) {
81 ptr = (EXMPL*)malloc( sizeof(EXMPL) );
83 fprintf(stderr,"No memory\n");
86 ptr->value=(char*)malloc( 64 );
88 fprintf(stderr,"No memory\n");
92 ptr->id = random() % LENARR;
93 sprintf( ptr->value, "Value: %d, startnum: %d", ptr->id, i );
95 if ( PS_insert( &sobj, (void*)ptr ) ) {
96 /* tuple has been inserted, in
97 other case we can reuse space :) */
103 PS_init_iterator( &sobj, 0 );
105 while( (ptr=(EXMPL*)PS_getnext( &sobj )) != NULL ) {
106 printf("%d: '%s'\n", i, ptr->value);
109 printf("Stat: total %d; limit %d; inserted %d;\n", LENARR, PS_number(&sobj) , inserted);
113 printf("Test bulk insert:\n");
114 aptr = (EXMPL**)malloc( sizeof(EXMPL*) * LENARR );
116 fprintf(stderr,"No memory\n");
119 for(i=0;i<LENARR;i++) {
120 aptr[i] = (EXMPL*)malloc( sizeof(EXMPL) );
122 fprintf(stderr,"No memory\n");
125 aptr[i]->value = (char*)malloc( 64 );
126 if ( ! aptr[i]->value ) {
127 fprintf(stderr,"No memory\n");
130 aptr[i]->id = random() % LENARR;
131 sprintf( aptr[i]->value, "Value: %d, startnum: %d", aptr[i]->id, i );
134 PS_bulkinsert( &sobj, (void**)aptr, LENARR );
136 if ( PS_init_iterator( &sobj, 7 ) ) {
140 while( (ptr=(EXMPL*)PS_getnext( &sobj )) != NULL ) {
141 printf("%d: '%s'\n", i, ptr->value);
144 printf("Stat: total %d; limit %d;\n", LENARR, PS_number(&sobj));
150 printf("Test bulk plain insert:\n");
152 if ( PS_init( &sobj, LIMIT, exstr_cmp, NULL ) ) {
153 fprintf(stderr,"No memory\n");
156 ptr = (EXMPL*)malloc( sizeof(EXMPL) * LENARR );
158 fprintf(stderr,"No memory\n");
162 for(i=0;i<LENARR;i++) {
163 ptr[i].value = (char*)malloc( 64 );
164 if ( ! ptr[i].value ) {
165 fprintf(stderr,"No memory\n");
168 ptr[i].id = random() % LENARR;
169 sprintf( ptr[i].value, "Value: %d, startnum: %d", ptr[i].id, i );
171 PS_bulkplain( &sobj, (void*)ptr, sizeof(EXMPL), LENARR );
172 if ( PS_init_iterator( &sobj, 7 ) ) {
176 while( (ptr=(EXMPL*)PS_getnext( &sobj )) != NULL ) {
177 printf("%d: '%s'\n", i, ptr->value);
180 printf("Stat: total %d; limit %d;\n", LENARR, PS_number(&sobj));