1 /* check program for fifocache. This file is public domain.
2  * made by radim kolar.
3  */
4 
5 #include "tweak.h"
6 #include <stdio.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <stdlib.h>
10 #include "../server/fifocache.h"
11 
intcompare(const void * vi1,const void * vi2)12 static int intcompare(const void *vi1,const void *vi2)
13 {
14     const int *i1,*i2;
15     i1 = vi1;
16     i2 = vi2;
17     if(i1==NULL || i2==NULL) return 1;
18     if(*i1==*i2) return 0;
19     return 1;
20 }
21 
string_free(void * entry)22 static void string_free (void * entry)
23 {
24     char **s=entry;
25     if(*s!=NULL)
26 	free(*s);
27 }
28 
string_compare(const void * e1,const void * e2)29 static int string_compare (const void *e1,const void *e2)
30 {
31 
32     char *const *s1=e1;
33     char *const *s2=e2;
34 
35     /* strcmp do not likes NULLs */
36     if(*s1 && *s2)
37     {
38        return strcmp(*s1,*s2);
39     }else
40 	return 1;
41 }
42 
main(int argv,char ** argc)43 int main(int argv,char **argc)
44 {
45     struct FifoCache * cache;
46     char file[20];
47     const char *s;
48     int i;
49 
50     cache=f_cache_new(4,sizeof(file),NULL,sizeof(int),NULL,intcompare);
51     assert(cache!=NULL);
52     strcpy(file,"/jeden/soubor");
53     i=1;
54     printf("Puting key %d: %s\n",i,file);
55     f_cache_put(cache,&i,file);
56     strcpy(file,"/druhy");
57     i=2;
58     printf("Puting key %d: %s\n",i,file);
59     f_cache_put(cache,&i,file);
60     strcpy(file,"/treti");
61     i=3;
62     printf("Puting key %d: %s\n",i,file);
63     f_cache_put(cache,&i,file);
64     strcpy(file,"/ctvrty/soubor");
65     i=4;
66     printf("Puting key %d: %s\n",i,file);
67     f_cache_put(cache,&i,file);
68 
69     for(i=0;i<=5;i++)
70     {
71        printf("Finding key %d: %s\n",i,(char *)f_cache_find(cache,&i));
72     }
73     f_cache_clear(cache);
74     f_cache_destroy(cache);
75 
76     cache=f_cache_new(4,0,0,sizeof(char *),string_free,string_compare);
77     assert(cache!=NULL);
78 
79     s="lamer1";
80     f_cache_put(cache,&s,NULL);
81     s="lamer2";
82     f_cache_put(cache,&s,NULL);
83     s="lamer co tu neni";
84     printf("find2: %s\n",(char *)f_cache_find(cache,&s));
85     s="lamer1";
86     printf("find2: %s\n",(char *)f_cache_find(cache,&s));
87 
88     return 0;
89 }
90