summaryrefslogtreecommitdiff
path: root/src/tests/eina/eina_test_tmpstr.c
blob: 33e7db709fbaddbb5d863b18d0db228897dba6ed (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* EINA - EFL data type library
 * Copyright (C) 2013 Vlad Brovko
 *
 * 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.h>
#include "eina_tmpstr.h"

#include "eina_suite.h"

EFL_START_TEST(tmpstr_simple)
{

   const int cnt_tmp_strings = 10;
   const int max_str_len = 255;
   char buf[max_str_len + 1];
   Eina_Tmpstr *tmp_strings[cnt_tmp_strings];

   // Add several tmp strings
   for (int i = 0; i != cnt_tmp_strings; ++i)
     {
        snprintf(buf, max_str_len, "Tmp string %d", (i + 1));
        tmp_strings[i] = eina_tmpstr_add(buf);

        fail_if(strcmp(buf, tmp_strings[i]));
     }

   // Delete these tmp strings
   for (int i = 0; i != cnt_tmp_strings; ++i)
     {
        snprintf(buf, max_str_len, "Tmp string %d", (cnt_tmp_strings - i - 1 + 1));

        fail_if(strcmp(buf, tmp_strings[cnt_tmp_strings - i - 1]));

        eina_tmpstr_del(tmp_strings[cnt_tmp_strings - i - 1]);
        tmp_strings[cnt_tmp_strings - i - 1] = 0;
     }

   // Delete non tmp string (should do nothing)
   eina_tmpstr_del("Some non tmp string");

}
EFL_END_TEST

EFL_START_TEST(tmpstr_simple_len)
{

   const int cnt_tmp_strings = 10;
   const int max_str_len = 255;
   char buf[max_str_len + 1];
   Eina_Tmpstr *tmp_strings[cnt_tmp_strings];

   // Add several tmp strings
   for (int i = 0; i != cnt_tmp_strings; ++i)
     {
        snprintf(buf, max_str_len, "Tmp string %d", (i + 1));
        tmp_strings[i] = eina_tmpstr_add_length(buf, (max_str_len + 1));

        fail_if(strcmp(buf, tmp_strings[i]));
     }

   // Delete these tmp strings
   for (int i = 0; i != cnt_tmp_strings; ++i)
     {
        snprintf(buf, max_str_len, "Tmp string %d", (cnt_tmp_strings - i - 1 + 1));

        fail_if(strcmp(buf, tmp_strings[cnt_tmp_strings - i - 1]));

        eina_tmpstr_del(tmp_strings[cnt_tmp_strings - i - 1]);
        tmp_strings[cnt_tmp_strings - i - 1] = 0;
     }

   // Delete non tmp string (should do nothing)
   eina_tmpstr_del("Some non tmp string");

}
EFL_END_TEST

EFL_START_TEST(tmpstr_manage)
{

   char *buf = malloc(7);
   strcpy(buf, "tmpstr");
   Eina_Tmpstr *tstr1 = eina_tmpstr_manage_new(buf);
   fail_if(strcmp(buf, tstr1));
   eina_tmpstr_del(tstr1);

}
EFL_END_TEST

EFL_START_TEST(tmpstr_manage_len)
{

   char *buf = malloc(10);
   strcpy(buf, "tmpstr");
   Eina_Tmpstr *tstr1 = eina_tmpstr_manage_new_length(buf, 7);
   fail_if(strcmp(buf, tstr1));
   eina_tmpstr_del(tstr1);

}
EFL_END_TEST

EFL_START_TEST(tmpstr_len)
{

   const char *str1 = "12345";
   const char *str2 = "123456789";
   Eina_Tmpstr *tstr1 = eina_tmpstr_add(str1);
   Eina_Tmpstr *tstr2 = eina_tmpstr_add(str2);
   Eina_Tmpstr *tstr_empty = eina_tmpstr_add("");

   ck_assert_int_eq(eina_tmpstr_len(tstr1), strlen(str1));
   ck_assert_int_eq(eina_tmpstr_len(tstr2), strlen(str2));

   ck_assert_int_eq(eina_tmpstr_len(tstr_empty), 0);

   eina_tmpstr_del(tstr1);
   eina_tmpstr_del(tstr2);

}
EFL_END_TEST

void
eina_test_tmpstr(TCase *tc)
{
   tcase_add_test(tc, tmpstr_simple);
   tcase_add_test(tc, tmpstr_simple_len);
   tcase_add_test(tc, tmpstr_manage);
   tcase_add_test(tc, tmpstr_manage_len);
   tcase_add_test(tc, tmpstr_len);
}