summaryrefslogtreecommitdiff
path: root/src/tests/eina/eina_test_convert.c
blob: 8dd06e65fe78d8a685e72f9774f3d6eff53bc9e1 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* EINA - EFL data type library
 * Copyright (C) 2008 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 <stdio.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <limits.h>

#include <Eina.h>

#include "eina_suite.h"

START_TEST(eina_convert_simple)
{
   char tmp[128];
   char ref[128];

   fail_if(eina_convert_itoa(0, tmp) != 1);
   fail_if(strcmp(tmp, "0") != 0);

   fail_if(eina_convert_itoa(-1, tmp) != 2);
   fail_if(strcmp(tmp, "-1") != 0);

   fail_if(eina_convert_itoa(100, tmp) != 3);
   fail_if(strcmp(tmp, "100") != 0);

   fail_if(eina_convert_itoa(-100, tmp) != 4);
   fail_if(strcmp(tmp, "-100") != 0);

   fail_if(eina_convert_itoa(10000000, tmp) != 8);
   fail_if(strcmp(tmp, "10000000") != 0);

   snprintf(ref, sizeof (ref), "%d", INT_MIN);
   fail_if(eina_convert_itoa(INT_MIN, tmp) != (int) strlen(ref));
   fail_if(strcmp(tmp, ref) != 0);

   fail_if(eina_convert_xtoa(0, tmp) != 1);
   fail_if(strcmp(tmp, "0") != 0);

   fail_if(eina_convert_xtoa(0xA1, tmp) != 2);
   fail_if(strcmp(tmp, "a1") != 0);

   fail_if(eina_convert_xtoa(0xFF00EF0E, tmp) != 8);
   fail_if(strcmp(tmp, "ff00ef0e") != 0);
}
END_TEST

#define EET_TEST_DOUBLE0 123.45689
#define EET_TEST_DOUBLE1 1.0
#define EET_TEST_DOUBLE2 0.25
#define EET_TEST_DOUBLE3 0.0001234
#define EET_TEST_DOUBLE4 123456789.9876543210

static void
_eina_convert_check(double test, int length)
{
   char tmp[128];
   long long int m = 0;
   long e = 0;
   double r;

   fail_if(eina_convert_dtoa(test, tmp) != length);
   fail_if(eina_convert_atod(tmp, 128, &m, &e) != EINA_TRUE);
   r = ldexp((double)m, e);
   fail_if(fabs(r - test) > DBL_MIN);
}

   START_TEST(eina_convert_double)
{
   long long int m = 0;
   long e = 0;

   eina_init();

   _eina_convert_check(EET_TEST_DOUBLE0,  20);
   _eina_convert_check(-EET_TEST_DOUBLE0, 21);
   _eina_convert_check(EET_TEST_DOUBLE1,   6);
   _eina_convert_check(EET_TEST_DOUBLE2,   6);
   _eina_convert_check(EET_TEST_DOUBLE3,  21);
   _eina_convert_check(EET_TEST_DOUBLE4,  21);

   fail_if(eina_convert_atod("ah ah ah", 8, &m, &e) != EINA_FALSE);
   fail_if(eina_convert_atod("0xjo", 8, &m, &e) != EINA_FALSE);
   fail_if(eina_convert_atod("0xp", 8, &m, &e) != EINA_FALSE);

   eina_shutdown();
}
END_TEST

static void
_eina_convert_fp_check(double d, Eina_F32p32 fp, int length)
{
   char tmp1[128];
   char tmp2[128];
   Eina_F32p32 fpc;
   double fpd;
   int l1;
   int l2;

   l1 = eina_convert_dtoa(d, tmp1);
   l2 = eina_convert_fptoa(fp, tmp2);
/*    fprintf(stderr, "[%s](%i) vs [%s](%i)\n", tmp1, l1, tmp2, l2); */
   fail_if(l1 != l2);
   fail_if(length != l1);
   fail_if(strcmp(tmp1, tmp2) != 0);

   fail_if(!eina_convert_atofp(tmp2, l2, &fpc));
/*    fprintf(stderr, "%016x vs %016x\n", fpc, fp); */
   fail_if(fpc != fp);

   fail_if(!eina_convert_atofp(tmp1, l1, &fpc));
   fpd = eina_f32p32_double_to(fpc);
/*    fprintf(stderr, "%0.16f vs %0.16f\n", fpd, d); */
   fail_if(fabs(fpd - d) > DBL_MIN);

   d = -d;
   fp = -fp;

   l1 = eina_convert_dtoa(d, tmp1);
   l2 = eina_convert_fptoa(fp, tmp2);
   fail_if(l1 != l2);
   fail_if(length + 1 != l1);
   fail_if(strcmp(tmp1, tmp2) != 0);

   fail_if(!eina_convert_atofp(tmp2, l2, &fpc));
/*    fprintf(stderr, "%016x vs %016x\n", fpc, fp); */
   fail_if(fpc != fp);

   fail_if(!eina_convert_atofp(tmp1, l1, &fpc));
   fpd = eina_f32p32_double_to(fpc);
/*    fprintf(stderr, "%0.16f vs %0.16f\n", fpd, d); */
   fail_if(fabs(fpd - d) > DBL_MIN);
}

   START_TEST(eina_convert_fp)
{
   _eina_convert_fp_check(1.0,     0x0000000100000000,  6);
   _eina_convert_fp_check(0.5,     0x0000000080000000,  8);
   _eina_convert_fp_check(0.625,   0x00000000a0000000,  8);
   _eina_convert_fp_check(256.0,   0x0000010000000000,  6);
   _eina_convert_fp_check(0.5,     0x0000000080000000,  8);
   _eina_convert_fp_check(128.625, 0x00000080a0000000, 10);
}
END_TEST

void
eina_test_convert(TCase *tc)
{
   tcase_add_test(tc, eina_convert_simple);
   tcase_add_test(tc, eina_convert_double);
   tcase_add_test(tc,     eina_convert_fp);
}