1 /*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #define _GNU_SOURCE
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include "wayland-private.h"
31 #include "test-runner.h"
32
TEST(fixed_double_conversions)33 TEST(fixed_double_conversions)
34 {
35 wl_fixed_t f;
36 double d;
37
38 d = 62.125;
39 f = wl_fixed_from_double(d);
40 fprintf(stderr, "double %lf to fixed %x\n", d, f);
41 assert(f == 0x3e20);
42
43 d = -1200.625;
44 f = wl_fixed_from_double(d);
45 fprintf(stderr, "double %lf to fixed %x\n", d, f);
46 assert(f == -0x4b0a0);
47
48 f = random();
49 d = wl_fixed_to_double(f);
50 fprintf(stderr, "fixed %x to double %lf\n", f, d);
51 assert(d == f / 256.0);
52
53 f = 0x012030;
54 d = wl_fixed_to_double(f);
55 fprintf(stderr, "fixed %x to double %lf\n", f, d);
56 assert(d == 288.1875);
57
58 f = 0x70000000;
59 d = wl_fixed_to_double(f);
60 fprintf(stderr, "fixed %x to double %lf\n", f, d);
61 assert(d == f / 256);
62
63 f = -0x012030;
64 d = wl_fixed_to_double(f);
65 fprintf(stderr, "fixed %x to double %lf\n", f, d);
66 assert(d == -288.1875);
67
68 f = 0x80000000;
69 d = wl_fixed_to_double(f);
70 fprintf(stderr, "fixed %x to double %lf\n", f, d);
71 assert(d == f / 256);
72 }
73
TEST(fixed_int_conversions)74 TEST(fixed_int_conversions)
75 {
76 wl_fixed_t f;
77 int i;
78
79 i = 62;
80 f = wl_fixed_from_int(i);
81 assert(f == 62 * 256);
82
83 i = -2080;
84 f = wl_fixed_from_int(i);
85 assert(f == -2080 * 256);
86
87 f = 0x277013;
88 i = wl_fixed_to_int(f);
89 assert(i == 0x2770);
90
91 f = -0x5044;
92 i = wl_fixed_to_int(f);
93 assert(i == -0x50);
94 }
95