1 /* EINA - EFL data type library
2 * Copyright (C) 2008 Cedric Bail
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.
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.
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/>.
28 #include "eina_suite.h"
31 START_TEST(eina_convert_simple)
35 fail_if(eina_convert_itoa(0, tmp) != 1);
36 fail_if(strcmp(tmp, "0") != 0);
38 fail_if(eina_convert_itoa(-1, tmp) != 2);
39 fail_if(strcmp(tmp, "-1") != 0);
41 fail_if(eina_convert_itoa(100, tmp) != 3);
42 fail_if(strcmp(tmp, "100") != 0);
44 fail_if(eina_convert_itoa(-100, tmp) != 4);
45 fail_if(strcmp(tmp, "-100") != 0);
47 fail_if(eina_convert_itoa(10000000, tmp) != 8);
48 fail_if(strcmp(tmp, "10000000") != 0);
50 fail_if(eina_convert_xtoa(0, tmp) != 1);
51 fail_if(strcmp(tmp, "0") != 0);
53 fail_if(eina_convert_xtoa(0xA1, tmp) != 2);
54 fail_if(strcmp(tmp, "a1") != 0);
56 fail_if(eina_convert_xtoa(0xFF00EF0E, tmp) != 8);
57 fail_if(strcmp(tmp, "ff00ef0e") != 0);
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
68 _eina_convert_check(double test, int length)
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);
81 START_TEST(eina_convert_double)
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);
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);
104 _eina_convert_fp_check(double d, Eina_F32p32 fp, int length)
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); */
117 fail_if(length != l1);
118 fail_if(strcmp(tmp1, tmp2) != 0);
120 fail_if(!eina_convert_atofp(tmp2, l2, &fpc));
121 /* fprintf(stderr, "%016x vs %016x\n", fpc, fp); */
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);
132 l1 = eina_convert_dtoa(d, tmp1);
133 l2 = eina_convert_fptoa(fp, tmp2);
135 fail_if(length + 1 != l1);
136 fail_if(strcmp(tmp1, tmp2) != 0);
138 fail_if(!eina_convert_atofp(tmp2, l2, &fpc));
139 /* fprintf(stderr, "%016x vs %016x\n", fpc, fp); */
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);
148 START_TEST(eina_convert_fp)
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);
160 eina_test_convert(TCase *tc)
162 tcase_add_test(tc, eina_convert_simple);
163 tcase_add_test(tc, eina_convert_double);
164 tcase_add_test(tc, eina_convert_fp);