1
+ #include <assert.h>
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <string.h>
5
+
6
+ #include "cache.h"
7
+ #define TESTSIZE 1
8
+
9
+ void printValue (int * val )
10
+ {
11
+ if (val )
12
+ printf ("%d\n" , * val );
13
+ else
14
+ printf ("NULL\n" );
15
+ }
16
+
17
+ void split (char * * arr , char * str , const char * del )
18
+ {
19
+ char * s = strtok (str , del );
20
+
21
+ while (s != NULL ) {
22
+ * arr ++ = s ;
23
+ s = strtok (NULL , del );
24
+ }
25
+ }
26
+
27
+ int main (__attribute__((unused )) int argc , char * argv [])
28
+ {
29
+ FILE * fp = fopen (argv [1 ], "r" );
30
+ char * line = NULL , * ptr = NULL ;
31
+ size_t len = 0 ;
32
+ struct cache * cache = NULL ;
33
+ int key , * ans , * val ;
34
+ while (getline (& line , & len , fp ) != -1 ) {
35
+ char * arr [3 ];
36
+ split (arr , line , " " );
37
+ if (!strcmp (arr [0 ], "GET" )) {
38
+ key = (int ) strtol (arr [1 ], & ptr , 10 );
39
+ ans = cache_get (cache , key );
40
+ printValue (ans );
41
+ } else if (!strcmp (arr [0 ], "PUT" )) {
42
+ key = (int ) strtol (arr [1 ], & ptr , 10 );
43
+ val = malloc (sizeof (int ));
44
+ * val = (int ) strtol (arr [2 ], & ptr , 10 );
45
+ val = cache_put (cache , key , val );
46
+ if (val ) {
47
+ printf ("REPLACE %d\n" , * val );
48
+ free (val );
49
+ }
50
+ } else if (!strcmp (arr [0 ], "NEW\n" )) {
51
+ cache = cache_create ();
52
+ if (cache ) {
53
+ printf ("NEW CACHE\n" );
54
+ } else {
55
+ printf ("FAIL\n" );
56
+ }
57
+ } else if (!strcmp (arr [0 ], "FREE" )) {
58
+ cache_free (cache , free );
59
+ printf ("FREE CACHE\n" );
60
+ }
61
+ }
62
+ fclose (fp );
63
+ return 0 ;
64
+ }
0 commit comments