-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathictest.c
126 lines (107 loc) · 2.97 KB
/
ictest.c
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
/* vim: set ts=2 sw=2:
* =====================================================================================
*
* Filename: ictest.c
*
* Description: ItemCache testing program
*
* Version: 1.0
* Created: 2011-10-05 12:30
* Revision: none
* Compiler: gcc
*
* Author: Peter Meszaros (pme), [email protected]
* Company: Infalk Co.
*
* =====================================================================================
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <string.h>
#include <err.h>
#include <libgen.h>
#include <stdint.h>
#include "ic.h"
#define PATH "./ic.test"
#define FILES 256
#define CACHE_SIZE 20
struct test_item {
uint32_t id;
struct timeval ts;
float value;
uint8_t state;
};
void printtestitem(FILE *f, char *item)
{
struct test_item *ti = (struct test_item *)item;
fprintf(f, "ti: %u %06ld.%06ld %f 0x%02X\n",
ti->id,
ti->ts.tv_sec,
ti->ts.tv_usec,
ti->value,
ti->state);
}
int main(int argc, char *argv[])
{
int delete, opt;
struct ic *ic;
struct test_item ti;
int i;
delete = 0;
while ((opt = getopt(argc, argv, "dh")) != -1) {
switch (opt) {
case 'd':
delete = 1;
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-d]\n", basename(argv[0]));
fprintf(stderr, "\t-d - Delete test data direcrory\n");
exit(EXIT_FAILURE);
}
}
/* ------------------------------------------------------ */
if (mkdir(PATH, 0755) == -1)
err(EXIT_FAILURE, "mkdir(%s)", PATH);
ic = ICcreate(FILES, sizeof(struct test_item), CACHE_SIZE, PATH);
for (i=0; i<(FILES*(CACHE_SIZE+1)); i++) {
ti.id = i;
gettimeofday(&ti.ts, NULL);
ti.value = i*3;
ti.state = i+1;
ICadd(ic, i % FILES, (char *)&ti);
if ((i % FILES) == 0)
ICprintcache(stdout, ic, 1, printtestitem);
}
ICdrop(ic);
if (delete && (system("rm -rf " PATH) == -1))
err(EXIT_FAILURE, "system(\"rm -rf %s\")", PATH);
return 0;
}
#if 0
struct ic * ICcreate(int nfiles, int itemsize, int nitem, char *path);
ssize_t ICflush(struct ic *ic, int fd);
int ICflushall(struct ic *ic);
int ICadd(struct ic *ic, int fd, char *item);
void ICprint(FILE *f, struct ic *ic, int data);
#endif