Merge branch '7.8'
[mesa.git] / progs / gallium / unit / u_format_test.c
1 /**************************************************************************
2 *
3 * Copyright 2009-2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #include "util/u_format.h"
33 #include "util/u_format_tests.h"
34 #include "util/u_format_pack.h"
35
36
37 static boolean
38 test_format_unpack_4f(const struct util_format_test_case *test)
39 {
40 float unpacked[4];
41 unsigned i;
42 boolean success;
43
44 util_format_unpack_4f(test->format, unpacked, test->packed);
45
46 success = TRUE;
47 for (i = 0; i < 4; ++i)
48 if (test->unpacked[i] != unpacked[i])
49 success = FALSE;
50
51 if (!success) {
52 printf("FAILED: (%f %f %f %f) obtained\n", unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
53 printf(" (%f %f %f %f) expected\n", test->unpacked[0], test->unpacked[1], test->unpacked[2], test->unpacked[3]);
54 }
55
56 return success;
57 }
58
59
60 static boolean
61 test_format_pack_4f(const struct util_format_test_case *test)
62 {
63 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
64 unsigned i;
65 boolean success;
66
67 memset(packed, 0, sizeof packed);
68
69 util_format_pack_4f(test->format, packed, test->unpacked[0], test->unpacked[1], test->unpacked[2], test->unpacked[3]);
70
71 success = TRUE;
72 for (i = 0; i < UTIL_FORMAT_MAX_PACKED_BYTES; ++i)
73 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
74 success = FALSE;
75
76 if (!success) {
77 /* TODO: print more than 4 bytes */
78 printf("FAILED: (%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x) obtained\n",
79 packed[0], packed[1], packed[2], packed[3],
80 packed[4], packed[5], packed[6], packed[7],
81 packed[8], packed[9], packed[10], packed[11],
82 packed[12], packed[13], packed[14], packed[15]);
83 printf(" (%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x) expected\n",
84 test->packed[0], test->packed[1], test->packed[2], test->packed[3],
85 test->packed[4], test->packed[5], test->packed[6], test->packed[7],
86 test->packed[8], test->packed[9], test->packed[10], test->packed[11],
87 test->packed[12], test->packed[13], test->packed[14], test->packed[15]);
88 }
89
90 return success;
91 }
92
93
94 static boolean
95 convert_4f_to_4ub(uint8_t *dst, const double *src)
96 {
97 unsigned i;
98 boolean accurate = TRUE;
99
100 for (i = 0; i < 4; ++i) {
101 if (src[i] < 0.0) {
102 accurate = FALSE;
103 dst[i] = 0;
104 }
105 else if (src[i] > 1.0) {
106 accurate = FALSE;
107 dst[i] = 255;
108 }
109 else {
110 dst[i] = src[i] * 255.0;
111 }
112 }
113
114 return accurate;
115 }
116
117
118 static boolean
119 test_format_unpack_4ub(const struct util_format_test_case *test)
120 {
121 uint8_t unpacked[4];
122 uint8_t expected[4];
123 unsigned i;
124 boolean success;
125
126 util_format_unpack_4ub(test->format, unpacked, test->packed);
127
128 convert_4f_to_4ub(expected, test->unpacked);
129
130 success = TRUE;
131 for (i = 0; i < 4; ++i)
132 if (expected[i] != unpacked[i])
133 success = FALSE;
134
135 if (!success) {
136 printf("FAILED: (0x%02x 0x%02x 0x%02x 0x%02x) obtained\n", unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
137 printf(" (0x%02x 0x%02x 0x%02x 0x%02x) expected\n", expected[0], expected[1], expected[2], expected[3]);
138 }
139
140 return success;
141 }
142
143
144 static boolean
145 test_format_pack_4ub(const struct util_format_test_case *test)
146 {
147 uint8_t unpacked[4];
148 uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
149 unsigned i;
150 boolean success;
151
152 if (!convert_4f_to_4ub(unpacked, test->unpacked)) {
153 /*
154 * Skip test cases which cannot be represented by four unorm bytes.
155 */
156 return TRUE;
157 }
158
159 memset(packed, 0, sizeof packed);
160
161 util_format_pack_4ub(test->format, packed, unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
162
163 success = TRUE;
164 for (i = 0; i < UTIL_FORMAT_MAX_PACKED_BYTES; ++i)
165 if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
166 success = FALSE;
167
168 if (!success) {
169 /* TODO: print more than 4 bytes */
170 printf("FAILED: (%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x) obtained\n",
171 packed[0], packed[1], packed[2], packed[3],
172 packed[4], packed[5], packed[6], packed[7],
173 packed[8], packed[9], packed[10], packed[11],
174 packed[12], packed[13], packed[14], packed[15]);
175 printf(" (%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x) expected\n",
176 test->packed[0], test->packed[1], test->packed[2], test->packed[3],
177 test->packed[4], test->packed[5], test->packed[6], test->packed[7],
178 test->packed[8], test->packed[9], test->packed[10], test->packed[11],
179 test->packed[12], test->packed[13], test->packed[14], test->packed[15]);
180 }
181
182 return success;
183 }
184
185
186 typedef boolean
187 (*test_func_t)(const struct util_format_test_case *test);
188
189
190 static boolean
191 test_one(test_func_t func, const char *suffix)
192 {
193 enum pipe_format last_format = PIPE_FORMAT_NONE;
194 unsigned i;
195 bool success = TRUE;
196
197 for (i = 0; i < util_format_nr_test_cases; ++i) {
198 const struct util_format_test_case *test = &util_format_test_cases[i];
199 if (test->format != last_format) {
200 const struct util_format_description *format_desc;
201 format_desc = util_format_description(test->format);
202 printf("Testing util_format_%s_%s ...\n", format_desc->short_name, suffix);
203 last_format = test->format;
204 }
205
206 if (!func(&util_format_test_cases[i]))
207 success = FALSE;
208 }
209
210 return success;
211 }
212
213
214 static boolean
215 test_all(void)
216 {
217 bool success = TRUE;
218
219 if (!test_one(&test_format_pack_4f, "pack_4f"))
220 success = FALSE;
221
222 if (!test_one(&test_format_unpack_4f, "unpack_4f"))
223 success = FALSE;
224
225 if (!test_one(&test_format_pack_4ub, "pack_4ub"))
226 success = FALSE;
227
228 if (!test_one(&test_format_unpack_4ub, "unpack_4ub"))
229 success = FALSE;
230
231 return success;
232 }
233
234
235 int main(int argc, char **argv)
236 {
237 boolean success;
238
239 success = test_all();
240
241 return success ? 0 : 1;
242 }