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.
37 #define LENARR (1<<10)
52 exstr_cmp(const void *a, const void *b) {
53 return ((EXMPL*)a)->id - ((EXMPL*)b)->id;
60 exstr_free( void *a ) {
62 if ( ((EXMPL*)a)->value ) free( ((EXMPL*)a)->value );
68 main(int argn, char *argv[]) {
75 opentlog( TL_OPEN_STDERR, TL_DEBUG, NULL);
77 if ( PS_init( &sobj, LIMIT, exstr_cmp, exstr_free ) ) {
78 fprintf(stderr,"No memory\n");
82 printf("Test insert:\n");
83 for(i=0;i<LENARR;i++) {
85 ptr = (EXMPL*)tmalloc( sizeof(EXMPL) );
86 ptr->value=(char*)tmalloc( 64 );
88 ptr->id = random() % LENARR;
89 sprintf( ptr->value, "Value: %d, startnum: %d", ptr->id, i );
91 if ( PS_insert( &sobj, (void*)ptr ) ) {
92 /* tuple has been inserted, in
93 other case we can reuse space :) */
99 PS_init_iterator( &sobj, 0 );
101 while( (ptr=(EXMPL*)PS_getnext( &sobj )) != NULL ) {
102 printf("%d: '%s'\n", i, ptr->value);
105 printf("Stat: total %d; limit %d; inserted %d;\n", LENARR, PS_number(&sobj) , inserted);
109 printf("Test bulk insert:\n");
110 aptr = (EXMPL**)tmalloc( sizeof(EXMPL*) * LENARR );
111 for(i=0;i<LENARR;i++) {
112 aptr[i] = (EXMPL*)tmalloc( sizeof(EXMPL) );
113 aptr[i]->value = (char*)tmalloc( 64 );
114 aptr[i]->id = random() % LENARR;
115 sprintf( aptr[i]->value, "Value: %d, startnum: %d", aptr[i]->id, i );
118 PS_bulkinsert( &sobj, (void**)aptr, LENARR );
120 if ( PS_init_iterator( &sobj, 7 ) ) {
124 while( (ptr=(EXMPL*)PS_getnext( &sobj )) != NULL ) {
125 printf("%d: '%s'\n", i, ptr->value);
128 printf("Stat: total %d; limit %d;\n", LENARR, PS_number(&sobj));
134 printf("Test bulk plain insert:\n");
136 if ( PS_init( &sobj, LIMIT, exstr_cmp, NULL ) ) {
137 fprintf(stderr,"No memory\n");
140 ptr = (EXMPL*)tmalloc( sizeof(EXMPL) * LENARR );
142 for(i=0;i<LENARR;i++) {
143 ptr[i].value = (char*)tmalloc( 64 );
144 ptr[i].id = random() % LENARR;
145 sprintf( ptr[i].value, "Value: %d, startnum: %d", ptr[i].id, i );
147 PS_bulkplain( &sobj, (void*)ptr, sizeof(EXMPL), LENARR );
148 if ( PS_init_iterator( &sobj, 7 ) ) {
152 while( (ptr=(EXMPL*)PS_getnext( &sobj )) != NULL ) {
153 printf("%d: '%s'\n", i, ptr->value);
156 printf("Stat: total %d; limit %d;\n", LENARR, PS_number(&sobj));