summaryrefslogtreecommitdiff
path: root/src/tests/eina/eina_test_cow.c
blob: 4d4d8e8f8a97baff44143329839687505b01fad2 (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
/* EINA - EFL data type library
 * Copyright (C) 2012 Cedric Bail
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "eina_suite.h"
#include "Eina.h"

typedef struct _Eina_Cow_Test Eina_Cow_Test;
struct _Eina_Cow_Test
{
   unsigned char c;
   int i;
   void *d;
};

START_TEST(eina_cow)
{
   const Eina_Cow_Test *prev;
   const Eina_Cow_Test *cur;
   Eina_Cow_Test *write;
   Eina_Cow *cow;
   Eina_Cow_Test default_value = { 42, 0, NULL };

   cow = eina_cow_add("COW Test", sizeof (Eina_Cow_Test), 16, &default_value);
   fail_if(cow == NULL);

   prev = eina_cow_alloc(cow);
   cur = eina_cow_alloc(cow);
   fail_if(prev == NULL || cur == NULL);

   write = eina_cow_write(cow, &cur);
   fail_if(write == NULL || write == &default_value);

   write->i = 7;
   eina_cow_commit(cow, &cur, write);
   fail_if(cur->i != 7 || prev->i != 0);

   eina_cow_memcpy(cow, &prev, cur);
   fail_if(cur->i != 7 || prev->i != 7);
   fail_if(default_value.i != 0);

   write = eina_cow_write(cow, &cur);
   fail_if(write == NULL || write == &default_value);

   write->i = 42; write->c = 5;
   eina_cow_commit(cow, &cur, write);
   fail_if(cur->i != 42 || cur->c != 5 ||
           prev->i != 7 || prev->c != 42 ||
           default_value.c != 42 || default_value.i != 0);

   fail_if(eina_cow_gc(cow) == EINA_FALSE);
   fail_if(eina_cow_gc(cow) == EINA_FALSE);

   write = eina_cow_write(cow, &cur);
   write->i = 7; write->c = 42;
   eina_cow_commit(cow, &cur, write);

   fail_if(eina_cow_gc(cow) == EINA_FALSE);
   fail_if(cur != prev);
}
END_TEST

void
eina_test_cow(TCase *tc)
{
   tcase_add_test(tc, eina_cow);
}