EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / tests / eina_test_convert.c
1 /* EINA - EFL data type library
2  * Copyright (C) 2008 Cedric Bail
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library;
16  * if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <math.h>
26 #include <float.h>
27
28 #include "eina_suite.h"
29 #include "Eina.h"
30
31 START_TEST(eina_convert_simple)
32 {
33    char tmp[128];
34
35    fail_if(eina_convert_itoa(0, tmp) != 1);
36    fail_if(strcmp(tmp, "0") != 0);
37
38    fail_if(eina_convert_itoa(-1, tmp) != 2);
39    fail_if(strcmp(tmp, "-1") != 0);
40
41    fail_if(eina_convert_itoa(100, tmp) != 3);
42    fail_if(strcmp(tmp, "100") != 0);
43
44    fail_if(eina_convert_itoa(-100, tmp) != 4);
45    fail_if(strcmp(tmp, "-100") != 0);
46
47    fail_if(eina_convert_itoa(10000000, tmp) != 8);
48    fail_if(strcmp(tmp, "10000000") != 0);
49
50    fail_if(eina_convert_xtoa(0, tmp) != 1);
51    fail_if(strcmp(tmp, "0") != 0);
52
53    fail_if(eina_convert_xtoa(0xA1, tmp) != 2);
54    fail_if(strcmp(tmp, "a1") != 0);
55
56    fail_if(eina_convert_xtoa(0xFF00EF0E, tmp) != 8);
57    fail_if(strcmp(tmp, "ff00ef0e") != 0);
58 }
59 END_TEST
60
61 #define EET_TEST_DOUBLE0 123.45689
62 #define EET_TEST_DOUBLE1 1.0
63 #define EET_TEST_DOUBLE2 0.25
64 #define EET_TEST_DOUBLE3 0.0001234
65 #define EET_TEST_DOUBLE4 123456789.9876543210
66
67 static void
68 _eina_convert_check(double test, int length)
69 {
70    char tmp[128];
71    long long int m = 0;
72    long e = 0;
73    double r;
74
75    fail_if(eina_convert_dtoa(test, tmp) != length);
76    fail_if(eina_convert_atod(tmp, 128, &m, &e) != EINA_TRUE);
77    r = ldexp((double)m, e);
78    fail_if(fabs(r - test) > DBL_MIN);
79 }
80
81    START_TEST(eina_convert_double)
82 {
83    long long int m = 0;
84    long e = 0;
85
86    eina_init();
87
88    _eina_convert_check(EET_TEST_DOUBLE0,  20);
89    _eina_convert_check(-EET_TEST_DOUBLE0, 21);
90    _eina_convert_check(EET_TEST_DOUBLE1,   6);
91    _eina_convert_check(EET_TEST_DOUBLE2,   6);
92    _eina_convert_check(EET_TEST_DOUBLE3,  21);
93    _eina_convert_check(EET_TEST_DOUBLE4,  21);
94
95    fail_if(eina_convert_atod("ah ah ah", 8, &m, &e) != EINA_FALSE);
96    fail_if(eina_convert_atod("0xjo", 8, &m, &e) != EINA_FALSE);
97    fail_if(eina_convert_atod("0xp", 8, &m, &e) != EINA_FALSE);
98
99    eina_shutdown();
100 }
101 END_TEST
102
103 static void
104 _eina_convert_fp_check(double d, Eina_F32p32 fp, int length)
105 {
106    char tmp1[128];
107    char tmp2[128];
108    Eina_F32p32 fpc;
109    double fpd;
110    int l1;
111    int l2;
112
113    l1 = eina_convert_dtoa(d, tmp1);
114    l2 = eina_convert_fptoa(fp, tmp2);
115 /*    fprintf(stderr, "[%s](%i) vs [%s](%i)\n", tmp1, l1, tmp2, l2); */
116    fail_if(l1 != l2);
117    fail_if(length != l1);
118    fail_if(strcmp(tmp1, tmp2) != 0);
119
120    fail_if(!eina_convert_atofp(tmp2, l2, &fpc));
121 /*    fprintf(stderr, "%016x vs %016x\n", fpc, fp); */
122    fail_if(fpc != fp);
123
124    fail_if(!eina_convert_atofp(tmp1, l1, &fpc));
125    fpd = eina_f32p32_double_to(fpc);
126 /*    fprintf(stderr, "%0.16f vs %0.16f\n", fpd, d); */
127    fail_if(fabs(fpd - d) > DBL_MIN);
128
129    d = -d;
130    fp = -fp;
131
132    l1 = eina_convert_dtoa(d, tmp1);
133    l2 = eina_convert_fptoa(fp, tmp2);
134    fail_if(l1 != l2);
135    fail_if(length + 1 != l1);
136    fail_if(strcmp(tmp1, tmp2) != 0);
137
138    fail_if(!eina_convert_atofp(tmp2, l2, &fpc));
139 /*    fprintf(stderr, "%016x vs %016x\n", fpc, fp); */
140    fail_if(fpc != fp);
141
142    fail_if(!eina_convert_atofp(tmp1, l1, &fpc));
143    fpd = eina_f32p32_double_to(fpc);
144 /*    fprintf(stderr, "%0.16f vs %0.16f\n", fpd, d); */
145    fail_if(fabs(fpd - d) > DBL_MIN);
146 }
147
148    START_TEST(eina_convert_fp)
149 {
150    _eina_convert_fp_check(1.0,     0x0000000100000000,  6);
151    _eina_convert_fp_check(0.5,     0x0000000080000000,  8);
152    _eina_convert_fp_check(0.625,   0x00000000a0000000,  8);
153    _eina_convert_fp_check(256.0,   0x0000010000000000,  6);
154    _eina_convert_fp_check(0.5,     0x0000000080000000,  8);
155    _eina_convert_fp_check(128.625, 0x00000080a0000000, 10);
156 }
157 END_TEST
158
159 void
160 eina_test_convert(TCase *tc)
161 {
162    tcase_add_test(tc, eina_convert_simple);
163    tcase_add_test(tc, eina_convert_double);
164    tcase_add_test(tc,     eina_convert_fp);
165 }