summaryrefslogtreecommitdiff
path: root/src/tests/eio/eio_test_map.c
blob: 929938269099f09f26aacbd04ca34a312ca6f99b (plain) (blame)
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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <Ecore.h>
#include <Eio.h>
#include <Eet.h>

#include "eio_suite.h"

Eina_File *ee;

static void
_done_cb(void *data EINA_UNUSED, Eio_File *handler EINA_UNUSED)
{
   ecore_main_loop_quit();
}

static void
_open_cb(void *data EINA_UNUSED, Eio_File *handler EINA_UNUSED, Eina_File *ef)
{
   ee = ef;
   ecore_main_loop_quit();
}

static Eina_Bool
_filter_cb(void *data, Eio_File *handler EINA_UNUSED, void *map, size_t length)
{
   char *str = data;
   size_t len = strlen(str);

   fail_if(len != length);
   fail_if(memcmp(data, map, length) != 0);
   return EINA_TRUE;
}

static void
_map_cb(void *data, Eio_File *handler EINA_UNUSED, void *map, size_t length)
{
   ecore_main_loop_quit();
}

static void
_error_cb(void *data EINA_UNUSED, Eio_File *handler EINA_UNUSED, int error)
{
   fprintf(stderr, "Error:%s\n", strerror(error));
   ecore_main_loop_quit();
}

START_TEST(eio_test_map_simple)
{
   int fd;
   char *file = strdup("/tmp/eio_map_example.txt");
   const char *data = "This is the data to save in file";
   Eio_File *ef;

   ecore_init();
   eina_init();
   eio_init();

   fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG);
   fail_if(fd == 0);
   fail_if(write(fd, data, strlen(data)) != (ssize_t)strlen(data));
   close(fd);

   ef = eio_file_open(file, EINA_FALSE, _open_cb, _error_cb, NULL);
   ecore_main_loop_begin();
   fail_if(!ef);

   ef = eio_file_map_all(ee, EINA_FILE_POPULATE, _filter_cb, _map_cb,
                    _error_cb, data);
   ecore_main_loop_begin();
   fail_if(!ef);

   ef = eio_file_map_new(ee, EINA_FILE_WILLNEED, 0, strlen(data), _filter_cb,
                         _map_cb, _error_cb, data);
   ecore_main_loop_begin();
   fail_if(!ef);

   ef = eio_file_close(ee, _done_cb, _error_cb, NULL);
   ecore_main_loop_begin();
   fail_if(!ef);

   eio_shutdown();
   eina_shutdown();
   ecore_shutdown();
}
END_TEST

void
eio_test_map(TCase *tc)
{
   tcase_add_test(tc, eio_test_map_simple);
}